-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
6 additions
and
8 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,20 +1,18 @@ | ||
import {observe} from "./observe.js"; | ||
|
||
// Watches dark mode based on the color-scheme set by the current theme. | ||
// 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 probe = document.createElement("div"); | ||
probe.style.transitionProperty = "color"; | ||
probe.style.transitionDuration = "0.001s"; | ||
const media = matchMedia("(prefers-color-scheme: dark)"); | ||
const changed = () => { | ||
const d = getComputedStyle(probe).getPropertyValue("color-scheme") === "dark"; // TODO "light dark"? | ||
const d = getComputedStyle(document.body).getPropertyValue("color-scheme") === "dark"; | ||
if (dark === d) return; // only notify if changed | ||
notify((dark = d)); | ||
}; | ||
document.body.append(probe); | ||
probe.addEventListener("transitionstart", changed); | ||
changed(); | ||
return () => probe.remove(); | ||
media.addEventListener("change", changed); | ||
return () => media.removeEventListener("change", changed); | ||
}); | ||
} |