Skip to content

Commit 493a75d

Browse files
authored
Merge pull request #18 from react18-tools/minify
Refactor and upgrade r18gs
2 parents 0542c46 + 38b327d commit 493a75d

File tree

9 files changed

+56
-56
lines changed

9 files changed

+56
-56
lines changed

lib/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# nextjs-darkmode
22

3+
## 1.0.7
4+
5+
### Patch Changes
6+
7+
- 0357fca: Upgrade r18gs
8+
39
## 1.0.5
410

511
### Patch Changes

lib/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "nextjs-darkmode",
33
"author": "Mayank Kumar Chaudhari <https://mayank-chaudhari.vercel.app>",
44
"private": false,
5-
"version": "1.0.6",
5+
"version": "1.0.7",
66
"description": "Unleash the Power of React Server Components! Use dark/light mode on your site with confidence, without losing any advantages of React Server Components",
77
"license": "MPL-2.0",
88
"main": "./dist/index.js",
@@ -74,7 +74,7 @@
7474
"vitest": "^2.1.8"
7575
},
7676
"dependencies": {
77-
"r18gs": "^2.0.0"
77+
"r18gs": "^2.0.1"
7878
},
7979
"peerDependencies": {
8080
"@types/react": "16.8 - 19",

lib/src/client/switch/switch.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe("switch", () => {
2424
test("color-scheme-toggle with skip system", async ({ expect }) => {
2525
const hook = renderHook(() => useMode());
2626
act(() => {
27-
hook.result.current.setMode(SYSTEM);
27+
hook.result.current.setMode(LIGHT);
2828
});
2929
render(<Switch skipSystem>Switch with children</Switch>);
3030
const element = screen.getByTestId("switch");

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
});

packages/shared/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# @repo/shared
22

3+
## 0.0.14
4+
5+
### Patch Changes
6+
7+
- 0357fca: Upgrade r18gs
8+
- Updated dependencies [0357fca]
9+
10+
311
## 0.0.13
412

513
### Patch Changes

packages/shared/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@repo/shared",
3-
"version": "0.0.13",
3+
"version": "0.0.14",
44
"private": true,
55
"sideEffects": false,
66
"main": "./dist/index.js",
@@ -42,7 +42,7 @@
4242
"@repo/scripts": "workspace:*",
4343
"nextjs-darkmode": "workspace:*",
4444
"nextjs-themes": "^4.0.3",
45-
"r18gs": "^2.0.0",
45+
"r18gs": "^2.0.1",
4646
"react-live": "^4.1.8",
4747
"react18-loaders": "^1.1.3"
4848
},

pnpm-lock.yaml

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)