Skip to content

Commit 6e3b1cd

Browse files
ryanrozichclaude
andcommitted
fix: auto-detect theme for code block styling
- Add runtime theme detection to CodeBlock component - Automatically use light theme when parent has light background - Detect dark theme by checking for dark background classes - Maintain explicit variant control for hero and light variants - Fixes code block contrast issues when CSS doesn't load properly 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent bb2ab11 commit 6e3b1cd

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

src/demo/components/CodeBlock.tsx

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,52 @@ export const CodeBlock: React.FC<CodeBlockProps> = ({
4444
variant = "default",
4545
}) => {
4646
const [copied, setCopied] = useState(false);
47+
const [detectedTheme, setDetectedTheme] = useState<"light" | "dark">("dark");
48+
49+
// Detect the actual theme based on background
50+
React.useEffect(() => {
51+
if (variant !== "default") return;
52+
53+
const checkTheme = () => {
54+
const body = document.body;
55+
const computedStyle = window.getComputedStyle(body);
56+
const bgColor = computedStyle.backgroundColor;
57+
58+
// Check if background is light
59+
if (bgColor === "rgb(255, 255, 255)" || bgColor === "white") {
60+
setDetectedTheme("light");
61+
} else {
62+
// Check the nearest parent with a background
63+
let foundDarkBg = false;
64+
65+
// Look for elements with dark backgrounds
66+
const darkElements = document.querySelectorAll(
67+
".bg-gray-950, .bg-gray-900, .min-h-screen",
68+
);
69+
darkElements.forEach((el) => {
70+
const elBg = window.getComputedStyle(el).backgroundColor;
71+
if (elBg && elBg !== "rgba(0, 0, 0, 0)" && elBg !== "transparent") {
72+
// Parse RGB values
73+
const match = elBg.match(/\d+/g);
74+
if (match) {
75+
const [r, g, b] = match.map(Number);
76+
// If all values are low, it's a dark background
77+
if (r < 50 && g < 50 && b < 50) {
78+
foundDarkBg = true;
79+
}
80+
}
81+
}
82+
});
83+
84+
setDetectedTheme(foundDarkBg ? "dark" : "light");
85+
}
86+
};
87+
88+
checkTheme();
89+
// Re-check on window resize or other changes
90+
window.addEventListener("resize", checkTheme);
91+
return () => window.removeEventListener("resize", checkTheme);
92+
}, [variant]);
4793

4894
const handleCopy = async () => {
4995
try {
@@ -64,8 +110,9 @@ export const CodeBlock: React.FC<CodeBlockProps> = ({
64110
// Always disable line numbers for now
65111
const shouldShowLineNumbers = false;
66112

67-
// Use light theme for light variant
68-
const isLightTheme = variant === "light";
113+
// Use light theme for light variant or detected light theme
114+
const isLightTheme =
115+
variant === "light" || (variant === "default" && detectedTheme === "light");
69116
const theme = isLightTheme ? vs : vscDarkPlus;
70117

71118
return (

src/demo/version-info.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"version": "0.1.0",
33
"git": {
4-
"commitHash": "58b8300b8c41fccee809f106e3849b026c6dffad",
5-
"shortHash": "58b8300",
4+
"commitHash": "bb2ab115052487299a10ab2826049fc73df5f2e0",
5+
"shortHash": "bb2ab11",
66
"branch": "release/v0.1.1-rc2",
7-
"commitDate": "2025-07-07 22:30:18 -0500",
7+
"commitDate": "2025-07-07 22:38:13 -0500",
88
"latestTag": "v0.1.0",
9-
"commitsSinceTag": 78,
9+
"commitsSinceTag": 79,
1010
"isDirty": false
1111
},
1212
"deployment": {
@@ -15,7 +15,7 @@
1515
"isMainBranch": false,
1616
"deployPath": "ag-grid-react-components"
1717
},
18-
"buildTime": "2025-07-08T03:31:04.557Z",
19-
"displayVersion": "v0.1.0+78",
18+
"buildTime": "2025-07-08T03:39:35.295Z",
19+
"displayVersion": "v0.1.0+79",
2020
"displayLabel": "release/v0.1.1-rc2"
2121
}

0 commit comments

Comments
 (0)