compound-components
БесплатноБез исполняемых скриптовНе проверенBuilds accessible composable components using Radix/Headless UI patterns. Use when creating Select, Dialog, Tabs, Accordion, Menu, or Dropdown components with…
Об этом скилле
Compound Components
Overview
Build accessible, composable components using the compound component pattern. This is how modern component libraries (Radix UI, Headless UI, React Aria) create flexible, accessible primitives that handle complex interactions.
When to Use
- Building a component library from scratch
- Creating accessible interactive components (Select, Dialog, Tabs, Accordion)
- Need flexibility in how components render
- Want to separate behavior from styling
- Components with complex state (open/closed, selected, focused)
Implementation Checklist
Copy this checklist when building a compound component:
Compound Component Build:
- [ ] Define component parts and responsibilities (Root, Trigger, Content, Item, etc.)
- [ ] Create Root with context provider (state, refs, generated IDs)
- [ ] Implement child components with ARIA roles and data-state attributes
- [ ] Add keyboard navigation (Arrow keys, Enter/Space, Escape, Tab)
- [ ] Handle focus management (initial focus, focus trap for modals, focus restoration)
- [ ] Test with keyboard-only navigation and screen reader
Quick Reference: Pattern Comparison
| Pattern | Example | Flexibility | Complexity |
|---|---|---|---|
| Monolithic | <Select options={[...]} /> |
Low | Low |
| Compound | <Select.Root><Select.Trigger /><Select.Content /></Select.Root> |
High | Medium |
| Headless | useSelect() hook |
Highest | High |
The Compound Pattern
// Usage example - caller controls structure
<Dialog.Root>
<Dialog.Trigger>Open</Dialog.Trigger>
<Dialog.Portal>
<Dialog.Overlay />
<Dialog.Content>
<Dialog.Title>Settings</Dialog.Title>
<Dialog.Description>Manage preferences</Dialog.Description>
{/* Custom content */}
<Dialog.Close>Done</Dialog.Close>
</Dialog.Content>
</Dialog.Portal>
</Dialog.Root>
Architecture
Component Parts
| Part | Purpose | Required |
|---|---|---|
| Root | Provides context, manages state | Yes |
| Trigger | Opens/activates the component | Usually |
| Content | Main content area | Yes |
| Portal | Renders outside DOM hierarchy | For overlays |
| Overlay | Backdrop behind content | For modals |
| Title | Accessible heading | For dialogs |
| Description | Accessible description | Optional |
| Close | Closes the component | Usually |
| Item | Individual selectable items | For lists |
Context Flow
┌─────────────────────────────────────────────────┐
│ Root (Context Provider) │
│ ┌───────────────────────────────────────────┐ │
│ │ • open/setOpen state │ │
│ │ • triggerRef │ │
│ │ • contentRef │ │
│ │ • generated IDs │ │
│ └───────────────────────────────────────────┘ │
│ │ │
│ ┌─────────┴─────────┐ │
│ ▼ ▼ │
│ Trigger Content │
│ (reads context) (reads context) │
│ │ │
│ ┌────────┴────────┐ │
│ ▼ ▼ ▼ │
│ Title Items Close │
└─────────────────────────────────────────────────┘
Implementation: React
Select Component
// select.tsx
import * as React from 'react';
import { createContext, useContext, useId, useRef, useState } from 'react';
// Types
interface SelectContextValue {
open: boolean;
setOpen: (open: boolean) => void;
value: string | undefined;
setValue: (value: string) => void;
triggerId: string;
contentId: string;
triggerRef: React.RefObject<HTMLButtonElement>;
}
interface SelectRootProps {
children: React.ReactNode;
value?: string;
onValueChange?: (value: string) => void;
defaultValue?: string;
}
// Context
const SelectContext = createContext<SelectContextValue | null>(null);
function useSelectContext() {
const context = useContext(SelectContext);
if (!context) {
throw new Error('Select components must be used within Select.Root');
}
return context;
}
// Root - manages all state
function SelectRoot({
children,
value: controlledValue,
onValueChange,
defaultValue
}: SelectRootProps) {
const [open, setOpen] = useState(false);
const [internalValue, setInternalValue] = useState(defaultValue);
const triggerRef = useRef<HTMLButtonElement>(null);
const id = useId();
const triggerId = `select-trigger-${id}`;
const contentId = `select-content-${id}`;
// Controlled vs uncontrolled
const value = controlledValue ?? internalValue;
const setValue = (newValue: string) => {
setInternalValue(newValue);
onValueChange?.(newValue);
setOpen(false);
};
return (
<SelectContext.Provider value={{
open,
setOpen,
value,
se
Установить compound-components в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
FAQ
Что делает скилл compound-components?
Builds accessible composable components using Radix/Headless UI patterns. Use when creating Select, Dialog, Tabs, Accordion, Menu, or Dropdown components with proper ARIA, keyboard navigation, and focus management.
Как установить скилл compound-components?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл compound-components запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
frontend-design
Guidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making…
от Anthropicwebapp-testing
Toolkit for interacting with and testing local web applications using Playwright. Supports verifying frontend functionality, debugging UI behavior, capturing…
от Anthropicbrand-guidelines
Applies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand…
от Anthropicweb-artifacts-builder
Suite of tools for creating elaborate, multi-component claude.ai HTML artifacts using modern frontend web technologies (React, Tailwind CSS, shadcn/ui). Use…
от Anthropic