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 <[email protected]>
Reviewed-By: Claudio Wunder <[email protected]>
  • Loading branch information
demakoff authored and targos committed Dec 4, 2023
1 parent eea8930 commit ec74b93
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 ec74b93

Please sign in to comment.