Skip to content

Commit ec50168

Browse files
authored
Fix white flashes on page load in dark mode (#2574)
We set the class indicating that dark mode is enabled before the page is rendered (the script is called inside `<head>`). The class is now set on the `<html>` element instead of `<body>` because `document.body` is not ready yet when the script is executed.
1 parent 9075b91 commit ec50168

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

dhall-docs/src/Dhall/data/assets/index.css

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ span.of-type-token {
194194
color: #EBEBEB;
195195
}
196196

197-
body.dark-mode {
197+
.dark-mode body {
198198
background-color: #484848;
199199
}
200200

@@ -222,7 +222,6 @@ body.dark-mode {
222222
color: #D4D4D4;
223223
}
224224

225-
226225
.dark-mode a.copy-to-clipboard {
227226
color: #bdbdbd !important;
228227
}

dhall-docs/src/Dhall/data/assets/index.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ const DARK_MODE_OPT = 'dark-mode'
22
const DARK_MODE_ACTIVE = 'dark-mode-active'
33
const DARK_MODE_INACTIVE = 'dark-mode-inactive'
44

5+
if (localStorage.getItem(DARK_MODE_OPT) == DARK_MODE_ACTIVE) {
6+
document.documentElement.classList.add('dark-mode')
7+
}
8+
59
function onReady() {
6-
if (localStorage.getItem(DARK_MODE_OPT) == DARK_MODE_ACTIVE) {
7-
document.body.classList.add('dark-mode')
8-
} else {
9-
document.body.classList.remove('dark-mode')
10-
}
1110
document.getElementById('switch-light-dark-mode')
1211
.addEventListener('click', () => {
13-
document.body.classList.toggle('dark-mode')
14-
if (document.body.classList.contains('dark-mode')) {
12+
document.documentElement.classList.toggle('dark-mode')
13+
if (document.documentElement.classList.contains('dark-mode')) {
1514
localStorage.setItem(DARK_MODE_OPT, DARK_MODE_ACTIVE)
1615
} else {
1716
localStorage.setItem(DARK_MODE_OPT, DARK_MODE_INACTIVE)

0 commit comments

Comments
 (0)