Skip to content

Commit 92e6580

Browse files
mbostockFil
andauthored
dark probe (#1802)
* dark probe * fix tests --------- Co-authored-by: Philippe Rivière <[email protected]>
1 parent 324c7eb commit 92e6580

File tree

11 files changed

+23
-13
lines changed

11 files changed

+23
-13
lines changed

src/client/stdlib/generators/dark.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
import {observe} from "./observe.js";
22

33
// Watches dark mode based on theme and user preference.
4-
// TODO: in preview, also watch for changes in the theme meta.
54
export function dark() {
65
return observe((notify: (dark: boolean) => void) => {
76
let dark: boolean | undefined;
87
const media = matchMedia("(prefers-color-scheme: dark)");
8+
const probe = document.createElement("div");
9+
probe.style.transitionProperty = "color, background-color";
10+
probe.style.transitionDuration = "1ms";
911
const changed = () => {
10-
const d = getComputedStyle(document.body).getPropertyValue("color-scheme") === "dark";
12+
const s = getComputedStyle(document.body).getPropertyValue("color-scheme").split(/\s+/);
13+
let d: boolean;
14+
if (s.includes("light") && s.includes("dark")) d = media.matches;
15+
else d = s.includes("dark");
1116
if (dark === d) return; // only notify if changed
1217
notify((dark = d));
1318
};
19+
document.body.appendChild(probe);
1420
changed();
21+
probe.addEventListener("transitionstart", changed);
1522
media.addEventListener("change", changed);
16-
return () => media.removeEventListener("change", changed);
23+
return () => {
24+
probe.removeEventListener("transitionstart", changed);
25+
media.removeEventListener("change", changed);
26+
};
1727
});
1828
}

test/output/build/duckdb/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
99
<link rel="preload" as="style" href="https://fonts.googleapis.com/css2?family=Source+Serif+4:ital,opsz,wght@0,8..60,200..900;1,8..60,200..900&amp;display=swap" crossorigin>
1010
<link rel="preload" as="style" href="./_observablehq/theme-air,near-midnight.00000004.css">
11-
<link rel="preload" as="style" href="./_observablehq/stdlib/inputs.00000006.css">
11+
<link rel="preload" as="style" href="./_observablehq/stdlib/inputs.00000007.css">
1212
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css2?family=Source+Serif+4:ital,opsz,wght@0,8..60,200..900;1,8..60,200..900&amp;display=swap" crossorigin>
1313
<link rel="stylesheet" type="text/css" href="./_observablehq/theme-air,near-midnight.00000004.css">
14-
<link rel="stylesheet" type="text/css" href="./_observablehq/stdlib/inputs.00000006.css">
14+
<link rel="stylesheet" type="text/css" href="./_observablehq/stdlib/inputs.00000007.css">
1515
<link rel="modulepreload" href="./_observablehq/client.00000001.js">
1616
<link rel="modulepreload" href="./_observablehq/runtime.00000002.js">
1717
<link rel="modulepreload" href="./_observablehq/stdlib.00000003.js">
1818
<link rel="modulepreload" href="./_observablehq/stdlib/duckdb.00000005.js">
19-
<link rel="modulepreload" href="./_observablehq/stdlib/inputs.00000007.js">
19+
<link rel="modulepreload" href="./_observablehq/stdlib/inputs.00000006.js">
2020
<link rel="modulepreload" href="./_npm/@duckdb/[email protected]/cd372fb8.js">
2121
<link rel="modulepreload" href="./_npm/[email protected]/cd372fb8.js">
2222
<link rel="modulepreload" href="./_npm/[email protected]/cd372fb8.js">
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
import "../_observablehq/stdlib/inputs.00000006.js";
1+
import "../_observablehq/stdlib/inputs.00000005.js";

test/output/build/npm/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
88
<link rel="preload" as="style" href="https://fonts.googleapis.com/css2?family=Source+Serif+4:ital,opsz,wght@0,8..60,200..900;1,8..60,200..900&amp;display=swap" crossorigin>
99
<link rel="preload" as="style" href="./_observablehq/theme-air,near-midnight.00000004.css">
10-
<link rel="preload" as="style" href="./_observablehq/stdlib/inputs.00000005.css">
10+
<link rel="preload" as="style" href="./_observablehq/stdlib/inputs.00000006.css">
1111
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css2?family=Source+Serif+4:ital,opsz,wght@0,8..60,200..900;1,8..60,200..900&amp;display=swap" crossorigin>
1212
<link rel="stylesheet" type="text/css" href="./_observablehq/theme-air,near-midnight.00000004.css">
13-
<link rel="stylesheet" type="text/css" href="./_observablehq/stdlib/inputs.00000005.css">
13+
<link rel="stylesheet" type="text/css" href="./_observablehq/stdlib/inputs.00000006.css">
1414
<link rel="modulepreload" href="./_observablehq/client.00000001.js">
1515
<link rel="modulepreload" href="./_observablehq/runtime.00000002.js">
1616
<link rel="modulepreload" href="./_observablehq/stdlib.00000003.js">
1717
<link rel="modulepreload" href="./_import/index.4bdc071f.js">
18-
<link rel="modulepreload" href="./_observablehq/stdlib/inputs.00000006.js">
18+
<link rel="modulepreload" href="./_observablehq/stdlib/inputs.00000005.js">
1919
<link rel="modulepreload" href="./_npm/[email protected]/cd372fb8.js">
2020
<link rel="modulepreload" href="./_npm/[email protected]/cd372fb8.js">
2121
<script type="module">

0 commit comments

Comments
 (0)