Skip to content

Commit e4d2915

Browse files
committed
add leptos_theme
1 parent 510719a commit e4d2915

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

example/start-csr/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ gloo-timers = "0.3.0"
1919
serde = { version = "1.0" }
2020
reqwest = { version = "0.11.24", features = ["json"] }
2121
leptos-use = "0.10.3"
22+
leptos_theme = "0.1.2"
2223

2324
# utils
2425
# strum = { version = "0.25", features = ["derive", "strum_macros"] }

example/start-csr/src/layout.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,23 @@ pub fn Layout(children: Children) -> impl IntoView {
3737

3838
#[component]
3939
fn ThemeToggle() -> impl IntoView {
40-
let dark_mode = create_rw_signal(true);
40+
let current_theme = use_theme();
41+
let is_dark = Signal::derive(move || current_theme.get() == Theme::Dark);
4142

4243
view! {
43-
<Html class=move || if dark_mode.get() { "dark" } else { "" }/>
44+
// <Html class=move || if is_dark.get() { "dark" } else { "" }/>
4445
<label
4546
for="dark-mode-toggle"
4647
class="text-xs font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
4748
>
4849
Dark Mode
4950
</label>
5051
<Switch
51-
enabled=dark_mode
52-
on_click=move |_| { dark_mode.set(!dark_mode.get_untracked()) }
52+
enabled=is_dark
53+
on_click=Callback::new(move |_| {
54+
let new_theme = if is_dark.get() { Theme::Light } else { Theme::Dark };
55+
current_theme.set(new_theme);
56+
})
5357

5458
attr:id="dark-mode-toggle"
5559
/>

example/start-csr/src/lib.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use leptos_meta::*;
33
use leptos_query::{provide_query_client_with_options, DefaultQueryOptions};
44
use leptos_query_devtools::LeptosQueryDevtools;
55
use leptos_router::*;
6+
use leptos_theme::ThemeProvider;
67

78
// Modules
89
mod components;
@@ -54,16 +55,18 @@ pub fn App() -> impl IntoView {
5455
}
5556
}>
5657

57-
<Layout>
58-
<Router>
59-
<Routes>
60-
<Route path="/" view=Home/>
61-
<Route path="/single" view=pages::single::QueryVsResource/>
62-
<Route path="/todos" view=pages::interactive::Interactive/>
63-
<Route path="/*" view=NotFound/>
64-
</Routes>
65-
</Router>
66-
</Layout>
58+
<ThemeProvider>
59+
<Layout>
60+
<Router>
61+
<Routes>
62+
<Route path="/" view=Home/>
63+
<Route path="/single" view=pages::single::QueryVsResource/>
64+
<Route path="/todos" view=pages::interactive::Interactive/>
65+
<Route path="/*" view=NotFound/>
66+
</Routes>
67+
</Router>
68+
</Layout>
69+
</ThemeProvider>
6770

6871
</ErrorBoundary>
6972
}

0 commit comments

Comments
 (0)