@codefast/theme 0.3.13-canary.2 → 0.3.13-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.
Files changed (58) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/adapters/tanstack-start/{server.d.ts → server.d.mts} +25 -20
  3. package/dist/adapters/tanstack-start/server.mjs +83 -0
  4. package/dist/{constants.d.ts → constants.d.mts} +10 -7
  5. package/dist/constants.mjs +33 -0
  6. package/dist/core/{context.d.ts → context.d.mts} +7 -4
  7. package/dist/core/context.mjs +14 -0
  8. package/dist/core/provider.d.mts +87 -0
  9. package/dist/core/provider.mjs +145 -0
  10. package/dist/core/{use-theme.d.ts → use-theme.d.mts} +6 -3
  11. package/dist/core/use-theme.mjs +36 -0
  12. package/dist/index.d.mts +7 -0
  13. package/dist/index.mjs +7 -0
  14. package/dist/script/{theme-script.d.ts → theme-script.d.mts} +13 -9
  15. package/dist/script/theme-script.mjs +30 -0
  16. package/dist/start.d.mts +2 -0
  17. package/dist/start.mjs +2 -0
  18. package/dist/{types.d.ts → types.d.mts} +16 -13
  19. package/dist/types.mjs +21 -0
  20. package/dist/utils/{dom.d.ts → dom.d.mts} +7 -4
  21. package/dist/utils/dom.mjs +50 -0
  22. package/dist/utils/{system.d.ts → system.d.mts} +7 -4
  23. package/dist/utils/system.mjs +36 -0
  24. package/package.json +37 -53
  25. package/dist/adapters/tanstack-start/server.d.ts.map +0 -1
  26. package/dist/adapters/tanstack-start/server.js +0 -97
  27. package/dist/constants.d.ts.map +0 -1
  28. package/dist/constants.js +0 -36
  29. package/dist/core/context.d.ts.map +0 -1
  30. package/dist/core/context.js +0 -11
  31. package/dist/core/index.d.ts +0 -4
  32. package/dist/core/index.d.ts.map +0 -1
  33. package/dist/core/index.js +0 -3
  34. package/dist/core/provider.d.ts +0 -77
  35. package/dist/core/provider.d.ts.map +0 -1
  36. package/dist/core/provider.js +0 -165
  37. package/dist/core/use-theme.d.ts.map +0 -1
  38. package/dist/core/use-theme.js +0 -38
  39. package/dist/index.d.ts +0 -17
  40. package/dist/index.d.ts.map +0 -1
  41. package/dist/index.js +0 -19
  42. package/dist/script/index.d.ts +0 -2
  43. package/dist/script/index.d.ts.map +0 -1
  44. package/dist/script/index.js +0 -1
  45. package/dist/script/theme-script.d.ts.map +0 -1
  46. package/dist/script/theme-script.js +0 -32
  47. package/dist/start.d.ts +0 -47
  48. package/dist/start.d.ts.map +0 -1
  49. package/dist/start.js +0 -46
  50. package/dist/types.d.ts.map +0 -1
  51. package/dist/types.js +0 -17
  52. package/dist/utils/dom.d.ts.map +0 -1
  53. package/dist/utils/dom.js +0 -67
  54. package/dist/utils/index.d.ts +0 -3
  55. package/dist/utils/index.d.ts.map +0 -1
  56. package/dist/utils/index.js +0 -2
  57. package/dist/utils/system.d.ts.map +0 -1
  58. package/dist/utils/system.js +0 -42
package/dist/start.mjs ADDED
@@ -0,0 +1,2 @@
1
+ import { getRootThemeServerFn, getSsrSystemThemeServerFn, getThemeServerFn, persistThemeCookie, setThemeServerFn } from "./adapters/tanstack-start/server.mjs";
2
+ export { getRootThemeServerFn, getSsrSystemThemeServerFn, getThemeServerFn, persistThemeCookie, setThemeServerFn };
@@ -1,14 +1,16 @@
1
1
  import { z } from "zod";
2
+
3
+ //#region src/types.d.ts
2
4
  /**
3
5
  * Zod schema for theme validation.
4
6
  *
5
7
  * Acts as the single source of truth for all theme-related types.
6
8
  * Valid values: 'light', 'dark', 'system'.
7
9
  */
8
- export declare const themeSchema: z.ZodEnum<{
9
- dark: "dark";
10
- light: "light";
11
- system: "system";
10
+ declare const themeSchema: z.ZodEnum<{
11
+ light: "light";
12
+ dark: "dark";
13
+ system: "system";
12
14
  }>;
13
15
  /**
14
16
  * User's theme preference.
@@ -17,19 +19,19 @@ export declare const themeSchema: z.ZodEnum<{
17
19
  * - `'dark'` - Force dark mode
18
20
  * - `'system'` - Follow OS preference
19
21
  */
20
- export type Theme = z.infer<typeof themeSchema>;
22
+ type Theme = z.infer<typeof themeSchema>;
21
23
  /**
22
24
  * Actual theme applied to the UI after resolving 'system'.
23
25
  *
24
26
  * When theme is 'system', this reflects the OS preference (light/dark).
25
27
  */
26
- export type ResolvedTheme = Exclude<Theme, "system">;
28
+ type ResolvedTheme = Exclude<Theme, "system">;
27
29
  /**
28
30
  * Array of all available theme options: `['light', 'dark', 'system']`.
29
31
  *
30
32
  * Useful for rendering theme selectors in UI components.
31
33
  */
32
- export declare const themes: ("dark" | "light" | "system")[];
34
+ declare const themes: ("light" | "dark" | "system")[];
33
35
  /**
34
36
  * Shape of the theme context value provided by ThemeProvider.
35
37
  *
@@ -38,10 +40,11 @@ export declare const themes: ("dark" | "light" | "system")[];
38
40
  * - `setTheme` - Async function to update theme (triggers optimistic update)
39
41
  * - `isPending` - True while theme change is being persisted
40
42
  */
41
- export interface ThemeContextType {
42
- isPending: boolean;
43
- resolvedTheme: ResolvedTheme;
44
- setTheme: (value: Theme) => Promise<void>;
45
- theme: Theme;
43
+ interface ThemeContextType {
44
+ isPending: boolean;
45
+ resolvedTheme: ResolvedTheme;
46
+ setTheme: (value: Theme) => Promise<void>;
47
+ theme: Theme;
46
48
  }
47
- //# sourceMappingURL=types.d.ts.map
49
+ //#endregion
50
+ export { ResolvedTheme, Theme, ThemeContextType, themeSchema, themes };
package/dist/types.mjs ADDED
@@ -0,0 +1,21 @@
1
+ import { z } from "zod";
2
+ //#region src/types.ts
3
+ /**
4
+ * Zod schema for theme validation.
5
+ *
6
+ * Acts as the single source of truth for all theme-related types.
7
+ * Valid values: 'light', 'dark', 'system'.
8
+ */
9
+ const themeSchema = z.enum([
10
+ "light",
11
+ "dark",
12
+ "system"
13
+ ]);
14
+ /**
15
+ * Array of all available theme options: `['light', 'dark', 'system']`.
16
+ *
17
+ * Useful for rendering theme selectors in UI components.
18
+ */
19
+ const themes = themeSchema.options;
20
+ //#endregion
21
+ export { themeSchema, themes };
@@ -1,4 +1,6 @@
1
- import type { ResolvedTheme } from "#types";
1
+ import { ResolvedTheme } from "../types.mjs";
2
+
3
+ //#region src/utils/dom.d.ts
2
4
  /**
3
5
  * Apply theme to the DOM by updating `<html>` element.
4
6
  *
@@ -8,7 +10,7 @@ import type { ResolvedTheme } from "#types";
8
10
  *
9
11
  * @param resolved - The resolved theme to apply ('light' or 'dark')
10
12
  */
11
- export declare function applyTheme(resolved: ResolvedTheme): void;
13
+ declare function applyTheme(resolved: ResolvedTheme): void;
12
14
  /**
13
15
  * Temporarily disable all CSS transitions during theme changes.
14
16
  *
@@ -25,5 +27,6 @@ export declare function applyTheme(resolved: ResolvedTheme): void;
25
27
  * enableTransitions(); // Re-enables CSS transitions
26
28
  * ```
27
29
  */
28
- export declare function disableAnimation(nonce?: string): () => void;
29
- //# sourceMappingURL=dom.d.ts.map
30
+ declare function disableAnimation(nonce?: string): () => void;
31
+ //#endregion
32
+ export { applyTheme, disableAnimation };
@@ -0,0 +1,50 @@
1
+ //#region src/utils/dom.ts
2
+ /**
3
+ * Apply theme to the DOM by updating `<html>` element.
4
+ *
5
+ * Updates both:
6
+ * - CSS class (for Tailwind's `dark:` variants)
7
+ * - `color-scheme` style (for native form controls and scrollbars)
8
+ *
9
+ * @param resolved - The resolved theme to apply ('light' or 'dark')
10
+ */
11
+ function applyTheme(resolved) {
12
+ const root = window.document.documentElement;
13
+ root.classList.remove("light", "dark", "system");
14
+ root.classList.add(resolved);
15
+ root.style.colorScheme = resolved;
16
+ }
17
+ /**
18
+ * Temporarily disable all CSS transitions during theme changes.
19
+ *
20
+ * Prevents jarring color animations when switching between light/dark themes.
21
+ * Respects user's `prefers-reduced-motion` preference (does nothing if enabled).
22
+ *
23
+ * @param nonce - Optional CSP nonce for the injected style element
24
+ * @returns Cleanup function to re-enable transitions. Call after theme is applied.
25
+ *
26
+ * @example
27
+ * ```tsx
28
+ * const enableTransitions = disableAnimation();
29
+ * applyTheme('dark');
30
+ * enableTransitions(); // Re-enables CSS transitions
31
+ * ```
32
+ */
33
+ function disableAnimation(nonce) {
34
+ if (typeof globalThis.window === "undefined") return () => {};
35
+ if (globalThis.window.matchMedia("(prefers-reduced-motion: reduce)").matches) return () => {};
36
+ const css = document.createElement("style");
37
+ if (nonce) css.setAttribute("nonce", nonce);
38
+ css.append(document.createTextNode(`*,*::before,*::after{-webkit-transition:none!important;-moz-transition:none!important;-o-transition:none!important;-ms-transition:none!important;transition:none!important}`));
39
+ document.head.append(css);
40
+ return () => {
41
+ window.getComputedStyle(document.body);
42
+ requestAnimationFrame(() => {
43
+ requestAnimationFrame(() => {
44
+ if (css.parentNode) css.remove();
45
+ });
46
+ });
47
+ };
48
+ }
49
+ //#endregion
50
+ export { applyTheme, disableAnimation };
@@ -1,4 +1,6 @@
1
- import type { ResolvedTheme, Theme } from "#types";
1
+ import { ResolvedTheme, Theme } from "../types.mjs";
2
+
3
+ //#region src/utils/system.d.ts
2
4
  /**
3
5
  * Detect the user's OS theme preference.
4
6
  *
@@ -7,7 +9,7 @@ import type { ResolvedTheme, Theme } from "#types";
7
9
  *
8
10
  * @returns 'light' or 'dark' based on OS preference
9
11
  */
10
- export declare function getSystemTheme(): ResolvedTheme;
12
+ declare function getSystemTheme(): ResolvedTheme;
11
13
  /**
12
14
  * Resolve theme preference to actual light/dark value.
13
15
  *
@@ -23,5 +25,6 @@ export declare function getSystemTheme(): ResolvedTheme;
23
25
  * Ignored for non-`system` themes and on the client (where {@link getSystemTheme} wins).
24
26
  * @returns The resolved theme to apply (`light` or `dark`)
25
27
  */
26
- export declare function resolveTheme(theme: Theme, ssrSystemTheme?: ResolvedTheme): ResolvedTheme;
27
- //# sourceMappingURL=system.d.ts.map
28
+ declare function resolveTheme(theme: Theme, ssrSystemTheme?: ResolvedTheme): ResolvedTheme;
29
+ //#endregion
30
+ export { getSystemTheme, resolveTheme };
@@ -0,0 +1,36 @@
1
+ import { DEFAULT_RESOLVED_THEME } from "../constants.mjs";
2
+ //#region src/utils/system.ts
3
+ /**
4
+ * Detect the user's OS theme preference.
5
+ *
6
+ * Uses `matchMedia()` to query the `prefers-color-scheme` media feature.
7
+ * Returns {@link DEFAULT_RESOLVED_THEME} during SSR since `window` is unavailable.
8
+ *
9
+ * @returns 'light' or 'dark' based on OS preference
10
+ */
11
+ function getSystemTheme() {
12
+ if (typeof globalThis.window === "undefined") return DEFAULT_RESOLVED_THEME;
13
+ return globalThis.window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
14
+ }
15
+ /**
16
+ * Resolve theme preference to actual light/dark value.
17
+ *
18
+ * - 'light' → 'light'
19
+ * - 'dark' → 'dark'
20
+ * - 'system' → on the client, result of {@link getSystemTheme}; on the server,
21
+ * `ssrSystemTheme` when provided, otherwise {@link DEFAULT_RESOLVED_THEME}
22
+ *
23
+ * @param theme - User's theme preference (`light`, `dark`, or `system`)
24
+ * @param ssrSystemTheme - When `theme` is `system` and this runs during SSR (no `window`),
25
+ * uses this as the resolved appearance—typically from Client Hints
26
+ * (`Sec-CH-Prefers-Color-Scheme`) so `<html class>` matches the real OS preference.
27
+ * Ignored for non-`system` themes and on the client (where {@link getSystemTheme} wins).
28
+ * @returns The resolved theme to apply (`light` or `dark`)
29
+ */
30
+ function resolveTheme(theme, ssrSystemTheme) {
31
+ if (theme !== "system") return theme;
32
+ if (typeof globalThis.window === "undefined") return ssrSystemTheme ?? "dark";
33
+ return getSystemTheme();
34
+ }
35
+ //#endregion
36
+ export { getSystemTheme, resolveTheme };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codefast/theme",
3
- "version": "0.3.13-canary.2",
3
+ "version": "0.3.13-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",
@@ -30,13 +30,11 @@
30
30
  ],
31
31
  "type": "module",
32
32
  "sideEffects": false,
33
- "main": "./dist/index.js",
34
- "types": "./dist/index.d.ts",
33
+ "main": "./dist/index.mjs",
34
+ "module": "./dist/index.mjs",
35
+ "types": "./dist/index.d.mts",
35
36
  "imports": {
36
- "#*": [
37
- "./dist/*",
38
- "./dist/*.js",
39
- "./dist/*/index.js",
37
+ "#/*": [
40
38
  "./src/*",
41
39
  "./src/*.ts",
42
40
  "./src/*.tsx",
@@ -46,60 +44,48 @@
46
44
  },
47
45
  "exports": {
48
46
  ".": {
49
- "types": "./dist/index.d.ts",
50
- "import": "./dist/index.js"
47
+ "types": "./dist/index.d.mts",
48
+ "import": "./dist/index.mjs"
51
49
  },
52
50
  "./adapters/tanstack-start/server": {
53
- "types": "./dist/adapters/tanstack-start/server.d.ts",
54
- "import": "./dist/adapters/tanstack-start/server.js"
51
+ "types": "./dist/adapters/tanstack-start/server.d.mts",
52
+ "import": "./dist/adapters/tanstack-start/server.mjs"
55
53
  },
56
54
  "./constants": {
57
- "types": "./dist/constants.d.ts",
58
- "import": "./dist/constants.js"
59
- },
60
- "./core": {
61
- "types": "./dist/core/index.d.ts",
62
- "import": "./dist/core/index.js"
55
+ "types": "./dist/constants.d.mts",
56
+ "import": "./dist/constants.mjs"
63
57
  },
64
58
  "./core/context": {
65
- "types": "./dist/core/context.d.ts",
66
- "import": "./dist/core/context.js"
59
+ "types": "./dist/core/context.d.mts",
60
+ "import": "./dist/core/context.mjs"
67
61
  },
68
62
  "./core/provider": {
69
- "types": "./dist/core/provider.d.ts",
70
- "import": "./dist/core/provider.js"
63
+ "types": "./dist/core/provider.d.mts",
64
+ "import": "./dist/core/provider.mjs"
71
65
  },
72
66
  "./core/use-theme": {
73
- "types": "./dist/core/use-theme.d.ts",
74
- "import": "./dist/core/use-theme.js"
75
- },
76
- "./script": {
77
- "types": "./dist/script/index.d.ts",
78
- "import": "./dist/script/index.js"
67
+ "types": "./dist/core/use-theme.d.mts",
68
+ "import": "./dist/core/use-theme.mjs"
79
69
  },
80
70
  "./script/theme-script": {
81
- "types": "./dist/script/theme-script.d.ts",
82
- "import": "./dist/script/theme-script.js"
71
+ "types": "./dist/script/theme-script.d.mts",
72
+ "import": "./dist/script/theme-script.mjs"
83
73
  },
84
74
  "./start": {
85
- "types": "./dist/start.d.ts",
86
- "import": "./dist/start.js"
75
+ "types": "./dist/start.d.mts",
76
+ "import": "./dist/start.mjs"
87
77
  },
88
78
  "./types": {
89
- "types": "./dist/types.d.ts",
90
- "import": "./dist/types.js"
91
- },
92
- "./utils": {
93
- "types": "./dist/utils/index.d.ts",
94
- "import": "./dist/utils/index.js"
79
+ "types": "./dist/types.d.mts",
80
+ "import": "./dist/types.mjs"
95
81
  },
96
82
  "./utils/dom": {
97
- "types": "./dist/utils/dom.d.ts",
98
- "import": "./dist/utils/dom.js"
83
+ "types": "./dist/utils/dom.d.mts",
84
+ "import": "./dist/utils/dom.mjs"
99
85
  },
100
86
  "./utils/system": {
101
- "types": "./dist/utils/system.d.ts",
102
- "import": "./dist/utils/system.js"
87
+ "types": "./dist/utils/system.d.mts",
88
+ "import": "./dist/utils/system.mjs"
103
89
  },
104
90
  "./package.json": "./package.json"
105
91
  },
@@ -110,20 +96,19 @@
110
96
  "zod": "^4.3.6"
111
97
  },
112
98
  "devDependencies": {
113
- "@swc/core": "^1.15.24",
114
- "@swc/jest": "^0.2.39",
115
99
  "@tanstack/react-start": "^1.167.32",
116
100
  "@testing-library/dom": "^10.4.1",
117
101
  "@testing-library/jest-dom": "^6.9.1",
118
102
  "@testing-library/react": "^16.3.2",
119
103
  "@testing-library/user-event": "^14.6.1",
120
- "@types/jest": "^30.0.0",
121
104
  "@types/node": "^25.6.0",
122
105
  "@typescript/native-preview": "7.0.0-dev.20260411.1",
123
- "jest": "^30.3.0",
124
- "jest-environment-jsdom": "^30.3.0",
106
+ "@vitejs/plugin-react": "^6.0.1",
107
+ "@vitest/coverage-v8": "^4.1.4",
108
+ "jsdom": "^29.0.2",
125
109
  "typescript": "^6.0.2",
126
- "@codefast/typescript-config": "0.3.13-canary.2"
110
+ "vitest": "^4.1.4",
111
+ "@codefast/typescript-config": "0.3.13-canary.4"
127
112
  },
128
113
  "peerDependencies": {
129
114
  "@tanstack/react-start": "^1.0",
@@ -144,13 +129,12 @@
144
129
  }
145
130
  },
146
131
  "scripts": {
147
- "build": "rm -rf dist && tsgo -p tsconfig.build.json",
132
+ "build": "tsdown",
148
133
  "check-types": "tsgo --noEmit",
149
134
  "clean": "rm -rf dist",
150
- "dev": "tsgo -p tsconfig.build.json --watch --preserveWatchOutput",
151
- "test": "jest",
152
- "test:coverage": "jest --coverage",
153
- "test:coverage:ci": "jest --coverage --ci",
154
- "test:watch": "jest --watch"
135
+ "dev": "tsdown --watch",
136
+ "test": "vitest run",
137
+ "test:coverage": "vitest run --coverage",
138
+ "test:watch": "vitest"
155
139
  }
156
140
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../../src/adapters/tanstack-start/server.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAmCnD;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC;;;;;OAKG;IACH,cAAc,EAAE,aAAa,CAAC;IAC9B,8EAA8E;IAC9E,KAAK,EAAE,KAAK,CAAC;CACd;AASD;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,4FAKhC,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,gBAAgB,oGAA+D,CAAC;AAE7F;;;;;;;GAOG;AACH,eAAO,MAAM,yBAAyB,sFAErC,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,gBAAgB;;;;SAUzB,CAAC;AAEL;;;GAGG;AACH,wBAAsB,kBAAkB,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAEpE"}
@@ -1,97 +0,0 @@
1
- import { createServerFn } from "@tanstack/react-start";
2
- import { getCookie, getRequestHeader, setCookie } from "@tanstack/react-start/server";
3
- import { themeSchema } from "#types";
4
- import { DEFAULT_RESOLVED_THEME, DEFAULT_THEME, THEME_STORAGE_KEY } from "#constants";
5
- /* -----------------------------------------------------------------------------
6
- * Request helpers (one round-trip when composed)
7
- * -------------------------------------------------------------------------- */
8
- function readThemeFromCookie() {
9
- const cookieTheme = getCookie(THEME_STORAGE_KEY);
10
- const validationResult = themeSchema.safeParse(cookieTheme);
11
- if (validationResult.success) {
12
- return validationResult.data;
13
- }
14
- return DEFAULT_THEME;
15
- }
16
- function readSsrSystemThemeFromHeaders() {
17
- const hint = getRequestHeader("Sec-CH-Prefers-Color-Scheme") ?? getRequestHeader("Prefers-Color-Scheme");
18
- if (hint === "dark") {
19
- return "dark";
20
- }
21
- if (hint === "light") {
22
- return "light";
23
- }
24
- return DEFAULT_RESOLVED_THEME;
25
- }
26
- /* -----------------------------------------------------------------------------
27
- * Server Functions
28
- *
29
- * These functions run on the server and handle cookie-based theme persistence.
30
- * Cookies are httpOnly for security - the client cannot read them directly.
31
- * -------------------------------------------------------------------------- */
32
- /**
33
- * Everything needed for the root shell in **one** server call (fewer loader awaits).
34
- *
35
- * In your shell: `resolveTheme(theme, ssrSystemTheme)` on `<html>`, plus `ThemeScript` and `ThemeProvider`.
36
- */
37
- export const getRootThemeServerFn = createServerFn().handler(() => ({
38
- theme: readThemeFromCookie(),
39
- ssrSystemTheme: readSsrSystemThemeFromHeaders(),
40
- }));
41
- /**
42
- * Read the user's theme preference from cookies.
43
- *
44
- * Validates the cookie value using Zod schema for type safety.
45
- * Returns {@link DEFAULT_THEME} if cookie is missing or invalid.
46
- *
47
- * @returns User's stored theme preference or default
48
- *
49
- * @example
50
- * ```tsx
51
- * const theme = await getThemeServerFn();
52
- * ```
53
- */
54
- export const getThemeServerFn = createServerFn().handler(() => readThemeFromCookie());
55
- /**
56
- * Resolved OS theme for SSR / hydration, from Client Hints when the browser sends them.
57
- *
58
- * Mirrors client `matchMedia("(prefers-color-scheme: dark)")` when hints are present.
59
- * Prefer {@link getRootThemeServerFn} for root loaders.
60
- *
61
- * Send `Accept-CH: Sec-CH-Prefers-Color-Scheme` (and `Vary`) so navigations include the hint.
62
- */
63
- export const getSsrSystemThemeServerFn = createServerFn().handler(() => readSsrSystemThemeFromHeaders());
64
- /**
65
- * Persist theme preference to an HTTP-only cookie.
66
- *
67
- * **Security:**
68
- * - `httpOnly: true` - Cookie cannot be accessed from JavaScript (XSS protection)
69
- * - `secure: true` in production - Cookie only sent over HTTPS
70
- * - `sameSite: 'lax'` - Prevents CSRF while allowing navigation
71
- *
72
- * **Cookie Settings:**
73
- * - `maxAge: 1 year` - Preference persists across sessions
74
- * - `path: '/'` - Available to all routes
75
- *
76
- * @param data - Theme to persist ('light', 'dark', or 'system')
77
- *
78
- * @see [TanStack Start Server Functions](https://tanstack.com/start/latest/docs/framework/react/server-functions)
79
- */
80
- export const setThemeServerFn = createServerFn({ method: "POST" })
81
- .inputValidator(themeSchema)
82
- .handler(({ data }) => {
83
- setCookie(THEME_STORAGE_KEY, data, {
84
- httpOnly: true,
85
- maxAge: 60 * 60 * 24 * 365, // 1 year
86
- path: "/",
87
- sameSite: "lax",
88
- secure: process.env.NODE_ENV === "production",
89
- });
90
- });
91
- /**
92
- * Default {@link ThemeProvider} `persistTheme` for the Start adapter: writes the httpOnly
93
- * theme cookie via {@link setThemeServerFn}.
94
- */
95
- export async function persistThemeCookie(value) {
96
- await setThemeServerFn({ data: value });
97
- }
@@ -1 +0,0 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAMnD;;;;GAIG;AACH,eAAO,MAAM,aAAa,EAAE,KAAgB,CAAC;AAE7C;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,EAAE,aAAsB,CAAC;AAE5D;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,aAAa,CAAC;AAM5C;;;GAGG;AACH,eAAO,MAAM,KAAK,iCAAiC,CAAC;AAEpD;;;;;GAKG;AACH,eAAO,MAAM,aAAa,eAAe,CAAC"}
package/dist/constants.js DELETED
@@ -1,36 +0,0 @@
1
- /* -----------------------------------------------------------------------------
2
- * Public Constants
3
- * -------------------------------------------------------------------------- */
4
- /**
5
- * Default theme when no user preference exists.
6
- *
7
- * Set to 'system' to respect OS preference by default.
8
- */
9
- export const DEFAULT_THEME = "system";
10
- /**
11
- * Fallback theme for SSR when system preference cannot be detected.
12
- *
13
- * Used during server-side rendering since `window.matchMedia()` is unavailable.
14
- */
15
- export const DEFAULT_RESOLVED_THEME = "dark";
16
- /**
17
- * Cookie name for the httpOnly theme preference (`@codefast/theme/start`).
18
- *
19
- * Re-exported only from `@codefast/theme/constants` so custom middleware matches the adapter.
20
- */
21
- export const THEME_STORAGE_KEY = "ui-theme";
22
- /* -----------------------------------------------------------------------------
23
- * Internal Constants
24
- * -------------------------------------------------------------------------- */
25
- /**
26
- * CSS media query for detecting dark mode preference.
27
- * @internal
28
- */
29
- export const MEDIA = "(prefers-color-scheme: dark)";
30
- /**
31
- * BroadcastChannel name for cross-tab theme synchronization.
32
- *
33
- * Enables instant theme updates across all open browser tabs.
34
- * @internal
35
- */
36
- export const THEME_CHANNEL = "theme-sync";
@@ -1 +0,0 @@
1
- {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/core/context.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAC;AAM/C,KAAK,iBAAiB,GAAG,IAAI,GAAG,gBAAgB,CAAC;AAEjD;;;;;;;;GAQG;AACH,eAAO,MAAM,YAAY,4CAAyC,CAAC"}
@@ -1,11 +0,0 @@
1
- import { createContext } from "react";
2
- /**
3
- * React context for theme state management.
4
- *
5
- * Provides access to current theme, resolved theme, setter function, and pending state.
6
- * Use {@link useTheme} hook instead of consuming this context directly.
7
- *
8
- * @see {@link ThemeProvider} - Provider component
9
- * @see {@link useTheme} - Consumer hook
10
- */
11
- export const ThemeContext = createContext(null);
@@ -1,4 +0,0 @@
1
- export { ThemeContext } from "#core/context";
2
- export { ThemeProvider } from "#core/provider";
3
- export { useTheme } from "#core/use-theme";
4
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC"}
@@ -1,3 +0,0 @@
1
- export { ThemeContext } from "#core/context";
2
- export { ThemeProvider } from "#core/provider";
3
- export { useTheme } from "#core/use-theme";
@@ -1,77 +0,0 @@
1
- import type { JSX, ReactNode } from "react";
2
- import type { ResolvedTheme, Theme } from "#types";
3
- interface ThemeProviderProps {
4
- children: ReactNode;
5
- /**
6
- * When true, temporarily disables CSS transitions during theme changes.
7
- * Prevents jarring color animations when switching themes.
8
- * Default: false
9
- */
10
- disableTransitionOnChange?: boolean;
11
- /**
12
- * CSP nonce for inline styles when `disableTransitionOnChange` is enabled.
13
- */
14
- nonce?: string;
15
- /**
16
- * Async function to persist theme changes to server/storage.
17
- *
18
- * For TanStack Start: use `setThemeServerFn` from `@codefast/theme/start`
19
- * For Next.js: implement a server action
20
- */
21
- persistTheme?: (value: Theme) => Promise<void>;
22
- /**
23
- * Initial theme from server (typically from cookie via loader).
24
- */
25
- theme: Theme;
26
- /**
27
- * OS light/dark guess from the SSR request (e.g. `Sec-CH-Prefers-Color-Scheme`).
28
- *
29
- * When the stored preference is `system`, {@link useSyncExternalStore} uses this for
30
- * `getServerSnapshot` so the first client snapshot matches `matchMedia` and avoids a
31
- * dark → light flip after hydration.
32
- */
33
- ssrSystemTheme?: ResolvedTheme;
34
- /**
35
- * After mount, re-read the canonical theme from the server (e.g. httpOnly cookie).
36
- *
37
- * Fixes **stale HTML / disk cache** on “Duplicate tab” or back-forward cache where the
38
- * document was saved with an old `ThemeScript` / loader value while the cookie was already updated
39
- * in another tab. Use a stable reference (e.g. `getThemeServerFn` from `@codefast/theme/start`).
40
- */
41
- syncThemeFromServer?: () => Promise<Theme>;
42
- }
43
- /**
44
- * Provider component for theme state management.
45
- *
46
- * **React 19 Features Used:**
47
- * - `useOptimistic` - Immediate UI feedback while persisting theme
48
- * - `useSyncExternalStore` - SSR-safe subscription to OS theme preference
49
- * - `useEffectEvent` - Stable callback for cross-tab sync without effect re-runs
50
- *
51
- * @param props - Component props
52
- * @param props.children - React tree to provide context for
53
- * @param props.theme - Initial preference from the server (cookie, loader, etc.)
54
- * @param props.ssrSystemTheme - Best-effort `light` or `dark` from the SSR request
55
- * (e.g. `Sec-CH-Prefers-Color-Scheme`). When preference is `system`, this value is used as
56
- * `useSyncExternalStore`'s server snapshot so the first client snapshot matches `matchMedia`
57
- * and avoids a post-hydration flip. Omit if unavailable; falls back to {@link DEFAULT_RESOLVED_THEME}.
58
- * @param props.syncThemeFromServer - Optional one-shot RPC after mount to align with the cookie when SSR/HTML was stale.
59
- * @param props.persistTheme - Optional async handler to persist preference (cookie, server action, …)
60
- * @param props.disableTransitionOnChange - Temporarily disable CSS transitions on theme change
61
- * @param props.nonce - CSP nonce for inline transition-blocking styles when `disableTransitionOnChange` is set
62
- * @returns Provider element wrapping `children`
63
- *
64
- * @example
65
- * ```tsx
66
- * // In your root layout
67
- * <ThemeProvider
68
- * theme={theme}
69
- * persistTheme={(value) => setThemeServerFn({ data: value })}
70
- * >
71
- * {children}
72
- * </ThemeProvider>
73
- * ```
74
- */
75
- export declare function ThemeProvider({ children, disableTransitionOnChange, nonce, persistTheme, ssrSystemTheme, syncThemeFromServer, theme: initialTheme }: ThemeProviderProps): JSX.Element;
76
- export {};
77
- //# sourceMappingURL=provider.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../src/core/provider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAc5C,OAAO,KAAK,EAAE,aAAa,EAAE,KAAK,EAAoB,MAAM,QAAQ,CAAC;AAwCrE,UAAU,kBAAkB;IAC1B,QAAQ,EAAE,SAAS,CAAC;IACpB;;;;OAIG;IACH,yBAAyB,CAAC,EAAE,OAAO,CAAC;IACpC;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;;OAKG;IACH,YAAY,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC/C;;OAEG;IACH,KAAK,EAAE,KAAK,CAAC;IACb;;;;;;OAMG;IACH,cAAc,CAAC,EAAE,aAAa,CAAC;IAC/B;;;;;;OAMG;IACH,mBAAmB,CAAC,EAAE,MAAM,OAAO,CAAC,KAAK,CAAC,CAAC;CAC5C;AAMD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,aAAa,CAAC,EAC5B,QAAQ,EACR,yBAAiC,EACjC,KAAK,EACL,YAAY,EACZ,cAAc,EACd,mBAAmB,EACnB,KAAK,EAAE,YAAY,EACpB,EAAE,kBAAkB,GAAG,GAAG,CAAC,OAAO,CA4IlC"}