# Base UI This is the documentation for the `@base-ui/react` package. It contains a collection of components and utilities for building user interfaces in React. The library is designed to be composable and styling agnostic. The Tailwind CSS examples are written for Tailwind CSS v4. If `package.json` uses Tailwind CSS v3, automatically convert unsupported styles to v3-compatible equivalents. ## Overview ### Quick start A quick guide to getting started with Base UI. #### Install the library Install Base UI using a package manager. ##### pnpm ```bash pnpm add @base-ui/react ``` ##### npm ```bash npm i @base-ui/react ``` ##### yarn ```bash yarn add @base-ui/react ``` ##### bun ```bash bun add @base-ui/react ``` All components are included in a single package. Base UI is tree-shakable, so your app bundle will contain only the components that you actually use. #### Set up ##### Portals Base UI uses portals for components that render popups, such as Dialog and Popover. To make portaled components always appear on top of the entire page, add the following style to your application layout root: ```tsx title="layout.tsx"
{/* prettier-ignore */} {children}
``` ```css title="styles.css" .root { isolation: isolate; } ``` This style creates a separate stacking context for your application's `.root` element. This way, popups always appear above the page contents, and any `z-index` property in your styles won't interfere with them. ##### iOS 26+ Safari Starting with iOS 26, Safari allows content beneath the UI chrome to be visible. Backdrops such as those used by dialogs must use `position: absolute` instead of `position: fixed` to cover the entire visual viewport. For this to work after the page is scrolled, the following style must be added to your global styles: ```css title="styles.css" body { position: relative; } ``` #### Assemble a component This demo shows you how to import a [Popover](#popover) component, assemble its parts, and apply styles. There are examples for both Tailwind and CSS Modules below, but since Base UI is unstyled, you can use CSS-in-JS, plain CSS, or any other styling solution you prefer. #### Demo ##### Tailwind This example shows how to implement the component using Tailwind CSS. ```tsx /* index.tsx */ import { Popover } from '@base-ui/react/popover'; const triggerClassName = 'flex h-8 items-center justify-center border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 px-3 text-sm font-normal whitespace-nowrap text-neutral-950 dark:text-white select-none hover:not-data-disabled:bg-neutral-100 dark:hover:not-data-disabled:bg-neutral-800 active:not-data-disabled:bg-neutral-200 dark:active:not-data-disabled:bg-neutral-700 data-disabled:border-neutral-500 data-disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:data-disabled:border-neutral-400 dark:data-disabled:text-neutral-400 data-popup-open:bg-neutral-100 dark:data-popup-open:bg-neutral-800 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white'; export default function ExamplePopover() { return ( Notifications Notifications You are all caught up. Good job! ); } ``` ##### CSS Modules This example shows how to implement the component using CSS Modules. ```css /* index.module.css */ .Positioner { width: var(--positioner-width); height: var(--positioner-height); max-width: var(--available-width); } .Popup { box-sizing: border-box; position: relative; display: flex; flex-direction: column; gap: 0.25rem; padding: 0.75rem; outline: none; border: 1px solid oklch(14.5% 0 0deg); background-color: white; color: oklch(14.5% 0 0deg); box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%); transform-origin: var(--transform-origin); transition: transform 100ms ease-out, opacity 100ms ease-out; width: var(--popup-width, auto); height: var(--popup-height, auto); max-width: 500px; @media (prefers-color-scheme: dark) { border: 1px solid white; background-color: oklch(14.5% 0 0deg); color: white; box-shadow: none; } &[data-starting-style], &[data-ending-style] { opacity: 0; transform: scale(0.98); } } .Arrow { display: block; position: relative; width: 12px; height: 6px; overflow: clip; &[data-side='top'] { bottom: -6px; rotate: 180deg; } &[data-side='bottom'] { top: -6px; rotate: 0deg; } &[data-side='left'] { right: -9px; rotate: 90deg; } &[data-side='right'] { left: -9px; rotate: -90deg; } &::before { content: ''; display: block; position: absolute; bottom: 0; left: 50%; box-sizing: border-box; width: calc(6px * sqrt(2)); height: calc(6px * sqrt(2)); background-color: white; border: 1px solid oklch(14.5% 0 0deg); transform: translate(-50%, 50%) rotate(45deg); @media (prefers-color-scheme: dark) { background-color: oklch(14.5% 0 0deg); border: 1px solid white; } } } .Title { margin: 0; font-size: 0.875rem; line-height: 1.25rem; font-weight: 700; } .Description { margin: 0; font-size: 0.875rem; line-height: 1.25rem; color: oklch(43.9% 0 0deg); @media (prefers-color-scheme: dark) { color: oklch(70.8% 0 0deg); } } .Container { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; } .Button { box-sizing: border-box; display: flex; align-items: center; justify-content: center; gap: 0.5rem; height: 2rem; padding: 0 0.75rem; margin: 0; border: 1px solid oklch(14.5% 0 0deg); background-color: white; font-family: inherit; font-size: 0.875rem; font-weight: 400; line-height: 1; white-space: nowrap; color: oklch(14.5% 0 0deg); -webkit-user-select: none; user-select: none; @media (prefers-color-scheme: dark) { border: 1px solid white; background-color: oklch(14.5% 0 0deg); color: white; } @media (hover: hover) { &:hover:not([data-disabled], :disabled) { background-color: oklch(97% 0 0deg); @media (prefers-color-scheme: dark) { background-color: oklch(26.9% 0 0deg); } } } &:active:not([data-disabled], :disabled) { background-color: oklch(92.2% 0 0deg); @media (prefers-color-scheme: dark) { background-color: oklch(37.1% 0 0deg); } } &[data-popup-open]:not([data-disabled], :disabled) { background-color: oklch(97% 0 0deg); @media (prefers-color-scheme: dark) { background-color: oklch(26.9% 0 0deg); } } &[data-disabled], &:disabled { color: oklch(55.6% 0 0deg); border-color: oklch(55.6% 0 0deg); @media (prefers-color-scheme: dark) { color: oklch(70.8% 0 0deg); border-color: oklch(70.8% 0 0deg); } } &:focus-visible { outline: 2px solid oklch(14.5% 0 0deg); outline-offset: -1px; @media (prefers-color-scheme: dark) { outline-color: white; } } } ``` ```tsx /* index.tsx */ import * as React from 'react'; import { Popover } from '@base-ui/react/popover'; import styles from './index.module.css'; export default function ExamplePopover() { return ( Notifications Notifications You are all caught up. Good job! ); } ``` #### Pre-styled components [shadcn/ui](https://ui.shadcn.com/) is a great place to start if you need pre-styled components with higher-level abstractions. It uses Base UI as its unstyled foundation. Take a look at the [Community](#community) page to see more styled libraries powered by Base UI. #### Working with LLMs For those of you working with LLMs, each docs page has a "View as Markdown" link at the top, which can be shared with AI chat assistants to help them understand Base UI concepts and component APIs. Additionally, there is an ["llms.txt"](https://base-ui.com/llms.txt) link in the "Handbook" section of the navigation sidebar, which you can feed to AI chat assistants to help them navigate the docs. #### Next steps This walkthrough outlines the basics of putting together a Base UI component. Continue to the [Handbook](#styling) section for broader guidance on topics like styling, animation, and composition, or explore the [components](#accordion). ### Accessibility Learn how to make the most of Base UI's accessibility features and guidelines. Accessibility is a top priority for Base UI. Base UI components handle many complex accessibility details including ARIA attributes, role attributes, pointer interactions, keyboard navigation, and focus management. The goal is to provide an accessible user experience out of the box, with intuitive APIs for configuration. This page highlights some of the key accessibility features of Base UI, as well as some ways you need to augment the library, to ensure that your application is accessible to everyone. #### Keyboard navigation Base UI components adhere to the [WAI-ARIA Authoring Practices](https://www.w3.org/WAI/ARIA/apg/) to provide basic keyboard accessibility out of the box. This is critical for users who have difficulty using a pointer device, but it's also important for users who prefer navigating with a keyboard or other input mode. Many components provide support for arrow keys, alphanumeric keys, Home, End, Enter, and Esc. #### Focus management Base UI components manage focus automatically following a user interaction. Additionally, some components provide props like `initialFocus` and `finalFocus`, to configure focus management. While Base UI components manage focus, it's the developer's responsibility to visually indicate focus. This is typically handled by styling the `:focus` or `:focus-visible` CSS pseudo-classes. WCAG provides [guidelines on focus appearance](https://www.w3.org/WAI/WCAG22/Understanding/focus-appearance). #### Color contrast When styling elements, it's important to meet the minimum requirements for color contrast between each foreground element and its corresponding background element. Unless your application has strict requirements around compliance with current standards, consider adhering to [APCA](https://apcacontrast.com/). #### Accessible labels Base UI provides components like Form, Input, Field, Fieldset to automatically associate form controls. Additionally, you can use the native HTML `