Skip to content

Commit 0d69d7a

Browse files
committed
Move color detection to head and set CSS variables as inline styles
Previous approach applied classes after styles were loaded, causing a timing issue. Now the color scheme is detected in a script in the head, before any styles are applied, and the CSS variables are set directly as inline styles on the html element. Changes: - Move color scheme detection script from body to head - Set --bg-color and --text-color as inline styles via setProperty - Apply color and background-color directly to :root - Change * selector to use 'inherit' for better color inheritance - Remove duplicate color scheme script from body This ensures colors are applied immediately before any content renders, preventing black text from appearing in dark mode. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent fb1815a commit 0d69d7a

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

website/src/components/code-preview.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,42 @@ function generateJavaScriptIFrameHTML(
1616
<head>
1717
<meta charset="utf-8">
1818
<meta name="viewport" content="width=device-width,initial-scale=1">
19+
<script>
20+
// Detect and apply color scheme before any rendering
21+
const colorScheme = sessionStorage.getItem("color-scheme") ||
22+
(
23+
window.matchMedia &&
24+
window.matchMedia("(prefers-color-scheme: dark)").matches
25+
? "dark"
26+
: "light"
27+
);
28+
29+
// Set colors directly on html element
30+
const isDark = colorScheme === "dark";
31+
const bgColor = isDark ? "#0a0e1f" : "#e7f4f5";
32+
const textColor = isDark ? "#f5f9ff" : "#0a0e1f";
33+
34+
document.documentElement.style.setProperty("--bg-color", bgColor);
35+
document.documentElement.style.setProperty("--text-color", textColor);
36+
37+
if (!isDark) {
38+
document.documentElement.classList.add("color-scheme-light");
39+
}
40+
</script>
1941
<style>
20-
/* Inline styles to ensure text is visible before external CSS loads */
42+
/* Inline styles to ensure text is visible */
2143
:root {
2244
--bg-color: #0a0e1f;
2345
--text-color: #f5f9ff;
46+
color: var(--text-color);
47+
background-color: var(--bg-color);
2448
}
2549
.color-scheme-light {
2650
--bg-color: #e7f4f5;
2751
--text-color: #0a0e1f;
2852
}
2953
* {
30-
color: var(--text-color);
54+
color: inherit;
3155
box-sizing: border-box;
3256
}
3357
body {
@@ -45,23 +69,6 @@ function generateJavaScriptIFrameHTML(
4569
/>
4670
</head>
4771
<body>
48-
<!-- TODO: extract these scripts to a separate file or something -->
49-
<script>
50-
const colorScheme = sessionStorage.getItem("color-scheme") ||
51-
(
52-
window.matchMedia &&
53-
window.matchMedia("(prefers-color-scheme: dark)").matches
54-
? "dark"
55-
: "light"
56-
);
57-
if (colorScheme === "dark") {
58-
document.documentElement.classList.remove("color-scheme-light");
59-
document.body.classList.remove("color-scheme-light");
60-
} else {
61-
document.documentElement.classList.add("color-scheme-light");
62-
document.body.classList.add("color-scheme-light");
63-
}
64-
</script>
6572
<script>
6673
window.addEventListener("load", (ev) => {
6774
window.parent.postMessage(

0 commit comments

Comments
 (0)