Skip to content

Commit ca70496

Browse files
authored
fix(website): remove js execution on load by assigning themes sig on click (#939)
1 parent 2b38edf commit ca70496

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

apps/website/src/components/copy-css-config/copy-css-config.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ export default component$(() => {
1212

1313
const cssThemeOutput = useSignal<string>('');
1414

15-
const { themeSig } = useTheme();
15+
const { themeSig, defaultTheme, storageKey } = useTheme();
1616

1717
return (
1818
<Modal.Root bind:show={showSig}>
1919
<Button
2020
onClick$={() => {
21+
themeSig.value = localStorage.getItem(storageKey) ?? defaultTheme;
2122
cssThemeOutput.value = extractThemeCSS(
2223
themeSig.value === 'dark' || themeSig.value === 'light'
2324
? 'border-radius-0 simple primary-cyan-600 light base-slate'

packages/themes/src/lib/provider.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,9 @@ export const ThemeProvider = component$<ThemeProviderProps>(
7171
}
7272
});
7373

74-
// DO NOT UNCOMMENT. THIS CAUSES BUNDLE ISSUE ACROSS SITE -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
7574
// eslint-disable-next-line qwik/no-use-visible-task -- not possible atm to useOnWindow for a MediaQueryList event
7675
useVisibleTask$(
7776
({ cleanup }) => {
78-
setTimeout(() => {
79-
themeSig.value = localStorage.getItem(storageKey) || defaultTheme;
80-
}, 2000);
8177
const media = window.matchMedia('(prefers-color-scheme: dark)');
8278

8379
const handleMediaQuery = $(async (e: MediaQueryListEvent | MediaQueryList) => {
@@ -133,9 +129,11 @@ export const ThemeProvider = component$<ThemeProviderProps>(
133129
});
134130

135131
useContextProvider(ThemeContext, {
132+
defaultTheme,
136133
themeSig,
137134
resolvedThemeSig,
138135
forcedTheme,
136+
storageKey,
139137
systemTheme: (enableSystem ? resolvedThemeSig.value : undefined) as
140138
| SystemTheme
141139
| undefined,

packages/themes/src/lib/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ export type SystemTheme = 'dark' | 'light';
99
export type Theme = 'dark' | 'light' | string | string[] | undefined;
1010

1111
export interface UseThemeProps {
12+
// system or light
13+
defaultTheme: string;
1214
/** theme signal */
1315
themeSig: Signal<Theme>;
1416
/** Forced theme name for the current page */
1517
resolvedThemeSig: Signal<Theme>;
1618
/** If enableSystem is true, returns the System theme preference ("dark" or "light"), regardless what the active theme is */
1719
forcedTheme: string | undefined;
20+
// localStorage key
21+
storageKey: string;
1822
/** If `enableSystem` is true and the active theme is "system", this returns whether the system preference resolved to "dark" or "light". Otherwise, identical to `theme` */
1923
systemTheme: SystemTheme | undefined;
2024
/** List of all available theme names */

0 commit comments

Comments
 (0)