Skip to content

Commit

Permalink
Eliminate theme flicker on reload
Browse files Browse the repository at this point in the history
  • Loading branch information
senekor committed Dec 26, 2024
1 parent d23b993 commit 5647a06
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
11 changes: 11 additions & 0 deletions static/scripts/theme-switch-post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";

// The regular theme-switch.js script runs in the header and blocks the initial
// page render to prevent flickering. The following code cannot run as part of
// that, because the page must have been rendered first.

// close the theme dropdown if clicking somewhere else
document.querySelector('.theme-icon').onblur = handleBlur;

// show the theme selector only if JavaScript is enabled/available
document.querySelector('.theme-icon').style.display = 'block';
6 changes: 0 additions & 6 deletions static/scripts/theme-switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ function setThemeToSystemPref() {
}
}

// close the theme dropdown if clicking somewhere else
document.querySelector('.theme-icon').onblur = handleBlur;

// Check for saved user preference on load, else check and save user agent prefs
let savedTheme = null;
if (storageAvailable("localStorage")) {
Expand All @@ -73,6 +70,3 @@ if (savedTheme) {
} else {
setThemeToSystemPref();
}

// show the theme selector only if JavaScript is enabled/available
document.querySelector('.theme-icon').style.display = 'block';
3 changes: 3 additions & 0 deletions templates/headers.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@

<!-- atom -->
<link type="application/atom+xml" rel="alternate" href="https://blog.rust-lang.org/{{blog.prefix}}feed.xml" title="{{blog.title}}" />

<!-- theme switcher -->
<script src="{{root}}scripts/theme-switch.js"></script>
2 changes: 1 addition & 1 deletion templates/nav.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
<li class="theme-item" onclick="changeThemeTo('system');">System</li>
</ul>
</button>
<script src="{{root}}scripts/theme-switch.js"></script>
<script src="{{root}}scripts/theme-switch-post.js"></script>
</ul>
</nav>

0 comments on commit 5647a06

Please sign in to comment.