Radix UI
Radix Primitives
An accessibility-first headless UI component library.
Radix Primitives
An accessibility-first headless UI component library.
Radix UI is an accessibility (a11y)-focused headless React component library developed by WorkOS. Headless means it provides only pure functionality and logic without styles, so developers get complete design freedom while automatically meeting complex accessibility requirements. It fully complies with WAI-ARIA guidelines and has built-in keyboard navigation and focus management.
Radix Primitives provides more than 30 low-level UI components, including Dialog, Dropdown Menu, Popover, Tooltip, Tabs, Accordion, and Select. Each component uses the Compound Component pattern and is composed of subcomponents such as Root, Trigger, and Content. This pattern handles state sharing between parent and child through the Context API, giving developers a flexible structure.
shadcn/ui is a collection of components that applies Tailwind CSS styles on top of Radix UI and is integrated into a project by copy and paste. This is a new approach that gives developers code ownership without package dependencies. The Radix + Tailwind combination has become the standard UI stack for modern React projects.
Another feature of Radix UI is that it supports both uncontrolled and controlled components. For simple use you can rely on the default state management, and when complex logic is needed you can connect it to external state. The Portal and asChild patterns also allow control over the DOM structure and component composition.
// Radix UI Dialog μ»΄ν¬λνΈ with Tailwind CSS
import * as Dialog from '@radix-ui/react-dialog';
import { X } from 'lucide-react';
interface ModalProps {
open: boolean;
onOpenChange: (open: boolean) => void;
title: string;
description?: string;
children: React.ReactNode;
}
export function Modal({ open, onOpenChange, title, description, children }: ModalProps) {
return (
<Dialog.Root open={open} onOpenChange={onOpenChange}>
<Dialog.Portal>
{/* Overlay - λ°°κ²½ μ΄λ‘κ² */}
<Dialog.Overlay className="fixed inset-0 bg-black/50 data-[state=open]:animate-fadeIn" />
{/* Content - μ€μ λͺ¨λ¬ */}
<Dialog.Content className="fixed left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2
w-full max-w-md bg-white rounded-lg shadow-xl p-6
data-[state=open]:animate-contentShow
focus:outline-none">
{/* λ«κΈ° λ²νΌ */}
<Dialog.Close className="absolute right-4 top-4 rounded-sm opacity-70
hover:opacity-100 focus:outline-none focus:ring-2
focus:ring-slate-400 focus:ring-offset-2">
<X className="h-4 w-4" />
<span className="sr-only">λ«κΈ°</span>
</Dialog.Close>
{/* μ λͺ© - μ€ν¬λ¦° 리λκ° μ½μ */}
<Dialog.Title className="text-lg font-semibold text-gray-900">
{title}
</Dialog.Title>
{/* μ€λͺ
- μ νμ */}
{description && (
<Dialog.Description className="mt-2 text-sm text-gray-500">
{description}
</Dialog.Description>
)}
{/* λ΄μ© */}
<div className="mt-4">
{children}
</div>
</Dialog.Content>
</Dialog.Portal>
</Dialog.Root>
);
}
// μ¬μ© μμ
function App() {
const [open, setOpen] = useState(false);
return (
<>
<button onClick={() => setOpen(true)}>λͺ¨λ¬ μ΄κΈ°</button>
<Modal
open={open}
onOpenChange={setOpen}
title="νμκ°μ
"
description="μλ μ 보λ₯Ό μ
λ ₯ν΄μ£ΌμΈμ."
>
<form className="space-y-4">
<input type="email" placeholder="μ΄λ©μΌ" className="w-full border p-2 rounded" />
<input type="password" placeholder="λΉλ°λ²νΈ" className="w-full border p-2 rounded" />
<button type="submit" className="w-full bg-blue-500 text-white p-2 rounded">
κ°μ
νκΈ°
</button>
</form>
</Modal>
</>
);
}
// Radix UI Dropdown Menu
import * as DropdownMenu from '@radix-ui/react-dropdown-menu';
import { ChevronRight, User, Settings, LogOut } from 'lucide-react';
export function UserMenu() {
return (
<DropdownMenu.Root>
{/* Trigger - λ©λ΄λ₯Ό μ΄ λ²νΌ */}
<DropdownMenu.Trigger asChild>
<button className="flex items-center gap-2 px-3 py-2 rounded-lg
hover:bg-gray-100 focus:outline-none focus:ring-2 focus:ring-blue-500">
<img src="/avatar.jpg" className="w-8 h-8 rounded-full" alt="νλ‘ν" />
<span>κΉμ² μ</span>
</button>
</DropdownMenu.Trigger>
<DropdownMenu.Portal>
<DropdownMenu.Content
className="min-w-[200px] bg-white rounded-lg shadow-lg border p-1
data-[state=open]:animate-slideDownAndFade"
sideOffset={5}
align="end"
>
{/* μΌλ° λ©λ΄ μμ΄ν
*/}
<DropdownMenu.Item className="flex items-center gap-2 px-3 py-2 rounded
cursor-pointer hover:bg-gray-100 focus:bg-gray-100 outline-none">
<User className="w-4 h-4" />
νλ‘ν
</DropdownMenu.Item>
{/* μλΈ λ©λ΄ */}
<DropdownMenu.Sub>
<DropdownMenu.SubTrigger className="flex items-center justify-between
px-3 py-2 rounded cursor-pointer hover:bg-gray-100 outline-none">
<div className="flex items-center gap-2">
<Settings className="w-4 h-4" />
μ€μ
</div>
<ChevronRight className="w-4 h-4" />
</DropdownMenu.SubTrigger>
<DropdownMenu.Portal>
<DropdownMenu.SubContent className="min-w-[160px] bg-white
rounded-lg shadow-lg border p-1">
<DropdownMenu.Item className="px-3 py-2 rounded cursor-pointer
hover:bg-gray-100 outline-none">
κ³μ μ€μ
</DropdownMenu.Item>
<DropdownMenu.Item className="px-3 py-2 rounded cursor-pointer
hover:bg-gray-100 outline-none">
μλ¦Ό μ€μ
</DropdownMenu.Item>
</DropdownMenu.SubContent>
</DropdownMenu.Portal>
</DropdownMenu.Sub>
{/* ꡬλΆμ */}
<DropdownMenu.Separator className="h-px bg-gray-200 my-1" />
{/* λ‘κ·Έμμ - μν λμ μ€νμΌ */}
<DropdownMenu.Item className="flex items-center gap-2 px-3 py-2 rounded
cursor-pointer text-red-600 hover:bg-red-50 outline-none">
<LogOut className="w-4 h-4" />
λ‘κ·Έμμ
</DropdownMenu.Item>
{/* νμ΄ν */}
<DropdownMenu.Arrow className="fill-white" />
</DropdownMenu.Content>
</DropdownMenu.Portal>
</DropdownMenu.Root>
);
}
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free