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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Form field primitives — port of `@heroui/label`, `@heroui/description`,
//! `@heroui/error-message`, `@heroui/field-error` and `@heroui/fieldset`.
//!
//! These are the composition-friendly slots that HeroUI v3 field components
//! assemble internally, exposed here so applications can build custom fields
//! with the same typography and states.
//!
//! `FieldsetRoot` in v3.2.4 also accepts the native `disabled` attribute and
//! forwards it to descendants through React Aria contexts. GPUI has no
//! ancestor context propagation and no DOM attribute graph, so this port
//! deliberately ships no disabled state on [`Fieldset`]: a field composed
//! inside one keeps every interaction
//! (`text_fields::fieldset_disabled_disables_its_children` proves it).
use gpui::{
div, px, AnyElement, App, ElementId, InteractiveElement, IntoElement, ParentElement, Pixels,
RenderOnce, SharedString, StatefulInteractiveElement, Styled, Window,
};
use crate::a11y::{self, A11y as _};
use herogpui_theme::ActiveTheme;
/// HeroUI Label — `slot="label"`.
///
/// Mirrors the React API: `isRequired`, `isDisabled`, `isInvalid`. The
/// `htmlFor` prop has no gpui analogue (there is no DOM id graph), so labels
/// are associated by composition instead.
#[derive(IntoElement)]
pub struct Label {
text: SharedString,
is_required: bool,
is_disabled: bool,
is_invalid: bool,
/// `htmlFor` — the field this label names.
label_for: Option<(ElementId, gpui::FocusHandle)>,
}
impl Label {
pub fn new(text: impl Into<SharedString>) -> Self {
Self {
text: text.into(),
is_required: false,
is_disabled: false,
is_invalid: false,
label_for: None,
}
}
/// `htmlFor` — associates the label with a field.
///
/// In HTML this is an id reference; here it is the field's focus handle,
/// which is what makes the association do the one thing it does visibly:
/// clicking the label focuses the field. Pass a distinct `id` per label so
/// the click target has one.
pub fn label_for(mut self, id: impl Into<ElementId>, handle: gpui::FocusHandle) -> Self {
self.label_for = Some((id.into(), handle));
self
}
pub fn is_required(mut self, v: bool) -> Self {
self.is_required = v;
self
}
pub fn is_disabled(mut self, v: bool) -> Self {
self.is_disabled = v;
self
}
pub fn is_invalid(mut self, v: bool) -> Self {
self.is_invalid = v;
self
}
}
impl RenderOnce for Label {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let colors = cx.colors();
let mut el = div()
.flex()
.items_center()
.gap(px(2.))
.text_size(px(14.))
.line_height(px(20.))
.font_weight(gpui::FontWeight::MEDIUM)
.text_color(if self.is_invalid {
colors.danger.color
} else {
colors.foreground
})
.child(self.text.to_string());
if self.is_required {
el = el.child(div().text_color(colors.danger.color).child("*".to_owned()));
}
if self.is_disabled {
el = el.opacity(cx.layout().disabled_opacity);
}
// `htmlFor`: clicking the label focuses the field it names.
match self.label_for {
Some((id, handle)) if !self.is_disabled => el
.id(id)
.cursor(crate::util::interactive_cursor(cx))
.on_click(move |_: &gpui::ClickEvent, window: &mut Window, cx| {
window.focus(&handle, cx);
})
.into_any_element(),
_ => el.into_any_element(),
}
}
}
/// HeroUI Description — `slot="description"`. De-emphasised helper copy shown
/// beneath a field.
#[derive(IntoElement)]
pub struct Description {
text: SharedString,
}
impl Description {
pub fn new(text: impl Into<SharedString>) -> Self {
Self { text: text.into() }
}
}
impl RenderOnce for Description {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
div()
.text_size(px(12.))
.line_height(px(16.))
.text_color(cx.colors().muted)
.child(self.text.to_string())
}
}
/// HeroUI ErrorMessage — `slot="errorMessage"`. Always rendered when present.
#[derive(IntoElement)]
pub struct ErrorMessage {
text: SharedString,
}
impl ErrorMessage {
pub fn new(text: impl Into<SharedString>) -> Self {
Self { text: text.into() }
}
}
impl RenderOnce for ErrorMessage {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
div()
.text_size(px(12.))
.line_height(px(16.))
.text_color(cx.colors().danger.color)
.child(self.text.to_string())
}
}
/// HeroUI FieldError — validation-driven error text.
///
/// Unlike [`ErrorMessage`], a `FieldError` manages its own visibility from the
/// validation state: it renders nothing unless the field is invalid and a
/// message is present.
#[derive(IntoElement)]
pub struct FieldError {
text: Option<SharedString>,
is_invalid: bool,
/// Optional stable identity used to retain and animate the row across
/// validation updates. Without an id the standalone slot keeps its
/// allocation-free static behavior for one-shot compositions.
id: Option<ElementId>,
}
impl FieldError {
pub fn new() -> Self {
Self {
text: None,
is_invalid: false,
id: None,
}
}
/// The validation message. Setting a message also marks the field invalid,
/// matching React Aria's `ValidationResult` behaviour.
pub fn message(mut self, text: impl Into<SharedString>) -> Self {
self.text = Some(text.into());
self.is_invalid = true;
self
}
/// Overrides visibility independently of the message.
pub fn is_invalid(mut self, v: bool) -> Self {
self.is_invalid = v;
self
}
/// Supplies the stable element identity needed for the v3 height/opacity
/// transition when a standalone FieldError is toggled across renders.
pub fn id(mut self, id: impl Into<ElementId>) -> Self {
self.id = Some(id.into());
self
}
}
impl Default for FieldError {
fn default() -> Self {
Self::new()
}
}
impl RenderOnce for FieldError {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let message = self.text.clone().filter(|_| self.is_invalid);
if let Some(id) = self.id {
return crate::anim::field_error_panel(&id, message, window, cx)
.unwrap_or_else(|| div().into_any_element());
}
match (self.is_invalid, self.text) {
// `.field-error` is `px-1`, which `.error-message` is not.
(true, Some(text)) => div()
.px(px(4.))
.child(ErrorMessage::new(text))
.into_any_element(),
_ => div().into_any_element(),
}
}
}
/// HeroUI Fieldset — groups related form controls under a legend.
///
/// Compose with [`FieldsetLegend`], [`FieldGroup`] and [`FieldsetActions`],
/// mirroring `Fieldset.Legend` / `.Group` / `.Actions` in React.
#[derive(IntoElement)]
pub struct Fieldset {
gap: Pixels,
children: Vec<AnyElement>,
id: Option<ElementId>,
}
impl Fieldset {
pub fn new() -> Self {
Self {
gap: px(24.),
children: Vec::new(),
id: None,
}
}
/// Names this fieldset so it can report `role="group"`. Unnamed fieldsets
/// produce no AccessKit node.
pub fn id(mut self, id: impl Into<ElementId>) -> Self {
self.id = Some(id.into());
self
}
pub fn gap(mut self, gap: impl Into<Pixels>) -> Self {
self.gap = gap.into();
self
}
}
impl Default for Fieldset {
fn default() -> Self {
Self::new()
}
}
impl ParentElement for Fieldset {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for Fieldset {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let el = div()
.flex()
.flex_col()
.gap(self.gap)
// `.fieldset` also carries `shrink grow basis-0` so it fills a flex
// parent like any other section.
.flex_shrink(1.)
.flex_grow(1.)
.flex_basis(px(0.))
.text_color(cx.colors().foreground)
.children(self.children);
match self.id {
Some(id) => el.id(id).a11y(a11y::Role::Group).into_any_element(),
None => el.into_any_element(),
}
}
}
/// `Fieldset.Legend` — the group's caption.
#[derive(IntoElement)]
pub struct FieldsetLegend {
text: SharedString,
}
impl FieldsetLegend {
pub fn new(text: impl Into<SharedString>) -> Self {
Self { text: text.into() }
}
}
impl RenderOnce for FieldsetLegend {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
div()
.text_size(px(16.))
.line_height(px(24.))
// `.fieldset__legend` is `text-base font-medium`, not semibold.
.font_weight(gpui::FontWeight::MEDIUM)
.text_color(cx.colors().foreground)
.child(self.text.to_string())
}
}
/// `Fieldset.Group` — layout wrapper for the grouped controls.
#[derive(IntoElement)]
pub struct FieldGroup {
gap: Pixels,
children: Vec<AnyElement>,
}
impl FieldGroup {
pub fn new() -> Self {
Self {
gap: px(16.),
children: Vec::new(),
}
}
pub fn gap(mut self, gap: impl Into<Pixels>) -> Self {
self.gap = gap.into();
self
}
}
impl Default for FieldGroup {
fn default() -> Self {
Self::new()
}
}
impl ParentElement for FieldGroup {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
/// `.fieldset__field_group` is `w-full space-y-4`: the stack of fields, 16px
/// apart (`--spacing` × 4).
impl RenderOnce for FieldGroup {
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
div()
.flex()
.flex_col()
.w_full()
.gap(self.gap)
.children(self.children)
}
}
/// `Fieldset.Actions` — trailing row for submit/cancel controls.
#[derive(IntoElement)]
pub struct FieldsetActions {
gap: Pixels,
children: Vec<AnyElement>,
}
impl FieldsetActions {
pub fn new() -> Self {
Self {
gap: px(8.),
children: Vec::new(),
}
}
pub fn gap(mut self, gap: impl Into<Pixels>) -> Self {
self.gap = gap.into();
self
}
}
impl Default for FieldsetActions {
fn default() -> Self {
Self::new()
}
}
impl ParentElement for FieldsetActions {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for FieldsetActions {
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
div()
.flex()
.flex_row()
.items_center()
// `.fieldset__actions` is `gap-2 pt-1`.
.pt(px(4.))
.gap(self.gap)
.children(self.children)
}
}