-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move to css variables + persist dark mode settings
- Loading branch information
1 parent
29e5b63
commit fa12cb0
Showing
11 changed files
with
252 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const DARK: &str = "dark"; | ||
const LIGHT: &str = "light"; | ||
const DARK_MODE_KEY: &str = "dark-mode"; | ||
|
||
const BODY_INVERT_KEY: &str = "body-invert"; | ||
const INVERT: &str = "invert"; | ||
const NO_INVERT: &str = "no-invert"; | ||
|
||
pub fn init_dark_mode() { | ||
// fetch dark mode setting with media query, override with local storage | ||
let local_storage = web_sys::window().unwrap().local_storage().unwrap().unwrap(); | ||
let dark_mode = match local_storage.get_item(DARK_MODE_KEY).unwrap().as_deref() { | ||
Some(DARK) => true, | ||
Some(LIGHT) => false, | ||
_ => { | ||
web_sys::window() | ||
.unwrap() | ||
.match_media("(prefers-color-scheme: dark)") | ||
.unwrap() | ||
.map(|l| l.matches()) | ||
== Some(true) | ||
} | ||
}; | ||
|
||
let body_invert = match local_storage.get_item(BODY_INVERT_KEY).unwrap().as_deref() { | ||
Some(INVERT) => true, | ||
_ => false, | ||
}; | ||
|
||
let body = web_sys::window() | ||
.unwrap() | ||
.document() | ||
.unwrap() | ||
.body() | ||
.unwrap(); | ||
body.class_list() | ||
.add_1(if dark_mode { DARK } else { LIGHT }) | ||
.unwrap(); | ||
body.class_list() | ||
.add_1(if body_invert { INVERT } else { NO_INVERT }) | ||
.unwrap(); | ||
} | ||
|
||
pub fn toggle_dark_mode() { | ||
let body = web_sys::window() | ||
.unwrap() | ||
.document() | ||
.unwrap() | ||
.body() | ||
.unwrap(); | ||
let dark_mode = body.class_list().contains(DARK); | ||
let new_mode = if dark_mode { LIGHT } else { DARK }; | ||
|
||
body.class_list().remove_2(DARK, LIGHT).unwrap(); | ||
body.class_list().add_1(new_mode).unwrap(); | ||
|
||
let local_storage = web_sys::window().unwrap().local_storage().unwrap().unwrap(); | ||
local_storage.set_item(DARK_MODE_KEY, new_mode).unwrap() | ||
} | ||
|
||
pub fn toggle_body_invert() { | ||
let body = web_sys::window() | ||
.unwrap() | ||
.document() | ||
.unwrap() | ||
.body() | ||
.unwrap(); | ||
let body_invert = body.class_list().contains(INVERT); | ||
let new_mode = if body_invert { NO_INVERT } else { INVERT }; | ||
|
||
body.class_list().remove_2(NO_INVERT, INVERT).unwrap(); | ||
body.class_list().add_1(new_mode).unwrap(); | ||
|
||
let local_storage = web_sys::window().unwrap().local_storage().unwrap().unwrap(); | ||
local_storage.set_item(BODY_INVERT_KEY, new_mode).unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
use overview::Overview; | ||
|
||
mod api; | ||
mod dark_mode; | ||
mod formatted; | ||
mod list; | ||
mod message_header; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.