Course contentsShow
← React
Lesson 1186 of 2,39630. Composition: Inversion of ControlFree lesson

Radix Example: Building a Styled Dialog

Compose Dialog.Root, Dialog.Trigger, Dialog.Portal, Dialog.Overlay, and Dialog.Content with your own classes for a fully accessible modal.

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.Root manages the open/closed state
  • Dialog.Trigger is the button that opens the dialog
  • Dialog.Portal teleports content outside the DOM hierarchy
  • Dialog.Overlay is the semi-transparent backdrop
  • Dialog.Content is 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.