Sub topic 2
Headless components: behavior without styles
This chapter assumes you can write React function components with props and hooks, read basic TypeScript, and know what Tailwind utility classes are. It also assumes you have seen the keyboard and ARIA basics from the previous chapter.
What "headless" means
A headless component is a component that supplies behavior and accessibility but no visual design. It renders real elements, manages state, and wires up keyboard handling and ARIA attributes. It ships no colors, spacing, or fonts. You add those yourself.
A modal dialog shows why this matters. To work properly, a dialog must:
- Open and close from a trigger.
- Trap focus inside while open.
- Close on Escape.
- Return focus to the trigger when it closes.
- Expose
role="dialog"and a title and description to screen readers.
Getting this right takes real effort. Making it look good is a separate job. A headless library does the first job and leaves the second to you.
A dialog with no built-in styles
The example below uses Radix UI's dialog primitive. The only styling is the classes you see.
import * as Dialog from "@radix-ui/react-dialog"
export function ConfirmDialog() {
return (
<Dialog.Root>
<Dialog.Trigger>Delete item</Dialog.Trigger>
<Dialog.Portal>
<Dialog.Overlay className="fixed inset-0 bg-black/50" />
<Dialog.Content className="fixed left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 rounded-md bg-white p-6">
<Dialog.Title>Delete this item?</Dialog.Title>
<Dialog.Description>This action cannot be undone.</Dialog.Description>
<Dialog.Close>Cancel</Dialog.Close>
</Dialog.Content>
</Dialog.Portal>
</Dialog.Root>
)
}Each part has a job:
Dialog.Rootholds the open and closed state.Dialog.Triggerrenders a<button>that opens the dialog.Dialog.Portalrenders its children at the end ofdocument.body, so parentoverfloworz-indexrules cannot clip them.Dialog.TitleandDialog.Descriptionare linked to the content through ARIA attributes. Radix warns in development if the title is missing.
Without the classes, the dialog still opens, traps focus, and closes on Escape. It just looks unstyled.
How you style state
Because the library does not know your design, it exposes state as data attributes. For example, Radix sets data-state="open" or data-state="closed" on the overlay and content. Tailwind can target these directly:
<Dialog.Overlay className="fixed inset-0 bg-black/50 data-[state=open]:opacity-100 data-[state=closed]:opacity-0 transition-opacity" />The library reports what the component is doing, and your CSS decides how that looks.
Trade-offs
| Approach | You get | You pay |
|---|---|---|
| Hand-built component | Full control | You must implement and test accessibility yourself |
| Styled library | Fast results | You work against its design and override its CSS |
| Headless primitive | Correct behavior and full visual control | You write all the styling |
Headless components remove the hardest part, which is correct accessible behavior. You still own every visual decision.
The next chapters show how shadcn/ui builds on this idea. It pairs Radix primitives with Tailwind classes and copies the result into your project.
Exercise
A teammate says, "We use a headless dialog, so we don't need to check keyboard behavior or write a title." What is wrong with this claim?
Show answer
Headless primitives supply focus trapping, Escape handling, and ARIA wiring, but you can still break them. A dialog without a title has no accessible name for screen readers. Removing focus outlines or overriding behavior in custom code can also break keyboard use. You should still test with the keyboard and a screen reader.