✨ 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,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);
|
||||
}
|
||||
@@ -4,3 +4,5 @@
|
||||
@import "./button.css";
|
||||
@import "./textfield.css";
|
||||
@import "./alert.css";
|
||||
@import "./accordion.css";
|
||||
@import "./modal.css";
|
||||
@@ -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,3 +1,4 @@
|
||||
export { ChevronDownIcon } from "./ChevronDownIcon";
|
||||
export { InformationIcon } from "./InformationIcon";
|
||||
export { CheckmarkIcon } from "./CheckmarkIcon";
|
||||
export { WarningIcon } from "./WarningIcon";
|
||||
|
||||
@@ -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";
|
||||
@@ -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