Skip to content

Commit

Permalink
Move to css variables + persist dark mode settings
Browse files Browse the repository at this point in the history
  • Loading branch information
marlonbaeten committed Sep 11, 2023
1 parent 29e5b63 commit fa12cb0
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 249 deletions.
6 changes: 5 additions & 1 deletion backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] }
uuid = { version = "1.2", features = ["v4", "serde"] }

[dev-dependencies]
lettre = { version = "0.10", default-features = false, features=["smtp-transport", "hostname", "builder"] }
lettre = { version = "0.10", default-features = false, features=[
"builder",
"hostname",
"smtp-transport"
] }
fake = { version = "2.5", features=["derive"]}
reqwest = { version = "0.11", features = ["json"] }
local-ip-address = "0.5"
11 changes: 6 additions & 5 deletions frontend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ timeago = "0.4"
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
web-sys = { version = "0.3", features = [
"Event",
"EventTarget",
"NodeList",
"HtmlLinkElement",
"HtmlIFrameElement",
"CssStyleDeclaration",
"DomTokenList",
"Element",
"Event",
"EventTarget",
"HtmlElement",
"HtmlIFrameElement",
"HtmlLinkElement",
"MediaQueryList",
"NodeList"
] }
yew = "0.20"
yew-hooks = "0.2"
4 changes: 2 additions & 2 deletions frontend/img/dark-mode.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 13 additions & 13 deletions frontend/img/envelope-open.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 15 additions & 15 deletions frontend/img/envelope.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion frontend/img/trash.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 76 additions & 0 deletions frontend/src/dark_mode.rs
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()
}
1 change: 1 addition & 0 deletions frontend/src/main.rs
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;
Expand Down
11 changes: 8 additions & 3 deletions frontend/src/message_header.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::types::MailMessage;
use yew::{function_component, html, html_nested, Html, Properties};
use crate::{dark_mode::toggle_body_invert, types::MailMessage};
use yew::{function_component, html, html_nested, Callback, Html, Properties};

#[derive(Properties, Eq, PartialEq)]
pub struct MessageHeaderProps {
Expand Down Expand Up @@ -68,7 +68,7 @@ pub fn view(props: &MessageHeaderProps) -> Html {
</tr>
</tbody>
</table>
<div class="attachments">
<div class="actions">
{message.attachments.iter().map(|a| {
html! {
<a
Expand All @@ -81,6 +81,11 @@ pub fn view(props: &MessageHeaderProps) -> Html {
</a>
}
}).collect::<Html>()}
<button class="invert-body" onclick={Callback::from(|_| {
toggle_body_invert();
})}>
{"Invert body"}
</button>
</div>
</>
}
Expand Down
14 changes: 8 additions & 6 deletions frontend/src/overview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use wasm_bindgen_futures::spawn_local;
use yew::prelude::*;

use crate::api::fetch_messages_metadata;
use crate::dark_mode::{init_dark_mode, toggle_dark_mode};
use crate::list::MessageList;
use crate::types::{Action, MailMessageMetadata};
use crate::view::ViewMessage;
Expand Down Expand Up @@ -58,6 +59,10 @@ impl Component for Overview {
}
});

spawn_local(async {
init_dark_mode();
});

Self {
messages: vec![],
tab: Tab::Formatted,
Expand Down Expand Up @@ -129,17 +134,14 @@ impl Component for Overview {
<header>
<h1>{"Mail"}<span>{"Crab"}</span></h1>
<div>
<button class="no-trashcan" onclick={Callback::from(|_| {
let body = web_sys::window().unwrap().document().unwrap().body().unwrap();
body.class_list().toggle("body-invert").unwrap();
})}>
{"Invert body"}
</button>
if !self.messages.is_empty() {
<button onclick={link.callback(|_| Msg::RemoveAll)}>
{"Remove all"}<span>{"("}{self.messages.len()}{")"}</span>
</button>
}
<button class="dark-mode" title="Toggle dark mode" onclick={Callback::from(|_| {
toggle_dark_mode();
})} />
</div>
</header>
if self.messages.is_empty() {
Expand Down
Loading

0 comments on commit fa12cb0

Please sign in to comment.