Skip to content

Commit

Permalink
avoid top-level await
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Nov 6, 2024
1 parent 9d0cdfd commit d3498b9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
12 changes: 7 additions & 5 deletions src/client/dark.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {Generators} from "observablehq:stdlib";

for await (const dark of Generators.dark()) {
const {classList} = document.body;
classList.toggle("dark", dark);
classList.toggle("light", !dark);
}
(async () => {
for await (const dark of Generators.dark()) {
const {classList} = document.body;
classList.toggle("dark", dark);
classList.toggle("light", !dark);
}
})();
6 changes: 3 additions & 3 deletions src/client/stdlib/generators/dark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ export function dark() {
return observe((notify: (dark: boolean) => void) => {
let dark: boolean | undefined;
const probe = document.createElement("div");
probe.style.setProperty("transition-property", "color");
probe.style.setProperty("transition-duration", "0.001s");
probe.style.transitionProperty = "color";
probe.style.transitionDuration = "0.001s";
const changed = () => {
const d = getComputedStyle(probe).getPropertyValue("color-scheme") === "dark";
const d = getComputedStyle(probe).getPropertyValue("color-scheme") === "dark"; // TODO "light dark"?
if (dark === d) return; // only notify if changed
notify((dark = d));
};
Expand Down

0 comments on commit d3498b9

Please sign in to comment.