adam_web_ui/components/button/component.rs
1//! Styled upstream button implementation; see `../UPSTREAM.md`.
2use dioxus::prelude::*;
3use dioxus_primitives::dioxus_attributes::attributes;
4use dioxus_primitives::merge_attributes;
5
6include!(concat!(env!("OUT_DIR"), "/button-styles.rs"));
7
8/// Selects the button's visual intent without changing native button semantics.
9///
10/// # Examples
11/// ```
12/// use adam_web_ui::components::button::ButtonVariant;
13/// let variant = ButtonVariant::Primary;
14/// ```
15#[derive(Copy, Clone, PartialEq, Default)]
16#[non_exhaustive]
17pub enum ButtonVariant {
18 #[default]
19 /// Marks the primary action.
20 Primary,
21 /// Marks a secondary action.
22 Secondary,
23 /// Marks an irreversible action.
24 Destructive,
25 /// Draws an outlined action.
26 Outline,
27 /// Omits the background until interaction.
28 Ghost,
29 /// Uses link-like styling without changing the element.
30 Link,
31}
32
33impl ButtonVariant {
34 /// Returns the upstream visual intent token.
35 ///
36 /// # Examples
37 /// ```
38 /// use adam_web_ui::components::button::ButtonVariant;
39 /// assert_eq!(ButtonVariant::Primary.class(), "primary");
40 /// ```
41 pub fn class(&self) -> &'static str {
42 match self {
43 ButtonVariant::Primary => "primary",
44 ButtonVariant::Secondary => "secondary",
45 ButtonVariant::Destructive => "destructive",
46 ButtonVariant::Outline => "outline",
47 ButtonVariant::Ghost => "ghost",
48 ButtonVariant::Link => "link",
49 }
50 }
51}
52
53/// Selects the button's layout size.
54///
55/// # Examples
56/// ```
57/// use adam_web_ui::components::button::ButtonSize;
58/// let size = ButtonSize::Default;
59/// ```
60#[derive(Copy, Clone, PartialEq, Default)]
61#[non_exhaustive]
62pub enum ButtonSize {
63 /// Uses an extra-small text button.
64 Xs,
65 /// Uses a small text button.
66 Sm,
67 #[default]
68 /// Uses the standard text button.
69 Default,
70 /// Uses a large text button.
71 Lg,
72 /// Uses a standard square icon button.
73 Icon,
74 /// Uses an extra-small square icon button.
75 IconXs,
76 /// Uses a small square icon button.
77 IconSm,
78 /// Uses a large square icon button.
79 IconLg,
80}
81
82impl ButtonSize {
83 /// Returns the upstream layout size token.
84 ///
85 /// # Examples
86 /// ```
87 /// use adam_web_ui::components::button::ButtonSize;
88 /// assert_eq!(ButtonSize::Default.class(), "default");
89 /// ```
90 pub fn class(&self) -> &'static str {
91 match self {
92 ButtonSize::Xs => "xs",
93 ButtonSize::Sm => "sm",
94 ButtonSize::Default => "default",
95 ButtonSize::Lg => "lg",
96 ButtonSize::Icon => "icon",
97 ButtonSize::IconXs => "icon-xs",
98 ButtonSize::IconSm => "icon-sm",
99 ButtonSize::IconLg => "icon-lg",
100 }
101 }
102}
103
104/// Renders a styled native button with caller-supplied attributes and events.
105///
106/// - Complexity: O(attributes + children) render work.
107///
108/// # Examples
109/// ```
110/// use adam_web_ui::components::button::Button;
111/// use dioxus::prelude::*;
112/// fn app() -> Element { rsx! { Button { "Save" } } }
113/// ```
114#[component]
115pub fn Button(
116 #[props(default)] variant: ButtonVariant,
117 #[props(default)] size: ButtonSize,
118 #[props(extends=GlobalAttributes)]
119 #[props(extends=button)]
120 attributes: Vec<Attribute>,
121 onclick: Option<EventHandler<MouseEvent>>,
122 onmousedown: Option<EventHandler<MouseEvent>>,
123 onmouseup: Option<EventHandler<MouseEvent>>,
124 onkeydown: Option<EventHandler<KeyboardEvent>>,
125 children: Element,
126) -> Element {
127 crate::components::styles::use_control_styles();
128 let base = attributes!(button {
129 class: Styles::dx_button,
130 "data-style": variant.class(),
131 "data-size": size.class(),
132 });
133 let merged = merge_attributes(vec![base, attributes]);
134
135 rsx! {
136 button {
137 onclick: move |event| {
138 if let Some(f) = &onclick {
139 f.call(event);
140 }
141 },
142 onmousedown: move |event| {
143 if let Some(f) = &onmousedown {
144 f.call(event);
145 }
146 },
147 onmouseup: move |event| {
148 if let Some(f) = &onmouseup {
149 f.call(event);
150 }
151 },
152 onkeydown: move |event| {
153 if let Some(f) = &onkeydown {
154 f.call(event);
155 }
156 },
157 ..merged,
158 {children}
159 }
160 }
161}