Sub topic 4
Radix UI primitives in practice
This chapter assumes you know React function components, props and hooks, basic TypeScript, and the idea of a headless component from the previous chapter. You will see how Radix UI primitives are structured and what they handle for you.
What a primitive gives you
A Radix primitive is an unstyled component that supplies behavior and accessibility. It does not ship CSS. You pass your own className values, and Radix handles the rest.
For a dialog, Radix provides:
- Focus moves into the dialog when it opens and returns to the trigger when it closes.
- Focus stays inside the dialog while it is open.
- Escape closes the dialog.
- The right ARIA attributes are set, such as
role="dialog"and the links between the title and description.
Anatomy: one part per job
Radix splits each widget into small parts that you assemble yourself. Each part is a separate component, and you control the markup order and the classes.
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>
)
}The parts work like this:
| Part | Role |
|---|---|
| `Root` | Holds the open or closed state. It renders no element. |
| `Trigger` | The button that opens the dialog. |
| `Portal` | Renders its children at the end of `document.body`, outside your layout. |
| `Overlay` | The backdrop behind the dialog. |
| `Content` | The dialog panel. It traps focus and handles Escape. |
| `Title`, `Description` | Give the dialog its accessible name and description. |
| `Close` | A button that closes the dialog. |
The only styling here is the Tailwind classes on Overlay and Content. Everything else is Radix.
Controlled and uncontrolled state
By default Root manages its own state, which is called uncontrolled use. When you need to read or set the state, pass open and onOpenChange. This is controlled use.
import { useState } from "react"
import * as Dialog from "@radix-ui/react-dialog"
export function ControlledDialog() {
const [open, setOpen] = useState(false)
return (
<Dialog.Root open={open} onOpenChange={setOpen}>
<Dialog.Trigger>Open</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>Saved</Dialog.Title>
<Dialog.Description>Your changes are stored.</Dialog.Description>
<button onClick={() => setOpen(false)}>Done</button>
</Dialog.Content>
</Dialog.Portal>
</Dialog.Root>
)
}Styling by state
Radix writes its state into data- attributes, such as data-state="open". Tailwind can target these attributes, so you can style states without extra JavaScript.
<Dialog.Overlay className="fixed inset-0 bg-black/50 data-[state=open]:opacity-100 data-[state=closed]:opacity-0" />Common errors
- Missing title. Radix warns in the console if
Contenthas noTitle. Screen readers need it. If you want it hidden visually, use a visually hidden element instead of removing it. - Forgetting the Portal. Without it, a parent with
overflow: hiddenor a lower stacking context can clip the dialog. - Putting your own button inside
Trigger.Triggeralready renders abutton, so nesting another produces invalid HTML. TheasChildprop solves this, and a later chapter covers it.
Exercise
A teammate removes Dialog.Title because the design has no visible heading. What breaks, and what is the fix?
Show answer
The dialog loses its accessible name, so screen readers cannot announce what it is, and Radix logs a console warning. Keep a Dialog.Title and hide it visually with a visually hidden element or a screen-reader-only class such as Tailwind's sr-only.