at main 62 lines 1.6 kB view raw
1use dioxus::prelude::*; 2 3#[derive(Copy, Clone, PartialEq, Default)] 4#[non_exhaustive] 5pub enum ButtonVariant { 6 #[default] 7 Primary, 8 Secondary, 9 Destructive, 10 Outline, 11 Ghost, 12} 13 14impl ButtonVariant { 15 pub fn class(&self) -> &'static str { 16 match self { 17 ButtonVariant::Primary => "primary", 18 ButtonVariant::Secondary => "secondary", 19 ButtonVariant::Destructive => "destructive", 20 ButtonVariant::Outline => "outline", 21 ButtonVariant::Ghost => "ghost", 22 } 23 } 24} 25 26#[component] 27pub fn Button( 28 #[props(default)] variant: ButtonVariant, 29 #[props(extends=GlobalAttributes)] 30 #[props(extends=button)] 31 attributes: Vec<Attribute>, 32 onclick: Option<EventHandler<MouseEvent>>, 33 onmousedown: Option<EventHandler<MouseEvent>>, 34 onmouseup: Option<EventHandler<MouseEvent>>, 35 children: Element, 36) -> Element { 37 rsx! { 38 document::Link { rel: "stylesheet", href: asset!("/assets/styling/button.css") } 39 40 button { 41 class: "button", 42 "data-style": variant.class(), 43 onclick: move |event| { 44 if let Some(f) = &onclick { 45 f.call(event); 46 } 47 }, 48 onmousedown: move |event| { 49 if let Some(f) = &onmousedown { 50 f.call(event); 51 } 52 }, 53 onmouseup: move |event| { 54 if let Some(f) = &onmouseup { 55 f.call(event); 56 } 57 }, 58 ..attributes, 59 {children} 60 } 61 } 62}