🌐 Web Development

Radix UI

Radix Primitives

An accessibility-first headless UI component library.

πŸ“– Detailed Explanation

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.

πŸ’» Code Example

Radix Dialog + Tailwind
// 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 Dropdown Menu
// 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>
    );
}

πŸ—£οΈ Examples of Practical Conversations

πŸ’Ό UI library selection meeting
"Should we pick MUI or Radix? MUI seems to have more components."

"MUI forces the Material Design style and is hard to customize. Radix is headless, so you can style it freely to fit your design system. With shadcn/ui you can get started quickly with Tailwind, and you can edit the code directly if needed, so it's very popular these days."
πŸ” Code Review
"When the modal opens, the background scroll isn't locked and the focus trap doesn't work."

"That's because it's a hand-built modal. With Radix Dialog, scroll lock, focus trap, closing with ESC, and even the aria-modal attribute are all automatic. If you implement accessibility yourself, you miss a lot, so it's better to use a proven library."
πŸ“± Technical interview
"What are the advantages of headless UI components?"

"Styles and logic are separated, so you can fully customize them to fit your design system. You don't have to fight CSS specificity battles overriding framework styles like Bootstrap or MUI. Radix also has accessibility built in, so WCAG compliance comes automatically."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

⚠️ Cautions

  • Styling required - Radix Primitives has no styles at all, so you have to write the CSS yourself. If you want a quick start, use shadcn/ui or Radix Themes.
  • React only - Radix UI works only with React. For Vue or Svelte, you need to find alternatives such as Headless UI (@headlessui/vue).
  • Animations implemented by hand - Open/close animations must be implemented yourself using the data-state attribute with CSS transitions or Framer Motion.

πŸ”— Related Terms

πŸ“š Learn More