Skip to content

Commit 3a8a25e

Browse files
committed
Set CSS variables via JavaScript to prevent external CSS override
The external client.css was overriding our inline CSS variable definitions because it loaded after our inline <style> tag. Both had the same specificity (:root selector), so cascade order determined the winner. By setting CSS variables via JavaScript using setProperty(), we create inline styles on the html element which have higher specificity than any CSS selector (equivalent to style="--text-color: #f5f9ff"). This ensures the variables are set correctly before the external CSS loads and cannot be overridden. This fixes the black-text-on-dark-background issue while still allowing user inline styles to override colors on specific elements. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 3771e6f commit 3a8a25e

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

website/src/components/code-preview.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,16 @@ function generateJavaScriptIFrameHTML(
2626
: "light"
2727
);
2828
29-
if (colorScheme !== "dark") {
29+
// Set CSS variables as inline styles - these have higher specificity
30+
// than external CSS and will not be overridden
31+
const isDark = colorScheme === "dark";
32+
const bgColor = isDark ? "#0a0e1f" : "#e7f4f5";
33+
const textColor = isDark ? "#f5f9ff" : "#0a0e1f";
34+
35+
document.documentElement.style.setProperty("--bg-color", bgColor);
36+
document.documentElement.style.setProperty("--text-color", textColor);
37+
38+
if (!isDark) {
3039
document.documentElement.classList.add("color-scheme-light");
3140
}
3241
</script>

0 commit comments

Comments
 (0)