Skip to content

Commit

Permalink
dark.js ; use color not background-color
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil committed Nov 6, 2024
1 parent 43074b3 commit bcab913
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/client/dark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {Generators} from "observablehq:stdlib";

for await (const dark of Generators.dark()) {
const {classList} = document.body;
classList.toggle("dark", dark);
classList.toggle("light", !dark);
}
1 change: 1 addition & 0 deletions src/client/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "./pre.js";
import "./sidebar.js";
import "./toc.js";
import "./dark.js";

export {define} from "./main.js";
2 changes: 1 addition & 1 deletion src/client/stdlib/generators/dark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export function dark() {
return observe((notify: (dark: boolean) => void) => {
let dark: boolean | undefined;
const probe = document.createElement("div");
probe.style.setProperty("transition-property", "background-color");
probe.style.setProperty("transition-property", "color");
probe.style.setProperty("transition-duration", "0.001s");
const changed = () => {
const d = getComputedStyle(probe).getPropertyValue("color-scheme") === "dark";
Expand Down
3 changes: 0 additions & 3 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ ${preview ? `\nopen({hash: ${JSON.stringify(resolvers.hash)}, eval: (body) => ev
${html.unsafe(rewriteHtml(page.body, resolvers))}</main>${renderFooter(page.footer, resolvers, options)}
</div>
</body>
<script>{import('${resolveImport(
"observablehq:stdlib"
)}').then(async({Generators:{dark:k}})=>{for await(let d of k()){document.body.classList.toggle("dark",d);document.body.classList.toggle("light",!d)}})}</script>
</html>
`);
}
Expand Down

0 comments on commit bcab913

Please sign in to comment.