-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from m417z/flashing-fix
Add a stylesheet loader script to fix flashing (issue #77)
- Loading branch information
Showing
7 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<link rel="stylesheet" href="light.css" media="(prefers-color-scheme: light)"> | ||
<link rel="stylesheet" href="dark.css" media="(prefers-color-scheme: dark)"> | ||
</head> | ||
<body> | ||
<h1>Demo with flashing</h1> | ||
<div> | ||
Switch theme: | ||
<dark-mode-toggle permanent></dark-mode-toggle> | ||
</div> | ||
<div> | ||
Refresh to test: <a href="with-flashing.html">Refresh</a> | ||
</div> | ||
<div> | ||
Go to the version <a href="without-flashing.html">without flashing</a> | ||
</div> | ||
|
||
<script type="module" src="../src/dark-mode-toggle.mjs"></script> | ||
<script> | ||
// Load uncached script to simulate a slow loading. | ||
const rand = Math.random(); | ||
document.write('<script src="1-mb.js?r=' + rand + '"><\/script>'); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<noscript id="dark-mode-toggle-stylesheets"> | ||
<link rel="stylesheet" href="light.css" media="(prefers-color-scheme: light)"> | ||
<link rel="stylesheet" href="dark.css" media="(prefers-color-scheme: dark)"> | ||
</noscript> | ||
<script src="../src/dark-mode-toggle-stylesheets-loader.js"></script> | ||
</head> | ||
<body> | ||
<h1>Demo without flashing</h1> | ||
<div> | ||
Switch theme: | ||
<dark-mode-toggle permanent></dark-mode-toggle> | ||
</div> | ||
<div> | ||
Refresh to test: <a href="without-flashing.html">Refresh</a> | ||
</div> | ||
<div> | ||
Go to the version <a href="with-flashing.html">with flashing</a> | ||
</div> | ||
|
||
<script type="module" src="../src/dark-mode-toggle.mjs"></script> | ||
<script> | ||
// Load uncached script to simulate a slow loading. | ||
const rand = Math.random(); | ||
document.write('<script src="1-mb.js?r=' + rand + '"><\/script>'); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"use strict"; | ||
|
||
(() => { | ||
const ELEMENT_ID = "dark-mode-toggle-stylesheets"; | ||
const STORAGE_NAME = "dark-mode-toggle"; | ||
const LIGHT = "light"; | ||
const DARK = "dark"; | ||
|
||
let stylesheets = document.getElementById(ELEMENT_ID).textContent; | ||
|
||
let mode = null; | ||
try { | ||
mode = localStorage.getItem(STORAGE_NAME); | ||
} catch (e) {} | ||
|
||
const lightCSSMediaRegex = /\(\s*prefers-color-scheme\s*:\s*light\s*\)/gi; | ||
const darkCSSMediaRegex = /\(\s*prefers-color-scheme\s*:\s*dark\s*\)/gi; | ||
|
||
switch (mode) { | ||
case LIGHT: | ||
stylesheets = stylesheets | ||
.replace(lightCSSMediaRegex, "$&, all") | ||
.replace(darkCSSMediaRegex, "$& and not all"); | ||
break; | ||
|
||
case DARK: | ||
stylesheets = stylesheets | ||
.replace(darkCSSMediaRegex, "$&, all") | ||
.replace(lightCSSMediaRegex, "$& and not all"); | ||
break; | ||
} | ||
|
||
document.write(stylesheets); | ||
})(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.