Skip to content

Commit

Permalink
touch up
Browse files Browse the repository at this point in the history
  • Loading branch information
mayank1513 committed Jun 27, 2024
1 parent 961b453 commit 9b06e4d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
3 changes: 1 addition & 2 deletions lib/src/client/color-switch/color-switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ export const ColorSwitch = ({
}: ColorSwitchProps) => {
const { toggleColorScheme, colorSchemePref } = useTheme(targetSelector);

const cls = [styles.s, styles[colorSchemePref], className].join(" ");
return (
<button
className={cls}
className={[styles.s, styles[colorSchemePref], className].join(" ")}
data-testid="color-switch"
// skipcq: JS-0417
onClick={() => toggleColorScheme(skipSystem)}
Expand Down
8 changes: 8 additions & 0 deletions lib/src/client/theme-switcher/no-fouc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ declare global {
var g: UpdateForcedPropsFunc;
}

export type ScriptArgs = [
string,
ThemeStoreType,
Record<string, string> | undefined,
string | undefined,
ColorSchemeType | undefined,
];

/** @internal Script to be injected for avoiding FOUC */
export const noFOUCScript = (
key: string,
Expand Down
15 changes: 10 additions & 5 deletions lib/src/client/theme-switcher/theme-switcher.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ColorSchemeType } from "../../types";
import { noFOUCScript } from "./no-fouc";
import { noFOUCScript, type ScriptArgs } from "./no-fouc";
import { initialState } from "../../store";
import { DEFAULT_ID } from "../../constants";
import { Switcher } from "../switcher";
Expand Down Expand Up @@ -45,15 +45,20 @@ const Script = ({
forcedTheme,
forcedColorScheme,
}: ThemeSwitcherProps) => {
const k = targetSelector || `#${DEFAULT_ID}`;
const args = [
targetSelector || `#${DEFAULT_ID}`,
initialState,
styles,
forcedTheme,
forcedColorScheme,
] as ScriptArgs;
// handle client side exceptions when script is not run. <- for client side apps like vite or CRA
if (typeof window !== "undefined" && !window.m)
noFOUCScript(k, initialState, styles, forcedTheme, forcedColorScheme);
if (typeof window !== "undefined" && !window.m) noFOUCScript(...args);
return (
<script
// skipcq: JS-0440
dangerouslySetInnerHTML={{
__html: `(${noFOUCScript})(${JSON.stringify([k, initialState, styles, forcedTheme, forcedColorScheme]).slice(1, -1)})`,
__html: `(${noFOUCScript})(${JSON.stringify(args).slice(1, -1)})`,
}}
nonce={nonce}
/>
Expand Down

0 comments on commit 9b06e4d

Please sign in to comment.