Fix: do not auto-detect color scheme preference by default

This commit is contained in:
Cecile Tonglet 2023-05-17 14:23:34 +02:00
parent 04e071e419
commit 6061517742
2 changed files with 10 additions and 6 deletions

View file

@ -193,16 +193,19 @@ pub struct Dark;
impl Dark {
pub fn with<T>(&self, f: impl FnOnce(&Cell<bool>) -> T) -> T {
thread_local! {
static DARK: Cell<bool> = {
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<bool> = 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())
}

View file

@ -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::<app::AppRoot>::new().render();
Ok(())