✨ Add Accordion and Modal components

Accordion.Item manages its own open state with aria-expanded wiring.
Modal renders through a portal with backdrop click handling. Also adds
the ChevronDown icon used by the accordion header.
This commit is contained in:
Aksel Mini committed 2026-08-21 15:10:00 +02:00
1 parent bb9501ea31
commit adfcead7be
13 files changed
+389 -1

No files matched your search

+53
View File
@@ -0,0 +1,53 @@
/**
* Accordion
*/
.navds-accordion {
border: 1px solid var(--a-border-subtle);
border-radius: var(--a-border-radius-medium);
overflow: hidden;
font-family: var(--a-font-family);
}
.navds-accordion__item + .navds-accordion__item {
border-top: 1px solid var(--a-border-subtle);
}
.navds-accordion__header {
display: flex;
align-items: center;
gap: var(--a-spacing-2);
width: 100%;
padding: var(--a-spacing-3) var(--a-spacing-4);
border: none;
background-color: var(--a-bg-default);
color: var(--a-text-default);
font-size: var(--a-font-size-heading-xsmall);
font-weight: var(--a-font-weight-bold);
text-align: left;
cursor: pointer;
}
.navds-accordion__header:hover {
background-color: var(--a-bg-subtle);
}
.navds-accordion__header:focus-visible {
outline: 2px solid var(--a-border-focus);
outline-offset: -2px;
}
.navds-accordion__chevron {
flex-shrink: 0;
transition: transform 150ms ease;
}
.navds-accordion__item--open .navds-accordion__chevron {
transform: rotate(180deg);
}
.navds-accordion__content {
padding: var(--a-spacing-3) var(--a-spacing-4) var(--a-spacing-4);
border-top: 1px solid var(--a-border-subtle);
color: var(--a-text-default);
}
+2
View File
@@ -4,3 +4,5 @@
@import "./button.css";
@import "./textfield.css";
@import "./alert.css";
@import "./accordion.css";
@import "./modal.css";
+49
View File
@@ -0,0 +1,49 @@
/**
* Modal
*/
.navds-modal__overlay {
position: fixed;
inset: 0;
z-index: 1000;
display: flex;
align-items: center;
justify-content: center;
padding: var(--a-spacing-4);
background-color: rgb(35 38 42 / 60%);
}
.navds-modal {
position: relative;
width: 100%;
max-width: 30rem;
max-height: calc(100vh - 2 * var(--a-spacing-8));
overflow-y: auto;
padding: var(--a-spacing-6);
border-radius: var(--a-border-radius-large);
background-color: var(--a-bg-default);
color: var(--a-text-default);
font-family: var(--a-font-family);
}
.navds-modal__close {
position: absolute;
top: var(--a-spacing-3);
right: var(--a-spacing-3);
display: inline-flex;
padding: var(--a-spacing-2);
border: none;
border-radius: var(--a-border-radius-medium);
background-color: transparent;
color: var(--a-icon-default);
cursor: pointer;
}
.navds-modal__close:hover {
background-color: var(--a-bg-subtle);
}
.navds-modal__close:focus-visible {
outline: 2px solid var(--a-border-focus);
outline-offset: 2px;
}
@@ -0,0 +1,27 @@
import { forwardRef } from "react";
import type { SVGProps } from "react";
export const ChevronDownIcon = forwardRef<SVGSVGElement, SVGProps<SVGSVGElement>>(
function ChevronDownIcon(props, ref) {
return (
<svg
xmlns="http://www.w3.org/2000/svg"
width="1em"
height="1em"
viewBox="0 0 24 24"
fill="none"
focusable={false}
aria-hidden
ref={ref}
{...props}
>
<path
fillRule="evenodd"
clipRule="evenodd"
d="M5.4 8.9 12 15.5l6.6-6.6-1.4-1.4-5.2 5.2-5.2-5.2-1.4 1.4Z"
fill="currentColor"
/>
</svg>
);
},
);
+1
View File
@@ -1,3 +1,4 @@
export { ChevronDownIcon } from "./ChevronDownIcon";
export { InformationIcon } from "./InformationIcon";
export { CheckmarkIcon } from "./CheckmarkIcon";
export { WarningIcon } from "./WarningIcon";
+3
View File
@@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none">
<path fill-rule="evenodd" clip-rule="evenodd" d="M5.4 8.9 12 15.5l6.6-6.6-1.4-1.4-5.2 5.2-5.2-5.2-1.4 1.4Z" fill="#23262A"/>
</svg>

After

Width:  |  Height:  |  Size: 230 B

@@ -0,0 +1,59 @@
import { forwardRef, useId, useState } from "react";
import type { HTMLAttributes, ReactNode } from "react";
import { ChevronDownIcon } from "@navikt/aksel-icons";
import { cn } from "../util/cn";
export interface AccordionProps extends HTMLAttributes<HTMLDivElement> {
children: ReactNode;
}
const AccordionRoot = forwardRef<HTMLDivElement, AccordionProps>(function Accordion(
{ className, children, ...rest },
ref,
) {
return (
<div ref={ref} className={cn("navds-accordion", className)} {...rest}>
{children}
</div>
);
});
export interface AccordionItemProps
extends Omit<HTMLAttributes<HTMLDivElement>, "title"> {
/** Header title. */
title: ReactNode;
/** Start expanded without controlling state. */
defaultOpen?: boolean;
children: ReactNode;
}
export const AccordionItem = forwardRef<HTMLDivElement, AccordionItemProps>(
function AccordionItem({ title, defaultOpen = false, className, children, ...rest }, ref) {
const [open, setOpen] = useState(defaultOpen);
const contentId = useId();
return (
<div
ref={ref}
className={cn("navds-accordion__item", open && "navds-accordion__item--open", className)}
{...rest}
>
<button
type="button"
className="navds-accordion__header"
aria-expanded={open}
aria-controls={contentId}
onClick={() => setOpen((prev) => !prev)}
>
<ChevronDownIcon className="navds-accordion__chevron" />
<span className="navds-accordion__title">{title}</span>
</button>
<div id={contentId} className="navds-accordion__content" hidden={!open}>
{children}
</div>
</div>
);
},
);
export const Accordion = Object.assign(AccordionRoot, { Item: AccordionItem });
@@ -0,0 +1,2 @@
export { Accordion, AccordionItem } from "./Accordion";
export type { AccordionProps, AccordionItemProps } from "./Accordion";
+4
View File
@@ -8,3 +8,7 @@ export { TextField } from "./text-field";
export type { TextFieldProps } from "./text-field";
export { Alert } from "./alert";
export type { AlertProps } from "./alert";
export { Accordion, AccordionItem } from "./accordion";
export type { AccordionProps, AccordionItemProps } from "./accordion";
export { Modal } from "./modal";
export type { ModalProps } from "./modal";
+60
View File
@@ -0,0 +1,60 @@
import { forwardRef } from "react";
import type { HTMLAttributes, ReactNode } from "react";
import { createPortal } from "react-dom";
import { XMarkIcon } from "@navikt/aksel-icons";
import { cn } from "../util/cn";
export interface ModalProps extends HTMLAttributes<HTMLDivElement> {
/** Whether the modal is rendered. */
open: boolean;
/** Called when the user closes the modal via the close button or backdrop. */
onClose?: () => void;
/** Accessible label for the dialog. */
label?: string;
/**
* Close when clicking the backdrop.
* @default true
*/
closeOnBackdropClick?: boolean;
children: ReactNode;
}
export const Modal = forwardRef<HTMLDivElement, ModalProps>(function Modal(
{ open, onClose, label, closeOnBackdropClick = true, className, children, ...rest },
ref,
) {
if (!open) {
return null;
}
return createPortal(
<div
className="navds-modal__overlay"
onClick={(event) => {
if (closeOnBackdropClick && event.target === event.currentTarget) {
onClose?.();
}
}}
>
<div
ref={ref}
role="dialog"
aria-modal="true"
aria-label={label}
className={cn("navds-modal", className)}
{...rest}
>
<button
type="button"
className="navds-modal__close"
aria-label="Lukk"
onClick={onClose}
>
<XMarkIcon />
</button>
{children}
</div>
</div>,
document.body,
);
});
+2
View File
@@ -0,0 +1,2 @@
export { Modal } from "./Modal";
export type { ModalProps } from "./Modal";
+1 -1
View File
@@ -1,4 +1,4 @@
type ClassValue = string | false | null | undefined;
type ClassValue = string | number | false | null | undefined;
export function cn(...classes: ClassValue[]): string {
return classes.filter(Boolean).join(" ");
+126
View File
@@ -0,0 +1,126 @@
lockfileVersion: '9.0'
settings:
autoInstallPeers: true
excludeLinksFromLockfile: false
importers:
.: {}
packages/core/css:
devDependencies:
'@navikt/ds-tokens':
specifier: workspace:*
version: link:../tokens
packages/core/icons:
devDependencies:
'@types/react':
specifier: 18.3.11
version: 18.3.11
react:
specifier: 18.3.1
version: 18.3.1
typescript:
specifier: 5.5.4
version: 5.5.4
packages/core/react:
dependencies:
'@navikt/aksel-icons':
specifier: workspace:*
version: link:../icons
devDependencies:
'@types/react':
specifier: 18.3.11
version: 18.3.11
'@types/react-dom':
specifier: 18.3.1
version: 18.3.1
react:
specifier: 18.3.1
version: 18.3.1
react-dom:
specifier: 18.3.1
version: 18.3.1(react@18.3.1)
typescript:
specifier: 5.5.4
version: 5.5.4
packages/core/tokens: {}
packages:
'@types/prop-types@15.7.15':
resolution: {integrity: sha512-F6bEyamV9jKGAFBEmlQnesRPGOQqS2+Uwi0Em15xenOxHaf2hv6L8YCVn3rPdPJOiJfPiCnLIRyvwVaqMY3MIw==}
'@types/react-dom@18.3.1':
resolution: {integrity: sha512-qW1Mfv8taImTthu4KoXgDfLuk4bydU6Q/TkADnDWWHwi4NX4BR+LWfTp2sVmTqRrsHvyDDTelgelxJ+SsejKKQ==}
'@types/react@18.3.11':
resolution: {integrity: sha512-r6QZ069rFTjrEYgFdOck1gK7FLVsgJE7tTz0pQBczlBNUhBNk0MQH4UbnFSwjpQLMkLzgqvBBa+qGpLje16eTQ==}
csstype@3.2.3:
resolution: {integrity: sha512-z1HGKcYy2xA8AGQfwrn0PAy+PB7X/GSj3UVJW9qKyn43xWa+gl5nXmU4qqLMRzWVLFC8KusUX8T/0kCiOYpAIQ==}
js-tokens@4.0.0:
resolution: {integrity: sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==}
loose-envify@1.4.0:
resolution: {integrity: sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==}
hasBin: true
react-dom@18.3.1:
resolution: {integrity: sha512-5m4nQKp+rZRb09LNH59GM4BxTh9251/ylbKIbpe7TpGxfJ+9kv6BLkLBXIjjspbgbnIBNqlI23tRnTWT0snUIw==}
peerDependencies:
react: ^18.3.1
react@18.3.1:
resolution: {integrity: sha512-wS+hAgJShR0KhEvPJArfuPVN1+Hz1t0Y6n5jLrGQbkb4urgPE/0Rve+1kMB1v/oWgHgm4WIcV+i7F2pTVj+2iQ==}
engines: {node: '>=0.10.0'}
scheduler@0.23.2:
resolution: {integrity: sha512-UOShsPwz7NrMUqhR6t0hWjFduvOzbtv7toDH1/hIrfRNIDBnnBWd0CwJTGvTpngVlmwGCdP9/Zl/tVrDqcuYzQ==}
typescript@5.5.4:
resolution: {integrity: sha512-Mtq29sKDAEYP7aljRgtPOpTvOfbwRWlS6dPRzwjdE+C0R4brX/GUyhHSecbHMFLNBLcJIPt9nl9yG5TZ1weH+Q==}
engines: {node: '>=14.17'}
hasBin: true
snapshots:
'@types/prop-types@15.7.15': {}
'@types/react-dom@18.3.1':
dependencies:
'@types/react': 18.3.11
'@types/react@18.3.11':
dependencies:
'@types/prop-types': 15.7.15
csstype: 3.2.3
csstype@3.2.3: {}
js-tokens@4.0.0: {}
loose-envify@1.4.0:
dependencies:
js-tokens: 4.0.0
react-dom@18.3.1(react@18.3.1):
dependencies:
loose-envify: 1.4.0
react: 18.3.1
scheduler: 0.23.2
react@18.3.1:
dependencies:
loose-envify: 1.4.0
scheduler@0.23.2:
dependencies:
loose-envify: 1.4.0
typescript@5.5.4: {}