Skip to content

Commit 5ce90b8

Browse files
committed
added light/dark mode startup script
1 parent bbe96a6 commit 5ce90b8

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

content/assets/startup.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
let storageResult = localStorage.getItem('DarkModePreferred');
2+
var preferDark = JSON.parse(storageResult ? storageResult : 'null');
3+
if (preferDark === null) {
4+
preferDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
5+
}
6+
if (preferDark === null) {
7+
preferDark = false;
8+
}
9+
document.documentElement.dataset.appliedMode = preferDark ? 'dark' : 'light';

content/assets/styles.css

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
1+
:root[data-applied-mode="light"] {
2+
color-scheme: light;
3+
--base-color: var(--ctp-latte-base);
4+
--text-color: var(--ctp-latte-base);
5+
6+
--mantle-color: var(--ctp-latte-mantle);
7+
--crust-color: var(--ctp-latte-crust);
8+
9+
}
10+
11+
:root[data-applied-mode="dark"] {
12+
color-scheme: dark;
13+
--base-color: var(--ctp-mocha-base);
14+
--text-color: var(--ctp-mocha-text);
15+
16+
--mantle-color: var(--ctp-mocha-mantle);
17+
--crust-color: var(--ctp-mocha-crust);
18+
}
19+
20+
html, body {
21+
font-family: "Courier New", monospace;
22+
margin: 0px;
23+
overflow-x: clip;
24+
background-color: var(--base-color);
25+
}
26+
127
.blog {
2-
border-color: black;
28+
border-color: var(--mantle-color);
29+
background-color: var(--crust-color);
30+
31+
color: inherit;
332
border-radius: 2px;
433
border-width: 2px;
534
border-style: solid;

src/blog/render.gleam

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import gleam/dict
33
import gleam/list
44
import gleam/result
55
import lustre/attribute.{attribute}
6-
import lustre/element/html.{a, body, div, head, html, link, p, text}
6+
import lustre/element/html.{a, body, div, head, html, link, p, script, text}
77
import lustre/ssg/djot
88
import tom
99

@@ -51,6 +51,7 @@ pub fn render_links(base: String, sources: List(posts.PostSource)) {
5151
attribute("rel", "stylesheet"),
5252
attribute("href", "assets/styles.css"),
5353
]),
54+
script([attribute.src("assets/startup.js")], ""),
5455
]),
5556
body([], [div([], list.map(matters, render_matter(base, _)))]),
5657
])

0 commit comments

Comments
 (0)