Adding a component
The conventions every new component must follow.
The conventions a new component has to follow to belong in this library. They exist so the thirteenth component looks like the first twelve.
Where the files go
The canonical component is the CSS Modules one, and it lives in its own folder with its styles and its test side by side:
packages/ui/src/flavors/css-modules/components/Badge/
Badge.tsx
badge.module.css
Badge.test.tsxNaming: PascalCase.tsx, kebab-case.module.css, PascalCase.test.tsx. The stylesheet is imported by the component, so a reader copies it along with the .tsx and the class names come with it.
The canonical tree under packages/ui/src/components/ is what the docs site renders and what the existing tests cover. A new component is added there first, exactly as before, and the three flavor trees under packages/ui/src/flavors/ follow it.
Keep the import graph shallow and relative. A component may import from ../../internal/ and from another component's folder, and nothing else — those relative paths are what let a reader copy a folder into their project and have it resolve, so a new import edge is a new file they must copy.
The component file
Four rules, in order of how often they are broken:
- No dependencies. Not a helper package, not an icon set, not a utility library. If you need a function, write it in
src/internal/and keep it short. - No inline styles for anything on the token scale. Colours, spacing, sizes, durations all come from
var(--fr-*). - No new radius.
border-radius: var(--fr-radius)or nothing. - Take
refas a prop, and spread...rest. React 19 handsrefto a function component as an ordinary prop, so there is nothing to wrap. A component that swallows props is a component you cannot compose with.
import { cx } from "../../internal/cx";
export interface PanelProps extends React.ComponentProps<"div"> {
tone?: "plain" | "accent";
}
export function Panel({
tone = "plain",
className,
children,
...rest
}: PanelProps): React.JSX.Element {
return (
<div
className={cx("fr-panel", `fr-panel--${tone}`, className)}
{...rest}
>
{children}
</div>
);
}React.ComponentProps<"div"> is what makes this work: it includes ref, which React.HTMLAttributes<HTMLDivElement> does not. ref rides along inside ...rest and reaches the element, so the component forwards it without naming it.
Note the destructuring of every prop the component owns before ...rest. Anything left in rest reaches the DOM, so a custom prop that is not destructured becomes an invalid HTML attribute and a React warning.
Never import forwardRef. It still works in React 19 but is deprecated, and this library does not use it.
Forms use Field
Any component that renders a form control must use the Field primitive rather than wiring htmlFor, aria-describedby, and aria-invalid itself. Field also provides useFieldContext() so a control can pick up the ids when it is used inside a parent field.
export interface PanelInputProps extends React.ComponentProps<"input"> {
label?: string;
hint?: string;
error?: string;
}
export function PanelInput({
label,
hint,
error,
id,
...rest
}: PanelInputProps): React.JSX.Element {
const control = <PanelInputControl id={id} error={error} {...rest} />;
if (label === undefined) return control;
return (
<Field label={label} hint={hint} error={error} id={id}>
{control}
</Field>
);
}ref needs no separate mention here: it is part of ComponentProps<"input">, so it sits in ...rest and travels with the rest of the element's props into PanelInputControl, which spreads them onto the <input>.
States a component must handle
Every interactive component needs all of these before it ships:
| State | Requirement |
| --- | --- |
| Default | — |
| Hover | Uses --fr-surface-sunken, never a new colour |
| Focus visible | 2px ring at 2px offset, square — accent, or ink on an accent-filled control |
| Disabled | Native disabled, plus --fr-disabled-* tokens |
| Invalid | aria-invalid, plus a rule in --fr-danger |
Focus must never be removed. If the design calls for a focus style other than the default ring, it still has to be a visible 2px square.
Styles
- Prefix every class with
fr-. The canonical tree is a global stylesheet; a generic.cardwill collide with the host application. In the CSS Modules flavor the prefix is a reading aid rather than a requirement, since the class name is hashed either way — keep it anyway, so the two trees can be compared line by line. - Use BEM-ish naming:
.fr-card,.fr-card__header,.fr-card--raised. - One modifier per axis, composed with
cx. - No
!important. If you need it, the cascade is wrong. - No
@media (max-width)inside component CSS unless the component genuinely reflows. Layout belongs to the consumer.
Three flavors
A new component is not finished until it exists in all three. The point of the copy block is that a reader gets a component in the styling system they already use, so a flavor that lags behind is a component that is only half-published.
Port in this order, because each one is a stricter test of the previous:
- CSS Modules. The canonical implementation. Add
components/<Name>/<Name>.tsxand<name>.module.cssunderflavors/css-modules/, importing any shared module frominternal/. - Tailwind. Same props, same ARIA, and every visual decision as a utility class resolving to a theme name. Where a pseudo-element or a keyframe is genuinely needed, ship a small stylesheet beside the component and say so in a comment — an unexplained
.cssfile in this tree is a bug. - StyleX. Same again, with
stylex.createandstylex.props, and every value read fromtokens.stylex.ts. Use thesxandxpropshelpers ininternal/sx.tsrather than callingstylex.propsdirectly.
Then add the component to src/flavors/flavors.test.tsx. That test renders each component in all three flavors and asserts the same role, the same accessible name, the same compound keys, and the same prop surface. It is the only thing that keeps the trees from drifting, and it is not optional.
Tests
Every component ships a test file covering, at minimum:
- It renders and is reachable by its accessible role and name.
- The ARIA wiring is correct (label, description, invalid state).
- Keyboard behaviour, if it has any.
- Controlled and uncontrolled usage, if it holds state.
- That its own props are not leaked onto the DOM node.
it("does not leak its own props onto the DOM node", () => {
render(<Panel tone="accent" aria-label="Panel" />);
const panel = screen.getByLabelText("Panel");
expect(panel.hasAttribute("tone")).toBe(false);
});That last one is not pedantry. It catches a missing destructure, which otherwise produces a React console error on every render in the consumer's app — a bug they will blame on React, not on you.
Documentation
A new component is not finished until it has a page:
- Add an entry to
apps/docs/lib/nav.ts. That single array drives the sidebar, the previous/next links, the page header, and the landing-page index. - Add the component's stylesheet to
src/styles/index.css, so the docs site renders it. - Export it from
src/index.ts— the docs site's live previews import from there. - Write
apps/docs/demos/<name>.tsxwith a'use client'demo per section. - Write
apps/docs/app/docs/components/<name>/page.mdxwith: an intro sentence, a Copy source block as the first section, live previews, a props table, an accessibility section, and do/don't. - Add a
metadataexport — notimport type { Metadata }. The MDX compiler's acorn pass cannot parse type-only imports, and the page will fail to compile.
The copy block is one line, and it prints the component's real files, its stylesheet, and every file it imports — in whichever of the three flavors the reader picks:
<CopySource components={["Badge"]} />It goes directly after the intro paragraph, before the first example. A reader arriving on the page should be able to see how large the thing is and what it needs before they read an argument for it.
A reader who lands on that page has to be able to copy their way to a working component. If the component needs a file outside its own folder, CopySource follows the import and prints that too — which is the check that keeps the import graph honest.
Badge is the smallest complete example.pnpm typecheck, pnpm test, pnpm lint, and pnpm build before opening a PR.Checklist
- [ ] No new dependencies — the folder stands on React alone
- [ ] Every owned prop destructured; nothing leaks to the DOM
- [ ]
ComponentProps<"tag">for the prop type, sorefis part of it - [ ]
refreaches the element, through...restor a named destructure - [ ] No
forwardRef - [ ] Styles use tokens;
border-radius: var(--fr-radius) - [ ] Focus ring visible and square
- [ ] Form controls built on
Field - [ ] Test file covering role, ARIA, keyboard, and prop leakage
- [ ] Implemented in all three flavors: CSS Modules, Tailwind, StyleX
- [ ] Added to
src/flavors/flavors.test.tsxand passing in all three - [ ] Any
.cssfile in the Tailwind tree explains why it could not be a utility - [ ] Exported from
src/index.ts - [ ] Stylesheet added to
src/styles/index.css - [ ] Nav entry added in
apps/docs/lib/nav.ts - [ ] Docs page with a
<CopySource />block first, then previews, props, and accessibility notes