Skip to content

Commit d3ba198

Browse files
authored
Merge pull request #3 from react18-tools/fix/doc-comments
Fix/doc-comments
2 parents 2cd1fe7 + 767b124 commit d3ba198

File tree

11 files changed

+46
-26
lines changed

11 files changed

+46
-26
lines changed

.tkb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,20 @@
125125
"id": "W_uPAM5Vm86aJ8yEEMKHh",
126126
"description": "Update examples\n- Add example for tailwind",
127127
"columnId": "column-todo"
128+
},
129+
"zE0aqgk59cyZ95s5TkECd": {
130+
"id": "zE0aqgk59cyZ95s5TkECd",
131+
"description": "Update doc comments",
132+
"columnId": "column-todo"
128133
}
129134
},
130135
"columns": [
131136
{
132137
"id": "column-todo",
133138
"title": "To do",
134139
"tasksIds": [
135-
"W_uPAM5Vm86aJ8yEEMKHh"
140+
"W_uPAM5Vm86aJ8yEEMKHh",
141+
"zE0aqgk59cyZ95s5TkECd"
136142
]
137143
},
138144
{

examples/nextjs/src/app/layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default function RootLayout({ children }: { children: React.ReactNode }):
1414
<html lang="en">
1515
<body className={inter.className}>
1616
<ServerTarget />
17-
<Core />
17+
<Core t="all .5s" />
1818
<Layout>
1919
<Header />
2020
{children}

lib/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# nextjs-darkmode
2+
3+
## 0.0.1
4+
5+
### Patch Changes
6+
7+
- Update doc comments, improve minification.

lib/package.json

Lines changed: 1 addition & 1 deletion
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": "0.0.0",
5+
"version": "0.0.1",
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",

lib/src/client/core/core.tsx

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
1-
import { COOKIE_KEY, DARK, LIGHT, MAX_AGE, SYSTEM, modes } from "../../constants";
1+
import { COOKIE_KEY, DARK, LIGHT, SYSTEM, modes } from "../../constants";
22
import { ColorSchemePreference, Store, useStore } from "../../utils";
33
import { useEffect } from "react";
44

55
const useEffectMinify = useEffect;
6+
const localStorageMinify = localStorage;
67
export interface CoreProps {
78
/** force apply CSS transition property to all the elements during theme switching. E.g., `all .3s` */
89
t?: string;
910
}
1011

12+
/** Modify transition globally to avoid patched transitions */
1113
const modifyTransition = (documentMinify: Document, themeTransition = "none") => {
1214
const css = documentMinify.createElement("style");
1315
/** split by ';' to prevent CSS injection */
14-
const transition = `transition: ${themeTransition.split(";")[0]} !important;`;
15-
css.appendChild(
16-
documentMinify.createTextNode(
17-
`*{-webkit-${transition}-moz-${transition}-o-${transition}-ms-${transition}${transition}}`,
18-
),
19-
);
20-
documentMinify.head.appendChild(css);
16+
css.textContent = `*{transition:${themeTransition.split(";")[0]} !important;}`;
17+
const head = documentMinify.head;
18+
head.appendChild(css);
2119

2220
return () => {
2321
// Force restyle
2422
getComputedStyle(documentMinify.body);
2523
// Wait for next tick before removing
26-
setTimeout(() => {
27-
documentMinify.head.removeChild(css);
28-
}, 1);
24+
setTimeout(() => head.removeChild(css), 1);
2925
};
3026
};
3127

3228
/**
33-
*
29+
* The Core component wich applies classes and transitions.
30+
* Cookies are set only if corresponding ServerTarget is detected.
3431
*
3532
* @example
3633
* ```tsx
37-
* <Core />
34+
* <Core [t="background-color .3s"]/>
3835
* ```
3936
*
4037
* @source - Source code
@@ -53,7 +50,7 @@ export const Core = ({ t }: CoreProps) => {
5350

5451
setThemeState(state => ({
5552
...state,
56-
m: (localStorage.getItem(COOKIE_KEY) ?? SYSTEM) as ColorSchemePreference,
53+
m: (localStorageMinify?.getItem(COOKIE_KEY) ?? SYSTEM) as ColorSchemePreference,
5754
}));
5855
/** Sync the tabs */
5956
const storageListener = (e: StorageEvent): void => {
@@ -83,10 +80,10 @@ export const Core = ({ t }: CoreProps) => {
8380
});
8481
restoreTransitions();
8582
// System mode is decided by current system state and need not be stored in localStorage
86-
localStorage.setItem(COOKIE_KEY, mode);
83+
localStorageMinify?.setItem(COOKIE_KEY, mode);
8784
if (serverTargetEl)
88-
documentMinify.cookie = `${COOKIE_KEY}=${resolvedMode};max-age=${MAX_AGE};SameSite=Strict;`;
89-
}, [resolvedMode, systemMode, mode]);
85+
documentMinify.cookie = `${COOKIE_KEY}=${resolvedMode};max-age=31536000;SameSite=Strict;`;
86+
}, [resolvedMode, systemMode, mode, t]);
9087

9188
return null;
9289
};

lib/src/client/switch/switch.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ export interface SwitchProps extends HTMLProps<HTMLElement> {
1313
}
1414

1515
/**
16-
*
16+
* Switch button to quickly toggle user preference.
1717
*
1818
* @example
1919
* ```tsx
20-
* <Switch />
20+
* <Switch [size={20} skipSystem]/>
2121
* ```
2222
*
2323
* @source - Source code

lib/src/constants.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { ColorSchemePreference } from "./utils";
33
export const SYSTEM: ColorSchemePreference = "system";
44
export const DARK: ColorSchemePreference = "dark";
55
export const LIGHT: ColorSchemePreference = "light";
6-
export const MAX_AGE = 31536000;
76

87
export const COOKIE_KEY = "gx";
98
export const modes: ColorSchemePreference[] = [SYSTEM, DARK, LIGHT];

lib/src/hooks/use-mode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ export interface UseModeYeild {
99
}
1010

1111
/**
12-
*
12+
* use this hook to gain access to mode state and setter.
1313
*
1414
* @example
1515
* ```tsx
16-
* const [] = useMode(options);
16+
* const {resolvedMode, setMode} = useMode(options);
1717
* ```
1818
*
1919
* @source - Source code

lib/src/server/server-target/server-target.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { COOKIE_KEY, LIGHT } from "../../constants";
33

44
/**
55
* Server Side target for avoiding flash of un-themed content.
6+
*
67
* @example
78
* ```tsx
89
* <html>
@@ -13,6 +14,8 @@ import { COOKIE_KEY, LIGHT } from "../../constants";
1314
* </body>
1415
* </html>
1516
* ```
17+
*
18+
* @source - Source code
1619
*/
1720
export const ServerTarget = () => {
1821
const rm = cookies().get(COOKIE_KEY)?.value ?? LIGHT;

packages/shared/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# @repo/shared
2+
3+
## 0.0.1
4+
5+
### Patch Changes
6+
7+
- Updated dependencies
8+

0 commit comments

Comments
 (0)