--- name: radix-ui description: Build accessible, unstyled React UI components with Radix Primitives when_to_use: When you need accessible, composable UI components that give you full control over styling while ensuring WCAG compliance --- # Radix UI Primitives Skill Radix UI Primitives provides low-level, unstyled React components with built-in accessibility, keyboard navigation, and focus management. Perfect for building design systems and custom UI components. ## Quick Start ### Basic Dialog Example ```jsx import * as Dialog from "@radix-ui/react-dialog"; import { Cross2Icon } from "@radix-ui/react-icons"; function BasicDialog() { return ( Edit profile Make changes to your profile here.
); } ``` ### Dropdown Menu Example ```jsx import * as DropdownMenu from "@radix-ui/react-dropdown-menu"; import { HamburgerMenuIcon, CheckIcon } from "@radix-ui/react-icons"; function UserMenu() { const [showBookmarks, setShowBookmarks] = React.useState(true); return ( New Tab
⌘+T
New Window
⌘+N
Show Bookmarks
); } ``` ## Common Patterns ### Primitive Components Radix provides 30+ primitive components for common UI patterns: ```jsx // Accordion with animation import * as Accordion from "@radix-ui/react-accordion"; function FAQAccordion() { return ( Is it accessible? Yes. It adheres to WAI-ARIA design patterns. ); } // Tooltip with delay import * as Tooltip from "@radix-ui/react-tooltip"; function TooltipExample() { return ( Additional information ); } ``` ### Accessibility Features Radix automatically handles accessibility: ```jsx // All components include proper ARIA attributes // Focus management is handled automatically // Keyboard navigation works out of the box // Screen reader support is built-in // Example: Select with proper labeling import * as Select from "@radix-ui/react-select"; function AccessibleSelect() { return ( Fruits Apple ); } ``` ### Composition with asChild Compose Radix primitives with your own components: ```jsx // Use your existing button styles import { Dialog, Tooltip } from "@radix-ui/react-dialog"; import { Button } from "./your-design-system"; function ComposedDialog() { return ( Dialog Title ); } // Multiple primitives on one element function TooltipDialogButton() { return ( Opens a modal dialog {/* Dialog content */} ); } ``` ### Custom Component Abstractions Create your own simplified APIs: ```jsx // Custom Dialog wrapper import * as DialogPrimitive from "@radix-ui/react-dialog"; export const CustomDialog = ({ children, trigger, title }) => { return ( {trigger} {title} {children} ); }; // Usage Open Settings}>
Settings content here
; ``` ### Styling with Data Attributes Style components based on their state: ```css /* Accordion animations */ .accordion-content[data-state="open"] { animation: slideDown 300ms ease-out; } .accordion-content[data-state="closed"] { animation: slideUp 300ms ease-out; } /* Dropdown positioning */ .dropdown-content { transform-origin: var(--radix-dropdown-menu-content-transform-origin); } .dropdown-content[data-side="top"] { animation: slideUp 0.3s ease-out; } .dropdown-content[data-side="bottom"] { animation: slideDown 0.3s ease-out; } /* Focus states */ .dropdown-item[data-highlighted] { background: #f0f0f0; } .dropdown-item[data-state="checked"] { background: #e0e0e0; } ``` ### Advanced Patterns ```jsx // Controlled components for async operations function AsyncDialog() { const [open, setOpen] = useState(false); const [loading, setLoading] = useState(false); const handleSubmit = async (e) => { e.preventDefault(); setLoading(true); await submitForm(); setLoading(false); setOpen(false); }; return (
); } // Popover with collision detection function SmartPopover() { return ( Content that avoids screen edges ); } ```