Skip to content

Commit

Permalink
doc: remove flicker on page load on dark theme
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
demakoff authored and richardlau committed Mar 25, 2024
1 parent 26a06b0 commit e059878
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
6 changes: 0 additions & 6 deletions doc/api_assets/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@
);
}
}

if (mq.matches) {
document.documentElement.classList.add('dark-mode');
}
} else if (storedTheme === 'dark') {
document.documentElement.classList.add('dark-mode');
}

if (themeToggleButton) {
Expand Down
13 changes: 13 additions & 0 deletions doc/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@
<link rel="stylesheet" href="assets/hljs.css">
<link rel="canonical" href="https://nodejs.org/api/__FILENAME__.html">
<script async defer src="assets/api.js" type="text/javascript"></script>
<script>
const storedTheme = localStorage.getItem('theme');

// Follow operating system theme preference
if (storedTheme === null && window.matchMedia) {
const mq = window.matchMedia('(prefers-color-scheme: dark)');
if (mq.matches) {
document.documentElement.classList.add('dark-mode');
}
} else if (storedTheme === 'dark') {
document.documentElement.classList.add('dark-mode');
}
</script>
__JS_FLAVORED_DYNAMIC_CSS__
</head>
<body class="alt apidoc" id="api-section-__FILENAME__">
Expand Down

0 comments on commit e059878

Please sign in to comment.