u0ui 0.3.5

Carbon UI: IBM Carbon v11 visuals with a shadcn-style API for GPUI Kit
Documentation
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! Item: a list row or standalone entry with media, title, description and
//! actions (`.cu-item*` in `custom.css`; Carbon `ContainedList` item anatomy).
//!
//! - `.cu-item { display: flex; flex-wrap: wrap; align-items: center; gap:
//!   var(--spacing-05); min-height: var(--size-lg); padding: var(--spacing-04)
//!   var(--spacing-05); color: var(--text-primary) }`; `sm` 40px min with
//!   8px/16px padding; `xs` 32px min, 4px/12px padding, 8px gap ([`Geometry`]).
//! - Variants: `outline` (1px inset `border-subtle`), `muted` (`layer-01`).
//!   With `href`/`on_click` the item is a link: hover `layer-hover-01`, 2px
//!   inset `focus` outline on keyboard focus only (`.cu-item--link:focus-visible`);
//!   a bare `#fragment` href opens nothing ([`opens_url`]); the CSS has no transition, so
//!   colors switch at once.
//! - Parts: `ItemMedia` (`icon`: 40px `layer-accent-01` square; `image`: 48px
//!   clipped square; icons in `icon-primary`), `ItemContent` (column, 2px gap),
//!   `ItemTitle` (14/18 semibold), `ItemDescription` (`body-01`,
//!   `text-secondary`), `ItemActions` (8px row), `ItemHeader`/`ItemFooter`
//!   (full-width rows), `ItemGroup` (column) and `ItemSeparator` (1px rule).
//! - **Behavior**: Base `Button` for link items (focus, Enter/Space, pointer).
use super::parts::part;
use crate::{
    theme::{LayerLevel, color},
    tokens::{ColorRole, FontFamilyKey, SPACING_01, SPACING_03, TypeStyle},
    typography::{TypeStyled as _, font},
};
use gpui_kit::{
    AnyElement, App, ClickEvent, CursorStyle, ElementId, Hsla, InteractiveElement as _,
    IntoElement, ParentElement, RenderOnce, SharedString, StatefulInteractiveElement as _, Styled,
    Window, div, prelude::FluentBuilder as _, px, rems,
};
use std::rc::Rc;

/// `index.d.ts` `ItemProps.variant`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ItemVariant {
    /// Transparent.
    #[default]
    Default,
    /// 1px inset `border-subtle` (`.cu-item--outline`).
    Outline,
    /// `layer-01` fill (`.cu-item--muted`).
    Muted,
}

impl ItemVariant {
    /// Every variant.
    pub const ALL: [ItemVariant; 3] = [Self::Default, Self::Outline, Self::Muted];

    /// The `index.d.ts` name.
    pub const fn name(self) -> &'static str {
        match self {
            Self::Default => "default",
            Self::Outline => "outline",
            Self::Muted => "muted",
        }
    }
}

/// `index.d.ts` `ItemProps.size`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ItemSize {
    /// 48px min height.
    #[default]
    Default,
    /// 40px min height.
    Sm,
    /// 32px min height.
    Xs,
}

impl ItemSize {
    /// Every size.
    pub const ALL: [ItemSize; 3] = [Self::Default, Self::Sm, Self::Xs];

    /// The `index.d.ts` name.
    pub const fn name(self) -> &'static str {
        match self {
            Self::Default => "default",
            Self::Sm => "sm",
            Self::Xs => "xs",
        }
    }
}

/// Item box geometry in px.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Geometry {
    /// `min-height`.
    pub min_height: f32,
    /// Vertical padding.
    pub padding_y: f32,
    /// Horizontal padding.
    pub padding_x: f32,
    /// Gap between media, content and actions.
    pub gap: f32,
}

impl Geometry {
    /// `.cu-item` / `--sm` / `--xs`.
    pub const fn of(size: ItemSize) -> Self {
        match size {
            ItemSize::Default => Self {
                min_height: 48.,
                padding_y: 12.,
                padding_x: 16.,
                gap: 16.,
            },
            ItemSize::Sm => Self {
                min_height: 40.,
                padding_y: 8.,
                padding_x: 16.,
                gap: 16.,
            },
            ItemSize::Xs => Self {
                min_height: 32.,
                padding_y: 4.,
                padding_x: 12.,
                gap: 8.,
            },
        }
    }
}

/// Whether a link item draws its focus ring: `.cu-item--link:focus-visible`,
/// so only for keyboard focus, never after a pointer press.
pub fn focus_ring_visible(link: bool, focused: bool, keyboard: bool) -> bool {
    link && focused && keyboard
}

/// Whether activating a link with `href` opens it. A bare fragment (`#`,
/// `#section`) points into the current page, which a desktop app has no
/// browser document for, so it navigates nowhere (the reference's `href="#"`).
pub fn opens_url(href: &str) -> bool {
    !href.is_empty() && !href.starts_with('#')
}

/// (background, outline) of an item: `muted` paints `layer-01`, link hover
/// `layer-hover-01`; `outline` draws `border-subtle`.
pub fn look(variant: ItemVariant, hovered_link: bool, level: LayerLevel) -> (Hsla, Option<Hsla>) {
    let background = if hovered_link {
        color(ColorRole::LayerHover01)
    } else if variant == ItemVariant::Muted {
        color(ColorRole::Layer01)
    } else {
        Hsla::transparent_black()
    };
    let outline = (variant == ItemVariant::Outline).then(|| super::separator::line_color(level));
    (background, outline)
}

type ClickHandler = Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>;

/// An item. See the module docs.
#[derive(IntoElement)]
pub struct Item {
    id: ElementId,
    variant: ItemVariant,
    size: ItemSize,
    level: LayerLevel,
    href: Option<SharedString>,
    on_click: Option<ClickHandler>,
    label: SharedString,
    children: Vec<AnyElement>,
}

impl Item {
    /// An item with a stable `id`.
    pub fn new(id: impl Into<ElementId>) -> Self {
        Self {
            id: id.into(),
            variant: ItemVariant::Default,
            size: ItemSize::Default,
            level: LayerLevel::One,
            href: None,
            on_click: None,
            label: SharedString::default(),
            children: Vec::new(),
        }
    }

    /// The variant.
    pub fn variant(mut self, variant: ItemVariant) -> Self {
        self.variant = variant;
        self
    }

    /// The size.
    pub fn size(mut self, size: ItemSize) -> Self {
        self.size = size;
        self
    }

    /// The layer it sits on (the outline's `border-subtle`).
    pub fn layer(mut self, level: LayerLevel) -> Self {
        self.level = level;
        self
    }

    /// Make the item a link to `href` (opened with the system handler; a bare
    /// `#fragment` opens nothing, see [`opens_url`]).
    pub fn href(mut self, href: impl Into<SharedString>) -> Self {
        self.href = Some(href.into());
        self
    }

    /// Make the item a link-like button.
    pub fn on_click(
        mut self,
        handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
    ) -> Self {
        self.on_click = Some(Rc::new(handler));
        self
    }

    /// Accessible name of a link item.
    pub fn label(mut self, label: impl Into<SharedString>) -> Self {
        self.label = label.into();
        self
    }
}

impl ParentElement for Item {
    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
        self.children.extend(elements);
    }
}

impl RenderOnce for Item {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let g = Geometry::of(self.size);
        let link = self.href.is_some() || self.on_click.is_some();
        let hovered = window.use_keyed_state((self.id.clone(), "hover"), cx, |_, _| false);
        let focus = window
            .use_keyed_state((self.id.clone(), "focus"), cx, |_, cx| cx.focus_handle())
            .read(cx)
            .clone();
        let focused = focus_ring_visible(
            link,
            focus.is_focused(window),
            crate::focus::focus_visible_mode(window),
        );
        let (background, outline) = look(self.variant, link && *hovered.read(cx), self.level);
        let r = |v: f32| rems(v / 16.);
        let row = div()
            .id((self.id.clone(), "row"))
            .relative()
            .flex()
            .flex_row()
            .flex_wrap()
            .items_center()
            .gap(r(g.gap))
            .min_h(r(g.min_height))
            .py(r(g.padding_y))
            .px(r(g.padding_x))
            .bg(background)
            .text_color(color(ColorRole::TextPrimary))
            .type_style(TypeStyle::BodyCompact01)
            .children(self.children)
            .when_some(outline, |this, outline| {
                // `outline: 1px solid var(--border-subtle); outline-offset: -1px`.
                this.child(
                    div()
                        .absolute()
                        .top_0()
                        .left_0()
                        .size_full()
                        .border_1()
                        .border_color(outline),
                )
            })
            .when(focused, |this| {
                // `.cu-item--link:focus-visible { outline: 2px solid var(--focus); outline-offset: -2px }`.
                this.child(
                    div()
                        .absolute()
                        .top_0()
                        .left_0()
                        .size_full()
                        .border_2()
                        .border_color(color(ColorRole::Focus)),
                )
            });
        if !link {
            return row.into_any_element();
        }
        let href = self.href.clone();
        let on_click = self.on_click.clone();
        row.cursor(CursorStyle::PointingHand)
            .on_hover(move |h, _, cx| {
                hovered.update(cx, |v, cx| {
                    *v = *h;
                    cx.notify();
                })
            })
            .child(
                // The Base button covers the row: focus, keyboard, pointer.
                gpui_kit::base::Button::new((self.id.clone(), "link"))
                    .track_focus(&focus)
                    .accessibility_label(self.label.clone())
                    .absolute()
                    .top_0()
                    .left_0()
                    .size_full()
                    .on_click(move |event, window, cx| {
                        if let Some(handler) = &on_click {
                            handler(event, window, cx);
                        }
                        if let Some(href) = href.as_ref().filter(|h| opens_url(h)) {
                            cx.open_url(href);
                        }
                    }),
            )
            .into_any_element()
    }
}

part!(
    /// `ItemGroup`: a column of items (`.cu-item-group`).
    ItemGroup,
    |children| div().flex().flex_col().children(children)
);

/// `ItemSeparator`: `.cu-item-sep { height: 1px; background: var(--border-subtle) }`.
#[derive(IntoElement, Default)]
pub struct ItemSeparator {
    level: LayerLevel,
}

impl ItemSeparator {
    /// A separator on `background`.
    pub fn new() -> Self {
        Self::default()
    }

    /// The layer it sits on.
    pub fn layer(mut self, level: LayerLevel) -> Self {
        self.level = level;
        self
    }
}

impl RenderOnce for ItemSeparator {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        div()
            .h(px(1.))
            .w_full()
            .bg(super::separator::line_color(self.level))
    }
}

/// `index.d.ts` `ItemMedia.variant`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ItemMediaVariant {
    /// As is (an avatar, an icon).
    #[default]
    Default,
    /// A 40px `layer-accent-01` square (`.cu-item-media--icon`).
    Icon,
    /// A 48px clipped square (`.cu-item-media--image`).
    Image,
}

impl ItemMediaVariant {
    /// Every variant.
    pub const ALL: [ItemMediaVariant; 3] = [Self::Default, Self::Icon, Self::Image];

    /// The `index.d.ts` name.
    pub const fn name(self) -> &'static str {
        match self {
            Self::Default => "default",
            Self::Icon => "icon",
            Self::Image => "image",
        }
    }
}

/// `ItemMedia`. Icons inside should take [`ItemMedia::icon_color`] (`icon-primary`).
#[derive(IntoElement, Default)]
pub struct ItemMedia {
    variant: ItemMediaVariant,
    children: Vec<AnyElement>,
}

impl ItemMedia {
    /// Default media.
    pub fn new() -> Self {
        Self::default()
    }

    /// The variant.
    pub fn variant(mut self, variant: ItemMediaVariant) -> Self {
        self.variant = variant;
        self
    }

    /// `.cu-item-media { color: var(--icon-primary) }`.
    pub fn icon_color() -> Hsla {
        color(ColorRole::IconPrimary)
    }
}

impl ParentElement for ItemMedia {
    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
        self.children.extend(elements);
    }
}

impl RenderOnce for ItemMedia {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        let media = div()
            .flex()
            .flex_none()
            .items_center()
            .justify_center()
            .text_color(Self::icon_color());
        match self.variant {
            ItemMediaVariant::Default => media,
            ItemMediaVariant::Icon => media.size(rems(2.5)).bg(color(ColorRole::LayerAccent01)),
            ItemMediaVariant::Image => media.size(rems(3.)).overflow_hidden(),
        }
        .children(self.children)
    }
}

part!(
    /// `ItemContent`: `.cu-item-content { display: flex; flex: 1; flex-direction:
    /// column; gap: var(--spacing-01); min-width: 0 }`.
    ItemContent,
    |children| div()
        .flex()
        .flex_col()
        .flex_1()
        .min_w_0()
        .gap(SPACING_01)
        .children(children)
);

part!(
    /// `ItemActions`: `.cu-item-actions { display: flex; gap: var(--spacing-03); align-items: center }`.
    ItemActions,
    |children| div()
        .flex()
        .flex_row()
        .flex_none()
        .items_center()
        .gap(SPACING_03)
        .children(children)
);

part!(
    /// `ItemHeader`: a full-width row (`flex-basis: 100%; justify-content: space-between`).
    ItemHeader,
    |children| div()
        .flex()
        .flex_row()
        .w_full()
        .justify_between()
        .items_center()
        .gap(SPACING_03)
        .children(children)
);

part!(
    /// `ItemFooter`: a full-width row (`flex-basis: 100%; justify-content: space-between`).
    ItemFooter,
    |children| div()
        .flex()
        .flex_row()
        .w_full()
        .justify_between()
        .items_center()
        .gap(SPACING_03)
        .children(children)
);

/// `ItemTitle`: `.cu-item-title { font-size: 0.875rem; font-weight: 600; line-height: 1.28572 }`.
#[derive(IntoElement)]
pub struct ItemTitle(SharedString);

impl ItemTitle {
    /// A title.
    pub fn new(text: impl Into<SharedString>) -> Self {
        Self(text.into())
    }
}

impl RenderOnce for ItemTitle {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        div()
            .font(font(FontFamilyKey::Sans, 600))
            .text_size(rems(0.875))
            .line_height(rems(1.125))
            .child(self.0)
    }
}

/// `ItemDescription`: `.cu-item-desc { font-size: 0.875rem; line-height: 1.42857;
/// color: var(--text-secondary) }`.
#[derive(IntoElement)]
pub struct ItemDescription(SharedString);

impl ItemDescription {
    /// A description.
    pub fn new(text: impl Into<SharedString>) -> Self {
        Self(text.into())
    }
}

impl RenderOnce for ItemDescription {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        div()
            .type_style(TypeStyle::Body01)
            .text_color(color(ColorRole::TextSecondary))
            .child(self.0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tokens::CarbonTheme;

    #[test]
    fn geometry_and_colors_follow_custom_css() {
        crate::theme::tests_set(CarbonTheme::White);
        assert_eq!(Geometry::of(ItemSize::Default).min_height, 48.);
        assert_eq!(Geometry::of(ItemSize::Sm).padding_y, 8.);
        let xs = Geometry::of(ItemSize::Xs);
        assert_eq!(
            (xs.min_height, xs.padding_y, xs.padding_x, xs.gap),
            (32., 4., 12., 8.)
        );
        let (bg, outline) = look(ItemVariant::Muted, false, LayerLevel::One);
        assert_eq!(bg, color(ColorRole::Layer01));
        assert!(outline.is_none());
        let (bg, _) = look(ItemVariant::Default, true, LayerLevel::One);
        assert_eq!(bg, color(ColorRole::LayerHover01));
        let (_, outline) = look(ItemVariant::Outline, false, LayerLevel::One);
        assert_eq!(outline, Some(color(ColorRole::BorderSubtle00)));
    }

    #[test]
    fn link_focus_ring_is_keyboard_only() {
        assert!(focus_ring_visible(true, true, true));
        // A pointer press focuses the link without `:focus-visible`.
        assert!(!focus_ring_visible(true, true, false));
        assert!(!focus_ring_visible(false, true, true));
    }

    #[test]
    fn fragment_links_open_nothing() {
        assert!(!opens_url("#"));
        assert!(!opens_url("#owner"));
        assert!(!opens_url(""));
        assert!(opens_url("https://carbondesignsystem.com"));
    }
}