Frontend Recipes

Quick start

Build a small form and dialog in five minutes.

This page builds a working settings form: a labelled field, a radio group, a primary action, and a confirmation dialog. Every component is copied from its own page, and none of them is wrapped in anything.

Copy a component

Every component page opens with a Copy source block containing the real files. Here is the smallest one, Button, so you can see the shape of it. The picker above it switches between CSS Modules, Tailwind, and StyleX — pick one and stay with it.

Styling

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/Button/Button.tsx
tsx
import { 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
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;
  }
}
ui/internal/cx.ts
ts
export 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(" ");
}

Every component reads from these two files. Copy them once, on your first component, and reuse them thereafter.

ui/styles/tokens.css
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
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".

css
/* app/layout.tsx — once, before your own CSS */
import "../ui/styles/tokens.css";
import "../ui/styles/base.css";
tsx
// app/page.tsx
"use client";

import { Button } from "../ui/components/Button/Button";

That is the whole setup. No manifest entry, no build step, no import specifier to resolve. The form below also needs Input, Radio, and Dialog — copy each from its page the same way, and take the shared tokens.css and base.css once.

The form

This is the entire component: four pieces of local state and the copied files.

A working settings form
Plan

What this example demonstrates

Labels are not optional

Every form control takes a label. The component renders it and wires htmlFor, aria-describedby, and aria-invalid for you. There is no path where a control has no accessible name, because there is no prop that removes one.

Errors are announced

Passing error does three things at once: the control gets aria-invalid="true", the message is linked via aria-describedby, and the message is rendered in a role="alert" region so a screen reader interrupts to read it. You do not need to manage any of that.

The dialog traps focus

Dialog is a portal to document.body with a focus trap, Escape handling, a scroll lock, and focus restoration on close. The element that opened it gets focus back when it closes — try it with the keyboard.

Composing rather than configuring

Notice that the form is not a <Form> component with a schema prop. It is ordinary React with a grid doing layout. The library gives you the interactive primitives and stays out of the composition, which is why there is no FormField abstraction to learn.

When you need spacing, use the token scale directly:

tsx
<div style={{ display: "grid", gap: "var(--fr-space-4)" }}>

That is the whole layout system for application code. If you would rather not write inline styles, use a class with the same properties — the tokens are the contract, not the inline style.

Editing a component

Because the component is a file in your project, changing it is a normal edit. It is not a fork and it is not a patch: the file you have is the library. When a component page ships a fix, the copy block shows the fixed version, and applying it is a diff you make yourself.