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.
61 lines
1.5 KiB
TypeScript
61 lines
1.5 KiB
TypeScript
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,
|
|
);
|
|
});
|