Skip to content

Commit e8c9f37

Browse files
committed
add dynamic css variables as url parameters
1 parent cdc4ffd commit e8c9f37

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

src/components/ThemeHandler.tsx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { useEffect, useState } from "react";
2+
3+
// Define valid CSS variables as constants
4+
const VALID_VARIABLES = {
5+
"host-primary": "--host-primary",
6+
"host-primary-dark": "--host-primary-dark",
7+
"host-primary-darker": "--host-primary-darker",
8+
"host-body-bg": "--host-body-bg",
9+
} as const;
10+
11+
type ValidVariableKey = keyof typeof VALID_VARIABLES;
12+
13+
const ThemeHandler = ({ children }: { children?: React.ReactNode }) => {
14+
const [isThemeReady, setIsThemeReady] = useState(false);
15+
16+
useEffect(() => {
17+
const urlParams = new URLSearchParams(window.location.search);
18+
const root = document.documentElement;
19+
20+
// Helper function to validate hex color
21+
const isValidHexColor = (color: string): boolean => {
22+
return /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/.test(color);
23+
};
24+
25+
// Process each URL parameter
26+
urlParams.forEach((value, key) => {
27+
const variableKey = key as ValidVariableKey;
28+
29+
if (VALID_VARIABLES[variableKey]) {
30+
if (isValidHexColor(value)) {
31+
root.style.setProperty(VALID_VARIABLES[variableKey], value);
32+
} else {
33+
console.warn(
34+
`Invalid color value for ${key}: ${value}. Must be a hex color (e.g., #005aa7)`
35+
);
36+
}
37+
}
38+
});
39+
40+
// Mark theme as ready
41+
setIsThemeReady(true);
42+
43+
// Cleanup function
44+
return () => {
45+
Object.values(VALID_VARIABLES).forEach((cssVar) => {
46+
root.style.removeProperty(cssVar);
47+
});
48+
};
49+
}, []);
50+
51+
if (!isThemeReady) {
52+
// Return an invisible div that takes up space to prevent layout shift
53+
return <div style={{ visibility: "hidden" }}>{children}</div>;
54+
}
55+
56+
return <>{children}</>;
57+
};
58+
59+
export default ThemeHandler;

src/main.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ import App from "./App";
77

88
// Import our custom styles.
99
import "./styles/custom.scss";
10+
import ThemeHandler from "./components/ThemeHandler";
1011

1112
ReactDOM.createRoot(document.getElementById("root")!).render(
1213
<React.StrictMode>
13-
<App />
14+
<ThemeHandler>
15+
<App />
16+
</ThemeHandler>
1417
</React.StrictMode>
1518
);

tsconfig.app.tsbuildinfo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"root":["./src/app.tsx","./src/main.tsx","./src/vite-env.d.ts","./src/components/codeblock.tsx","./src/components/intro.tsx","./src/components/output.tsx","./src/components/wizardform.tsx","./src/utils/dictionarytocsv.ts","./src/utils/theme.ts"],"version":"5.6.3"}
1+
{"root":["./src/app.tsx","./src/main.tsx","./src/vite-env.d.ts","./src/components/codeblock.tsx","./src/components/intro.tsx","./src/components/output.tsx","./src/components/themehandler.tsx","./src/components/wizardform.tsx","./src/utils/dictionarytocsv.ts","./src/utils/theme.ts"],"version":"5.6.3"}

0 commit comments

Comments
 (0)