Skip to main content

dioxus_bulma/
theme.rs

1use dioxus::prelude::*;
2
3#[derive(Debug, Clone, Copy, PartialEq)]
4pub enum BulmaTheme {
5    Auto,
6    Light,
7    Dark,
8}
9
10impl Default for BulmaTheme {
11    fn default() -> Self {
12        BulmaTheme::Auto
13    }
14}
15
16#[derive(Debug, Clone, Copy, PartialEq)]
17pub enum BulmaColor {
18    White,
19    Light,
20    Dark,
21    Black,
22    Text,
23    Ghost,
24    Primary,
25    Link,
26    Info,
27    Success,
28    Warning,
29    Danger,
30}
31
32#[derive(Debug, Clone, Copy, PartialEq)]
33pub enum BulmaSize {
34    Small,
35    Normal,
36    Medium,
37    Large,
38}
39
40impl Default for BulmaSize {
41    fn default() -> Self {
42        BulmaSize::Normal
43    }
44}
45
46impl BulmaColor {
47    pub fn as_str(&self) -> &'static str {
48        match self {
49            BulmaColor::White => "white",
50            BulmaColor::Light => "light",
51            BulmaColor::Dark => "dark",
52            BulmaColor::Black => "black",
53            BulmaColor::Text => "text",
54            BulmaColor::Ghost => "ghost",
55            BulmaColor::Primary => "primary",
56            BulmaColor::Link => "link",
57            BulmaColor::Info => "info",
58            BulmaColor::Success => "success",
59            BulmaColor::Warning => "warning",
60            BulmaColor::Danger => "danger",
61        }
62    }
63}
64
65impl BulmaSize {
66    pub fn as_str(&self) -> &'static str {
67        match self {
68            BulmaSize::Small => "small",
69            BulmaSize::Normal => "normal",
70            BulmaSize::Medium => "medium",
71            BulmaSize::Large => "large",
72        }
73    }
74    
75    pub fn as_class(&self) -> &'static str {
76        match self {
77            BulmaSize::Small => "is-small",
78            BulmaSize::Normal => "",
79            BulmaSize::Medium => "is-medium", 
80            BulmaSize::Large => "is-large",
81        }
82    }
83}
84
85#[derive(Props, Clone, PartialEq)]
86pub struct BulmaProviderProps {
87    #[props(default)]
88    pub theme: Option<BulmaTheme>,
89    #[props(default = true)]
90    pub load_bulma_css: bool,
91    pub children: Element,
92}
93
94#[component]
95pub fn BulmaProvider(props: BulmaProviderProps) -> Element {
96    let theme_mode = props.theme.unwrap_or_default();
97    
98    let theme_class = match theme_mode {
99        BulmaTheme::Auto => "theme-auto",
100        BulmaTheme::Light => "theme-light", 
101        BulmaTheme::Dark => "theme-dark",
102    };
103    
104    rsx! {
105        if props.load_bulma_css {
106            document::Link {
107                rel: "stylesheet",
108                href: "https://cdn.jsdelivr.net/npm/bulma@1.0.0/css/bulma.min.css"
109            }
110        }
111        div { 
112            class: "{theme_class}",
113            style: "min-height: 100vh;",
114            {props.children}
115        }
116    }
117}