This repository contains the source code and documentation for Base UI: a headless, unstyled React component library.
- Source code for components and private utils is in
packages/react/. - Source code for public shared utils is in
packages/utils/. - Experiments are located at
docs/src/app/(private)/experiments/. Use for creating demos that require manual testing in the browser. - Public documentation is located at
docs/src/app/(docs)/react/. Alter the docs where necessary when changes must be visible to library users. - When creating public demos on the docs, refer to the
herodemo for the given component and largely follow its styles (both CSS Modules and Tailwind CSS versions). Other demos may also contain relevant styling. Do not add custom styling beyond the critical layout styles necessary for new demos.
- The shared
/base-ui-reviewskill lives in.agents/skills/base-ui-review/SKILL.md. Update that file when the Base UI review workflow changes. - Claude Code discovers the same shared skill through
.claude/skills/base-ui-review/SKILL.md, which delegates to the.agentscopy. - The review skill is opt-in: only run it when the user explicitly asks for it by name (
/base-ui-review). Do not trigger it from a generic review request or after finishing a change.
- Always use the
useTimeoututility from@base-ui/utils/useTimeoutinstead ofwindow.setTimeout, anduseAnimationFramefrom@base-ui/utils/useAnimationFrameinstead ofrequestAnimationFrame. Search for other example usage in the codebase if unsure how to use them. - Use the
useStableCallbackutility from@base-ui/utils/useStableCallbackinstead ofReact.useCallbackif the function is called within an effect or event handler. The utility cannot be used to memoize functions that are called directly in the body of a component (during render), so continue withReact.useCallbackin those scenarios. - Always use the
useIsoLayoutEffectutility from@base-ui/utils/useIsoLayoutEffectinstead ofReact.useLayoutEffect. - Always use the shadow DOM-safe utilities for DOM traversal and event targeting:
closest,contains,getTarget, andactiveElement. Always use the owner utilitiesownerDocumentandownerWindowinstead of globaldocument/windowlookups when the code is tied to a DOM node, including realm-sensitive checks such asinstanceof. - Avoid duplicating logic where necessary. If two components can share logic (such as event handlers), define the logic/handlers in the parent and share it through a context to the child; use the existing context if it exists.
- In CSS Modules demos (
docs/src/app/(docs)/**/demos/**/*.module.css), use raw color values from the Tailwind@themeblock indocs/src/css/index.css. For example, useoklch(14.5% 0 0deg)instead ofvar(--color-neutral-950). - When using
user-select: none;in CSS, also add-webkit-user-select: none;to support Safari. Tailwind'sselect-noneclass already includes this.
- Do not randomly cast (for example
as any) if there are no type errors without doing so. Runpnpm typescriptto verify types. - Ensure your changes pass linting - run
pnpm eslint. - Ensure your styles pass stylelint - run
pnpm stylelint. - Ensure your changes are formatted correctly - run
pnpm prettier. - When you change a public component API (props or JSDoc), run
pnpm docs:api.
- If a repository command fails because dependencies are unavailable, run
pnpm ifirst and then retry the command. - Run tests in jsdom env with
pnpm test:jsdom {name} --no-watchsuch aspnpm test:jsdom NumberField --no-watchorpnpm test:jsdom parse --no-watch. - Run tests in Chromium env with
pnpm test:chromium {name} --no-watchsuch aspnpm test:chromium NumberField --no-watchorpnpm test:chromium parse --no-watch. - In
@base-ui/reacttests, do not callawait flushMicrotasks()directly afterawait render(...)when there are no interactions or state changes between them; the#test-utilsrenderis async and already awaited, so that immediate flush is unnecessary. - Do not group multiple
expect()assertions in a singlewaitFor()callback. Use one assertion perwaitFor()so retries are scoped to the specific condition that may change asynchronously. - For locale-formatted text, use
expect(element.textContent).toBe(expected)with the expected value formatted using the same locale and options as the component. When the component uses the runtime default locale, derive expectations using that default too (for example,new Intl.NumberFormat(undefined, options)); do not hardcodeen-USor US-formatted strings in expected values. Pin a locale only when testing locale-specific behavior, and use it consistently for both the component and expectations.toHaveTextContentnormalizes nonbreaking spaces in the rendered text but not in the expected string, causing failures in locales such aspt-BR. - Use
firePointerfrom#test-utilsinstead offireEvent.pointer*whenever a test depends on event timing.fireEventsilently dropstimeStamp, so the event inherits the environment's clock — the real one in a browser — and gesture velocity then depends on how long the runner took between calls. A lint rule enforces this. - If you made changes to the source code, ensure you verify your changes by running tests (see above), and writing new tests where applicable. If tests require the browser because, for example, they require layout measurements, restrict it to the Chromium env by using
it.skipIf(isJSDOM)ordescribe.skipIf(isJSDOM)(search other tests for example usage if unsure). - Follow the established conventions in existing tests. Each file/component is tested with the filename
name.test.tsx. For example,PopoverRoot.test.tsxis next to its source filePopoverRoot.tsx. - Tests use Vitest APIs only:
expect(),vi.fn(), and@testing-library/jest-domDOM matchers. Do not use Chai- or Sinon-style matcher chains or spies. - Import
createRenderer,describeConformanceand other helpers from#test-utils, and use theuserreturned byawait render(...)instead of creating a Testing Library user-event instance withuserEvent.setup(). - A test must be able to fail when the behavior in its title breaks. Before you finish a test, break that behavior in the source and check that the test fails, then revert. In particular:
- Assert the starting state before the action when the end state could already hold (for example, move focus away before testing that it returns).
- For "does not X when " tests, use an interaction that would do X without the guard, ideally with a sibling case that flips only the guard and shows X happening.
- When the title names an intermediate state ("while open", "after hover", "during swipe"), assert that state right before the action under test.
- Fire events the way the component expects them.
fireEvent.clickon a list item is a virtual click that items may ignore; preferuser.click. ApreventDefaultspy in afireEventinit object is never called; assert on the booleanfireEventreturns instead. - Never put negative assertions inside
waitFor(they pass on the first poll); flush or advance timers first, then assert synchronously. - Keep assertions in the test body: an
expectinside a callback that may not run, a conditional, orPromise.allSettledcan silently pass.
- Use
it.each(object rows with$nametitles) ordescribe.eachfor tests that differ only in data; don't register tests or suites insideforEachormap. Every test inside a parametrized block must use the parameter. - A test belongs in the file of the part whose prop or behavior it asserts, and under
describe('prop: X')only if it setsX. Titles must describe what the body asserts. - Shared behavior across components belongs in a shared suite in
packages/react/test, exported from#test-utils, rather than copied into each component's tests. Existing suites:describeConformance(every part),popupConformanceTests(popup roots),popupListConformanceTests(list interactions in Select, Menu and Combobox popups),popupFocusPropsTests(initialFocus/finalFocus),dialogRootSharedTests(Dialog and AlertDialog),detachedTriggersConformanceTests,positionerConformanceTestsandviewportConformanceTests. Don't re-test what a suite already covers for the same part; extend the suite instead. - After every test,
test/setupVitest.tsresetsBASE_UI_ANIMATIONS_DISABLED, mocks, deduplicated errors and warnings, and the animation frame scheduler, andcreateRendererunmounts what it rendered; don't repeat that per file.
- Commit messages follow the format
[scope] Imperative summary(for example[popover] Fix focus trap). Choose scopes that mirror package or component names that were changed. - Use
[all components]scope for changes that broadly affect most components.
These guidelines apply only to errors thrown by public packages.
Every error message must:
- Say what happened - Describe the problem clearly
- Say why it's a problem - Explain the consequence
- Point toward how to solve it - Give actionable guidance
Format:
- Prefix with
Base UI: - Use string concatenation for readability
- Include a documentation link when applicable (
https://base-ui.com/...)
You MUST run pnpm extract-error-codes to update docs/src/error-codes.json every time you add or update an error message in an Error constructor.
Important: If the update created a new error code, but the new and original message have the same number of arguments and semantics haven't changed, update the original error in error-codes.json instead of creating a new code.
Before any changes, review CONTRIBUTING.md for more detailed guidelines on contributing to this repository.