AgentSkillsCN

radix-ui

当shadcn没有你需要的组件时,用于可访问的React原语

SKILL.md
--- frontmatter
name: radix-ui
description: Use for accessible React primitives when shadcn doesn't have the component you need

Radix UI Primitives

When to Use

Use Radix directly when:

  • shadcn doesn't have the component you need
  • You need lower-level control over accessibility behavior
  • Building custom compound components with full a11y support

Check shadcn first - most Radix primitives have styled shadcn wrappers.

Installation

bash
npm install @radix-ui/<primitive>

# Examples:
npm install @radix-ui/react-toggle
npm install @radix-ui/react-toggle-group
npm install @radix-ui/react-toolbar

Primitives NOT in shadcn

These Radix primitives don't have direct shadcn equivalents:

PrimitiveUse Case
toggleSingle on/off toggle button
toggle-groupMutually exclusive toggle buttons
toolbarAccessible toolbar containers
visually-hiddenScreen-reader-only content
portalRender children in document.body
slotMerge props onto child element
direction-providerRTL/LTR direction context
accessible-iconIcon with accessible label

Usage Patterns

Toggle

tsx
import * as Toggle from "@radix-ui/react-toggle"

<Toggle.Root
  pressed={pressed}
  onPressedChange={setPressed}
  className="rounded p-2 data-[state=on]:bg-primary"
>
  {pressed ? <BoldIcon /> : <BoldOffIcon />}
</Toggle.Root>

Toggle Group

tsx
import * as ToggleGroup from "@radix-ui/react-toggle-group"

<ToggleGroup.Root type="single" value={value} onValueChange={setValue}>
  <ToggleGroup.Item value="left" className="...">
    <AlignLeftIcon />
  </ToggleGroup.Item>
  <ToggleGroup.Item value="center" className="...">
    <AlignCenterIcon />
  </ToggleGroup.Item>
  <ToggleGroup.Item value="right" className="...">
    <AlignRightIcon />
  </ToggleGroup.Item>
</ToggleGroup.Root>

Toolbar

tsx
import * as Toolbar from "@radix-ui/react-toolbar"

<Toolbar.Root className="flex gap-2 p-2 border rounded">
  <Toolbar.Button className="...">Bold</Toolbar.Button>
  <Toolbar.Separator className="w-px bg-border" />
  <Toolbar.Link href="/help">Help</Toolbar.Link>
  <Toolbar.ToggleGroup type="single">
    <Toolbar.ToggleItem value="left">Left</Toolbar.ToggleItem>
    <Toolbar.ToggleItem value="center">Center</Toolbar.ToggleItem>
  </Toolbar.ToggleGroup>
</Toolbar.Root>

Visually Hidden

tsx
import * as VisuallyHidden from "@radix-ui/react-visually-hidden"

<button>
  <GearIcon />
  <VisuallyHidden.Root>Settings</VisuallyHidden.Root>
</button>

Styling Radix Components

Radix uses data-* attributes for state styling:

tsx
// State attributes
data-[state=open]:bg-accent
data-[state=closed]:opacity-0
data-[state=on]:bg-primary
data-[state=off]:bg-muted
data-[disabled]:opacity-50
data-[highlighted]:bg-accent

// Orientation
data-[orientation=horizontal]:flex-row
data-[orientation=vertical]:flex-col

// Side (for positioned elements)
data-[side=top]:animate-slideDown
data-[side=bottom]:animate-slideUp

Composition Pattern

Radix primitives are composable. Build complex components from simple parts:

tsx
import * as Toolbar from "@radix-ui/react-toolbar"
import * as ToggleGroup from "@radix-ui/react-toggle-group"
import * as Tooltip from "@radix-ui/react-tooltip"

// Combine toolbar with tooltips on each button
<Toolbar.Root>
  <Tooltip.Provider>
    <Tooltip.Root>
      <Tooltip.Trigger asChild>
        <Toolbar.Button>Bold</Toolbar.Button>
      </Tooltip.Trigger>
      <Tooltip.Content>Make text bold</Tooltip.Content>
    </Tooltip.Root>
  </Tooltip.Provider>
</Toolbar.Root>

Key Props

PropDescription
asChildMerge props onto child element instead of rendering wrapper
forceMountKeep in DOM even when closed (for animations)
defaultOpen / openUncontrolled / controlled open state
onOpenChangeCallback when open state changes
modalWhether to trap focus (dialogs)