-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* dark probe * fix tests --------- Co-authored-by: Philippe Rivière <fil@rezo.net>
- Loading branch information
Showing
11 changed files
with
23 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,28 @@ | ||
import {observe} from "./observe.js"; | ||
|
||
// Watches dark mode based on theme and user preference. | ||
// TODO: in preview, also watch for changes in the theme meta. | ||
export function dark() { | ||
return observe((notify: (dark: boolean) => void) => { | ||
let dark: boolean | undefined; | ||
const media = matchMedia("(prefers-color-scheme: dark)"); | ||
const probe = document.createElement("div"); | ||
probe.style.transitionProperty = "color, background-color"; | ||
probe.style.transitionDuration = "1ms"; | ||
const changed = () => { | ||
const d = getComputedStyle(document.body).getPropertyValue("color-scheme") === "dark"; | ||
const s = getComputedStyle(document.body).getPropertyValue("color-scheme").split(/\s+/); | ||
let d: boolean; | ||
if (s.includes("light") && s.includes("dark")) d = media.matches; | ||
else d = s.includes("dark"); | ||
if (dark === d) return; // only notify if changed | ||
notify((dark = d)); | ||
}; | ||
document.body.appendChild(probe); | ||
changed(); | ||
probe.addEventListener("transitionstart", changed); | ||
media.addEventListener("change", changed); | ||
return () => media.removeEventListener("change", changed); | ||
return () => { | ||
probe.removeEventListener("transitionstart", changed); | ||
media.removeEventListener("change", changed); | ||
}; | ||
}); | ||
} |
File renamed without changes.
File renamed without changes.
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 +1 @@ | ||
import "../_observablehq/stdlib/inputs.00000006.js"; | ||
import "../_observablehq/stdlib/inputs.00000005.js"; |
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
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