From 6cd87cf790412bfa53d32327edf080bbe0c4d034 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 7 Nov 2024 15:38:10 +0100 Subject: [PATCH] Better dark mode storage using cookie --- Cargo.lock | 14 +++--------- Cargo.toml | 3 ++- src/frontend/app.rs | 13 +++--------- src/frontend/components/nav.rs | 14 ++++-------- src/frontend/dark_mode.rs | 39 ++++++++++++++++++++++++++++++++++ src/frontend/mod.rs | 1 + 6 files changed, 52 insertions(+), 32 deletions(-) create mode 100644 src/frontend/dark_mode.rs diff --git a/Cargo.lock b/Cargo.lock index 41fe652..3c7c254 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1780,6 +1780,7 @@ dependencies = [ "axum-macros", "bcrypt", "chrono", + "codee", "config", "console_error_panic_hook", "diesel", @@ -1796,7 +1797,6 @@ dependencies = [ "leptos", "leptos-use", "leptos_axum", - "leptos_darkmode", "leptos_meta", "leptos_router", "log", @@ -2022,9 +2022,11 @@ dependencies = [ "default-struct-builder", "futures-util", "gloo-timers", + "http", "js-sys", "lazy_static", "leptos", + "leptos_axum", "paste", "thiserror", "unic-langid", @@ -2070,16 +2072,6 @@ 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 eeb1811..9e94a2b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ ssr = [ "katex/duktape", "leptos/ssr", "leptos-use/ssr", + "leptos-use/axum" ] hydrate = [ "leptos/hydrate", @@ -108,7 +109,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" +codee = "0.2.0" [dev-dependencies] pretty_assertions = "1.4.1" diff --git a/src/frontend/app.rs b/src/frontend/app.rs index 7fe3e6f..e057eb4 100644 --- a/src/frontend/app.rs +++ b/src/frontend/app.rs @@ -3,6 +3,7 @@ use crate::{ frontend::{ api::ApiClient, components::nav::Nav, + dark_mode::DarkMode, pages::{ article::{ actions::ArticleActions, @@ -37,7 +38,6 @@ use leptos::{ SignalGetUntracked, SignalUpdate, }; -use leptos_darkmode::Darkmode; use leptos_meta::{provide_meta_context, *}; use leptos_router::{Route, Router, Routes}; use reqwest::Client; @@ -91,18 +91,11 @@ pub fn App() -> impl IntoView { GlobalState::update_my_profile(); provide_context(create_rw_signal(global_state)); - let darkmode = Darkmode::init(); + 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 3a9d0ef..5297e0c 100644 --- a/src/frontend/components/nav.rs +++ b/src/frontend/components/nav.rs @@ -1,6 +1,5 @@ -use crate::frontend::app::GlobalState; +use crate::frontend::{app::GlobalState, dark_mode::DarkMode}; use leptos::{component, use_context, view, IntoView, RwSignal, SignalWith, *}; -use leptos_darkmode::Darkmode; use leptos_router::*; #[component] @@ -22,6 +21,7 @@ pub fn Nav() -> impl IntoView { ); let (search_query, set_search_query) = create_signal(String::new()); + let mut dark_mode = expect_context::(); view! {