1use dioxus::prelude::*;
4
5use crate::class::compose;
6
7#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
9#[non_exhaustive]
10pub enum ButtonVariant {
11 #[default]
13 Default,
14 Secondary,
16 Outline,
18 Ghost,
20 Destructive,
22 Link,
24}
25
26impl ButtonVariant {
27 pub fn as_str(self) -> &'static str {
29 match self {
30 Self::Default => "default",
31 Self::Secondary => "secondary",
32 Self::Outline => "outline",
33 Self::Ghost => "ghost",
34 Self::Destructive => "destructive",
35 Self::Link => "link",
36 }
37 }
38}
39
40#[derive(Clone, Copy, Debug, PartialEq, Eq)]
42#[non_exhaustive]
43pub enum ButtonTone {
44 Danger,
46 Success,
48}
49
50impl ButtonTone {
51 fn class(self) -> &'static str {
52 match self {
53 Self::Danger => "ui-button--tone-danger",
54 Self::Success => "ui-button--tone-success",
55 }
56 }
57}
58
59#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
61#[non_exhaustive]
62pub enum ButtonSize {
63 #[default]
65 Default,
66 Sm,
68 Lg,
70 Icon,
72}
73
74impl ButtonSize {
75 pub(crate) fn class(self) -> &'static str {
77 match self {
78 Self::Default => "ui-button--default-size",
79 Self::Sm => "ui-button--sm",
80 Self::Lg => "ui-button--lg",
81 Self::Icon => "ui-button--icon",
82 }
83 }
84}
85
86#[derive(Props, Clone, PartialEq)]
88pub struct ButtonProps {
89 #[props(default)]
91 pub variant: ButtonVariant,
92 pub tone: Option<ButtonTone>,
94 #[props(default)]
96 pub size: ButtonSize,
97 #[props(default)]
99 pub disabled: bool,
100 #[props(default)]
103 pub loading: bool,
104 #[props(default = "button".to_string(), into)]
106 pub r#type: String,
107 #[props(into)]
109 pub class: Option<String>,
110 pub onclick: Option<EventHandler<MouseEvent>>,
112 #[props(extends = GlobalAttributes, extends = button)]
114 pub attributes: Vec<Attribute>,
115 pub children: Element,
117}
118
119#[component]
123pub fn Button(props: ButtonProps) -> Element {
124 let loading = props.loading;
125 let variant = format!("ui-button--{}", props.variant.as_str());
126 let class = compose([
127 Some("ui-button"),
128 props.tone.map(ButtonTone::class),
129 Some(variant.as_str()),
130 Some(props.size.class()),
131 loading.then_some("ui-button--loading"),
132 props.class.as_deref(),
133 ]);
134 let onclick = props.onclick;
135
136 rsx! {
139 button {
140 aria_busy: loading.then_some("true"),
141 aria_disabled: loading.then_some("true"),
142 class,
143 "data-slot": "button",
144 disabled: props.disabled,
145 r#type: props.r#type,
146 onclick: move |event| {
147 if loading {
148 event.prevent_default();
149 event.stop_propagation();
150 } else if let Some(handler) = onclick {
151 handler.call(event);
152 }
153 },
154 ..props.attributes,
155 if loading {
156 span { aria_hidden: "true", class: "ui-spinner" }
157 }
158 span { class: "ui-button__content", {props.children} }
159 }
160 }
161}