Files
compiler-explorer/views/_layout.pug
T
Aryan Naraghi 62e6a813c8 Fixes the flash of white issue when theme is set to system and the user has dark mode enabled (#8720)
Currently, when the user's theme is set to "Same as system", the string
"system" is written as the settings.theme key in Local Storage. When the
page first loads, a JavaScript function in `<head>` injects the value of
settings.theme ("system") into the `data-theme` attribute of the
`<html>` tag with the aim of immediately selecting the correct CSS theme
file. However, there is no matching CSS file for "data-theme=system" in
static/styles/themes.

Eventually, the `setTheme()` function in static/themes.ts figures out
the correct theme CSS file to use. In the interim, for users who have
dark mode enabled on their system, they see a flash of white until
`setTheme()` finishes because DOM rendering has already begun before
`setTheme()` starts.

The fix is to set `data-theme` at the start to:

* "dark" when the system is in dark mode and (settings.theme == "system"
or unset); and
* "default" when the system is **not** in dark mode and (settings.theme
== "system" or unset).

This PR also fixes a second issue: the "real-dark" and "onedark"
settings.theme values are currently not handled properly by the `<head>`
script. For "real-dark", `data-theme` needs to be "dark" and for
"onedark", `data-theme` needs to be "one-dark". Again, this is handled
correctly by static/themes.ts, so this bug merely results in a temporary
flash were the wrong CSS is applied until static/themes.ts runs.

See demo:
https://github.com/user-attachments/assets/e6d232dd-a805-46f2-9bd6-e730391862c5
2026-05-26 22:55:15 +02:00

74 lines
2.4 KiB
Plaintext

doctype html
html(lang="en")
head
base(href=httpRoot)
block meta
include meta.pug
script
| (function () {
| var isIE11 = !!window.MSInputMethodContext && !!document.documentMode;
| var storedState = '#{storedStateId}';
| if (isIE11) {
| if (storedState) {
| location.href = '#{httpRoot}noscript/z/' + storedState;
| } else {
| location.href = '#{httpRoot}noscript';
| }
| }
| })();
block styles
link(href=require("vendor.css") rel="stylesheet")
link(href=require("main.css") rel="stylesheet")
//- partially apply theme as soon as possible to minimize the page load flashbang duration when using dark theme
script
| (function () {
| try {
| var settings = window.localStorage.getItem("!{localStoragePrefix}settings") || '{}';
| var theme = JSON.parse(settings).theme || 'system';
| if (theme === 'system') {
| if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
| theme = 'dark';
| } else {
| theme = 'default';
| }
| }
| if (theme === 'darkplus' || theme === 'real-dark') {
| theme = 'dark';
| }
| if (theme === 'onedark') {
| theme = 'one-dark';
| }
| document.documentElement.setAttribute('data-theme', theme);
|
| } catch (e) {
| document.documentElement.setAttribute('data-theme', 'default');
| }
| })();
body(class=extraBodyClass)
block content
#root
noscript
p Without Javascript the regular website is not functional. To go to the noscript version Compiler Explorer&nbsp;
if storedStateId
a(href=`${httpRoot}noscript/z/${storedStateId}`) click here
else
a(href=`${httpRoot}noscript`) click here
block printstuff
#printview Hello
block footer
include templates/templates
block scripts
script(src=`${httpRoot}client-options.js?hash=${optionsHash}`)
div#config(extraOptions=encodeURIComponent(compilerExplorerOptions), httpRoot=httpRoot, staticRoot=staticRoot)
script(src=require("runtime.js"))
script(src=require("vendor.js"))
script(src=require("main.js"))