✨ 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,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,
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user