Skip to content

Commit

Permalink
refactor: consolidate theme loading and application logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lemorage committed Jan 15, 2025
1 parent 2ca6a94 commit 8fe8c9a
Showing 1 changed file with 3 additions and 13 deletions.
16 changes: 3 additions & 13 deletions wasm-frontend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use web_sys::{window, Document, Element};
pub fn start() {
web_sys::console::log_1(&"Wasm and Sycamore app starting...".into());

apply_theme_from_local_storage();

let document = window().unwrap().document().unwrap();
if let Some(root_element) = document.get_element_by_id("sycamore-root") {
sycamore::render_to(
Expand All @@ -29,7 +27,7 @@ pub fn start() {
// Define the ThemeSwitcher component
#[component]
fn ThemeSwitcher<G: Html>(cx: Scope) -> View<G> {
let is_dark = create_signal(cx, load_theme_from_local_storage());
let is_dark = create_signal(cx, load_and_apply_theme());

// Toggle theme logic that modifies body class
let toggle_theme = move |_| {
Expand Down Expand Up @@ -74,21 +72,13 @@ fn ThemeSwitcher<G: Html>(cx: Scope) -> View<G> {
}
}

// Helper function to apply the theme from local storage
fn apply_theme_from_local_storage() {
// Helper function to load the theme from local storage and apply it
pub fn load_and_apply_theme() -> bool {
if let Some(storage) = window().unwrap().local_storage().unwrap() {
if let Ok(Some(theme)) = storage.get_item("theme") {
let document = window().unwrap().document().unwrap();
let body = document.body().unwrap();
body.set_class_name(&theme);
}
}
}

// Helper function to load the theme from local storage
fn load_theme_from_local_storage() -> bool {
if let Some(storage) = window().unwrap().local_storage().unwrap() {
if let Ok(Some(theme)) = storage.get_item("theme") {
return theme == "dark";
}
}
Expand Down

0 comments on commit 8fe8c9a

Please sign in to comment.