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
//! `Button`.
use dioxus::prelude::*;
use crate::class::compose;
/// Visual treatment of a [`Button`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ButtonVariant {
/// Solid brand button for the primary action.
#[default]
Default,
/// Quiet filled button.
Secondary,
/// Bordered button.
Outline,
/// Button without a border or fill.
Ghost,
/// Destructive action.
Destructive,
/// Text link styled as a button.
Link,
}
impl ButtonVariant {
/// The class suffix and `data-variant` value.
pub fn as_str(self) -> &'static str {
match self {
Self::Default => "default",
Self::Secondary => "secondary",
Self::Outline => "outline",
Self::Ghost => "ghost",
Self::Destructive => "destructive",
Self::Link => "link",
}
}
}
/// Semantic color, independent of button prominence.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ButtonTone {
/// A destructive action.
Danger,
/// A positive action.
Success,
}
impl ButtonTone {
fn class(self) -> &'static str {
match self {
Self::Danger => "ui-button--tone-danger",
Self::Success => "ui-button--tone-success",
}
}
}
/// Size of a [`Button`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ButtonSize {
/// Default control height.
#[default]
Default,
/// Compact control.
Sm,
/// Large control.
Lg,
/// Square icon-only control. Give it an `aria_label`.
Icon,
}
impl ButtonSize {
/// The size modifier class, such as `ui-button--sm`.
pub(crate) fn class(self) -> &'static str {
match self {
Self::Default => "ui-button--default-size",
Self::Sm => "ui-button--sm",
Self::Lg => "ui-button--lg",
Self::Icon => "ui-button--icon",
}
}
}
/// Props for [`Button`].
#[derive(Props, Clone, PartialEq)]
pub struct ButtonProps {
/// Visual treatment.
#[props(default)]
pub variant: ButtonVariant,
/// Optional semantic color. Omit to preserve the variant color.
pub tone: Option<ButtonTone>,
/// Control size.
#[props(default)]
pub size: ButtonSize,
/// Disables the button.
#[props(default)]
pub disabled: bool,
/// Shows a spinner and sets `aria-busy` and `aria-disabled`. The button stays focusable, so
/// keyboard focus survives the request, and ignores clicks until loading ends.
#[props(default)]
pub loading: bool,
/// The native button type. Defaults to `button`, so it never submits a form by accident.
#[props(default = "button".to_string(), into)]
pub r#type: String,
/// Extra classes appended after the Lumen classes.
#[props(into)]
pub class: Option<String>,
/// Click handler.
pub onclick: Option<EventHandler<MouseEvent>>,
/// Any other `<button>` or global attribute.
#[props(extends = GlobalAttributes, extends = button)]
pub attributes: Vec<Attribute>,
/// Button label and icons.
pub children: Element,
}
/// A button with Lumen's variants, sizes, and loading state.
///
/// Renders the same markup as `Button` in `@santi020k/lumen-react`.
#[component]
pub fn Button(props: ButtonProps) -> Element {
let loading = props.loading;
let variant = format!("ui-button--{}", props.variant.as_str());
let class = compose([
Some("ui-button"),
props.tone.map(ButtonTone::class),
Some(variant.as_str()),
Some(props.size.class()),
loading.then_some("ui-button--loading"),
props.class.as_deref(),
]);
let onclick = props.onclick;
// Loading uses `aria-disabled` rather than the native attribute, which would drop keyboard
// focus mid-request. Canceling the click also cancels a submit button's form submission.
rsx! {
button {
aria_busy: loading.then_some("true"),
aria_disabled: loading.then_some("true"),
class,
"data-slot": "button",
disabled: props.disabled,
r#type: props.r#type,
onclick: move |event| {
if loading {
event.prevent_default();
event.stop_propagation();
} else if let Some(handler) = onclick {
handler.call(event);
}
},
..props.attributes,
if loading {
span { aria_hidden: "true", class: "ui-spinner" }
}
span { class: "ui-button__content", {props.children} }
}
}
}