Radix Example: Building a Styled Dialog
What you'll learn: How to compose Radix Dialog primitives with your own CSS to create a fully accessible modal component.
The Radix Composition Approach
Radix provides the building blocks—you provide the visual design. A Dialog requires multiple parts working together: a trigger button, an overlay backdrop, and the content container. Radix gives you each piece as separate components that handle all the accessibility and interaction logic internally.
Building Your Styled Dialog
The key is understanding what each primitive does:
Dialog.Rootmanages the open/closed stateDialog.Triggeris the button that opens the dialogDialog.Portalteleports content outside the DOM hierarchyDialog.Overlayis the semi-transparent backdropDialog.Contentis the actual modal box
You compose these together and add your own className props:
import * as Dialog from '@radix-ui/react-dialog';
import './dialog.css'; // your styles
function MyDialog() {
return (
<Dialog.Root>
<Dialog.Trigger className="trigger-button">
Open Settings
</Dialog.Trigger>
<Dialog.Portal>
<Dialog.Overlay className="dialog-overlay" />
<Dialog.Content className="dialog-content">
<h2>Settings</h2>
<p>Configure your preferences here.</p>
<Dialog.Close className="close-button">Close</Dialog.Close>
</Dialog.Content>
</Dialog.Portal>
</Dialog.Root>
);
}
Radix handles focus trapping, ESC key closing, screen reader announcements, and scroll locking. You just style it with CSS classes. The components coordinate through internal context—you never wire up the state yourself.
Key Takeaway: Radix Dialog primitives give you accessible behavior while letting you own the presentation—compose the structural components with your CSS classes for complete design control.