Accordion
A vertically stacked set of interactive headings that each reveal a section of content.
Overview
默认一次只展开一个条目,首项默认打开。点击标题收起或切换,也可以用 ↑ ↓ 键在条目间移动。
GPUI · Live previewTry it out
Installation
Accordion 是单个 Rust 文件(src/accordion.rs),构建在 gpui-base 的无样式原语之上。复制到你的 crate,或下载完整示例直接运行。
[dependencies]gpui-kit = "0.6.6"# accordion.rs builds on the unstyled primitives# re-exported at gpui_kit::base:# Accordion, AccordionItem, AccordionHeader,# AccordionTrigger, AccordionPanel, MotionReveal# Download the runnable example crate, then:cargo run --locked -- --accordion# Other demos:cargo run --locked -- --accordion multiplecargo run --locked -- --accordion disabledcargo run --locked -- --accordion cardUsage
用 AccordionEntry 描述每一段内容,把实体保留在宿主视图上,渲染时放进任意容器。
use justdo_command::accordion::{Accordion, AccordionEntry};// Keep the entity on your host view.let accordion = cx.new(|_| { Accordion::new() .entry(AccordionEntry::new("shipping", "What are your shipping options?") .content("We offer standard (5-7 days) and express shipping.")) .entry(AccordionEntry::new("returns", "What is your return policy?") .content("Returns are accepted within 30 days of purchase.")) .entry(AccordionEntry::new("support", "How can I contact support?") .content("Reach us through live chat or email.")) .open("shipping")});// In render: div().child(self.accordion.clone())每个条目的 value 是稳定的标识符,open(value) 指定初始展开项。网页预览与桌面端运行同一份 Rust / GPUI 组件。
Composition
组合结构与 shadcn/ui 的 Base UI 版本一一对应。样式由组件实体统一提供,可访问性语义来自 gpui-base 原语。
Accordion├── AccordionItem│ ├── AccordionHeader│ │ └── AccordionTrigger button · aria-expanded│ └── AccordionPanel region└── AccordionItem ├── AccordionHeader │ └── AccordionTrigger └── AccordionPanelAccessibility
Keyboard
- ↑ ↓ 在条目间循环移动焦点
- Home End 跳到第一个 / 最后一个条目
- Enter Space 展开或收起当前条目
- Tab 沿页面 tab 序进入或离开组件
Semantics
标题携带 heading 语义(level 3),触发器是带 aria-expanded 的 button,面板是 region。焦点可见性、disabled 状态与 reduced-motion 由 GPUI 与浏览器偏好共同决定。
API Reference
组件实体暴露的方法与事件。结构原语由 gpui_kit::base 提供。
| API | Signature | Notes |
|---|---|---|
Accordion::new() | -> Accordion | Creates the entity content. Single-open and collapsible by default. |
.entry(..) | AccordionEntry | Appends one item; call order is presentation order. |
.open(value) | &str | Marks an entry expanded initially. Repeatable with multiple. |
.multiple(bool) | bool | Allows several entries to stay open at once. |
.collapsible(bool) | bool | Allows closing the last open entry (single mode). |
.dark(bool) | bool | Dark color scheme for surfaces, text and borders. |
AccordionEntry::new(..) | value, title | One row: stable value plus its heading. |
.content(..) | impl Into<SharedString> | Panel copy revealed by the trigger. |
.disabled(bool) | bool | Inert item: no clicks, skipped by arrow-key navigation. |
AccordionChange | { value, open } | Emitted on every toggle; subscribe with cx.subscribe. |
// AccordionChange { value, open } is emitted on every toggle.cx.subscribe(&self.accordion, |view, _accordion, event, cx| { println!("{} -> {}", event.value, event.open); cx.notify();});Source & inspiration
结构与视觉参考 shadcn/ui Accordion (Base UI 组合方式)。本页交互由 Rust / GPUI 绘制,无样式原语来自 gpui-base 。