Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate theme flicker on reload #1450

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Loading