forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksJavaScript3.6 MiBe100df4Tahinli1 month ago1 commit

button.rs154 lines, 4.9 KiBHistoryRawDownload
1use topcoat::{
2 Result,
3 view::{Attributes, Child, Class, StaticClass, View, class, component, view},
4};
5
6/// The visual style of a [`button`].
7///
8/// [`Default`] is `ButtonVariant::Primary`, used when no variant is given.
9#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
10#[allow(dead_code)]
11pub enum ButtonVariant {
12 /// The highlighter-filled button for the main action.
13 #[default]
14 Primary,
15 /// A muted tinted fill for secondary actions.
16 Secondary,
17 /// A neutral hairline button on the card surface. The everyday button.
18 Outline,
19 /// No fill until hovered, for toolbars and inline actions.
20 Ghost,
21 /// A red hairline button for actions that delete data.
22 Destructive,
23}
24
25impl ButtonVariant {
26 /// Classes for the button variant and its interaction states.
27 ///
28 /// Each variant sets its own border color. Keep border colors out of the shared
29 /// base to avoid conflicting classes.
30 fn classes(self) -> StaticClass {
31 match self {
32 Self::Primary => class!(
33 "border-transparent bg-primary text-primary-foreground",
34 "hover:bg-primary/90 active:bg-primary/80",
35 ),
36 Self::Secondary => class!(
37 "border-transparent bg-foreground/5 text-foreground",
38 "hover:bg-foreground/10 active:bg-foreground/15",
39 ),
40 Self::Outline => class!(
41 "border-border bg-card text-foreground",
42 "hover:bg-foreground/5 active:bg-foreground/10",
43 ),
44 Self::Ghost => class!(
45 "border-transparent text-foreground hover:bg-foreground/5 active:bg-foreground/10",
46 ),
47 Self::Destructive => class!(
48 "border-destructive bg-card text-destructive",
49 "hover:bg-destructive/5 active:bg-destructive/10",
50 ),
51 }
52 }
53}
54
55/// The size of a [`button`].
56///
57/// [`Default`] is `ButtonSize::Md`, used when no size is given.
58#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
59#[allow(dead_code)]
60pub enum ButtonSize {
61 /// A compact 28px button.
62 Sm,
63 /// The standard 32px button.
64 #[default]
65 Md,
66 /// A prominent 40px button.
67 Lg,
68 /// A square 32px button sized for a single icon.
69 Icon,
70}
71
72impl ButtonSize {
73 /// Classes for the button dimensions. Text size stays the same across sizes.
74 fn classes(self) -> StaticClass {
75 match self {
76 Self::Sm => class!("h-7 gap-1.5 px-2.5"),
77 Self::Md => class!("h-8 gap-2 px-3"),
78 Self::Lg => class!("h-10 gap-2 px-4"),
79 Self::Icon => class!("size-8"),
80 }
81 }
82}
83
84/// Classes shared by button variants and sizes. A border reserves the same space in
85/// every variant. Focus uses the global `:focus-visible` outline from `styles.css`.
86const BASE: StaticClass = class!(
87 "inline-flex shrink-0 items-center justify-center rounded-control border",
88 "text-sm font-medium whitespace-nowrap select-none transition-colors duration-150 ease-out",
89 "cursor-pointer disabled:cursor-not-allowed disabled:opacity-50",
90);
91
92/// Builds the full class list for a button of the given `variant` and `size`.
93///
94/// Use it to give button styling to an element that is not a `<button>`, such
95/// as a link styled as a button:
96///
97/// ```ignore
98/// view! {
99/// <a href="/login" class=(button_variants(ButtonVariant::Outline, ButtonSize::Md))>
100/// "Sign in"
101/// </a>
102/// }
103/// ```
104#[must_use]
105pub fn button_variants(
106 variant: ButtonVariant,
107 size: ButtonSize,
108) -> Class<(StaticClass, StaticClass, StaticClass)> {
109 class!(BASE, variant.classes(), size.classes())
110}
111
112/// A styled button.
113///
114/// `variant` defaults to `Primary` and `size` to `Md`. Pass the content as children.
115/// `keycap` shows a keyboard shortcut inside the button after the label, for example
116/// `kbd("Ctrl") kbd("K")`. `attrs` are forwarded to the `<button>`, with extra classes
117/// added to its classes. Use [`button_variants`] to apply the same styling to another
118/// element.
119///
120/// A disabled button stays visible and greyed out. Pass the reason in `title` or
121/// `aria-describedby`.
122///
123/// ```ignore
124/// view! {
125/// button(
126/// variant: ButtonVariant::Destructive,
127/// attrs: attributes! { type="submit" },
128/// "Delete"
129/// )
130/// }
131/// ```
132#[component]
133pub async fn button(
134 #[default] variant: ButtonVariant,
135 #[default] size: ButtonSize,
136 #[default] mut attrs: Attributes,
137 #[default] keycap: Child<'_>,
138 #[default] child: Child<'_>,
139) -> Result<impl View> {
140 Ok(view! {
141 <button
142 class=(class!(
143 BASE,
144 variant.classes(),
145 size.classes(),
146 attrs.remove("class"),
147 ))
148 (attrs)
149 >
150 (child)
151 <span class="inline-flex items-center gap-1 empty:hidden">(keycap)</span>
152 </button>
153 })
154}