From 2fd399934100a35f55cc15665a0a64b77be07bcf Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 7 Nov 2024 12:05:54 +0100 Subject: [PATCH] Persistent storage for dark mode --- Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + src/frontend/app.rs | 13 ++++++++++++- src/frontend/components/nav.rs | 6 ++++-- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5f0b68e..41fe652 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1796,6 +1796,7 @@ dependencies = [ "leptos", "leptos-use", "leptos_axum", + "leptos_darkmode", "leptos_meta", "leptos_router", "log", @@ -2069,6 +2070,16 @@ dependencies = [ "typed-builder", ] +[[package]] +name = "leptos_darkmode" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35ec7c7c56b312da0a739680cc2cb70d641eb82adbc1f83a565c2e9562a96253" +dependencies = [ + "leptos", + "web-sys", +] + [[package]] name = "leptos_dom" version = "0.6.15" diff --git a/Cargo.toml b/Cargo.toml index c6c468e..eeb1811 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -108,6 +108,7 @@ markdown-it-footnote = "0.2.0" markdown-it-sub = "1.0.0" markdown-it-sup = "1.0.0" leptos-use = "0.13.6" +leptos_darkmode = "0.2.0" [dev-dependencies] pretty_assertions = "1.4.1" diff --git a/src/frontend/app.rs b/src/frontend/app.rs index 71ee801..7fe3e6f 100644 --- a/src/frontend/app.rs +++ b/src/frontend/app.rs @@ -37,6 +37,7 @@ use leptos::{ SignalGetUntracked, SignalUpdate, }; +use leptos_darkmode::Darkmode; use leptos_meta::{provide_meta_context, *}; use leptos_router::{Route, Router, Routes}; use reqwest::Client; @@ -90,8 +91,18 @@ pub fn App() -> impl IntoView { GlobalState::update_my_profile(); provide_context(create_rw_signal(global_state)); + let darkmode = Darkmode::init(); + provide_context(darkmode.clone()); + let theme = move || { + if darkmode.is_light() { + "emerald" + } else { + "dim" + } + }; + view! { - + <> diff --git a/src/frontend/components/nav.rs b/src/frontend/components/nav.rs index 58b78f1..47642aa 100644 --- a/src/frontend/components/nav.rs +++ b/src/frontend/components/nav.rs @@ -1,5 +1,6 @@ use crate::frontend::app::GlobalState; use leptos::{component, use_context, view, IntoView, RwSignal, SignalWith, *}; +use leptos_darkmode::Darkmode; use leptos_router::*; #[component] @@ -124,8 +125,9 @@ pub fn Nav() -> impl IntoView { Light ().is_dark() } + on:click=move |_| { expect_context::().toggle() } /> Dark