tablo_ui/components/composites/
button.rs1use topcoat::{
5 Result,
6 context::Cx,
7 view::{Attributes, Child, Class, StaticClass, View, class, component, view},
8};
9
10#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
12pub enum ButtonVariant {
13 #[default]
15 Primary,
16 Secondary,
18 Outline,
20 Ghost,
22 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#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
66pub enum ButtonSize {
67 Sm,
69 #[default]
71 Md,
72 Lg,
74 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#[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#[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}