把展开前也能独立读懂的标签放入 summary,把补充内容放在子节点中。例如“排版细节”可以展开为具体的格式设置。链接和其他操作放在详情区域内。
Live example
Open playground ↗- Trim trailing spaces
- Keep paragraph breaks
App.tsx
'use client'
import { Disclosure } from '@ancrel/ui'
import '@ancrel/tokens/css'
import '@ancrel/ui/styles.css'
export default function App() {
return <Disclosure summary="Formatting details">
<ul><li>Trim trailing spaces</li><li>Keep paragraph breaks</li></ul>
</Disclosure>
}
Expected behavior
Formatting details starts collapsed. Activate the button to reveal both formatting options, and activate it again to collapse the list.
Build @ancrel/tokens and @ancrel/ui from the workspace root, then run pnpm --filter @ancrel/example-disclosure test.
键盘与状态
摘要是原生按钮,支持 Enter 和空格。按钮通过 aria-expanded 表示当前状态,aria-controls 指向对应的详情元素。收起时,详情中的表单字段保留其值并退出键盘导航。
默认收起。使用 defaultOpen 可让详情首次显示时展开。若其他控件也可以切换同一区域,传入 open 并在 onOpenChange 中更新它。减少动态效果时,按钮和详情直接切换到最终状态。
Props
| Prop | Type | Required | Default |
|---|---|---|---|
children | ReactNode | No | — |
className | string | No | — |
defaultOpen | boolean | No | — |
key | React.Key | null | undefined | No | — |
onOpenChange | ((open: boolean) => void) | undefined | No | — |
open | boolean | No | — |
ref | React.Ref<HTMLDetailsElement> | undefined | No | — |
summary | ReactNode | Yes | — |
--ac-sys-motion-normalConstraints
- Give summary a short label that identifies the details.
- Use open with onOpenChange when another control also changes visibility.
Anti-patterns
- Nesting links or additional buttons in the summary.
- Hiding information required to understand the summary.