Skip to content

Commit 32a9ef4

Browse files
committed
Address PR feedback: remove hard-coded colors, simplify utilities
PR #316 feedback addressed: 1. **Remove hard-coded CSS colors** (lines 47-48, 75-76) - Removed all hard-coded color values from utilities - Just toggle classes now - CSS defines the actual colors - Follows separation of concerns: utilities handle mechanism, CSS handles colors 2. **Remove syncIframes function** (line 90) - Wasn't working properly - Removed postMessage-based iframe syncing - Simplified color-scheme-toggle 3. **Simplify iframe template** - Removed duplicate color definitions from iframe HTML - Relies on linked client.css for colors - Only minimal inline styles for layout 4. **Clarify SSR default comment** (line 12) - Added note about why SSR defaults to "dark" Changes: - website/src/utils/color-scheme.ts: Removed color values, removed syncIframes - website/src/components/color-scheme-toggle.ts: Removed syncIframes import/call - website/src/components/code-preview.ts: Removed hard-coded colors from iframe 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 661c54d commit 32a9ef4

File tree

3 files changed

+15
-94
lines changed

3 files changed

+15
-94
lines changed

website/src/components/code-preview.ts

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,13 @@ function generateJavaScriptIFrameHTML(
2121
${getColorSchemeScript()}
2222
</script>
2323
<style>
24-
/* Ensure colors cascade to all elements */
25-
html, body {
26-
background-color: #0a0e1f;
27-
color: #f5f9ff;
28-
margin: 0;
29-
padding: 0;
30-
}
31-
32-
/* Light mode overrides */
33-
html.color-scheme-light, html.color-scheme-light body {
34-
background-color: #e7f4f5;
35-
color: #0a0e1f;
36-
}
37-
24+
/* Minimal inline styles - actual colors defined in client.css */
3825
* {
3926
box-sizing: border-box;
4027
}
41-
42-
/* CSS variables for compatibility with external CSS */
43-
:root {
44-
--bg-color: #0a0e1f;
45-
--text-color: #f5f9ff;
46-
--highlight-color: #daa520;
47-
}
48-
.color-scheme-light {
49-
--bg-color: #e7f4f5;
50-
--text-color: #0a0e1f;
51-
--highlight-color: #daa520;
52-
}
53-
54-
body {
55-
font-family: sans-serif;
28+
html, body {
29+
margin: 0;
30+
padding: 0;
5631
}
5732
</style>
5833
<link

website/src/components/color-scheme-toggle.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
getColorScheme,
55
setColorScheme,
66
applyColorScheme,
7-
syncIframes,
87
type ColorScheme,
98
} from "../utils/color-scheme.js";
109

@@ -27,7 +26,6 @@ export function ColorSchemeToggle(this: Context) {
2726

2827
if (typeof window !== "undefined") {
2928
applyColorScheme(colorScheme!);
30-
syncIframes(colorScheme!);
3129
}
3230

3331
return jsx`

website/src/utils/color-scheme.ts

Lines changed: 11 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ export type ColorScheme = "dark" | "light";
99
*/
1010
export function getColorScheme(): ColorScheme {
1111
if (typeof window === "undefined") {
12-
return "dark"; // SSR default
12+
// For SSR, we can't know the preference, so we return null/undefined
13+
// and let client-side hydration handle it
14+
return "dark"; // Still need a default for type safety
1315
}
1416

1517
const stored = sessionStorage.getItem("color-scheme");
@@ -33,7 +35,8 @@ export function setColorScheme(scheme: ColorScheme): void {
3335
}
3436

3537
/**
36-
* Applies color scheme classes and CSS variables to a document element
38+
* Applies color scheme class to document elements.
39+
* The actual colors are defined in CSS via :root and .color-scheme-light
3740
*/
3841
export function applyColorScheme(
3942
scheme: ColorScheme,
@@ -43,16 +46,8 @@ export function applyColorScheme(
4346
const body =
4447
target instanceof Document ? target.body : target.ownerDocument?.body;
4548

46-
const isDark = scheme === "dark";
47-
const bgColor = isDark ? "#0a0e1f" : "#e7f4f5";
48-
const textColor = isDark ? "#f5f9ff" : "#0a0e1f";
49-
50-
// Apply CSS variables as inline styles for highest specificity
51-
root.style.setProperty("--bg-color", bgColor);
52-
root.style.setProperty("--text-color", textColor);
53-
54-
// Apply classes
55-
if (isDark) {
49+
// Just toggle the class - CSS handles the actual colors
50+
if (scheme === "dark") {
5651
root.classList.remove("color-scheme-light");
5752
body?.classList.remove("color-scheme-light");
5853
} else {
@@ -62,66 +57,19 @@ export function applyColorScheme(
6257
}
6358

6459
/**
65-
* Returns the inline script code for applying color scheme before render
66-
* Use this in server-rendered HTML to prevent FOUC
60+
* Returns the inline script code for applying color scheme before render.
61+
* Use this in server-rendered HTML to prevent FOUC.
62+
* Only handles the mechanism - CSS defines the actual colors.
6763
*/
6864
export function getColorSchemeScript(): string {
6965
return `
7066
const colorScheme = sessionStorage.getItem("color-scheme") ||
7167
(window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches
7268
? "dark" : "light");
7369
74-
const isDark = colorScheme === "dark";
75-
const bgColor = isDark ? "#0a0e1f" : "#e7f4f5";
76-
const textColor = isDark ? "#f5f9ff" : "#0a0e1f";
77-
78-
document.documentElement.style.setProperty("--bg-color", bgColor);
79-
document.documentElement.style.setProperty("--text-color", textColor);
80-
81-
if (!isDark) {
70+
if (colorScheme === "light") {
8271
document.documentElement.classList.add("color-scheme-light");
8372
}
8473
`.trim();
8574
}
8675

87-
/**
88-
* Syncs color scheme to all playground iframes via postMessage
89-
*/
90-
export function syncIframes(scheme: ColorScheme): void {
91-
if (typeof window === "undefined") return;
92-
93-
const iframes = document.querySelectorAll<HTMLIFrameElement>(
94-
".playground-iframe",
95-
);
96-
97-
for (const iframe of iframes) {
98-
// Send message to iframe
99-
iframe.contentWindow?.postMessage(
100-
JSON.stringify({type: "color-scheme-change", scheme}),
101-
window.location.origin,
102-
);
103-
104-
// Also apply directly (for iframes that don't have message listener yet)
105-
if (iframe.contentDocument) {
106-
applyColorScheme(scheme, iframe.contentDocument);
107-
}
108-
}
109-
}
110-
111-
/**
112-
* Sets up a message listener in an iframe to receive color scheme updates
113-
*/
114-
export function setupIframeColorSchemeListener(): void {
115-
if (typeof window === "undefined") return;
116-
117-
window.addEventListener("message", (ev) => {
118-
try {
119-
const data = JSON.parse(ev.data);
120-
if (data.type === "color-scheme-change") {
121-
applyColorScheme(data.scheme as ColorScheme);
122-
}
123-
} catch {
124-
// Ignore non-JSON messages
125-
}
126-
});
127-
}

0 commit comments

Comments
 (0)