diff --git a/demo/Cargo.toml b/demo/Cargo.toml index a34fb70..eba34a1 100644 --- a/demo/Cargo.toml +++ b/demo/Cargo.toml @@ -15,7 +15,7 @@ icondata = { version = "0.1.0", features = [ ] } leptos_router = { version = "0.5.1", features = ["csr"] } leptos_devtools = "0.0.1" -prisms = { git = "https://github.com/luoxiaozero/prisms" } +prisms = { git = "https://github.com/luoxiaozero/prisms", rev = "16d4d34b93fc20578ebf03137d54ecc7eafa4d4b" } [features] tracing = ["leptos/tracing"] diff --git a/demo/src/app.rs b/demo/src/app.rs index 5d95042..cb1a309 100644 --- a/demo/src/app.rs +++ b/demo/src/app.rs @@ -5,8 +5,10 @@ use melt_ui::*; #[component] pub fn App() -> impl IntoView { + let theme = create_rw_signal(Theme::light()); + provide_context(theme); view! { - + @@ -47,6 +49,21 @@ pub fn App() -> impl IntoView { - + } } + +#[component] +fn Provider(theme: ReadSignal, 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 5c79421..9fd4d17 100644 --- a/demo/src/components/site_header.rs +++ b/demo/src/components/site_header.rs @@ -1,9 +1,19 @@ +use crate::app::use_rw_theme; use leptos::*; use leptos_router::use_navigate; use melt_ui::*; #[component] pub fn SiteHeader() -> impl IntoView { + let theme = use_rw_theme(); + let theme_name = create_memo(move |_| theme.with(|theme| theme.name.clone())); + let on_theme = move |_| { + if theme_name.get_untracked() != "Light".to_string() { + theme.set(Theme::light()) + } else { + theme.set(Theme::dark()) + } + }; view! { impl IntoView { "Melt UI" - + + - "Github" - } } diff --git a/src/lib.rs b/src/lib.rs index 70458be..6ad2912 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,7 +64,7 @@ pub use switch::*; pub use table::*; pub use tabs::*; pub use tag::*; -pub use theme::Theme; +pub use theme::*; pub use upload::*; pub use utils::{mount_style::mount_style, signal::SignalWatch}; pub use wave::*; diff --git a/src/theme/mod.rs b/src/theme/mod.rs index 5d3ad3d..5aa9f61 100644 --- a/src/theme/mod.rs +++ b/src/theme/mod.rs @@ -11,6 +11,7 @@ pub trait ThemeMethod { #[derive(Clone)] pub struct Theme { + pub name: String, pub common: CommonTheme, pub button: ButtonTheme, pub input: InputTheme, @@ -24,6 +25,7 @@ pub struct Theme { impl Theme { pub fn light() -> Self { Self { + name: "Light".into(), common: CommonTheme::light(), button: ButtonTheme::light(), input: InputTheme::light(), @@ -36,6 +38,7 @@ impl Theme { } pub fn dark() -> Self { Self { + name: "Dark".into(), common: CommonTheme::dark(), button: ButtonTheme::dark(), input: InputTheme::dark(), @@ -51,6 +54,7 @@ impl Theme { impl ThemeMethod for Theme { fn light() -> Self { Self { + name: "Light".into(), common: CommonTheme::light(), button: ButtonTheme::light(), input: InputTheme::light(), @@ -63,6 +67,7 @@ impl ThemeMethod for Theme { } fn dark() -> Self { Self { + name: "Dark".into(), common: CommonTheme::dark(), button: ButtonTheme::dark(), input: InputTheme::dark(), @@ -75,6 +80,20 @@ impl ThemeMethod for Theme { } } +#[component] +pub fn ThemeProvider( + #[prop(optional, into)] theme: Option>, + children: Children, +) -> impl IntoView { + let theme = if let Some(theme) = theme { + theme + } else { + create_signal(Theme::light()).0 + }; + provide_context(theme); + children() +} + pub fn use_theme(default: impl Fn() -> Theme) -> ReadSignal { use_context::>().unwrap_or_else(|| create_signal(default()).0) }