Skip to content

Commit 2272e00

Browse files
authored
feat: light mode with optional dark mode toggle (#999)
1 parent a3dc7b5 commit 2272e00

File tree

9 files changed

+376
-15
lines changed

9 files changed

+376
-15
lines changed

assets/_custom.scss

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
// @import "plugins/numbered";
66
@import "plugins/scrollbars";
7+
@import "plugins/toggles";
78
@import "table-sort";
89
@import "colors";
910
@import "dashboard";
@@ -165,26 +166,51 @@ blockquote {
165166
}
166167

167168
.book-toc {
168-
.toc-label {
169+
.book-toc-toggles {
170+
top:0px;
171+
opacity: 0.4;
172+
transition: opacity 0.3s ease-in-out;
173+
}
174+
.book-toc-toggles:hover {
175+
opacity: 1;
176+
}
177+
.toc {
178+
top:78px;
179+
}
180+
.toc-label, .dark-mode-toggle-label {
169181
font-size: 10px;
170182
font-weight: 700;
183+
display: block;
171184
}
172-
.gg-menu-motion {
185+
.gg-menu-motion, .gg-dark-mode {
173186
display: inline-block;
174187
margin-left: 0;
175188
margin-right: 0;
176-
vertical-align: text-top;
189+
vertical-align: middle;
177190
font-size: 10px;
178191
transform: scale(0.7);
179192
}
180193
}
181194

182-
h1,
183-
h2,
184-
h3,
185-
h4,
186-
h5 {
187-
color: rgba(255,255,255,0.87)
195+
// Colors
196+
.color-incorrect {
197+
color: #BF616A;
198+
}
199+
200+
.color-wip {
201+
color: #D08770;
202+
}
203+
204+
.color-incomplete {
205+
color: #EBCB8B;
206+
}
207+
208+
.color-stable {
209+
color: #5E81AC;
210+
}
211+
212+
.color-permanent {
213+
color: #A3BE8C;
188214
}
189215

190216
i[class^="gg-"] {
@@ -380,4 +406,45 @@ i[class^="gg-"] {
380406
box-shadow: 4px -6px 0,8px -12px 0;
381407
border-radius: 4px;
382408
background: currentColor
383-
}
409+
}
410+
411+
.gg-dark-mode {
412+
box-sizing: border-box;
413+
position: relative;
414+
display: block;
415+
transform: scale(var(--ggs,1));
416+
border:2px solid;
417+
border-radius:100px;
418+
width:20px;
419+
height:20px
420+
}
421+
422+
.gg-dark-mode::after,
423+
.gg-dark-mode::before {
424+
content: "";
425+
box-sizing: border-box;
426+
position: absolute;
427+
display: block
428+
}
429+
430+
.gg-dark-mode::before {
431+
border:5px solid;
432+
border-top-left-radius:100px;
433+
border-bottom-left-radius:100px;
434+
border-right: 0;
435+
width:9px;
436+
height:18px;
437+
top:-1px;
438+
left:-1px
439+
}
440+
441+
.gg-dark-mode::after {
442+
border:4px solid;
443+
border-top-right-radius:100px;
444+
border-bottom-right-radius:100px;
445+
border-left: 0;
446+
width:4px;
447+
height:8px;
448+
right:4px;
449+
top:4px
450+
}

assets/_variables.scss

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
11
/* You can override SASS variables here. */
2-
3-
@import "plugins/dark";
4-

assets/book-dark.scss

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@import "defaults";
2+
@import "variables";
3+
4+
// Import the dark overrides. This file is otherwise identical to dark.scss
5+
@import "plugins/dark.scss";
6+
7+
@import "normalize";
8+
@import "utils";
9+
@import "main";
10+
@import "fonts";
11+
@import "print";
12+
13+
@import "markdown";
14+
@import "shortcodes";
15+
16+
// Custom defined styles
17+
@import "custom";

assets/dark-mode/dark-mode.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// iife to avoid polluting the global.
2+
(function () {
3+
// Run me as soon as possible after the the css links are in the dom.
4+
// This assumes this js file is added to the page after the css links.
5+
const lightMode = document.getElementById('light-mode-link')
6+
const darkMode = document.getElementById('dark-mode-link')
7+
const btn = document.querySelector('.dark-mode-toggle')
8+
const prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)').matches
9+
let theme = prefersDarkScheme ? 'dark' : 'light'
10+
11+
function enableLightMode () {
12+
lightMode.disabled = false
13+
darkMode.disabled = true
14+
btn.setAttribute('aria-pressed', theme === 'dark')
15+
}
16+
17+
function enableDarkMode () {
18+
darkMode.disabled = false
19+
lightMode.disabled = true
20+
btn.setAttribute('aria-pressed', theme === 'dark')
21+
}
22+
23+
// enable dark theme optimistically on OS with dark theme enabled to reduce flashing of white theme.
24+
if (prefersDarkScheme) {
25+
enableDarkMode()
26+
}
27+
28+
// wait for localstorage...
29+
const previousChoice = localStorage.getItem('theme')
30+
theme = previousChoice || theme
31+
32+
// Light is default, so enable dark if user previously chose it but their OS pref is light.
33+
if (theme === 'dark') {
34+
enableDarkMode()
35+
}
36+
37+
// set up the toggle once the DOM is ready.
38+
document.addEventListener("DOMContentLoaded", function(event) {
39+
40+
btn.addEventListener('click', function () {
41+
theme = (theme === 'light' ? 'dark' : 'light')
42+
if (theme === 'dark') {
43+
enableDarkMode()
44+
} else {
45+
enableLightMode()
46+
}
47+
localStorage.setItem('theme', theme)
48+
})
49+
// init the button state to match the currently selected theme.
50+
btn.setAttribute('aria-pressed', theme === 'dark')
51+
});
52+
})()

assets/plugins/_dark.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ $gray-200: #282a36;
33

44
// https://www.colorhexa.com/090909
55
$body-background: #090909;
6-
$body-font-color: rgba(255, 255, 255, 0.60);
6+
$body-font-color: rgba(255, 255, 255, 0.80);
77

88
$color-link: #0090ff;
99
$color-visited-link: #0090ff;

0 commit comments

Comments
 (0)