Skip to content

Commit 186b984

Browse files
committed
minify
1 parent 0542c46 commit 186b984

File tree

3 files changed

+24
-38
lines changed

3 files changed

+24
-38
lines changed

lib/src/client/switch/switch.tsx

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,45 +27,31 @@ export const Switch = ({
2727
size = 24,
2828
skipSystem,
2929
children,
30-
...props
30+
...tagProps
3131
}: SwitchProps) => {
32-
const [state, setState] = useStore();
32+
const [{ m, s }, setState] = useStore();
33+
const n = modes.length - (skipSystem ? 1 : 0);
3334
/** toggle mode */
34-
const handleModeSwitch = () => {
35-
let index = modes.indexOf(state.m);
36-
const n = modes.length;
37-
if (skipSystem && index === n - 1) index = 0;
35+
tagProps.onClick = () =>
3836
setState({
39-
...state,
40-
m: modes[(index + 1) % n],
37+
s,
38+
m: modes[(modes.indexOf(m) + 1) % n],
4139
});
42-
};
43-
if (children) {
44-
return (
45-
// @ts-expect-error -- too complex types
46-
<Tag
47-
suppressHydrationWarning
48-
{...props}
49-
data-testid="switch"
50-
// skipcq: JS-0417
51-
onClick={handleModeSwitch}>
52-
{/* @ts-expect-error -> we are setting the CSS variable */}
53-
<div className={styles.switch} style={{ "--size": `${size}px` }} />
54-
{children}
55-
</Tag>
56-
);
40+
41+
const className = styles.switch;
42+
const style = { "--size": `${size}px` };
43+
44+
if (!children) {
45+
tagProps.className += " " + className;
46+
tagProps.style = { ...tagProps.style, ...style };
5747
}
48+
5849
return (
59-
<Tag
60-
// Hydration warning is caused when the data from localStorage differs from the default data provided while rendering on server
61-
suppressHydrationWarning
62-
{...props}
63-
className={[props.className, styles.switch].join(" ")}
64-
// @ts-expect-error -> we are setting the CSS variable
65-
style={{ "--size": `${size}px` }}
66-
data-testid="switch"
67-
// skipcq: JS-0417 -> tradeoff between size and best practices
68-
onClick={handleModeSwitch}
69-
/>
50+
// @ts-expect-error -- too complex types
51+
<Tag suppressHydrationWarning {...tagProps} data-testid="switch">
52+
{/* @ts-expect-error -> we are setting the CSS variable */}
53+
{children && <div {...{ className, style }} />}
54+
{children}
55+
</Tag>
7056
);
7157
};

lib/src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ export const SYSTEM: ColorSchemePreference = "system";
44
export const DARK: ResolvedScheme = "dark";
55
export const LIGHT: ResolvedScheme = "";
66

7-
export const modes: ColorSchemePreference[] = [SYSTEM, DARK, LIGHT];
7+
export const modes: ColorSchemePreference[] = [DARK, LIGHT, SYSTEM];

lib/src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ export interface Store {
1212
export const useStore = () =>
1313
useRGS<Store>("ndm", () => {
1414
if (typeof document === "undefined") return { m: SYSTEM, s: DARK };
15-
const el = document.documentElement;
15+
const [m, s] = ["m", "sm"].map(dt => document.documentElement.getAttribute("data-" + dt));
1616
return {
17-
m: (el.getAttribute("data-m") ?? SYSTEM) as ColorSchemePreference,
18-
s: el.getAttribute("data-sm") as ResolvedScheme,
17+
m: (m ?? SYSTEM) as ColorSchemePreference,
18+
s: s as ResolvedScheme,
1919
};
2020
});

0 commit comments

Comments
 (0)