From 60615177422b3064a2133bd816865028730f1596 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 17 May 2023 14:23:34 +0200 Subject: [PATCH] Fix: do not auto-detect color scheme preference by default --- src/lib.rs | 15 +++++++++------ yewprint-doc/src/lib.rs | 1 + 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a3880fb..c88ae0a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -193,16 +193,19 @@ pub struct Dark; impl Dark { pub fn with(&self, f: impl FnOnce(&Cell) -> T) -> T { thread_local! { - static DARK: Cell = { - Cell::new(web_sys::window() - .and_then(|x| x.match_media("(prefers-color-scheme: dark)").ok().flatten()) - .map(|x| x.matches()) - .unwrap_or(true)) - } + static DARK: Cell = Cell::new(false); } DARK.with(f) } + pub fn auto_detect(&self) { + let prefers_dark = web_sys::window() + .and_then(|x| x.match_media("(prefers-color-scheme: dark)").ok().flatten()) + .map(|x| x.matches()) + .unwrap_or(true); + self.set(prefers_dark); + } + pub fn get(&self) -> bool { self.with(|x| x.get()) } diff --git a/yewprint-doc/src/lib.rs b/yewprint-doc/src/lib.rs index d5d1c70..3d3c1a1 100644 --- a/yewprint-doc/src/lib.rs +++ b/yewprint-doc/src/lib.rs @@ -145,6 +145,7 @@ pub fn run_app() -> Result<(), wasm_bindgen::JsValue> { #[cfg(feature = "console_error_panic_hook")] console_error_panic_hook::set_once(); + yewprint::Dark.auto_detect(); yew::Renderer::::new().render(); Ok(())