Dialog
A modal dialog. Portal to <body>, focus trap, scroll lock, Escape to close, and focus restored to the trigger.
This is the one component with real machinery. Everything it does is visible in Dialog.tsx and internal/focus.ts — about 250 lines of code between them, and none of it is hidden behind a dependency.
Copy source
Requires: Nothing. Any bundler that understands .module.css.
This is the component, complete. Copy the files below into a ui/ folder at your project root and keep the folder structure — the imports between them are relative, so the paths have to come along. Each component imports its own .module.css, so its class names are scoped to it and cannot collide with your application's.
ui/components/Dialog/Dialog.tsximport { useEffect, useId, useRef } from "react";
import { createPortal } from "react-dom";
import { cx } from "../../internal/cx";
import { useFocusTrap, useScrollLock } from "../../internal/focus";
import { Button } from "../Button/Button";
import styles from "./dialog.module.css";
export type DialogSize = "sm" | "md" | "lg";
export interface DialogProps {
open: boolean;
onClose: () => void;
title: string;
/** Announced under the title and connected via aria-describedby. */
description?: string;
size?: DialogSize;
/** Prevents Escape and backdrop dismissal — use for destructive confirms. */
dismissible?: boolean;
footer?: React.ReactNode;
children?: React.ReactNode;
/** Renders the close affordance in the header. Defaults to true. */
showCloseButton?: boolean;
className?: string;
}
const SIZE_CLASS = {
sm: styles.sizeSm,
md: styles.sizeMd,
lg: styles.sizeLg,
} satisfies Record<DialogSize, string | undefined>;
// Rendered only while open, so children unmount on close.
export function Dialog({
open,
onClose,
title,
description,
size = "md",
dismissible = true,
footer,
children,
showCloseButton = true,
className,
}: DialogProps): React.JSX.Element | null {
const panelRef = useRef<HTMLDivElement>(null);
const baseId = useId();
const titleId = `${baseId}-title`;
const descriptionId = `${baseId}-description`;
useScrollLock(open);
const { onKeyDown } = useFocusTrap(open, panelRef);
useEffect(() => {
if (!open || !dismissible) return;
const onDocumentKeyDown = (event: KeyboardEvent) => {
if (event.key === "Escape") {
event.stopPropagation();
onClose();
}
};
document.addEventListener("keydown", onDocumentKeyDown);
return () => document.removeEventListener("keydown", onDocumentKeyDown);
}, [open, dismissible, onClose]);
if (!open) return null;
if (typeof document === "undefined") return null;
return createPortal(
<div className={styles.root}>
<div
className={styles.backdrop}
onClick={dismissible ? onClose : undefined}
aria-hidden="true"
/>
<div
ref={panelRef}
role="dialog"
aria-modal="true"
aria-labelledby={titleId}
aria-describedby={description ? descriptionId : undefined}
className={cx(styles.dialog, SIZE_CLASS[size], className)}
onKeyDown={onKeyDown}
>
<header className={styles.header}>
<div className={styles.heading}>
<h2 className={styles.title} id={titleId}>
{title}
</h2>
{description ? (
<p className={styles.description} id={descriptionId}>
{description}
</p>
) : null}
</div>
{showCloseButton ? (
<button
type="button"
className={styles.close}
onClick={onClose}
aria-label="Close dialog"
>
<svg viewBox="0 0 16 16" width="14" height="14" aria-hidden="true">
<path
d="M3 3l10 10M13 3L3 13"
stroke="currentColor"
strokeWidth="1.75"
/>
</svg>
</button>
) : null}
</header>
<div className={styles.body} data-focus-scope>{children}</div>
{footer ? <footer className={styles.footer}>{footer}</footer> : null}
</div>
</div>,
document.body,
);
}
export interface ConfirmDialogProps
extends Omit<DialogProps, "footer" | "children"> {
confirmLabel?: string;
cancelLabel?: string;
onConfirm: () => void;
/** Renders the confirm action as destructive. */
destructive?: boolean;
children?: React.ReactNode;
}
/**
* The two-action confirmation case, which is most dialogs in practice.
*/
export function ConfirmDialog({
confirmLabel = "Confirm",
cancelLabel = "Cancel",
onConfirm,
destructive = false,
...props
}: ConfirmDialogProps): React.JSX.Element | null {
return (
<Dialog
{...props}
footer={
<>
<Button variant="ghost" onClick={props.onClose}>
{cancelLabel}
</Button>
<Button
variant={destructive ? "danger" : "primary"}
onClick={() => {
onConfirm();
props.onClose();
}}
>
{confirmLabel}
</Button>
</>
}
/>
);
}ui/components/Dialog/dialog.module.css/* Dialog — a square panel on a hard scrim, lifted by an offset shadow. */
.root {
position: fixed;
inset: 0;
z-index: 101;
display: grid;
place-items: center;
padding: var(--fr-space-4);
overflow-y: auto;
}
.backdrop {
position: fixed;
inset: 0;
background: color-mix(in srgb, var(--fr-ink) 55%, transparent);
animation: fade var(--fr-duration-base) var(--fr-ease);
}
.dialog {
position: relative;
width: 100%;
max-height: calc(100vh - var(--fr-space-7));
display: flex;
flex-direction: column;
background: var(--fr-surface);
border: var(--fr-rule-width) solid var(--fr-rule);
border-radius: var(--fr-radius);
box-shadow: var(--fr-shadow-hard);
animation: dialogIn var(--fr-duration-base) var(--fr-ease);
}
@keyframes fade {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@keyframes dialogIn {
from {
opacity: 0;
transform: translateY(4px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@media (prefers-reduced-motion: reduce) {
.dialog {
animation: none;
}
}
.sizeSm {
max-width: 24rem;
}
.sizeMd {
max-width: 34rem;
}
.sizeLg {
max-width: 52rem;
}
.header {
display: flex;
align-items: flex-start;
gap: var(--fr-space-4);
padding: var(--fr-space-4);
border-bottom: var(--fr-rule-width) solid var(--fr-rule);
}
.heading {
flex: 1 1 auto;
min-width: 0;
}
.title {
font-size: var(--fr-text-h3);
font-weight: var(--fr-weight-semibold);
line-height: 1.2;
}
.description {
margin-top: var(--fr-space-1);
font-size: var(--fr-text-small);
color: var(--fr-ink-muted);
line-height: 1.45;
}
.close {
flex: none;
display: grid;
place-items: center;
width: 1.75rem;
height: 1.75rem;
padding: 0;
background: transparent;
border: var(--fr-rule-width) solid transparent;
border-radius: var(--fr-radius);
color: var(--fr-ink-muted);
cursor: pointer;
}
.close:hover {
background: var(--fr-surface-sunken);
color: var(--fr-ink);
border-color: var(--fr-rule-soft);
}
.body {
padding: var(--fr-space-4);
overflow-y: auto;
flex: 1 1 auto;
font-size: var(--fr-text-body);
color: var(--fr-ink-muted);
}
.footer {
display: flex;
justify-content: flex-end;
gap: var(--fr-space-2);
padding: var(--fr-space-3) var(--fr-space-4);
border-top: var(--fr-rule-width) solid var(--fr-rule-soft);
background: var(--fr-surface-sunken);
}ui/internal/cx.tsexport type ClassValue = string | false | null | undefined;
// The only part of `clsx` this library needs, without the dependency.
export function cx(...values: ClassValue[]): string {
const out: string[] = [];
for (const value of values) {
if (value) out.push(value);
}
return out.join(" ");
}ui/internal/focus.tsimport { useCallback, useEffect, useRef } from "react";
const FOCUSABLE = [
"a[href]",
"area[href]",
"button:not([disabled])",
"input:not([disabled]):not([type='hidden'])",
"select:not([disabled])",
"textarea:not([disabled])",
"iframe",
"object",
"embed",
"audio[controls]",
"video[controls]",
"summary",
"[contenteditable]:not([contenteditable='false'])",
"[tabindex]:not([tabindex='-1'])",
].join(",");
function isVisible(element: HTMLElement): boolean {
if (element.hasAttribute("hidden")) return false;
const rect = element.getBoundingClientRect();
// jsdom reports zero-sized rects for everything, so `offsetParent` is the fallback.
return rect.width > 0 || rect.height > 0 || element.offsetParent !== null;
}
export function getFocusable(container: HTMLElement | null): HTMLElement[] {
if (!container) return [];
const found = Array.from(
container.querySelectorAll<HTMLElement>(FOCUSABLE),
);
const visible = found.filter(isVisible);
return visible.length > 0 ? visible : found;
}
export interface FocusTrap {
/** Attach to the container's onKeyDown to cycle Tab within it. */
onKeyDown: (event: React.KeyboardEvent) => void;
}
// Confines Tab to `containerRef` while `active`, moving focus in on activation
// and returning it to the trigger on deactivation.
export function useFocusTrap(
active: boolean,
containerRef: React.RefObject<HTMLElement | null>,
): FocusTrap {
const previouslyFocused = useRef<HTMLElement | null>(null);
useEffect(() => {
if (!active) return;
const focused = document.activeElement;
previouslyFocused.current = focused instanceof HTMLElement ? focused : null;
const container = containerRef.current;
if (container) {
// Without the scope region, focus lands on the header close button and a
// reflexive Enter dismisses the dialog the user just opened.
const scoped = container.querySelector<HTMLElement>("[data-focus-scope]");
const autofocus =
container.querySelector<HTMLElement>("[data-autofocus]") ??
(scoped ? getFocusable(scoped)[0] : undefined) ??
getFocusable(container)[0] ??
container;
if (autofocus === container && !container.hasAttribute("tabindex")) {
container.setAttribute("tabindex", "-1");
}
autofocus.focus({ preventScroll: true });
}
return () => {
const previous = previouslyFocused.current;
if (previous && document.contains(previous)) {
previous.focus({ preventScroll: true });
}
};
}, [active, containerRef]);
const onKeyDown = useCallback(
(event: React.KeyboardEvent) => {
if (!active || event.key !== "Tab") return;
const container = containerRef.current;
if (!container) return;
const focusable = getFocusable(container);
if (focusable.length === 0) {
event.preventDefault();
container.focus();
return;
}
const first = focusable[0]!;
const last = focusable[focusable.length - 1]!;
const activeElement = document.activeElement;
if (event.shiftKey) {
if (activeElement === first || !container.contains(activeElement)) {
event.preventDefault();
last.focus();
}
} else if (activeElement === last || !container.contains(activeElement)) {
event.preventDefault();
first.focus();
}
},
[active, containerRef],
);
return { onKeyDown };
}
// Reference-counted so nested overlays do not fight over `body` overflow.
let scrollLocks = 0;
let originalOverflow = "";
let originalPaddingRight = "";
export function useScrollLock(active: boolean): void {
useEffect(() => {
if (!active) return;
if (scrollLocks === 0) {
const { body } = document;
originalOverflow = body.style.overflow;
originalPaddingRight = body.style.paddingRight;
// Replacing the scrollbar with padding keeps the page from shifting left.
const scrollbar = window.innerWidth - document.documentElement.clientWidth;
body.style.overflow = "hidden";
if (scrollbar > 0) {
body.style.paddingRight = `${scrollbar}px`;
}
}
scrollLocks += 1;
return () => {
scrollLocks -= 1;
if (scrollLocks === 0) {
document.body.style.overflow = originalOverflow;
document.body.style.paddingRight = originalPaddingRight;
}
};
}, [active]);
}ui/components/Button/Button.tsximport { cx } from "../../internal/cx";
import styles from "./button.module.css";
export type ButtonVariant = "primary" | "secondary" | "ghost" | "danger";
export type ButtonSize = "sm" | "md" | "lg";
/** Whichever element a branch renders, so the ref is the union of both. */
type ButtonNode = HTMLButtonElement | HTMLAnchorElement;
interface BaseProps {
variant?: ButtonVariant;
size?: ButtonSize;
/** Swaps the label for a spinner and blocks activation. Width stays stable. */
loading?: boolean;
fullWidth?: boolean;
/** Rendered before the label. Use a 16px square svg. */
iconStart?: React.ReactNode;
/** Rendered after the label. */
iconEnd?: React.ReactNode;
className?: string;
children?: React.ReactNode;
/** Points at the rendered `<a>` or `<button>`, whichever this instance is. */
ref?: React.Ref<ButtonNode>;
}
type ButtonAsButton = BaseProps &
Omit<React.ComponentProps<"button">, keyof BaseProps> & {
href?: undefined;
};
type ButtonAsLink = BaseProps &
Omit<React.ComponentProps<"a">, keyof BaseProps> & {
href: string;
};
export type ButtonProps = ButtonAsButton | ButtonAsLink;
const VARIANT_CLASS = {
primary: styles.variantPrimary,
secondary: styles.variantSecondary,
ghost: styles.variantGhost,
danger: styles.variantDanger,
} satisfies Record<ButtonVariant, string | undefined>;
const SIZE_CLASS = {
sm: styles.sizeSm,
md: styles.sizeMd,
lg: styles.sizeLg,
} satisfies Record<ButtonSize, string | undefined>;
/** Everything both forms need to paint themselves, already destructured. */
interface Chrome {
loading: boolean;
iconStart?: React.ReactNode;
iconEnd?: React.ReactNode;
children?: React.ReactNode;
}
function Spinner(): React.JSX.Element {
return (
<span aria-hidden="true" className={styles.spinner}>
<svg viewBox="0 0 16 16" width="16" height="16" fill="none">
<rect
x="1.5"
y="1.5"
width="13"
height="13"
stroke="currentColor"
strokeWidth="2"
strokeDasharray="26 26"
strokeDashoffset="13"
/>
</svg>
</span>
);
}
function Content({
loading,
iconStart,
iconEnd,
children,
}: Chrome): React.JSX.Element {
return (
<>
{loading ? <Spinner /> : null}
{iconStart ? <span className={styles.icon}>{iconStart}</span> : null}
<span className={styles.label}>{children}</span>
{iconEnd ? <span className={styles.icon}>{iconEnd}</span> : null}
</>
);
}
function classesFor(
variant: ButtonVariant,
size: ButtonSize,
loading: boolean,
fullWidth: boolean,
className: string | undefined,
): string {
return cx(
styles.button,
VARIANT_CLASS[variant],
SIZE_CLASS[size],
fullWidth && styles.full,
loading && styles.loading,
className,
);
}
// A callback ref is assignable to both `<a ref>` and `<button ref>`; a
// `Ref<A | B>` narrows to neither, which would need a cast.
function refSetter<T>(ref: React.Ref<T> | undefined): (node: T | null) => void {
return (node) => {
if (typeof ref === "function") ref(node);
else if (ref !== null && ref !== undefined) ref.current = node;
};
}
type ButtonAnchorProps = Omit<ButtonAsLink, "ref"> & {
ref?: React.Ref<HTMLAnchorElement>;
};
function ButtonAnchor({
ref,
...props
}: ButtonAnchorProps): React.JSX.Element {
const {
variant = "secondary",
size = "md",
loading = false,
fullWidth = false,
iconStart,
iconEnd,
className,
children,
href,
...anchorRest
} = props;
const classes = classesFor(variant, size, loading, fullWidth, className);
return (
<a
ref={ref}
href={href}
className={classes}
aria-busy={loading || undefined}
aria-disabled={loading || undefined}
tabIndex={loading ? -1 : undefined}
{...anchorRest}
>
<Content loading={loading} iconStart={iconStart} iconEnd={iconEnd}>
{children}
</Content>
</a>
);
}
type ButtonElementProps = Omit<ButtonAsButton, "ref"> & {
ref?: React.Ref<HTMLButtonElement>;
};
function ButtonElement({
ref,
...props
}: ButtonElementProps): React.JSX.Element {
const {
variant = "secondary",
size = "md",
loading = false,
fullWidth = false,
iconStart,
iconEnd,
className,
children,
type,
disabled,
...buttonRest
} = props;
const classes = classesFor(variant, size, loading, fullWidth, className);
return (
<button
ref={ref}
type={type ?? "button"}
className={classes}
disabled={disabled || loading}
aria-busy={loading || undefined}
{...buttonRest}
>
<Content loading={loading} iconStart={iconStart} iconEnd={iconEnd}>
{children}
</Content>
</button>
);
}
// The union is narrowed once, on `typeof href`, so each branch owns exactly one
// element's props. `refSetter` is what lets the union ref reach either element.
export function Button(props: ButtonProps): React.JSX.Element {
if (typeof props.href === "string") {
const { ref, ...anchorProps } = props;
return <ButtonAnchor {...anchorProps} ref={refSetter(ref)} />;
}
const { ref, ...buttonProps } = props;
return <ButtonElement {...buttonProps} ref={refSetter(ref)} />;
}ui/components/Button/button.module.css/* Button — square, ruled, and flat. The only motion is a colour change. */
.button {
--button-height: var(--fr-control-md);
--button-pad: var(--fr-space-4);
position: relative;
display: inline-flex;
align-items: center;
justify-content: center;
gap: var(--fr-space-2);
min-height: var(--button-height);
padding: 0 var(--button-pad);
border: var(--fr-rule-width) solid transparent;
border-radius: var(--fr-radius);
font-family: var(--fr-font-sans);
font-size: var(--fr-text-body);
font-weight: var(--fr-weight-semibold);
letter-spacing: -0.005em;
line-height: 1;
text-decoration: none;
white-space: nowrap;
cursor: pointer;
user-select: none;
transition:
background-color var(--fr-duration-fast) var(--fr-ease),
color var(--fr-duration-fast) var(--fr-ease),
border-color var(--fr-duration-fast) var(--fr-ease);
}
.button:focus-visible {
outline: var(--fr-focus-width) solid var(--fr-accent);
outline-offset: var(--fr-focus-offset);
}
/* The primary button is filled with the accent, so a ring in the same colour
would be invisible against its own edge. Focus marks it in ink instead. */
.variantPrimary:focus-visible,
.variantDanger:focus-visible {
outline-color: var(--fr-ink);
}
/* Sizes */
.sizeSm {
--button-height: var(--fr-control-sm);
--button-pad: var(--fr-space-3);
font-size: var(--fr-text-small);
}
.sizeLg {
--button-height: var(--fr-control-lg);
--button-pad: var(--fr-space-5);
font-size: 1rem;
}
.full {
width: 100%;
}
/* Variants: primary is the strongest call to action, secondary the workhorse,
ghost has no chrome until hovered, danger is for destructive confirmation. */
.variantPrimary {
background: var(--fr-accent);
color: var(--fr-accent-ink);
border-color: var(--fr-accent);
}
.variantPrimary:hover:not(:disabled) {
background: var(--fr-accent-hover);
border-color: var(--fr-accent-hover);
/* Restated because the base layer's `a:hover` outranks this class, and a link
button would otherwise repaint its label to the hover background. */
color: var(--fr-accent-ink);
}
.variantPrimary:active:not(:disabled) {
background: var(--fr-ink);
border-color: var(--fr-ink);
color: var(--fr-paper);
}
.variantSecondary {
background: var(--fr-surface);
color: var(--fr-ink);
border-color: var(--fr-rule-strong);
}
.variantSecondary:hover:not(:disabled) {
background: var(--fr-surface-sunken);
/* See the note on primary. */
color: var(--fr-ink);
}
.variantSecondary:active:not(:disabled) {
background: var(--fr-ink);
color: var(--fr-paper);
}
.variantGhost {
background: transparent;
color: var(--fr-ink);
border-color: transparent;
}
.variantGhost:hover:not(:disabled) {
background: var(--fr-surface-sunken);
/* See the note on primary. */
color: var(--fr-ink);
}
.variantGhost:active:not(:disabled) {
background: var(--fr-rule-soft);
}
.variantDanger {
background: var(--fr-danger);
color: var(--fr-danger-ink);
border-color: var(--fr-danger);
}
.variantDanger:hover:not(:disabled) {
background: var(--fr-ink);
border-color: var(--fr-ink);
color: var(--fr-paper);
}
/* Disabled and loading */
.button:disabled,
.button[aria-disabled="true"] {
cursor: not-allowed;
background: var(--fr-disabled-surface);
color: var(--fr-disabled-ink);
border-color: var(--fr-disabled-surface);
}
.variantGhost:disabled,
.variantGhost[aria-disabled="true"] {
background: transparent;
}
.loading {
cursor: progress;
}
/* The label keeps its box while loading, so the button never resizes. */
.loading .label {
opacity: 0.75;
}
.icon {
display: inline-flex;
align-items: center;
flex: none;
}
.icon > svg {
width: 1rem;
height: 1rem;
}
.spinner {
display: inline-flex;
flex: none;
animation: fr-spin 700ms linear infinite;
}
@keyframes fr-spin {
to {
transform: rotate(360deg);
}
}
@media (prefers-reduced-motion: reduce) {
.spinner {
animation-duration: 2s;
}
}Every component reads from these two files. Copy them once, on your first component, and reuse them thereafter.
ui/styles/tokens.css
/* Every ground is white and every value below cleared AA on it; dark mode
re-derives rather than inverting. Check contrast again before changing a value. */
:root {
/* The only radius token. Do not override per component. */
--fr-radius: 0px;
/* Core surfaces — flat by default; separation comes from rules, not fills. */
--fr-paper: #ffffff;
--fr-surface: #ffffff;
--fr-surface-sunken: #f5f5f5;
--fr-ink: #262626;
--fr-ink-muted: #525252;
--fr-ink-subtle: #737373;
/* Rules — soft divides, rule bounds, strong marks a control edge. A boundary
is a hairline tone; only controls and active markers earn the ink one. */
--fr-rule: #e5e5e5;
--fr-rule-strong: #262626;
--fr-rule-soft: #ededed;
/* Accent: Emerald. Reserved for interaction, never decoration. `bright` is
ornament only — marks and arrows, never a label or a fill that carries one. */
--fr-accent: #047857;
--fr-accent-ink: #ffffff;
--fr-accent-soft: #ecfdf5;
--fr-accent-hover: #065f46;
--fr-accent-bright: #10b981;
/* Status. Success stays a true green rather than a second emerald, so a badge
tone never reads as the accent. */
--fr-danger: #a81f24;
--fr-danger-soft: #fbeaeb;
--fr-danger-ink: #ffffff;
--fr-success: #15803d;
--fr-success-soft: #f0fdf4;
--fr-success-ink: #ffffff;
--fr-warning: #7a5312;
--fr-warning-soft: #f8efdf;
--fr-warning-ink: #ffffff;
/* Inverse surfaces */
--fr-inverse-surface: #262626;
--fr-inverse-ink: #ffffff;
/* Disabled */
--fr-disabled-surface: #f0f0f0;
--fr-disabled-ink: #737373;
/* Hard shadow: no blur, ever. */
--fr-shadow-hard: 3px 3px 0 var(--fr-rule-strong);
--fr-shadow-hard-sm: 2px 2px 0 var(--fr-rule-strong);
/* No font ships with the library, so this is the platform's own UI face. */
--fr-font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue",
Arial, sans-serif;
--fr-font-mono: ui-monospace, "SF Mono", "Menlo", "Consolas", monospace;
--fr-text-display: 3.5rem;
--fr-text-h1: 2.5rem;
--fr-text-h2: 1.75rem;
--fr-text-h3: 1.25rem;
--fr-text-body: 0.9375rem;
--fr-text-small: 0.8125rem;
--fr-text-micro: 0.6875rem;
--fr-leading-display: 1.02;
--fr-leading-heading: 1.15;
--fr-leading-body: 1.6;
--fr-tracking-micro: 0.08em;
--fr-tracking-display: -0.02em;
--fr-weight-regular: 400;
--fr-weight-medium: 500;
--fr-weight-semibold: 600;
--fr-weight-bold: 700;
/* Spacing scale — 4px base */
--fr-space-0: 0;
--fr-space-1: 0.25rem;
--fr-space-2: 0.5rem;
--fr-space-3: 0.75rem;
--fr-space-4: 1rem;
--fr-space-5: 1.5rem;
--fr-space-6: 2rem;
--fr-space-7: 3rem;
--fr-space-8: 4rem;
--fr-space-9: 6rem;
/* Sizing */
--fr-control-sm: 1.75rem;
--fr-control-md: 2.25rem;
--fr-control-lg: 2.75rem;
--fr-rule-width: 1px;
--fr-rule-width-strong: 2px;
--fr-focus-width: 2px;
--fr-focus-offset: 2px;
/* Motion — short and linear; this library does not bounce. */
--fr-duration-fast: 90ms;
--fr-duration-base: 140ms;
--fr-ease: cubic-bezier(0.2, 0, 0, 1);
/* Layers */
--fr-z-sticky: 10;
--fr-z-overlay: 100;
--fr-z-dialog: 101;
/* Layout */
--fr-container: 78rem;
--fr-measure: 68ch;
}
/* Dark re-derives rather than inverting: the ground stays near-black and the
accent lifts so it still clears AA (#34D399 on #0A0A0A is 10.33:1). */
[data-theme="dark"] {
--fr-paper: #0a0a0a;
--fr-surface: #141414;
--fr-surface-sunken: #1c1c1c;
--fr-ink: #f5f5f5;
--fr-ink-muted: #a3a3a3;
--fr-ink-subtle: #7a7a7a;
--fr-rule: #3a3a3a;
--fr-rule-strong: #f5f5f5;
--fr-rule-soft: #2a2a2a;
--fr-accent: #34d399;
--fr-accent-ink: #022c22;
--fr-accent-soft: #052e22;
--fr-accent-hover: #6ee7b7;
--fr-accent-bright: #34d399;
--fr-danger: #fca5a5;
--fr-danger-soft: #2e1214;
--fr-danger-ink: #240708;
--fr-success: #86efac;
--fr-success-soft: #0d2415;
--fr-success-ink: #04150c;
--fr-warning: #e2b262;
--fr-warning-soft: #2a2011;
--fr-warning-ink: #1c1305;
--fr-inverse-surface: #f5f5f5;
--fr-inverse-ink: #0a0a0a;
--fr-disabled-surface: #1f1f1f;
--fr-disabled-ink: #6a6a6a;
--fr-shadow-hard: 3px 3px 0 #f5f5f5;
--fr-shadow-hard-sm: 2px 2px 0 #f5f5f5;
}
/* Opt-in inverse region: use for a closing band or a deliberate contrast strip. */
[data-theme="inverse"] {
--fr-paper: #171717;
--fr-surface: #171717;
--fr-surface-sunken: #242424;
--fr-ink: #ffffff;
--fr-ink-muted: #b5b5b5;
--fr-ink-subtle: #8a8a8a;
--fr-rule: #3d3d3d;
--fr-rule-strong: #ffffff;
--fr-rule-soft: #2e2e2e;
--fr-accent: #34d399;
--fr-accent-ink: #022c22;
--fr-accent-soft: #052e22;
--fr-accent-hover: #6ee7b7;
--fr-accent-bright: #34d399;
--fr-inverse-surface: #ffffff;
--fr-inverse-ink: #171717;
--fr-shadow-hard: 3px 3px 0 #ffffff;
--fr-shadow-hard-sm: 2px 2px 0 #ffffff;
}
@media (prefers-reduced-motion: reduce) {
:root {
--fr-duration-fast: 0ms;
--fr-duration-base: 0ms;
}
}ui/styles/base.css
/* Only what a module cannot scope: the page reset. Everything a component owns
lives in its .module.css, so no global class here can collide with yours. */
*,
*::before,
*::after {
box-sizing: border-box;
}
html {
-webkit-text-size-adjust: 100%;
text-size-adjust: 100%;
}
body {
margin: 0;
font-family: var(--fr-font-sans);
font-size: var(--fr-text-body);
line-height: var(--fr-leading-body);
color: var(--fr-ink);
background: var(--fr-paper);
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
h1,
h2,
h3,
h4,
h5,
h6,
p,
figure,
blockquote,
dl,
dd {
margin: 0;
}
ul,
ol {
margin: 0;
padding: 0;
}
h1,
h2,
h3,
h4,
h5,
h6 {
font-weight: var(--fr-weight-semibold);
line-height: var(--fr-leading-heading);
letter-spacing: -0.02em;
text-wrap: balance;
}
a {
color: var(--fr-accent);
text-decoration-thickness: 1px;
text-underline-offset: 2px;
}
a:hover {
color: var(--fr-accent-hover);
}
img,
svg,
video,
canvas {
display: block;
max-width: 100%;
}
button,
input,
select,
textarea {
font: inherit;
color: inherit;
letter-spacing: inherit;
}Then import the component from its file. A page that holds state needs "use client".
/* app/layout.tsx — once, before your own CSS */
import "../ui/styles/tokens.css";
import "../ui/styles/base.css";// app/page.tsx
"use client";
import { Dialog, ConfirmDialog } from "../ui/components/Dialog/Dialog";Basic
The dialog is rendered only while open is true, so its children unmount on close. If you need to preserve form state across open and close, lift it into the parent — which is also where it belongs if the form is meaningful.
Sizes
sm is 24rem and suits a confirmation. md is 34rem and is the default. lg is 52rem, for a dialog that genuinely contains a form with several fields.
A dialog that needs lg is usually a page. Resist the temptation.
Confirmation
ConfirmDialog composes Dialog with a cancel/confirm footer. destructive makes the confirm action the red danger variant. It closes itself after onConfirm runs.
Non-dismissible
Use this only when there is genuinely no safe way to abort — a migration already in flight, for example. A dialog the user cannot escape is a dialog they will resent.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| open* | boolean | — | Controls visibility. Children unmount when false. |
| onClose* | () => void | — | Called on Escape, backdrop click, and the close button. |
| title* | string | — | Rendered as the heading and used as the accessible name (aria-labelledby). |
| description | string | — | Secondary line, linked with aria-describedby. |
| size | 'sm' | 'md' | 'lg' | md | Maximum panel width: 24rem, 34rem, or 52rem. |
| dismissible | boolean | true | Allows Escape and backdrop dismissal. |
| showCloseButton | boolean | true | Renders the close affordance in the header. |
| footer | ReactNode | — | Footer content, usually actions. |
Accessibility
role="dialog"andaria-modal="true", labelled by the title and described by the description.- Focus moves into the panel on open, cycles within it on Tab and Shift+Tab, and returns to the previously focused element on close.
- Escape closes unless
dismissibleis false. The handler is ondocument, so it works regardless of where focus sits inside the panel. - Body scroll is locked while open, with scrollbar-width compensation so the page does not jump. The lock is reference-counted, so nested overlays do not fight over it.
- The backdrop is
aria-hidden, so it is not reachable by assistive technology. - The close button has an explicit
aria-label="Close dialog".
Do and don't
title prop, not as a heading inside children.