Skip to content

Commit 06eae80

Browse files
committed
touch up
1 parent 2ba7087 commit 06eae80

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

lib/nthul/src/client/theme-switcher/theme-switcher.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,17 @@ function modifyTransition(themeTransition = "none") {
7979
export interface UpdateDOMProps {
8080
targetId?: string;
8181
themeState: ThemeState;
82+
dontSync?: boolean;
8283
}
8384

84-
function updateDOM({ targetId, themeState }: UpdateDOMProps) {
85+
function updateDOM({ targetId, themeState, dontSync }: UpdateDOMProps) {
8586
const { theme, colorSchemePreference: csp, systemColorScheme: scs } = themeState;
8687
const resolvedColorScheme = csp === "system" ? scs : csp;
88+
const key = targetId ?? DEFAULT_ID;
8789
// update DOM
8890
let shoulCreateCookie = false;
89-
const target = document.getElementById(targetId ?? DEFAULT_ID);
90-
shoulCreateCookie = target?.getAttribute("data-nth") === "next";
91+
const target = document.getElementById(key);
92+
shoulCreateCookie = !dontSync && target?.getAttribute("data-nth") === "next";
9193

9294
/** do not update documentElement for local targets */
9395
const targets = targetId ? [target] : [target, document.documentElement];
@@ -102,7 +104,8 @@ function updateDOM({ targetId, themeState }: UpdateDOMProps) {
102104
t?.classList.add(resolvedColorScheme);
103105
});
104106

105-
return shoulCreateCookie;
107+
if (shoulCreateCookie)
108+
document.cookie = `${key}=${theme},${resolvedColorScheme}; max-age=31536000; SameSite=Strict;`;
106109
}
107110

108111
export function ThemeSwitcher({ targetId, dontSync, themeTransition }: ThemeSwitcherProps) {
@@ -116,16 +119,13 @@ export function ThemeSwitcher({ targetId, dontSync, themeTransition }: ThemeSwit
116119
/** update DOM and storage */
117120
React.useEffect(() => {
118121
const restoreTransitions = modifyTransition(themeTransition);
119-
const shoulCreateCookie = updateDOM({ targetId, themeState });
122+
updateDOM({ targetId, themeState, dontSync });
120123
if (!dontSync) {
121124
// save to localStorage
122125
const { theme, colorSchemePreference: csp, systemColorScheme: scs } = themeState;
123126
const stateToSave = [theme, csp, scs].join(",");
124127
const key = targetId ?? DEFAULT_ID;
125128
localStorage.setItem(key, stateToSave);
126-
if (shoulCreateCookie) {
127-
document.cookie = `${key}=${stateToSave}; max-age=31536000; SameSite=Strict;`;
128-
}
129129
}
130130
restoreTransitions();
131131
}, [dontSync, targetId, themeState, themeTransition]);
Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as React from "react";
22
import { cookies } from "next/headers";
33
import { DEFAULT_ID } from "../../constants";
4-
import type { ColorSchemePreference } from "../../hooks/use-theme";
54

65
interface ServerTargetProps {
76
tag?: keyof JSX.IntrinsicElements;
@@ -10,21 +9,18 @@ interface ServerTargetProps {
109

1110
/**
1211
* # ServerTarget
13-
* --todo
12+
* --todo
1413
* update comments
1514
* create colorswitch
1615
* update examples
1716
*/
1817
export function ServerTarget({ tag, targetId }: ServerTargetProps) {
1918
const key = targetId || DEFAULT_ID;
20-
const [theme, csp, scs] = (cookies().get(key)?.value ?? ",system,light").split(",") as [
21-
string,
22-
ColorSchemePreference,
23-
"dark" | "light",
24-
];
19+
const val = cookies().get(key)?.value ?? ",light";
20+
const [theme, cs] = val.split(",") as [string, "dark" | "light"];
2521

26-
const cls = `th-${theme} ${csp === "system" ? scs : csp}`;
22+
const cls = `th-${theme} ${cs}`;
2723

2824
const Tag = tag ?? "div";
29-
return <Tag className={cls} data-nth="next" id={targetId ?? DEFAULT_ID} />;
25+
return <Tag className={cls} data-nth="next" id={key} />;
3026
}

lib/nthul/touchup.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ fs.writeFileSync(
4646
JSON.stringify(packageJson, null, 2),
4747
);
4848

49-
fs.copyFileSync(path.resolve(__dirname, "..", "..", "README.md"), path.resolve(__dirname, "dist", "README.md"));
49+
fs.copyFileSync(
50+
path.resolve(__dirname, "..", "..", "README.md"),
51+
path.resolve(__dirname, "dist", "README.md"),
52+
);
5053

5154
const dirs = [path.resolve(__dirname, "dist")];
5255

0 commit comments

Comments
 (0)