helixui/components/common/
button.rs1use crate::components::{Icon, IconType};
2use dioxus::prelude::*;
3
4#[derive(Clone, PartialEq)]
6pub enum ButtonType {
7 Default,
8 Primary,
9 Info,
10 Success,
11 Warning,
12 Error,
13 Tertiary,
14}
15
16impl ButtonType {
17 pub fn to_class(&self) -> &str {
18 match self {
19 ButtonType::Default => "bg-white text-gray-700 border border-gray-300 hover:bg-gray-50",
20 ButtonType::Primary => "bg-green-500 text-white hover:bg-green-600",
21 ButtonType::Info => "bg-blue-500 text-white hover:bg-blue-600",
22 ButtonType::Success => "bg-emerald-500 text-white hover:bg-emerald-600",
23 ButtonType::Warning => "bg-orange-500 text-white hover:bg-orange-600",
24 ButtonType::Error => "bg-red-500 text-white hover:bg-red-600",
25 ButtonType::Tertiary => "bg-transparent text-gray-700 hover:bg-gray-100",
26 }
27 }
28}
29
30#[derive(Clone, PartialEq)]
32pub enum ButtonSize {
33 Tiny,
34 Small,
35 Medium,
36 Large,
37}
38
39#[derive(Clone, PartialEq)]
41pub enum ButtonVariant {
42 Text, Icon, IconText, }
46
47#[derive(Clone, PartialEq)]
49pub enum ButtonShape {
50 Default, Rounded, Circle, Ellipse, }
55
56impl ButtonSize {
57 pub fn to_class(&self) -> &str {
58 match self {
59 ButtonSize::Tiny => "px-2 py-0.5 text-xs",
60 ButtonSize::Small => "px-3 py-1 text-sm",
61 ButtonSize::Medium => "px-4 py-2 text-base",
62 ButtonSize::Large => "px-6 py-3 text-lg",
63 }
64 }
65
66 pub fn icon_class(&self) -> &str {
67 match self {
68 ButtonSize::Tiny => "w-3.5 h-3.5", ButtonSize::Small => "w-4 h-4",
70 ButtonSize::Medium => "w-5 h-5",
71 ButtonSize::Large => "w-6 h-6",
72 }
73 }
74
75 pub fn icon_only_class(&self) -> &str {
76 match self {
77 ButtonSize::Tiny => "p-1.5", ButtonSize::Small => "p-2", ButtonSize::Medium => "p-2.5", ButtonSize::Large => "p-3",
81 }
82 }
83}
84
85impl ButtonShape {
86 pub fn to_class(&self) -> &str {
87 match self {
88 ButtonShape::Default => "rounded-none",
89 ButtonShape::Rounded => "rounded-md",
90 ButtonShape::Circle => "rounded-full",
91 ButtonShape::Ellipse => "rounded-full",
92 }
93 }
94
95 pub fn size_class(&self, size: &ButtonSize) -> &str {
96 match self {
97 ButtonShape::Circle => match size {
98 ButtonSize::Tiny => "w-7 h-7", ButtonSize::Small => "w-8 h-8",
100 ButtonSize::Medium => "w-10 h-10",
101 ButtonSize::Large => "w-12 h-12",
102 },
103 ButtonShape::Ellipse => match size {
104 ButtonSize::Tiny => "w-8 h-6",
105 ButtonSize::Small => "w-12 h-8",
106 ButtonSize::Medium => "w-16 h-10",
107 ButtonSize::Large => "w-20 h-12",
108 },
109 _ => "", }
111 }
112}
113
114#[component]
116pub fn Button(
117 #[props(default = ButtonType::Default)] button_type: ButtonType,
118 #[props(default = ButtonSize::Medium)] size: ButtonSize,
119 #[props(default = false)] disabled: bool,
120 #[props(default = false)] secondary: bool,
121 #[props(default = ButtonVariant::Text)] variant: ButtonVariant,
122 #[props(default = ButtonShape::Rounded)] shape: ButtonShape,
123 #[props(default)] icon: Option<IconType>,
124 #[props(default)] onclick: Option<EventHandler<()>>,
125 #[props(default)] class: Option<String>,
126 children: Element,
127) -> Element {
128 let type_class = button_type.to_class();
129 let size_class = size.to_class();
130 let disabled_class = if disabled {
131 "opacity-50 cursor-not-allowed"
132 } else {
133 "cursor-pointer"
134 };
135 let secondary_class = if secondary {
136 match button_type {
137 ButtonType::Primary => "bg-green-100 text-green-700 hover:bg-green-200",
138 ButtonType::Info => "bg-blue-100 text-blue-700 hover:bg-blue-200",
139 ButtonType::Success => "bg-emerald-100 text-emerald-700 hover:bg-emerald-200",
140 ButtonType::Warning => "bg-orange-100 text-orange-700 hover:bg-orange-200",
141 ButtonType::Error => "bg-red-100 text-red-700 hover:bg-red-200",
142 _ => type_class,
143 }
144 } else {
145 type_class
146 };
147
148 let shape_class = shape.to_class();
150 let shape_size_class = shape.size_class(&size);
151
152 let base_class = match variant {
153 ButtonVariant::Text => {
154 let padding_class = if shape == ButtonShape::Circle || shape == ButtonShape::Ellipse {
155 "" } else {
157 size_class
158 };
159 format!(
160 "{} font-medium transition-colors duration-200 {} {} {} {}",
161 shape_class, secondary_class, padding_class, shape_size_class, disabled_class
162 )
163 }
164 ButtonVariant::Icon => {
165 let padding_class = if shape == ButtonShape::Circle || shape == ButtonShape::Ellipse {
166 "" } else {
168 size.icon_only_class()
169 };
170 format!(
171 "{} font-medium transition-colors duration-200 {} {} {} {}",
172 shape_class, secondary_class, padding_class, shape_size_class, disabled_class
173 )
174 }
175 ButtonVariant::IconText => {
176 let padding_class = if shape == ButtonShape::Circle || shape == ButtonShape::Ellipse {
177 "" } else {
179 size_class
180 };
181 format!(
182 "{} font-medium transition-colors duration-200 {} {} {} {} flex items-center gap-2",
183 shape_class, secondary_class, padding_class, shape_size_class, disabled_class
184 )
185 }
186 };
187
188 let final_class = if let Some(custom_class) = class {
189 format!("{} {}", base_class, custom_class)
190 } else {
191 base_class
192 };
193
194 rsx! {
195 button {
196 class: final_class,
197 disabled: disabled,
198 onclick: move |_| {
199 if let Some(handler) = &onclick {
200 handler.call(());
201 }
202 },
203 match variant {
204 ButtonVariant::Text => rsx! { {children} },
205 ButtonVariant::Icon => rsx! {
206 if let Some(icon_type) = icon {
207 Icon {
208 icon: icon_type,
209 class: size.icon_class().to_string(),
210 }
211 }
212 },
213 ButtonVariant::IconText => rsx! {
214 if let Some(icon_type) = icon {
215 Icon {
216 icon: icon_type,
217 class: size.icon_class().to_string(),
218 }
219 }
220 {children}
221 },
222 }
223 }
224 }
225}