Skip to main content

tablo_ui/components/composites/
button.rs

1//! Tablo's button: a variant for its weight, a size for its height. The markup carries
2//! `data-slot="button"`, `data-tone` and `data-size`, which each theme's skin keys on.
3
4use topcoat::{
5    Result,
6    context::Cx,
7    view::{Attributes, Child, Class, StaticClass, View, class, component, view},
8};
9
10/// How much a button weighs on its page.
11#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
12pub enum ButtonVariant {
13    /// The page's one main action, in the primary color.
14    #[default]
15    Primary,
16    /// A filled, quiet action.
17    Secondary,
18    /// A bordered action on the control surface: the page's other actions.
19    Outline,
20    /// No surface until hovered: toolbar and row actions.
21    Ghost,
22    /// An action that deletes or cannot be undone.
23    Destructive,
24}
25
26impl ButtonVariant {
27    const fn name(self) -> &'static str {
28        match self {
29            Self::Primary => "primary",
30            Self::Secondary => "secondary",
31            Self::Outline => "outline",
32            Self::Ghost => "ghost",
33            Self::Destructive => "destructive",
34        }
35    }
36
37    fn classes(self) -> StaticClass {
38        match self {
39            Self::Primary => class!(
40                "border-primary bg-primary text-primary-foreground shadow-button",
41                "hover:bg-[color-mix(in_oklab,var(--primary),var(--foreground)_12%)]",
42                "hover:border-[color-mix(in_oklab,var(--primary),var(--foreground)_12%)]",
43            ),
44            Self::Secondary => class!(
45                "border-transparent bg-foreground/[0.06] text-foreground",
46                "hover:bg-foreground/10 active:bg-foreground/[0.14]",
47            ),
48            Self::Outline => class!(
49                "border-input bg-control text-foreground shadow-control",
50                "hover:border-[color-mix(in_oklab,var(--input),var(--foreground)_20%)] hover:bg-muted",
51            ),
52            Self::Ghost => class!(
53                "border-transparent text-foreground hover:bg-foreground/[0.06]",
54                "active:bg-foreground/10",
55            ),
56            Self::Destructive => class!(
57                "border-destructive bg-destructive text-destructive-foreground shadow-button",
58                "hover:bg-[color-mix(in_oklab,var(--destructive),var(--foreground)_12%)]",
59            ),
60        }
61    }
62}
63
64/// How tall a button is.
65#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
66pub enum ButtonSize {
67    /// 2rem: toolbars and dense rows.
68    Sm,
69    /// 2.25rem, the height of a field.
70    #[default]
71    Md,
72    /// 2.5rem: a page outside the shell, such as sign-in.
73    Lg,
74    /// A square of the default height, for a lone icon.
75    Icon,
76}
77
78impl ButtonSize {
79    const fn name(self) -> &'static str {
80        match self {
81            Self::Sm => "sm",
82            Self::Md => "md",
83            Self::Lg => "lg",
84            Self::Icon => "icon",
85        }
86    }
87
88    fn classes(self) -> StaticClass {
89        match self {
90            Self::Sm => class!("h-8 gap-1.5 px-3 text-[0.8125rem] [&>svg]:size-3.5"),
91            Self::Md => class!("h-9 gap-2 px-3.5 text-sm"),
92            Self::Lg => class!("h-10 gap-2 px-5 text-sm"),
93            Self::Icon => class!("size-9"),
94        }
95    }
96}
97
98const BASE: StaticClass = class!(
99    "inline-flex shrink-0 cursor-pointer items-center justify-center rounded-md border",
100    "font-medium whitespace-nowrap outline-none select-none",
101    "transition-[color,background-color,border-color,box-shadow,translate] duration-150",
102    "focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
103    "focus-visible:ring-offset-background disabled:pointer-events-none disabled:opacity-50",
104    "aria-disabled:pointer-events-none aria-disabled:opacity-50",
105    "[&>svg]:size-4 [&>svg]:shrink-0",
106);
107
108/// The classes of a button, for a link or a summary that reads as one. Give the element
109/// `data-slot="button"` and the variant's `data-tone` too, so each theme's skin reaches it.
110#[must_use]
111pub fn button_variants(
112    variant: ButtonVariant,
113    size: ButtonSize,
114) -> Class<(StaticClass, StaticClass, StaticClass)> {
115    class!(BASE, variant.classes(), size.classes())
116}
117
118/// A `<button>`; its `type` is the browser's default unless `attrs` names one.
119#[component]
120pub async fn button(
121    cx: &Cx,
122    #[default] variant: ButtonVariant,
123    #[default] size: ButtonSize,
124    #[default] mut attrs: Attributes,
125    #[default] child: Child<'_>,
126) -> Result<impl View> {
127    let class = class!(
128        BASE,
129        variant.classes(),
130        size.classes(),
131        attrs.remove("class")
132    );
133    Ok(view! {
134        <button
135            class=(class)
136            data-slot="button"
137            data-tone=(variant.name())
138            data-size=(size.name())
139            (attrs)
140        >
141            (child)
142        </button>
143    })
144}