✨ 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:
1 parent
bb9501ea31
commit
adfcead7be
13 files changed
+389
-1
No files matched your search
@@ -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";
|
||||
@@ -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";
|
||||
@@ -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,
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,2 @@
|
||||
export { Modal } from "./Modal";
|
||||
export type { ModalProps } from "./Modal";
|
||||
@@ -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(" ");
|
||||
|
||||
Reference in new issue
Block a user