-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: attempt to fix color scheme on demo by forcing it on load (#37)
- Loading branch information
1 parent
addc87e
commit 060c398
Showing
3 changed files
with
19 additions
and
25 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
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 |
---|---|---|
@@ -1,14 +1,26 @@ | ||
/// <reference lib="dom" /> | ||
const prefers = matchMedia("(prefers-color-scheme: dark)") ? "dark" : "light" | ||
const prefers = matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light" | ||
const html = document.querySelector("html") | ||
html.dataset.colorScheme = prefers | ||
document.querySelectorAll(`body > header .color-scheme`).forEach((element) => { | ||
element.querySelector(`svg.${prefers}`).style.display = "inline-block" | ||
element.addEventListener("click", () => { | ||
toggleColorScheme(html.dataset, element) | ||
}) | ||
}) | ||
document.querySelectorAll(".example-tabs li.color-scheme").forEach((element) => { | ||
element.querySelector(`svg.${prefers}`).style.display = "inline-block" | ||
element.addEventListener("click", (_) => { | ||
element.addEventListener("click", () => { | ||
const example = element.parentNode.nextElementSibling | ||
if (example.tagName === "IFRAME") { | ||
example.contentWindow.document.documentElement.dataset.colorScheme = (example.contentWindow.document.documentElement.dataset.colorScheme ?? prefers) === "light" ? "dark" : "light" | ||
} | ||
example.dataset.colorScheme = (example.dataset.colorScheme ?? prefers) === "light" ? "dark" : "light" | ||
element.querySelector("svg.light").style.display = example.dataset.colorScheme === "light" ? "inline-block" : "none" | ||
element.querySelector("svg.dark").style.display = example.dataset.colorScheme === "dark" ? "inline-block" : "none" | ||
toggleColorScheme(example.dataset, element) | ||
}) | ||
}) | ||
|
||
function toggleColorScheme(dataset, element) { | ||
dataset.colorScheme = (dataset.colorScheme ?? prefers) === "light" ? "dark" : "light" | ||
element.querySelector("svg.light").style.display = dataset.colorScheme === "light" ? "inline-block" : "none" | ||
element.querySelector("svg.dark").style.display = dataset.colorScheme === "dark" ? "inline-block" : "none" | ||
} |