@codefast/theme 0.3.11-canary.2 → 0.3.12-canary.0

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 (47) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/README.md +56 -27
  3. package/dist/adapters/tanstack-start/server.d.ts +41 -13
  4. package/dist/adapters/tanstack-start/server.d.ts.map +1 -0
  5. package/dist/adapters/tanstack-start/server.js +91 -64
  6. package/dist/constants.d.ts +9 -12
  7. package/dist/constants.d.ts.map +1 -0
  8. package/dist/constants.js +31 -28
  9. package/dist/core/context.d.ts +4 -7
  10. package/dist/core/context.d.ts.map +1 -0
  11. package/dist/core/context.js +9 -12
  12. package/dist/core/index.d.ts +4 -4
  13. package/dist/core/index.d.ts.map +1 -0
  14. package/dist/core/index.js +3 -4
  15. package/dist/core/provider.d.ts +56 -35
  16. package/dist/core/provider.d.ts.map +1 -0
  17. package/dist/core/provider.js +155 -109
  18. package/dist/core/use-theme.d.ts +3 -6
  19. package/dist/core/use-theme.d.ts.map +1 -0
  20. package/dist/core/use-theme.js +35 -33
  21. package/dist/index.d.ts +17 -9
  22. package/dist/index.d.ts.map +1 -0
  23. package/dist/index.js +19 -9
  24. package/dist/script/index.d.ts +2 -2
  25. package/dist/script/index.d.ts.map +1 -0
  26. package/dist/script/index.js +1 -2
  27. package/dist/script/theme-script.d.ts +12 -15
  28. package/dist/script/theme-script.d.ts.map +1 -0
  29. package/dist/script/theme-script.js +30 -27
  30. package/dist/start.d.ts +47 -0
  31. package/dist/start.d.ts.map +1 -0
  32. package/dist/start.js +46 -0
  33. package/dist/types.d.ts +13 -16
  34. package/dist/types.d.ts.map +1 -0
  35. package/dist/types.js +14 -18
  36. package/dist/utils/dom.d.ts +4 -7
  37. package/dist/utils/dom.d.ts.map +1 -0
  38. package/dist/utils/dom.js +62 -46
  39. package/dist/utils/index.d.ts +3 -3
  40. package/dist/utils/index.d.ts.map +1 -0
  41. package/dist/utils/index.js +2 -3
  42. package/dist/utils/system.d.ts +12 -10
  43. package/dist/utils/system.d.ts.map +1 -0
  44. package/dist/utils/system.js +38 -25
  45. package/package.json +17 -11
  46. package/dist/adapters/tanstack-start/index.d.ts +0 -2
  47. package/dist/adapters/tanstack-start/index.js +0 -2
@@ -1,29 +1,32 @@
1
- import { jsx } from "react/jsx-runtime";
2
- //#region src/script/theme-script.tsx
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ /* -----------------------------------------------------------------------------
3
+ * Component
4
+ * -------------------------------------------------------------------------- */
3
5
  /**
4
- * Inline script that prevents Flash of Unstyled Content (FOUC).
5
- *
6
- * **Why this is needed:**
7
- * React hydration occurs after the browser has already painted the page.
8
- * Without this script, users would briefly see the wrong theme before
9
- * React takes over and applies the correct one.
10
- *
11
- * **How it works:**
12
- * This script runs synchronously in the `<head>` before first paint:
13
- * 1. Resolves 'system' to 'light' or 'dark' using `matchMedia()`
14
- * 2. Adds the theme class to `<html>` immediately
15
- * 3. Sets `color-scheme` for native form controls and scrollbars
16
- *
17
- * @example
18
- * ```tsx
19
- * // In __root.tsx (TanStack Start)
20
- * <head>
21
- * <ThemeScript theme={theme} />
22
- * </head>
23
- * ```
24
- */
25
- function ThemeScript({ theme }) {
26
- return /* @__PURE__ */ jsx("script", { dangerouslySetInnerHTML: { __html: `(function(){try{var theme="${theme}",resolvedTheme=theme;"system"===theme&&(resolvedTheme=window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"),document.documentElement.classList.add(resolvedTheme),document.documentElement.style.colorScheme=resolvedTheme}catch(error){}})()` } });
6
+ * Inline script that prevents Flash of Unstyled Content (FOUC).
7
+ *
8
+ * **Why this is needed:**
9
+ * React hydration occurs after the browser has already painted the page.
10
+ * Without this script, users would briefly see the wrong theme before
11
+ * React takes over and applies the correct one.
12
+ *
13
+ * **How it works:**
14
+ * This script runs synchronously in the `<head>` before first paint:
15
+ * 1. Resolves 'system' to 'light' or 'dark' using `matchMedia()`
16
+ * 2. Removes prior `light` / `dark` / `system` classes on `<html>` (SSR may
17
+ * have applied the wrong resolved class for `system`, e.g. default `dark`)
18
+ * 3. Adds the resolved theme class and sets `color-scheme` for native controls
19
+ *
20
+ * @example
21
+ * ```tsx
22
+ * // In __root.tsx (TanStack Start)
23
+ * <head>
24
+ * <ThemeScript theme={theme} />
25
+ * </head>
26
+ * ```
27
+ */
28
+ export function ThemeScript({ theme }) {
29
+ // Minified FOUC prevention script
30
+ const themeScript = `(function(){try{var theme="${theme}",resolvedTheme=theme;"system"===theme&&(resolvedTheme=window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"),document.documentElement.classList.remove("light","dark","system"),document.documentElement.classList.add(resolvedTheme),document.documentElement.style.colorScheme=resolvedTheme}catch(error){}})()`;
31
+ return _jsx("script", { dangerouslySetInnerHTML: { __html: themeScript } });
27
32
  }
28
- //#endregion
29
- export { ThemeScript };
@@ -0,0 +1,47 @@
1
+ /**
2
+ * TanStack Start integration: HTTP-only theme cookie + server functions.
3
+ *
4
+ * Peer: `@tanstack/react-start`. The app keeps full control of `<html>`, `<head>`, and `<body>`.
5
+ * Use {@link getRootThemeServerFn} in the root loader, then wire `@codefast/theme` primitives
6
+ * (`ThemeProvider`, `ThemeScript`, `resolveTheme`).
7
+ *
8
+ * @example
9
+ * ```tsx
10
+ * // __root.tsx
11
+ * import { HeadContent, Scripts, createRootRouteWithContext } from "@tanstack/react-router";
12
+ * import { getRootThemeServerFn, getThemeServerFn, persistThemeCookie } from "@codefast/theme/start";
13
+ * import { ThemeProvider, ThemeScript, resolveTheme } from "@codefast/theme";
14
+ *
15
+ * export const Route = createRootRouteWithContext<RootRouterContext>()({
16
+ * loader: async () => getRootThemeServerFn(),
17
+ * shellComponent: RootShellComponent,
18
+ * });
19
+ *
20
+ * function RootShellComponent({ children }: PropsWithChildren) {
21
+ * const { theme, ssrSystemTheme } = Route.useLoaderData();
22
+ * const resolvedTheme = resolveTheme(theme, ssrSystemTheme);
23
+ *
24
+ * return (
25
+ * <html className={resolvedTheme} lang="en" style={{ colorScheme: resolvedTheme }} suppressHydrationWarning>
26
+ * <head>
27
+ * <HeadContent />
28
+ * <ThemeScript theme={theme} />
29
+ * </head>
30
+ * <body>
31
+ * <ThemeProvider
32
+ * theme={theme}
33
+ * ssrSystemTheme={ssrSystemTheme}
34
+ * persistTheme={persistThemeCookie}
35
+ * syncThemeFromServer={getThemeServerFn}
36
+ * >
37
+ * {children}
38
+ * </ThemeProvider>
39
+ * <Scripts />
40
+ * </body>
41
+ * </html>
42
+ * );
43
+ * }
44
+ * ```
45
+ */
46
+ export { getRootThemeServerFn, getSsrSystemThemeServerFn, getThemeServerFn, persistThemeCookie, setThemeServerFn, type RootThemeLoaderData, } from "#adapters/tanstack-start/server";
47
+ //# sourceMappingURL=start.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"start.d.ts","sourceRoot":"","sources":["../src/start.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AAEH,OAAO,EACL,oBAAoB,EACpB,yBAAyB,EACzB,gBAAgB,EAChB,kBAAkB,EAClB,gBAAgB,EAChB,KAAK,mBAAmB,GACzB,MAAM,iCAAiC,CAAC"}
package/dist/start.js ADDED
@@ -0,0 +1,46 @@
1
+ /**
2
+ * TanStack Start integration: HTTP-only theme cookie + server functions.
3
+ *
4
+ * Peer: `@tanstack/react-start`. The app keeps full control of `<html>`, `<head>`, and `<body>`.
5
+ * Use {@link getRootThemeServerFn} in the root loader, then wire `@codefast/theme` primitives
6
+ * (`ThemeProvider`, `ThemeScript`, `resolveTheme`).
7
+ *
8
+ * @example
9
+ * ```tsx
10
+ * // __root.tsx
11
+ * import { HeadContent, Scripts, createRootRouteWithContext } from "@tanstack/react-router";
12
+ * import { getRootThemeServerFn, getThemeServerFn, persistThemeCookie } from "@codefast/theme/start";
13
+ * import { ThemeProvider, ThemeScript, resolveTheme } from "@codefast/theme";
14
+ *
15
+ * export const Route = createRootRouteWithContext<RootRouterContext>()({
16
+ * loader: async () => getRootThemeServerFn(),
17
+ * shellComponent: RootShellComponent,
18
+ * });
19
+ *
20
+ * function RootShellComponent({ children }: PropsWithChildren) {
21
+ * const { theme, ssrSystemTheme } = Route.useLoaderData();
22
+ * const resolvedTheme = resolveTheme(theme, ssrSystemTheme);
23
+ *
24
+ * return (
25
+ * <html className={resolvedTheme} lang="en" style={{ colorScheme: resolvedTheme }} suppressHydrationWarning>
26
+ * <head>
27
+ * <HeadContent />
28
+ * <ThemeScript theme={theme} />
29
+ * </head>
30
+ * <body>
31
+ * <ThemeProvider
32
+ * theme={theme}
33
+ * ssrSystemTheme={ssrSystemTheme}
34
+ * persistTheme={persistThemeCookie}
35
+ * syncThemeFromServer={getThemeServerFn}
36
+ * >
37
+ * {children}
38
+ * </ThemeProvider>
39
+ * <Scripts />
40
+ * </body>
41
+ * </html>
42
+ * );
43
+ * }
44
+ * ```
45
+ */
46
+ export { getRootThemeServerFn, getSsrSystemThemeServerFn, getThemeServerFn, persistThemeCookie, setThemeServerFn, } from "#adapters/tanstack-start/server";
package/dist/types.d.ts CHANGED
@@ -1,16 +1,14 @@
1
1
  import { z } from "zod";
2
-
3
- //#region src/types.d.ts
4
2
  /**
5
3
  * Zod schema for theme validation.
6
4
  *
7
5
  * Acts as the single source of truth for all theme-related types.
8
6
  * Valid values: 'light', 'dark', 'system'.
9
7
  */
10
- declare const themeSchema: z.ZodEnum<{
11
- light: "light";
12
- dark: "dark";
13
- system: "system";
8
+ export declare const themeSchema: z.ZodEnum<{
9
+ light: "light";
10
+ dark: "dark";
11
+ system: "system";
14
12
  }>;
15
13
  /**
16
14
  * User's theme preference.
@@ -19,19 +17,19 @@ declare const themeSchema: z.ZodEnum<{
19
17
  * - `'dark'` - Force dark mode
20
18
  * - `'system'` - Follow OS preference
21
19
  */
22
- type Theme = z.infer<typeof themeSchema>;
20
+ export type Theme = z.infer<typeof themeSchema>;
23
21
  /**
24
22
  * Actual theme applied to the UI after resolving 'system'.
25
23
  *
26
24
  * When theme is 'system', this reflects the OS preference (light/dark).
27
25
  */
28
- type ResolvedTheme = Exclude<Theme, "system">;
26
+ export type ResolvedTheme = Exclude<Theme, "system">;
29
27
  /**
30
28
  * Array of all available theme options: `['light', 'dark', 'system']`.
31
29
  *
32
30
  * Useful for rendering theme selectors in UI components.
33
31
  */
34
- declare const themes: ("light" | "dark" | "system")[];
32
+ export declare const themes: ("light" | "dark" | "system")[];
35
33
  /**
36
34
  * Shape of the theme context value provided by ThemeProvider.
37
35
  *
@@ -40,11 +38,10 @@ declare const themes: ("light" | "dark" | "system")[];
40
38
  * - `setTheme` - Async function to update theme (triggers optimistic update)
41
39
  * - `isPending` - True while theme change is being persisted
42
40
  */
43
- interface ThemeContextType {
44
- isPending: boolean;
45
- resolvedTheme: ResolvedTheme;
46
- setTheme: (value: Theme) => Promise<void>;
47
- theme: Theme;
41
+ export interface ThemeContextType {
42
+ isPending: boolean;
43
+ resolvedTheme: ResolvedTheme;
44
+ setTheme: (value: Theme) => Promise<void>;
45
+ theme: Theme;
48
46
  }
49
- //#endregion
50
- export { ResolvedTheme, Theme, ThemeContextType, themeSchema, themes };
47
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAMxB;;;;;GAKG;AACH,eAAO,MAAM,WAAW;;;;EAAsC,CAAC;AAE/D;;;;;;GAMG;AACH,MAAM,MAAM,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,WAAW,CAAC,CAAC;AAEhD;;;;GAIG;AACH,MAAM,MAAM,aAAa,GAAG,OAAO,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;AAErD;;;;GAIG;AACH,eAAO,MAAM,MAAM,iCAAsB,CAAC;AAE1C;;;;;;;GAOG;AACH,MAAM,WAAW,gBAAgB;IAC/B,SAAS,EAAE,OAAO,CAAC;IACnB,aAAa,EAAE,aAAa,CAAC;IAC7B,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1C,KAAK,EAAE,KAAK,CAAC;CACd"}
package/dist/types.js CHANGED
@@ -1,21 +1,17 @@
1
1
  import { z } from "zod";
2
- //#region src/types.ts
2
+ /* -----------------------------------------------------------------------------
3
+ * Schema & Types
4
+ * -------------------------------------------------------------------------- */
3
5
  /**
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
- ]);
6
+ * Zod schema for theme validation.
7
+ *
8
+ * Acts as the single source of truth for all theme-related types.
9
+ * Valid values: 'light', 'dark', 'system'.
10
+ */
11
+ export const themeSchema = z.enum(["light", "dark", "system"]);
14
12
  /**
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 };
13
+ * Array of all available theme options: `['light', 'dark', 'system']`.
14
+ *
15
+ * Useful for rendering theme selectors in UI components.
16
+ */
17
+ export const themes = themeSchema.options;
@@ -1,6 +1,4 @@
1
- import { ResolvedTheme } from "../types.js";
2
-
3
- //#region src/utils/dom.d.ts
1
+ import type { ResolvedTheme } from "#types";
4
2
  /**
5
3
  * Apply theme to the DOM by updating `<html>` element.
6
4
  *
@@ -10,7 +8,7 @@ import { ResolvedTheme } from "../types.js";
10
8
  *
11
9
  * @param resolved - The resolved theme to apply ('light' or 'dark')
12
10
  */
13
- declare function applyTheme(resolved: ResolvedTheme): void;
11
+ export declare function applyTheme(resolved: ResolvedTheme): void;
14
12
  /**
15
13
  * Temporarily disable all CSS transitions during theme changes.
16
14
  *
@@ -27,6 +25,5 @@ declare function applyTheme(resolved: ResolvedTheme): void;
27
25
  * enableTransitions(); // Re-enables CSS transitions
28
26
  * ```
29
27
  */
30
- declare function disableAnimation(nonce?: string): () => void;
31
- //#endregion
32
- export { applyTheme, disableAnimation };
28
+ export declare function disableAnimation(nonce?: string): () => void;
29
+ //# sourceMappingURL=dom.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dom.d.ts","sourceRoot":"","sources":["../../src/utils/dom.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAM5C;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CAAC,QAAQ,EAAE,aAAa,GAAG,IAAI,CAMxD;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,IAAI,CA2C3D"}
package/dist/utils/dom.js CHANGED
@@ -1,50 +1,66 @@
1
- //#region src/utils/dom.ts
1
+ /* -----------------------------------------------------------------------------
2
+ * DOM Utilities
3
+ * -------------------------------------------------------------------------- */
2
4
  /**
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;
5
+ * Apply theme to the DOM by updating `<html>` element.
6
+ *
7
+ * Updates both:
8
+ * - CSS class (for Tailwind's `dark:` variants)
9
+ * - `color-scheme` style (for native form controls and scrollbars)
10
+ *
11
+ * @param resolved - The resolved theme to apply ('light' or 'dark')
12
+ */
13
+ export function applyTheme(resolved) {
14
+ const root = window.document.documentElement;
15
+ root.classList.remove("light", "dark", "system");
16
+ root.classList.add(resolved);
17
+ root.style.colorScheme = resolved;
16
18
  }
17
19
  /**
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 window === "undefined") return () => {};
35
- if (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
- };
20
+ * Temporarily disable all CSS transitions during theme changes.
21
+ *
22
+ * Prevents jarring color animations when switching between light/dark themes.
23
+ * Respects user's `prefers-reduced-motion` preference (does nothing if enabled).
24
+ *
25
+ * @param nonce - Optional CSP nonce for the injected style element
26
+ * @returns Cleanup function to re-enable transitions. Call after theme is applied.
27
+ *
28
+ * @example
29
+ * ```tsx
30
+ * const enableTransitions = disableAnimation();
31
+ * applyTheme('dark');
32
+ * enableTransitions(); // Re-enables CSS transitions
33
+ * ```
34
+ */
35
+ export function disableAnimation(nonce) {
36
+ if (typeof window === "undefined")
37
+ return () => {
38
+ /* noop */
39
+ };
40
+ // Respect user's motion preferences
41
+ const prefersReducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
42
+ if (prefersReducedMotion) {
43
+ return () => {
44
+ /* noop */
45
+ };
46
+ }
47
+ // Inject style to disable all transitions
48
+ const css = document.createElement("style");
49
+ if (nonce) {
50
+ css.setAttribute("nonce", nonce);
51
+ }
52
+ 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}`));
53
+ document.head.append(css);
54
+ return () => {
55
+ // Force reflow to ensure styles are applied before removing
56
+ (() => window.getComputedStyle(document.body))();
57
+ // Use double RAF to ensure paint happens before removing style
58
+ requestAnimationFrame(() => {
59
+ requestAnimationFrame(() => {
60
+ if (css.parentNode) {
61
+ css.remove();
62
+ }
63
+ });
64
+ });
65
+ };
48
66
  }
49
- //#endregion
50
- export { applyTheme, disableAnimation };
@@ -1,3 +1,3 @@
1
- import { applyTheme, disableAnimation } from "./dom.js";
2
- import { getSystemTheme, resolveTheme } from "./system.js";
3
- export { applyTheme, disableAnimation, getSystemTheme, resolveTheme };
1
+ export { applyTheme, disableAnimation } from "#utils/dom";
2
+ export { getSystemTheme, resolveTheme } from "#utils/system";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAC1D,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC"}
@@ -1,3 +1,2 @@
1
- import { applyTheme, disableAnimation } from "./dom.js";
2
- import { getSystemTheme, resolveTheme } from "./system.js";
3
- export { applyTheme, disableAnimation, getSystemTheme, resolveTheme };
1
+ export { applyTheme, disableAnimation } from "#utils/dom";
2
+ export { getSystemTheme, resolveTheme } from "#utils/system";
@@ -1,6 +1,4 @@
1
- import { ResolvedTheme, Theme } from "../types.js";
2
-
3
- //#region src/utils/system.d.ts
1
+ import type { ResolvedTheme, Theme } from "#types";
4
2
  /**
5
3
  * Detect the user's OS theme preference.
6
4
  *
@@ -9,17 +7,21 @@ import { ResolvedTheme, Theme } from "../types.js";
9
7
  *
10
8
  * @returns 'light' or 'dark' based on OS preference
11
9
  */
12
- declare function getSystemTheme(): ResolvedTheme;
10
+ export declare function getSystemTheme(): ResolvedTheme;
13
11
  /**
14
12
  * Resolve theme preference to actual light/dark value.
15
13
  *
16
14
  * - 'light' → 'light'
17
15
  * - 'dark' → 'dark'
18
- * - 'system' → result of {@link getSystemTheme}
16
+ * - 'system' → on the client, result of {@link getSystemTheme}; on the server,
17
+ * `ssrSystemTheme` when provided, otherwise {@link DEFAULT_RESOLVED_THEME}
19
18
  *
20
- * @param theme - User's theme preference
21
- * @returns The resolved theme to apply
19
+ * @param theme - User's theme preference (`light`, `dark`, or `system`)
20
+ * @param ssrSystemTheme - When `theme` is `system` and this runs during SSR (no `window`),
21
+ * uses this as the resolved appearance—typically from Client Hints
22
+ * (`Sec-CH-Prefers-Color-Scheme`) so `<html class>` matches the real OS preference.
23
+ * Ignored for non-`system` themes and on the client (where {@link getSystemTheme} wins).
24
+ * @returns The resolved theme to apply (`light` or `dark`)
22
25
  */
23
- declare function resolveTheme(theme: Theme): ResolvedTheme;
24
- //#endregion
25
- export { getSystemTheme, resolveTheme };
26
+ export declare function resolveTheme(theme: Theme, ssrSystemTheme?: ResolvedTheme): ResolvedTheme;
27
+ //# sourceMappingURL=system.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"system.d.ts","sourceRoot":"","sources":["../../src/utils/system.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAQnD;;;;;;;GAOG;AACH,wBAAgB,cAAc,IAAI,aAAa,CAM9C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,KAAK,EAAE,cAAc,CAAC,EAAE,aAAa,GAAG,aAAa,CAUxF"}
@@ -1,29 +1,42 @@
1
- import { DEFAULT_RESOLVED_THEME } from "../constants.js";
2
- //#region src/utils/system.ts
1
+ import { DEFAULT_RESOLVED_THEME, MEDIA } from "#constants";
2
+ /* -----------------------------------------------------------------------------
3
+ * System Theme Detection
4
+ * -------------------------------------------------------------------------- */
3
5
  /**
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 window === "undefined") return DEFAULT_RESOLVED_THEME;
13
- return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
6
+ * Detect the user's OS theme preference.
7
+ *
8
+ * Uses `matchMedia()` to query the `prefers-color-scheme` media feature.
9
+ * Returns {@link DEFAULT_RESOLVED_THEME} during SSR since `window` is unavailable.
10
+ *
11
+ * @returns 'light' or 'dark' based on OS preference
12
+ */
13
+ export function getSystemTheme() {
14
+ if (typeof window === "undefined") {
15
+ return DEFAULT_RESOLVED_THEME;
16
+ }
17
+ return window.matchMedia(MEDIA).matches ? "dark" : "light";
14
18
  }
15
19
  /**
16
- * Resolve theme preference to actual light/dark value.
17
- *
18
- * - 'light' → 'light'
19
- * - 'dark' → 'dark'
20
- * - 'system' → result of {@link getSystemTheme}
21
- *
22
- * @param theme - User's theme preference
23
- * @returns The resolved theme to apply
24
- */
25
- function resolveTheme(theme) {
26
- return theme === "system" ? getSystemTheme() : theme;
20
+ * Resolve theme preference to actual light/dark value.
21
+ *
22
+ * - 'light' → 'light'
23
+ * - 'dark' → 'dark'
24
+ * - 'system' → on the client, result of {@link getSystemTheme}; on the server,
25
+ * `ssrSystemTheme` when provided, otherwise {@link DEFAULT_RESOLVED_THEME}
26
+ *
27
+ * @param theme - User's theme preference (`light`, `dark`, or `system`)
28
+ * @param ssrSystemTheme - When `theme` is `system` and this runs during SSR (no `window`),
29
+ * uses this as the resolved appearance—typically from Client Hints
30
+ * (`Sec-CH-Prefers-Color-Scheme`) so `<html class>` matches the real OS preference.
31
+ * Ignored for non-`system` themes and on the client (where {@link getSystemTheme} wins).
32
+ * @returns The resolved theme to apply (`light` or `dark`)
33
+ */
34
+ export function resolveTheme(theme, ssrSystemTheme) {
35
+ if (theme !== "system") {
36
+ return theme;
37
+ }
38
+ if (typeof window === "undefined") {
39
+ return ssrSystemTheme ?? DEFAULT_RESOLVED_THEME;
40
+ }
41
+ return getSystemTheme();
27
42
  }
28
- //#endregion
29
- export { getSystemTheme, resolveTheme };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codefast/theme",
3
- "version": "0.3.11-canary.2",
3
+ "version": "0.3.12-canary.0",
4
4
  "description": "Theme management with React 19 features - optimistic updates, cross-tab sync, and SSR support",
5
5
  "keywords": [
6
6
  "dark-mode",
@@ -32,6 +32,13 @@
32
32
  "sideEffects": false,
33
33
  "main": "./dist/index.js",
34
34
  "types": "./dist/index.d.ts",
35
+ "imports": {
36
+ "#*": [
37
+ "./dist/*",
38
+ "./dist/*.js",
39
+ "./dist/*/index.js"
40
+ ]
41
+ },
35
42
  "exports": {
36
43
  ".": {
37
44
  "types": "./dist/index.d.ts",
@@ -65,10 +72,6 @@
65
72
  "types": "./dist/utils/system.d.ts",
66
73
  "import": "./dist/utils/system.js"
67
74
  },
68
- "./adapters/tanstack-start": {
69
- "types": "./dist/adapters/tanstack-start/index.d.ts",
70
- "import": "./dist/adapters/tanstack-start/index.js"
71
- },
72
75
  "./adapters/tanstack-start/server": {
73
76
  "types": "./dist/adapters/tanstack-start/server.d.ts",
74
77
  "import": "./dist/adapters/tanstack-start/server.js"
@@ -85,6 +88,10 @@
85
88
  "types": "./dist/constants.d.ts",
86
89
  "import": "./dist/constants.js"
87
90
  },
91
+ "./start": {
92
+ "types": "./dist/start.d.ts",
93
+ "import": "./dist/start.js"
94
+ },
88
95
  "./types": {
89
96
  "types": "./dist/types.d.ts",
90
97
  "import": "./dist/types.js"
@@ -109,9 +116,8 @@
109
116
  "@types/node": "^25.5.0",
110
117
  "jest": "^30.3.0",
111
118
  "jest-environment-jsdom": "^30.3.0",
112
- "tsdown": "^0.21.6",
113
119
  "typescript": "^6.0.2",
114
- "@codefast/typescript-config": "0.3.11-canary.2"
120
+ "@codefast/typescript-config": "0.3.12-canary.0"
115
121
  },
116
122
  "peerDependencies": {
117
123
  "@tanstack/react-start": "^1.0",
@@ -132,13 +138,13 @@
132
138
  }
133
139
  },
134
140
  "scripts": {
135
- "build": "tsdown",
141
+ "build": "rm -rf dist && tsc -p tsconfig.build.json",
142
+ "check-types": "tsc --noEmit",
136
143
  "clean": "rm -rf dist",
137
- "dev": "tsdown --watch --no-clean",
144
+ "dev": "tsc -p tsconfig.build.json --watch --preserveWatchOutput",
138
145
  "test": "jest",
139
146
  "test:coverage": "jest --coverage",
140
147
  "test:coverage:ci": "jest --coverage --ci",
141
- "test:watch": "jest --watch",
142
- "typecheck": "tsc --noEmit"
148
+ "test:watch": "jest --watch"
143
149
  }
144
150
  }
@@ -1,2 +0,0 @@
1
- import { createPersistTheme, getThemeServerFn, setThemeServerFn } from "./server.js";
2
- export { createPersistTheme, getThemeServerFn, setThemeServerFn };
@@ -1,2 +0,0 @@
1
- import { createPersistTheme, getThemeServerFn, setThemeServerFn } from "./server.js";
2
- export { createPersistTheme, getThemeServerFn, setThemeServerFn };