Skip to content

Commit d79652e

Browse files
authored
Merge pull request #1450 from rust-lang/senekor/zkymkzrurmtr
Eliminate theme flicker on reload
2 parents c06e347 + 5647a06 commit d79652e

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

static/scripts/theme-switch-post.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
3+
// The regular theme-switch.js script runs in the header and blocks the initial
4+
// page render to prevent flickering. The following code cannot run as part of
5+
// that, because the page must have been rendered first.
6+
7+
// close the theme dropdown if clicking somewhere else
8+
document.querySelector('.theme-icon').onblur = handleBlur;
9+
10+
// show the theme selector only if JavaScript is enabled/available
11+
document.querySelector('.theme-icon').style.display = 'block';

static/scripts/theme-switch.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ function setThemeToSystemPref() {
6060
}
6161
}
6262

63-
// close the theme dropdown if clicking somewhere else
64-
document.querySelector('.theme-icon').onblur = handleBlur;
65-
6663
// Check for saved user preference on load, else check and save user agent prefs
6764
let savedTheme = null;
6865
if (storageAvailable("localStorage")) {
@@ -73,6 +70,3 @@ if (savedTheme) {
7370
} else {
7471
setThemeToSystemPref();
7572
}
76-
77-
// show the theme selector only if JavaScript is enabled/available
78-
document.querySelector('.theme-icon').style.display = 'block';

templates/headers.hbs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@
3636

3737
<!-- atom -->
3838
<link type="application/atom+xml" rel="alternate" href="https://blog.rust-lang.org/{{blog.prefix}}feed.xml" title="{{blog.title}}" />
39+
40+
<!-- theme switcher -->
41+
<script src="{{root}}scripts/theme-switch.js"></script>

templates/nav.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
<li class="theme-item" onclick="changeThemeTo('system');">System</li>
2121
</ul>
2222
</button>
23-
<script src="{{root}}scripts/theme-switch.js"></script>
23+
<script src="{{root}}scripts/theme-switch-post.js"></script>
2424
</ul>
2525
</nav>

0 commit comments

Comments
 (0)