Skip to main content

orbital_theme/
context.rs

1use leptos::prelude::*;
2
3use crate::Theme;
4
5#[derive(Clone, Copy)]
6pub struct ThemeInjection {
7    pub theme: RwSignal<Theme>,
8    pub dir: Option<RwSignal<crate::Direction>>,
9    id: StoredValue<String>,
10}
11
12impl ThemeInjection {
13    pub fn id(&self) -> String {
14        self.id.get_value()
15    }
16
17    pub fn use_theme(default: impl Fn() -> Theme) -> ReadSignal<Theme> {
18        use_context::<Self>()
19            .map_or_else(|| RwSignal::new(default()).split().0, |c| c.theme.split().0)
20    }
21
22    pub fn use_rw_theme() -> RwSignal<Theme> {
23        expect_context::<Self>().theme
24    }
25
26    pub(crate) fn new(
27        theme: RwSignal<Theme>,
28        dir: Option<RwSignal<crate::Direction>>,
29        id: String,
30    ) -> Self {
31        Self {
32            theme,
33            dir,
34            id: StoredValue::new(id),
35        }
36    }
37}
38
39pub fn scoped_selector(id: &str) -> String {
40    format!(".orbital-theme-provider[data-orbital-theme-id=\"{id}\"]")
41}
42
43pub fn scoped_css(id: &str, css_vars: &str) -> String {
44    format!("{} {{{css_vars}}}", scoped_selector(id))
45}