Skill站外收录 @skilllib/skill-2014279385027653633
component
构建可访问的无样式 UI 组件,支持跨框架复用,涵盖复合组件结构、受控与非受控状态管理、属性获取函数、数据属性驱动样式、无障碍交互规范及浮动定位能力,适用于设计系统与组件库开发。
软件开发 AI 技能
Skills.MD
# Component Architecture Patterns
Build accessible, headless UI components using proven patterns from Base UI, Radix, and Ark UI. These patterns are **framework-agnostic** — core concepts apply across React, Vue, Svelte, Solid, and vanilla JS.
**Primary sources:**
- [Base UI Handbook](https://base-ui.com/react/handbook/overview)
- [Ark UI](https://ark-ui.com/) — Cross-framework implementation
- [Zag.js](https://zagjs.com/) — State machines for UI components
**Framework-specific:** [react.md](references/react.md) | [lit.md](references/lit.md)
---
## Core Principles
1. **Headless over styled** — Separate behavior from presentation
2. **Compound over monolithic** — Small composable parts over config-heavy megacomponents
3. **Controlled + uncontrolled** — Support both state ownership models
4. **Accessible by default** — ARIA, keyboard nav, focus management built-in
5. **State via attributes** — Expose state through `data-*` for framework-agnostic styling
---
## Pattern 1: Compound Components
**What:** Components as related parts sharing state through context, each mapping 1:1 to DOM elements.
**Why:**
- Declarative — assemble like building blocks, reorder/omit parts freely
- Each part is an independent styling target
- DOM structure maps directly to ARIA roles
**Standard hierarchies:**
| Type | Parts |
| ----------- | ---------------------------------------------------- |
| Popups | Root → Trigger → Portal → Positioner → Popup → Arrow |
| Collections | Root → List → Trigger + Panel |
| Forms | Root → Label → Control → Description → Error |
**Ref:** [Base UI Composition](https://base-ui.com/react/handbook/composition)
---
## Pattern 2: Controlled & Uncontrolled State
**What:** Support external state control OR internal state with consistent prop naming.
**Why:**
- Flexibility for simple and complex use cases
- Predictable API across components
- Change details enable fine-grained control (cancel changes, track reasons)
**Convention:**
| State | Uncontrolled | Controlled | Handler |
| ------- | ---------------- | ---------- | ----------------------------------- |
| Open | `defaultOpen` | `open` | `onOpenChange(open, details)` |
| Value | `defaultValue` | `value` | `onValueChange(value, details)` |
| Checked | `defaultChecked` | `checked` | `onCheckedChange(checked, details)` |
**Change details:** `{ reason, event, cancel() }`
**Ref:** [Base UI Customization](https://base-ui.com/react/handbook/customization)
---
## Pattern 3: Prop Getters
**What:** Functions returning HTML attributes for DOM elements, abstracting logic from rendering.
**Why:**
- Portable across frameworks (React, Vue, Svelte, Solid)
- Clean separation of concerns
- Composable via `mergeProps()`
**Example:** `getTriggerProps()` returns `{ aria-expanded, aria-haspopup, onClick, onKeyDown }`
**Ref:** [Zag.js](https://zagjs.com/), [Downshift](https://www.downshift-js.com/)
---
## Pattern 4: State via Data Attributes
**What:** Expose state through `data-*` attributes for CSS targeting.
**Why:**
- Framework-agnostic styling
- No JS needed for state-based styles
- Inspectable in DevTools
**Standard attributes:**
- `data-open` / `data-closed` — Visibility
- `data-checked` / `data-unchecked` — Toggle state
- `data-highlighted` — Focus within group
- `data-disabled`, `data-valid`, `data-invalid`
- `data-side`, `data-align` — Positioning
**CSS variables:** `--available-height`, `--anchor-width`, `--transform-origin`
**Ref:** [Base UI Styling](https://base-ui.com/react/handbook/styling)
---
## Pattern 5: Accessibility
**What:** ARIA, keyboard navigation, focus management built into architecture.
**Why:**
- Accessibility is structural, not decorative
- Users expect standard keyboard interactions
- Consistent patterns reduce errors
**Key concerns:**
- **ARIA attributes** — Auto-managed from state
- **Focus trapping** — Modals trap focus within
- **Roving tabindex** — One tabbable item, arrows navigate
- **Virtual focus** — `aria-activedescendant` for long lists
- **Typeahead** — A-Z jumps to matches
**Ref:** [Base UI Accessibility](https://base-ui.com/react/overview/accessibility), [WAI-ARIA Practices](https://www.w3.org/WAI/ARIA/apg/patterns/)
For detailed accessibility patterns, load the `aria` skill.
---
## Pattern 6: Floating Positioning
**What:** Position popups relative to triggers with collision detection.
**Why:** Handles viewport boundaries, scroll, resize automatically.
**Config:** `side`, `align`, `sideOffset`, `collision` (flip/shift), `trackAnchor`
**Ref:** [Floating UI](https://floating-ui.com/)
---
## API Conventions
| Category | Props |
| ----------- | -------------------------------------------------------------- |
| Interaction | `disabled`, `required`, `readOnly` |
| Collections | `multiple`, `loopFocus`, `orientation` |
| Popups | `modal`, `closeOnEscape`, `closeOnOutsideClick`, `keepMounted` |
| Positioning | `side`, `align`, `sideOffset`, `collision` |
**Imperative actions:** `actionsRef` exposing `open()`, `close()`, `toggle()`
See [props.md](references/props.md) for naming conventions.
---
## Reference Files
| File | Contents |
| ----------------------------------------------- | ------------------------------------ |
| [lit.md](references/lit.md) | Lit controllers, mixins, context |
| [react.md](references/react.md) | React hooks, context, refs |
| [props.md](references/props.md) | Prop naming, conventions, defaults |
| [styling.md](references/styling.md) | Data attributes, CSS variables |
| [animation.md](references/animation.md) | CSS transitions, JS animation libs |
| [polymorphism.md](references/polymorphism.md) | render vs asChild patterns |
| [collection.md](references/collection.md) | Collections, portals, virtualization |
| [anti-patterns.md](references/anti-patterns.md) | Common component mistakes |
For accessibility patterns (ARIA, keyboard, focus), load the `aria` skill.
## Review
For structured component reviews, load the review workflow:
| File | Contents |
| ----------------------------------- | --------------------------- |
| [workflow.md](review/workflow.md) | Review process and severity |
| [checklist.md](review/checklist.md) | Component review checklist |
| [templates.md](review/templates.md) | Issue and report formats |
---
## Implementation Sources
| Resource | Use For |
| ------------------------------------------------------------------------------- | --------------------------------- |
| [Base UI Source](https://github.com/mui/base-ui/tree/master/packages/react/src) | React reference implementations |
| [Radix Primitives](https://github.com/radix-ui/primitives) | Alternative approach |
| [Zag.js](https://github.com/chakra-ui/zag) | Framework-agnostic state machines |
| [Floating UI](https://floating-ui.com/docs/getting-started) | Positioning |
---
## Related Skills
| Need | Use |
| ---------------------- | ------------ |
| Accessibility patterns | `aria` skill |
| API design principles | `api` skill |
| Documentation patterns | `docs` skill |
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「component」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次