@codefast/theme 0.3.16-canary.3 → 0.4.0-canary.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/README.md +2 -7
- package/package.json +8 -8
- package/src/adapters/tanstack-start/server.ts +3 -7
- package/src/core/provider.tsx +4 -17
- package/src/core/use-theme.ts +1 -2
- package/src/script/theme-script.tsx +3 -16
- package/src/utils/dom.ts +1 -3
- package/src/utils/system.ts +1 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @codefast/theme
|
|
2
2
|
|
|
3
|
+
## 0.4.0-canary.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#495](https://github.com/codefastlabs/codefast/pull/495) [`fa338d6`](https://github.com/codefastlabs/codefast/commit/fa338d61fbfafb94beaa4d05d93d01e2c005cc91) Thanks [@thevuong](https://github.com/thevuong)! - Normalize import statement order and package.json key order repo-wide via the new oxfmt `sortImports`/`sortPackageJson` settings — purely mechanical, no runtime behavior change.
|
|
8
|
+
|
|
3
9
|
## 0.3.16-canary.3
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -94,11 +94,7 @@ function App() {
|
|
|
94
94
|
function Page() {
|
|
95
95
|
const { theme, setTheme } = useTheme();
|
|
96
96
|
|
|
97
|
-
return (
|
|
98
|
-
<button onClick={() => setTheme(theme === "dark" ? "light" : "dark")}>
|
|
99
|
-
Current theme: {theme}
|
|
100
|
-
</button>
|
|
101
|
-
);
|
|
97
|
+
return <button onClick={() => setTheme(theme === "dark" ? "light" : "dark")}>Current theme: {theme}</button>;
|
|
102
98
|
}
|
|
103
99
|
```
|
|
104
100
|
|
|
@@ -166,8 +162,7 @@ For apps without a server framework, persist the theme in `localStorage`:
|
|
|
166
162
|
import { ThemeProvider } from "@codefast/theme";
|
|
167
163
|
|
|
168
164
|
function App() {
|
|
169
|
-
const saved =
|
|
170
|
-
typeof window !== "undefined" ? (localStorage.getItem("theme") ?? "system") : "system";
|
|
165
|
+
const saved = typeof window !== "undefined" ? (localStorage.getItem("theme") ?? "system") : "system";
|
|
171
166
|
|
|
172
167
|
return (
|
|
173
168
|
<ThemeProvider
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codefast/theme",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0-canary.4",
|
|
4
4
|
"description": "Theme management with React 19 features - optimistic updates, cross-tab sync, and SSR support",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dark-mode",
|
|
@@ -115,20 +115,20 @@
|
|
|
115
115
|
"zod": "^4.4.3"
|
|
116
116
|
},
|
|
117
117
|
"devDependencies": {
|
|
118
|
-
"@tanstack/react-start": "^1.168.
|
|
118
|
+
"@tanstack/react-start": "^1.168.25",
|
|
119
119
|
"@testing-library/dom": "^10.4.1",
|
|
120
120
|
"@testing-library/jest-dom": "^6.9.1",
|
|
121
121
|
"@testing-library/react": "^16.3.2",
|
|
122
122
|
"@testing-library/user-event": "^14.6.1",
|
|
123
|
-
"@types/node": "^25.9.
|
|
124
|
-
"@typescript/native-preview": "7.0.0-dev.
|
|
123
|
+
"@types/node": "^25.9.2",
|
|
124
|
+
"@typescript/native-preview": "7.0.0-dev.20260604.1",
|
|
125
125
|
"@vitejs/plugin-react": "^6.0.2",
|
|
126
|
-
"@vitest/coverage-v8": "^4.1.
|
|
126
|
+
"@vitest/coverage-v8": "^4.1.8",
|
|
127
127
|
"jsdom": "^29.1.1",
|
|
128
|
-
"tsdown": "^0.22.
|
|
128
|
+
"tsdown": "^0.22.2",
|
|
129
129
|
"typescript": "^6.0.3",
|
|
130
|
-
"vitest": "^4.1.
|
|
131
|
-
"@codefast/typescript-config": "0.
|
|
130
|
+
"vitest": "^4.1.8",
|
|
131
|
+
"@codefast/typescript-config": "0.4.0-canary.4"
|
|
132
132
|
},
|
|
133
133
|
"peerDependencies": {
|
|
134
134
|
"@types/react": ">=19.0.0",
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { createServerFn } from "@tanstack/react-start";
|
|
2
2
|
import { getCookie, getRequestHeader, setCookie } from "@tanstack/react-start/server";
|
|
3
3
|
|
|
4
|
+
import { DEFAULT_RESOLVED_COLOR_SCHEME, DEFAULT_COLOR_SCHEME, STORAGE_KEY } from "#/constants";
|
|
4
5
|
import type { ResolvedColorScheme, ColorScheme } from "#/types";
|
|
5
6
|
import { colorSchemeSchema } from "#/types";
|
|
6
7
|
|
|
7
|
-
import { DEFAULT_RESOLVED_COLOR_SCHEME, DEFAULT_COLOR_SCHEME, STORAGE_KEY } from "#/constants";
|
|
8
|
-
|
|
9
8
|
/* -----------------------------------------------------------------------------
|
|
10
9
|
* Request helpers (one round-trip when composed)
|
|
11
10
|
* -------------------------------------------------------------------------- */
|
|
@@ -22,8 +21,7 @@ function readColorSchemeFromCookie(): ColorScheme {
|
|
|
22
21
|
}
|
|
23
22
|
|
|
24
23
|
function readSsrColorSchemeFromHeaders(): ResolvedColorScheme {
|
|
25
|
-
const hint =
|
|
26
|
-
getRequestHeader("Sec-CH-Prefers-Color-Scheme") ?? getRequestHeader("Prefers-Color-Scheme");
|
|
24
|
+
const hint = getRequestHeader("Sec-CH-Prefers-Color-Scheme") ?? getRequestHeader("Prefers-Color-Scheme");
|
|
27
25
|
|
|
28
26
|
if (hint === "dark") {
|
|
29
27
|
return "dark";
|
|
@@ -91,9 +89,7 @@ export const getRootColorSchemeServerFn = createServerFn().handler(
|
|
|
91
89
|
*
|
|
92
90
|
* @since 0.3.16-canary.0
|
|
93
91
|
*/
|
|
94
|
-
export const getColorSchemeServerFn = createServerFn().handler(
|
|
95
|
-
(): ColorScheme => readColorSchemeFromCookie(),
|
|
96
|
-
);
|
|
92
|
+
export const getColorSchemeServerFn = createServerFn().handler((): ColorScheme => readColorSchemeFromCookie());
|
|
97
93
|
|
|
98
94
|
/**
|
|
99
95
|
* Resolved OS color scheme for SSR / hydration, from Client Hints when the browser sends them.
|
package/src/core/provider.tsx
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { JSX, ReactNode } from "react";
|
|
2
|
-
|
|
3
2
|
import {
|
|
4
3
|
startTransition,
|
|
5
4
|
useCallback,
|
|
@@ -12,16 +11,10 @@ import {
|
|
|
12
11
|
useSyncExternalStore,
|
|
13
12
|
} from "react";
|
|
14
13
|
|
|
14
|
+
import { DEFAULT_RESOLVED_COLOR_SCHEME, DEFAULT_COLOR_SCHEME, MEDIA, SYNC_CHANNEL } from "#/constants";
|
|
15
|
+
import { ColorSchemeContext } from "#/core/context";
|
|
15
16
|
import type { ResolvedColorScheme, ColorScheme, ColorSchemeContextType } from "#/types";
|
|
16
|
-
|
|
17
|
-
import {
|
|
18
|
-
DEFAULT_RESOLVED_COLOR_SCHEME,
|
|
19
|
-
DEFAULT_COLOR_SCHEME,
|
|
20
|
-
MEDIA,
|
|
21
|
-
SYNC_CHANNEL,
|
|
22
|
-
} from "#/constants";
|
|
23
17
|
import { colorSchemeSchema } from "#/types";
|
|
24
|
-
import { ColorSchemeContext } from "#/core/context";
|
|
25
18
|
import { applyColorScheme, suppressTransitions } from "#/utils/dom";
|
|
26
19
|
import { getSystemColorScheme } from "#/utils/system";
|
|
27
20
|
|
|
@@ -200,9 +193,7 @@ export function AppearanceProvider({
|
|
|
200
193
|
// C1: Re-sync state when the server (loader) provides a new color scheme value after mount
|
|
201
194
|
useEffect(() => {
|
|
202
195
|
startTransition(() => {
|
|
203
|
-
setColorSchemeState((prev) =>
|
|
204
|
-
prev === safeInitialColorScheme ? prev : safeInitialColorScheme,
|
|
205
|
-
);
|
|
196
|
+
setColorSchemeState((prev) => (prev === safeInitialColorScheme ? prev : safeInitialColorScheme));
|
|
206
197
|
});
|
|
207
198
|
}, [safeInitialColorScheme]);
|
|
208
199
|
|
|
@@ -245,11 +236,7 @@ export function AppearanceProvider({
|
|
|
245
236
|
}, [ssrColorScheme]);
|
|
246
237
|
|
|
247
238
|
// Subscribe to OS preference changes (SSR-safe via useSyncExternalStore)
|
|
248
|
-
const systemColorScheme = useSyncExternalStore(
|
|
249
|
-
subscribeToSystemColorScheme,
|
|
250
|
-
getSystemColorScheme,
|
|
251
|
-
getServerSnapshot,
|
|
252
|
-
);
|
|
239
|
+
const systemColorScheme = useSyncExternalStore(subscribeToSystemColorScheme, getSystemColorScheme, getServerSnapshot);
|
|
253
240
|
|
|
254
241
|
// Compute the actual color scheme to apply: resolve 'automatic' to light/dark
|
|
255
242
|
const resolvedColorScheme = useMemo<ResolvedColorScheme>(
|
package/src/core/use-theme.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { use } from "react";
|
|
2
2
|
|
|
3
|
-
import type { ColorSchemeContextType } from "#/types";
|
|
4
|
-
|
|
5
3
|
import { ColorSchemeContext } from "#/core/context";
|
|
4
|
+
import type { ColorSchemeContextType } from "#/types";
|
|
6
5
|
|
|
7
6
|
/* -----------------------------------------------------------------------------
|
|
8
7
|
* Hook
|
|
@@ -7,10 +7,7 @@ import type { ColorScheme } from "#/types";
|
|
|
7
7
|
/* JSON.stringify does not escape </script>, so we replace the three characters that
|
|
8
8
|
* can form a closing tag: < > /. Unicode escapes are valid JS and safe in HTML. */
|
|
9
9
|
function toScriptSafe(value: unknown): string {
|
|
10
|
-
return JSON.stringify(value)
|
|
11
|
-
.replaceAll("<", "\\u003C")
|
|
12
|
-
.replaceAll(">", "\\u003E")
|
|
13
|
-
.replaceAll("/", "\\u002F");
|
|
10
|
+
return JSON.stringify(value).replaceAll("<", "\\u003C").replaceAll(">", "\\u003E").replaceAll("/", "\\u002F");
|
|
14
11
|
}
|
|
15
12
|
|
|
16
13
|
/* Derived once from schema + constants — inline script uses these to stay in sync */
|
|
@@ -81,11 +78,7 @@ export type AppearanceScriptProps = {
|
|
|
81
78
|
*
|
|
82
79
|
* @since 0.3.16-canary.0
|
|
83
80
|
*/
|
|
84
|
-
export function AppearanceScript({
|
|
85
|
-
nonce,
|
|
86
|
-
storageKey,
|
|
87
|
-
colorScheme,
|
|
88
|
-
}: AppearanceScriptProps): JSX.Element {
|
|
81
|
+
export function AppearanceScript({ nonce, storageKey, colorScheme }: AppearanceScriptProps): JSX.Element {
|
|
89
82
|
// S2: toScriptSafe = JSON.stringify + escape <>/ so </script> cannot break out of the tag.
|
|
90
83
|
// F1: When storageKey is provided, the script reads localStorage before first paint so
|
|
91
84
|
// client-only apps (no SSR cookie) get the right color scheme without FOUC.
|
|
@@ -93,11 +86,5 @@ export function AppearanceScript({
|
|
|
93
86
|
const appearanceScript = `(function(){try{var sk=${toScriptSafe(storageKey ?? null)},fbt=${toScriptSafe(colorScheme)},s=sk&&localStorage.getItem(sk),theme=(${COLOR_SCHEME_VALID_CHECK})?s:fbt,resolvedTheme=theme;"automatic"===theme&&(resolvedTheme=window.matchMedia(${toScriptSafe(MEDIA)}).matches?"dark":"light"),document.documentElement.classList.remove(${COLOR_SCHEME_REMOVE_ARGS}),document.documentElement.classList.add(resolvedTheme),document.documentElement.style.colorScheme=resolvedTheme}catch(e){}})()`;
|
|
94
87
|
const nonceProps = nonce === undefined ? {} : { nonce };
|
|
95
88
|
|
|
96
|
-
return
|
|
97
|
-
<script
|
|
98
|
-
dangerouslySetInnerHTML={{ __html: appearanceScript }}
|
|
99
|
-
suppressHydrationWarning
|
|
100
|
-
{...nonceProps}
|
|
101
|
-
/>
|
|
102
|
-
);
|
|
89
|
+
return <script dangerouslySetInnerHTML={{ __html: appearanceScript }} suppressHydrationWarning {...nonceProps} />;
|
|
103
90
|
}
|
package/src/utils/dom.ts
CHANGED
|
@@ -49,9 +49,7 @@ export function suppressTransitions(nonce?: string): () => void {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
// Respect user's motion preferences
|
|
52
|
-
const prefersReducedMotion = globalThis.window.matchMedia(
|
|
53
|
-
"(prefers-reduced-motion: reduce)",
|
|
54
|
-
).matches;
|
|
52
|
+
const prefersReducedMotion = globalThis.window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
55
53
|
|
|
56
54
|
if (prefersReducedMotion) {
|
|
57
55
|
return () => {
|
package/src/utils/system.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type { ResolvedColorScheme, ColorScheme } from "#/types";
|
|
2
|
-
|
|
3
1
|
import { DEFAULT_RESOLVED_COLOR_SCHEME, MEDIA } from "#/constants";
|
|
2
|
+
import type { ResolvedColorScheme, ColorScheme } from "#/types";
|
|
4
3
|
|
|
5
4
|
/* -----------------------------------------------------------------------------
|
|
6
5
|
* System Color Scheme Detection
|