1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//! Collapse module.
//!
//! This public module implements the Liora collapsible disclosure panel and accordion component. It keeps the reusable
//! component logic inside `liora-components` rather than Gallery or Docs so
//! downstream GPUI applications can compose the same behavior with their own
//! app state, assets, and release policy.
//!
//! ## Usage model
//!
//! Components in this module render native GPUI element trees. Stateless builder
//! values can be constructed inline, while controls with focus, selection,
//! popup, drag, or editing state should be stored as `gpui::Entity<T>` fields in
//! the parent view so state survives GPUI render passes.
//!
//! ## Design contract
//!
//! The implementation should use Liora theme tokens from `liora-core` and
//! `liora-theme`, keep accessibility-oriented keyboard/pointer behavior close to
//! the component, and avoid app-specific Gallery/Docs resources in this SDK
//! crate.
use crate::gpui_compat::element_id;
use crate::motion::pop_in;
use gpui::{AnyElement, Context, IntoElement, Render, SharedString, Window, div, prelude::*, px};
use liora_core::{Config, unique_id};
use liora_icons::Icon;
use liora_icons_lucide::IconName;
use std::collections::HashSet;
use std::sync::Arc;
/// Data model used by collapse item rendering.
pub struct CollapseItem {
/// Display name shown to users for this item.
pub name: SharedString,
/// Primary heading or title text displayed by the component.
pub title: SharedString,
/// Content rendered inside the component body.
pub content: Arc<dyn Fn(&mut Window, &mut Context<Collapse>) -> AnyElement + 'static>,
}
/// Fluent native GPUI component for rendering Liora collapse.
pub struct Collapse {
items: Vec<CollapseItem>,
active_names: HashSet<SharedString>,
accordion: bool,
id: SharedString,
}
impl Collapse {
/// Creates `Collapse` with default theme-driven styling and no optional callbacks attached.
pub fn new() -> Self {
Self {
items: vec![],
active_names: HashSet::new(),
accordion: false,
id: unique_id("collapse"),
}
}
/// Sets the accordion value used by the component.
pub fn accordion(mut self) -> Self {
self.accordion = true;
self
}
/// Assigns a stable element id used by GPUI state, hit testing, and automated interaction tests.
pub fn id(mut self, id: impl Into<SharedString>) -> Self {
self.id = id.into();
self
}
/// Performs the item operation used by this component.
pub fn item<F, E>(
mut self,
name: impl Into<SharedString>,
title: impl Into<SharedString>,
f: F,
) -> Self
where
F: Fn(&mut Window, &mut Context<Self>) -> E + 'static,
E: IntoElement,
{
self.items.push(CollapseItem {
name: name.into(),
title: title.into(),
content: Arc::new(move |window, cx| f(window, cx).into_any_element()),
});
self
}
fn toggle(&mut self, name: SharedString, cx: &mut Context<Self>) {
if self.active_names.contains(&name) {
self.active_names.remove(&name);
} else {
if self.accordion {
self.active_names.clear();
}
self.active_names.insert(name);
}
cx.notify();
}
}
impl Render for Collapse {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = cx.global::<Config>().theme.clone();
div()
.flex()
.flex_col()
.border_1()
.border_color(theme.neutral.border)
.rounded(px(theme.radius.md))
.children(self.items.iter().enumerate().map(|(i, item)| {
let name = item.name.clone();
let is_active = self.active_names.contains(&name);
let is_last = i == self.items.len() - 1;
let header_id = format!("{}-header-{}", self.id, name);
let content_motion_id = format!("{}-content-motion-{}", self.id, name);
div()
.flex()
.flex_col()
.child(
div()
.id(element_id(header_id))
.cursor_pointer()
.px_4()
.py_3()
.flex()
.flex_row()
.items_center()
.justify_between()
.bg(if is_active {
theme.neutral.hover
} else {
theme.neutral.card
})
.hover(|s| s.bg(theme.neutral.hover))
.when(!is_last, |s| {
s.border_b_1().border_color(theme.neutral.border)
})
.on_click(cx.listener(move |this, _, _, cx| {
this.toggle(name.clone(), cx);
}))
.child(
div()
.font_weight(gpui::FontWeight::BOLD)
.child(item.title.clone()),
)
.child(
Icon::new(if is_active {
IconName::ChevronDown
} else {
IconName::ChevronRight
})
.size(px(16.0))
.color(theme.neutral.icon),
),
)
.when(is_active, |s| {
s.child(pop_in(
element_id(content_motion_id),
div()
.p_4()
.bg(theme.neutral.card)
.when(!is_last, |s| {
s.border_b_1().border_color(theme.neutral.border)
})
.child((item.content)(_window, cx)),
))
})
}))
}
}