diff --git a/demo/src/app.rs b/demo/src/app.rs index cb1a309..d2531d5 100644 --- a/demo/src/app.rs +++ b/demo/src/app.rs @@ -8,7 +8,7 @@ pub fn App() -> impl IntoView { let theme = create_rw_signal(Theme::light()); provide_context(theme); view! { - + @@ -54,16 +54,13 @@ pub fn App() -> impl IntoView { } #[component] -fn Provider(theme: ReadSignal, children: Children) -> impl IntoView { +fn Provider(theme: RwSignal, children: Children) -> impl IntoView { view! { + {children()} } } - -pub fn use_rw_theme() -> RwSignal { - expect_context::>() -} diff --git a/demo/src/components/site_header.rs b/demo/src/components/site_header.rs index 9fd4d17..10eb76a 100644 --- a/demo/src/components/site_header.rs +++ b/demo/src/components/site_header.rs @@ -1,4 +1,3 @@ -use crate::app::use_rw_theme; use leptos::*; use leptos_router::use_navigate; use melt_ui::*; diff --git a/src/global_style/mod.rs b/src/global_style/mod.rs new file mode 100644 index 0000000..80adab0 --- /dev/null +++ b/src/global_style/mod.rs @@ -0,0 +1,23 @@ +use crate::{use_theme, Theme}; +use leptos::*; + +#[component] +pub fn GlobalStyle() -> impl IntoView { + let theme = use_theme(Theme::light); + create_effect(move |_| { + theme.with(|theme| { + if let Some(body) = document().body() { + _ = body + .style() + .set_property("background-color", &theme.common.background_color); + _ = body.style().set_property("color", &theme.common.font_color); + _ = body + .style() + .set_property("font-family", &theme.common.font_family); + _ = body + .style() + .set_property("font-size", &theme.common.font_size); + } + }); + }); +} diff --git a/src/lib.rs b/src/lib.rs index 6ad2912..f58cd5d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ mod code; mod color_picker; mod components; mod divider; +mod global_style; mod grid; mod icon; mod image; @@ -45,6 +46,7 @@ pub use checkbox::*; pub use code::*; pub use color_picker::*; pub use divider::*; +pub use global_style::*; pub use grid::*; pub use icon::*; pub use image::*; diff --git a/src/theme/common.rs b/src/theme/common.rs index e989f5d..a2a21c3 100644 --- a/src/theme/common.rs +++ b/src/theme/common.rs @@ -3,6 +3,8 @@ use super::ThemeMethod; #[derive(Clone)] pub struct CommonTheme { pub font_family: String, + pub font_color: String, + pub background_color: String, pub color_primary: String, pub color_primary_hover: String, @@ -38,7 +40,9 @@ pub struct CommonTheme { impl CommonTheme { fn common() -> Self { Self { - font_family: "-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,'Helvetica Neue',Arial,'Noto Sans',sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol','Noto Color Emoji'".into(), + font_family: r#"Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Helvetica Neue, Arial, Noto Sans, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", Segoe UI Symbol, "Noto Color Emoji""#.into(), + font_color: "".into(), + background_color: "".into(), color_primary: "".into(), color_primary_hover: "".into(), color_primary_active: "".into(), @@ -72,6 +76,8 @@ impl CommonTheme { impl ThemeMethod for CommonTheme { fn light() -> Self { Self { + font_color: "#11181c".into(), + background_color: "#fff".into(), color_primary: "#f5222d".into(), color_primary_hover: "#ff4d4f".into(), color_primary_active: "#cf1322".into(), @@ -89,6 +95,8 @@ impl ThemeMethod for CommonTheme { } fn dark() -> Self { Self { + font_color: "#ecedee".into(), + background_color: "#1a1d1e".into(), color_primary: "#d32029".into(), color_primary_hover: "#e04648".into(), color_primary_active: "#ad111e".into(), diff --git a/src/theme/mod.rs b/src/theme/mod.rs index 5aa9f61..24dedbe 100644 --- a/src/theme/mod.rs +++ b/src/theme/mod.rs @@ -53,49 +53,36 @@ impl Theme { impl ThemeMethod for Theme { fn light() -> Self { - Self { - name: "Light".into(), - common: CommonTheme::light(), - button: ButtonTheme::light(), - input: InputTheme::light(), - menu: MenuTheme::light(), - table: TableTheme::light(), - alert: AlertTheme::light(), - skeletion: SkeletionTheme::light(), - tag: TagTheme::light(), - } + Theme::light() } fn dark() -> Self { - Self { - name: "Dark".into(), - common: CommonTheme::dark(), - button: ButtonTheme::dark(), - input: InputTheme::dark(), - menu: MenuTheme::dark(), - table: TableTheme::dark(), - alert: AlertTheme::dark(), - skeletion: SkeletionTheme::dark(), - tag: TagTheme::dark(), - } + Theme::dark() } } #[component] pub fn ThemeProvider( - #[prop(optional, into)] theme: Option>, + #[prop(optional, into)] theme: Option>, children: Children, ) -> impl IntoView { let theme = if let Some(theme) = theme { theme } else { - create_signal(Theme::light()).0 + create_rw_signal(Theme::light()) }; provide_context(theme); children() } pub fn use_theme(default: impl Fn() -> Theme) -> ReadSignal { - use_context::>().unwrap_or_else(|| create_signal(default()).0) + use_context::>() + .unwrap_or_else(|| create_rw_signal(default())) + .split() + .0 +} + +pub fn use_rw_theme() -> RwSignal { + expect_context::>() } #[cfg(test)]