Skip to content

Commit 91cce27

Browse files
demakoffaduh95
authored andcommitted
doc: remove flicker on page load on dark theme
Theme applying logic get loaded and executed in async mode, so often there is a delay in applying the proper theme to a page which leads to flicker on dark theme. Resolved by moving critical logic to the page head PR-URL: #50942 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Claudio Wunder <cwunder@gnome.org>
1 parent 522fbb0 commit 91cce27

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

doc/api_assets/api.js

-6
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@
2424
);
2525
}
2626
}
27-
28-
if (mq.matches) {
29-
document.documentElement.classList.add('dark-mode');
30-
}
31-
} else if (storedTheme === 'dark') {
32-
document.documentElement.classList.add('dark-mode');
3327
}
3428

3529
if (themeToggleButton) {

doc/template.html

+13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@
1010
<link rel="stylesheet" href="assets/hljs.css">
1111
<link rel="canonical" href="https://nodejs.org/api/__FILENAME__.html">
1212
<script async defer src="assets/api.js" type="text/javascript"></script>
13+
<script>
14+
const storedTheme = localStorage.getItem('theme');
15+
16+
// Follow operating system theme preference
17+
if (storedTheme === null && window.matchMedia) {
18+
const mq = window.matchMedia('(prefers-color-scheme: dark)');
19+
if (mq.matches) {
20+
document.documentElement.classList.add('dark-mode');
21+
}
22+
} else if (storedTheme === 'dark') {
23+
document.documentElement.classList.add('dark-mode');
24+
}
25+
</script>
1326
__JS_FLAVORED_DYNAMIC_CSS__
1427
</head>
1528
<body class="alt apidoc" id="api-section-__FILENAME__">

0 commit comments

Comments
 (0)