thaw/theme/
mod.rs

1mod color;
2mod common;
3
4use std::collections::HashMap;
5
6use crate::ConfigInjection;
7pub use color::ColorTheme;
8pub use common::CommonTheme;
9use leptos::prelude::*;
10
11#[derive(Clone)]
12pub struct Theme {
13    pub name: String,
14    pub common: CommonTheme,
15    pub color: ColorTheme,
16}
17
18impl Theme {
19    pub fn custom_light(brand_colors: &HashMap<i32, &str>) -> Self {
20        Self {
21            name: "light".into(),
22            common: CommonTheme::new(),
23            color: ColorTheme::custom_light(brand_colors),
24        }
25    }
26    pub fn custom_dark(brand_colors: &HashMap<i32, &str>) -> Self {
27        Self {
28            name: "dark".into(),
29            common: CommonTheme::new(),
30            color: ColorTheme::custom_dark(brand_colors),
31        }
32    }
33
34    pub fn light() -> Self {
35        Self {
36            name: "light".into(),
37            common: CommonTheme::new(),
38            color: ColorTheme::light(),
39        }
40    }
41    pub fn dark() -> Self {
42        Self {
43            name: "dark".into(),
44            common: CommonTheme::new(),
45            color: ColorTheme::dark(),
46        }
47    }
48
49    pub fn use_theme(default: impl Fn() -> Theme) -> ReadSignal<Theme> {
50        use_context::<ConfigInjection>()
51            .map_or_else(|| RwSignal::new(default()), |c| c.theme)
52            .split()
53            .0
54    }
55
56    pub fn use_rw_theme() -> RwSignal<Theme> {
57        expect_context::<ConfigInjection>().theme
58    }
59}
60
61impl From<String> for Theme {
62    fn from(value: String) -> Self {
63        if value == "dark" {
64            Theme::dark()
65        } else {
66            Theme::light()
67        }
68    }
69}