Collapsible
不规定触发器样式的可组合显隐区域。
和所有 GPUI Base 原语一样,Collapsible 只提供行为和语义结构,不规定产品视觉语言。请使用 GPUI 样式并组合导出的部件,使其符合你的设计系统。
示例
原生示例和页面上方的 WASM 预览共用同一份实现:
bash
cargo run -p gpui-base --example components -- collapsible导入
rust
use gpui_base::{Collapsible};结构与 API
示例组合上述公开类型。GPUI 的标准样式和事件 trait 负责表现,Base 类型负责交互结构。权威实现位于 components/collapsible.rs,原生与浏览器预览编译的是同一文件。
状态与事件
父级持有打开状态;内容可结合 Motion 做进入与退出过渡。
受控状态应保存在父渲染类型或 GPUI entity 中;在回调中更新并调用 cx.notify(),不要在每次渲染时重建持久 entity。
完整 Rust 示例
rust
use super::*;
impl BaseShowcase {
pub(in super::super) fn collapsible(&self, cx: &mut Context<Self>) -> impl IntoElement {
let open = self.collapsible_open;
let entity = cx.entity().downgrade();
Collapsible::new()
.open(open)
.w_64()
.child(
div()
.flex()
.items_center()
.justify_between()
.child(div().text_xs().child("@gpui/base · 3 repositories"))
.child(
Button::new("collapsible-trigger")
.size_7()
.border_1()
.border_color(super::example_rgb(0xd4d4d4))
.flex()
.items_center()
.justify_center()
.on_click(move |_, _, cx| {
_ = entity.update(cx, |this, cx| {
this.collapsible_open = !this.collapsible_open;
cx.notify();
});
})
.child(if open { "−" } else { "+" }),
),
)
.child(
div()
.mt_2()
.px_2()
.h_7()
.flex()
.items_center()
.border_1()
.border_color(super::example_rgb(0xd4d4d4))
.text_xs()
.child("gpui-component"),
)
.content(div().mt_2().flex().flex_col().gap_2().children(
["gpui-base", "gpui-storybook"].into_iter().map(|name| {
div()
.px_2()
.h_7()
.flex()
.items_center()
.border_1()
.border_color(super::example_rgb(0xd4d4d4))
.text_xs()
.child(name)
}),
))
}
}可访问性
触发器应暴露展开状态以及它所控制内容的关系。
注意事项
在支持的位置使用稳定元素 ID,并在消费端设计系统中验证焦点、悬停、按下、选中、禁用、减少动态效果和高对比度状态。