Skip to content

Commit

Permalink
chore: avoid theme flashes (#15927)
Browse files Browse the repository at this point in the history
* fix: avoid theme flashes

* fix: remove unwanted code and revamp `package.json`

* chore: revert pacakage.json changes
  • Loading branch information
harish-sethuraman committed Jun 3, 2022
1 parent baa0153 commit da694b9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
14 changes: 7 additions & 7 deletions docs/src/_includes/layouts/base.njk
Expand Up @@ -46,13 +46,13 @@
<link rel="preload" href="{{ '/assets/fonts/SpaceMono-Regular-subset.woff2' | url }}" as="font" type="font/woff2" crossorigin>

<script>
(function(){
var theme = window.localStorage.getItem("theme");
if (!theme && window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.documentElement.setAttribute('data-theme', 'dark');
}
else if (theme) document.documentElement.setAttribute('data-theme', theme);
})();
(function () {
var theme = window.localStorage.getItem("theme");
if (theme) document.documentElement.setAttribute('data-theme', theme)
else if (window.matchMedia('(prefers-color-scheme: dark)').matches)
document.documentElement.setAttribute('data-theme', 'dark');
else document.documentElement.setAttribute('data-theme', 'light');
})();
</script>


Expand Down
9 changes: 0 additions & 9 deletions docs/src/assets/js/themes.js
Expand Up @@ -8,11 +8,6 @@
btn.setAttribute("aria-pressed", "false");
}


let theme = window.localStorage.getItem("theme");
document.documentElement.setAttribute('data-theme', theme);
if (!theme) document.documentElement.setAttribute('data-theme', 'light');

document.addEventListener('DOMContentLoaded', function() {
var switcher = document.getElementById('js-theme-switcher');
switcher.removeAttribute('hidden');
Expand All @@ -22,10 +17,6 @@

// get any previously-chosen themes
var theme = window.localStorage.getItem("theme");
if (!theme && window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.documentElement.setAttribute('data-theme', 'dark');
}
else if (theme) document.documentElement.setAttribute('data-theme', theme);

if (theme == "light") {
enableToggle(light_theme_toggle);
Expand Down

0 comments on commit da694b9

Please sign in to comment.