@codefast/theme 0.3.16-canary.1 → 0.3.16-canary.3
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 +46 -0
- package/README.md +23 -18
- package/dist/adapters/tanstack-start/server.d.mts +28 -30
- package/dist/adapters/tanstack-start/server.mjs +29 -29
- package/dist/constants.d.mts +11 -13
- package/dist/constants.mjs +10 -12
- package/dist/core/context.d.mts +9 -10
- package/dist/core/context.mjs +7 -7
- package/dist/core/provider.d.mts +57 -44
- package/dist/core/provider.mjs +103 -67
- package/dist/core/use-theme.d.mts +10 -10
- package/dist/core/use-theme.mjs +12 -12
- package/dist/index.d.mts +7 -7
- package/dist/index.mjs +7 -7
- package/dist/script/theme-script.d.mts +41 -15
- package/dist/script/theme-script.mjs +20 -9
- package/dist/start.d.mts +2 -2
- package/dist/start.mjs +2 -2
- package/dist/types.d.mts +28 -28
- package/dist/types.mjs +9 -9
- package/dist/utils/dom.d.mts +11 -11
- package/dist/utils/dom.mjs +12 -12
- package/dist/utils/system.d.mts +13 -13
- package/dist/utils/system.mjs +17 -17
- package/package.json +25 -18
- package/src/adapters/tanstack-start/server.ts +150 -0
- package/src/constants.ts +52 -0
- package/src/core/context.tsx +22 -0
- package/src/core/provider.tsx +377 -0
- package/src/core/use-theme.ts +47 -0
- package/src/index.ts +29 -0
- package/src/script/theme-script.tsx +103 -0
- package/src/start.ts +54 -0
- package/src/types.ts +61 -0
- package/src/utils/dom.ts +87 -0
- package/src/utils/system.ts +57 -0
|
@@ -1,32 +1,43 @@
|
|
|
1
|
+
import { MEDIA } from "../constants.mjs";
|
|
2
|
+
import { colorSchemes } from "../types.mjs";
|
|
1
3
|
import { jsx } from "react/jsx-runtime";
|
|
2
4
|
//#region src/script/theme-script.tsx
|
|
5
|
+
function toScriptSafe(value) {
|
|
6
|
+
return JSON.stringify(value).replaceAll("<", "\\u003C").replaceAll(">", "\\u003E").replaceAll("/", "\\u002F");
|
|
7
|
+
}
|
|
8
|
+
const COLOR_SCHEME_REMOVE_ARGS = colorSchemes.map((s) => toScriptSafe(s)).join(",");
|
|
9
|
+
const COLOR_SCHEME_VALID_CHECK = colorSchemes.map((s) => `s===${toScriptSafe(s)}`).join("||");
|
|
3
10
|
/**
|
|
4
11
|
* Inline script that prevents Flash of Unstyled Content (FOUC).
|
|
5
12
|
*
|
|
6
13
|
* **Why this is needed:**
|
|
7
14
|
* React hydration occurs after the browser has already painted the page.
|
|
8
|
-
* Without this script, users would briefly see the wrong
|
|
15
|
+
* Without this script, users would briefly see the wrong appearance before
|
|
9
16
|
* React takes over and applies the correct one.
|
|
10
17
|
*
|
|
11
18
|
* **How it works:**
|
|
12
19
|
* This script runs synchronously in the `<head>` before first paint:
|
|
13
|
-
* 1. Resolves '
|
|
14
|
-
* 2. Removes prior `light` / `dark` / `
|
|
15
|
-
* have applied the wrong resolved class for `
|
|
16
|
-
* 3. Adds the resolved
|
|
20
|
+
* 1. Resolves 'automatic' to 'light' or 'dark' using `matchMedia()`
|
|
21
|
+
* 2. Removes prior `light` / `dark` / `automatic` classes on `<html>` (SSR may
|
|
22
|
+
* have applied the wrong resolved class for `automatic`, e.g. default `dark`)
|
|
23
|
+
* 3. Adds the resolved color scheme class and sets `color-scheme` for native controls
|
|
17
24
|
*
|
|
18
25
|
* @example
|
|
19
26
|
* ```tsx
|
|
20
27
|
* // In __root.tsx (TanStack Start)
|
|
21
28
|
* <head>
|
|
22
|
-
* <
|
|
29
|
+
* <AppearanceScript colorScheme={colorScheme} />
|
|
23
30
|
* </head>
|
|
24
31
|
* ```
|
|
25
32
|
*
|
|
26
33
|
* @since 0.3.16-canary.0
|
|
27
34
|
*/
|
|
28
|
-
function
|
|
29
|
-
return /* @__PURE__ */ jsx("script", {
|
|
35
|
+
function AppearanceScript({ nonce, storageKey, colorScheme }) {
|
|
36
|
+
return /* @__PURE__ */ jsx("script", {
|
|
37
|
+
dangerouslySetInnerHTML: { __html: `(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){}})()` },
|
|
38
|
+
suppressHydrationWarning: true,
|
|
39
|
+
...nonce === void 0 ? {} : { nonce }
|
|
40
|
+
});
|
|
30
41
|
}
|
|
31
42
|
//#endregion
|
|
32
|
-
export {
|
|
43
|
+
export { AppearanceScript };
|
package/dist/start.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { type
|
|
1
|
+
import { RootColorSchemeLoaderData, getColorSchemeServerFn, getRootColorSchemeServerFn, getSsrColorSchemeServerFn, persistColorSchemeCookie, setColorSchemeServerFn } from "./adapters/tanstack-start/server.mjs";
|
|
2
|
+
export { type RootColorSchemeLoaderData, getColorSchemeServerFn, getRootColorSchemeServerFn, getSsrColorSchemeServerFn, persistColorSchemeCookie, setColorSchemeServerFn };
|
package/dist/start.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
1
|
+
import { getColorSchemeServerFn, getRootColorSchemeServerFn, getSsrColorSchemeServerFn, persistColorSchemeCookie, setColorSchemeServerFn } from "./adapters/tanstack-start/server.mjs";
|
|
2
|
+
export { getColorSchemeServerFn, getRootColorSchemeServerFn, getSsrColorSchemeServerFn, persistColorSchemeCookie, setColorSchemeServerFn };
|
package/dist/types.d.mts
CHANGED
|
@@ -2,59 +2,59 @@ import { z } from "zod";
|
|
|
2
2
|
|
|
3
3
|
//#region src/types.d.ts
|
|
4
4
|
/**
|
|
5
|
-
* Zod schema for
|
|
5
|
+
* Zod schema for color scheme validation.
|
|
6
6
|
*
|
|
7
|
-
* Acts as the single source of truth for all
|
|
8
|
-
* Valid values: 'light', 'dark', '
|
|
7
|
+
* Acts as the single source of truth for all color-scheme-related types.
|
|
8
|
+
* Valid values: 'light', 'dark', 'automatic'.
|
|
9
9
|
*
|
|
10
10
|
* @since 0.3.16-canary.0
|
|
11
11
|
*/
|
|
12
|
-
declare const
|
|
12
|
+
declare const colorSchemeSchema: z.ZodEnum<{
|
|
13
13
|
light: "light";
|
|
14
14
|
dark: "dark";
|
|
15
|
-
|
|
15
|
+
automatic: "automatic";
|
|
16
16
|
}>;
|
|
17
17
|
/**
|
|
18
|
-
* User's
|
|
18
|
+
* User's color scheme preference.
|
|
19
19
|
*
|
|
20
|
-
* - `'light'` - Force light
|
|
21
|
-
* - `'dark'` - Force dark
|
|
22
|
-
* - `'
|
|
20
|
+
* - `'light'` - Force light appearance
|
|
21
|
+
* - `'dark'` - Force dark appearance
|
|
22
|
+
* - `'automatic'` - Follow OS preference
|
|
23
23
|
*
|
|
24
24
|
* @since 0.3.16-canary.0
|
|
25
25
|
*/
|
|
26
|
-
type
|
|
26
|
+
type ColorScheme = z.infer<typeof colorSchemeSchema>;
|
|
27
27
|
/**
|
|
28
|
-
* Actual
|
|
28
|
+
* Actual color scheme applied to the UI after resolving 'automatic'.
|
|
29
29
|
*
|
|
30
|
-
* When
|
|
30
|
+
* When color scheme is 'automatic', this reflects the OS preference (light/dark).
|
|
31
31
|
*
|
|
32
32
|
* @since 0.3.16-canary.0
|
|
33
33
|
*/
|
|
34
|
-
type
|
|
34
|
+
type ResolvedColorScheme = Exclude<ColorScheme, "automatic">;
|
|
35
35
|
/**
|
|
36
|
-
* Array of all available
|
|
36
|
+
* Array of all available color scheme options: `['light', 'dark', 'automatic']`.
|
|
37
37
|
*
|
|
38
|
-
* Useful for rendering
|
|
38
|
+
* Useful for rendering appearance selectors in UI components.
|
|
39
39
|
*
|
|
40
40
|
* @since 0.3.16-canary.0
|
|
41
41
|
*/
|
|
42
|
-
declare const
|
|
42
|
+
declare const colorSchemes: ("light" | "dark" | "automatic")[];
|
|
43
43
|
/**
|
|
44
|
-
* Shape of the
|
|
44
|
+
* Shape of the color scheme context value provided by AppearanceProvider.
|
|
45
45
|
*
|
|
46
|
-
* - `
|
|
47
|
-
* - `
|
|
48
|
-
* - `
|
|
49
|
-
* - `isPending` - True while
|
|
46
|
+
* - `colorScheme` - Current color scheme preference
|
|
47
|
+
* - `resolvedColorScheme` - Actual color scheme applied (automatic resolved to light/dark)
|
|
48
|
+
* - `setPreferredColorScheme` - Async function to update color scheme (triggers optimistic update)
|
|
49
|
+
* - `isPending` - True while color scheme change is being persisted
|
|
50
50
|
*
|
|
51
51
|
* @since 0.3.16-canary.0
|
|
52
52
|
*/
|
|
53
|
-
|
|
54
|
-
isPending: boolean;
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
53
|
+
type ColorSchemeContextType = {
|
|
54
|
+
readonly isPending: boolean;
|
|
55
|
+
readonly resolvedColorScheme: ResolvedColorScheme;
|
|
56
|
+
readonly setColorScheme: (value: ColorScheme) => Promise<void>;
|
|
57
|
+
readonly colorScheme: ColorScheme;
|
|
58
|
+
};
|
|
59
59
|
//#endregion
|
|
60
|
-
export {
|
|
60
|
+
export { ColorScheme, ColorSchemeContextType, ResolvedColorScheme, colorSchemeSchema, colorSchemes };
|
package/dist/types.mjs
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
//#region src/types.ts
|
|
3
3
|
/**
|
|
4
|
-
* Zod schema for
|
|
4
|
+
* Zod schema for color scheme validation.
|
|
5
5
|
*
|
|
6
|
-
* Acts as the single source of truth for all
|
|
7
|
-
* Valid values: 'light', 'dark', '
|
|
6
|
+
* Acts as the single source of truth for all color-scheme-related types.
|
|
7
|
+
* Valid values: 'light', 'dark', 'automatic'.
|
|
8
8
|
*
|
|
9
9
|
* @since 0.3.16-canary.0
|
|
10
10
|
*/
|
|
11
|
-
const
|
|
11
|
+
const colorSchemeSchema = z.enum([
|
|
12
12
|
"light",
|
|
13
13
|
"dark",
|
|
14
|
-
"
|
|
14
|
+
"automatic"
|
|
15
15
|
]);
|
|
16
16
|
/**
|
|
17
|
-
* Array of all available
|
|
17
|
+
* Array of all available color scheme options: `['light', 'dark', 'automatic']`.
|
|
18
18
|
*
|
|
19
|
-
* Useful for rendering
|
|
19
|
+
* Useful for rendering appearance selectors in UI components.
|
|
20
20
|
*
|
|
21
21
|
* @since 0.3.16-canary.0
|
|
22
22
|
*/
|
|
23
|
-
const
|
|
23
|
+
const colorSchemes = colorSchemeSchema.options;
|
|
24
24
|
//#endregion
|
|
25
|
-
export {
|
|
25
|
+
export { colorSchemeSchema, colorSchemes };
|
package/dist/utils/dom.d.mts
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ResolvedColorScheme } from "../types.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/utils/dom.d.ts
|
|
4
4
|
/**
|
|
5
|
-
* Apply
|
|
5
|
+
* Apply color scheme to the DOM by updating `<html>` element.
|
|
6
6
|
*
|
|
7
7
|
* Updates both:
|
|
8
8
|
* - CSS class (for Tailwind's `dark:` variants)
|
|
9
9
|
* - `color-scheme` style (for native form controls and scrollbars)
|
|
10
10
|
*
|
|
11
|
-
* @param resolved - The resolved
|
|
11
|
+
* @param resolved - The resolved color scheme to apply ('light' or 'dark')
|
|
12
12
|
*
|
|
13
13
|
* @since 0.3.16-canary.0
|
|
14
14
|
*/
|
|
15
|
-
declare function
|
|
15
|
+
declare function applyColorScheme(resolved: ResolvedColorScheme): void;
|
|
16
16
|
/**
|
|
17
|
-
* Temporarily
|
|
17
|
+
* Temporarily suppress all CSS transitions during color scheme changes.
|
|
18
18
|
*
|
|
19
|
-
* Prevents jarring color animations when switching between light/dark
|
|
19
|
+
* Prevents jarring color animations when switching between light/dark appearances.
|
|
20
20
|
* Respects user's `prefers-reduced-motion` preference (does nothing if enabled).
|
|
21
21
|
*
|
|
22
22
|
* @param nonce - Optional CSP nonce for the injected style element
|
|
23
|
-
* @returns Cleanup function to re-enable transitions. Call after
|
|
23
|
+
* @returns Cleanup function to re-enable transitions. Call after color scheme is applied.
|
|
24
24
|
*
|
|
25
25
|
* @example
|
|
26
26
|
* ```tsx
|
|
27
|
-
* const enableTransitions =
|
|
28
|
-
*
|
|
27
|
+
* const enableTransitions = suppressTransitions();
|
|
28
|
+
* applyColorScheme('dark');
|
|
29
29
|
* enableTransitions(); // Re-enables CSS transitions
|
|
30
30
|
* ```
|
|
31
31
|
*
|
|
32
32
|
* @since 0.3.16-canary.0
|
|
33
33
|
*/
|
|
34
|
-
declare function
|
|
34
|
+
declare function suppressTransitions(nonce?: string): () => void;
|
|
35
35
|
//#endregion
|
|
36
|
-
export {
|
|
36
|
+
export { applyColorScheme, suppressTransitions };
|
package/dist/utils/dom.mjs
CHANGED
|
@@ -1,45 +1,45 @@
|
|
|
1
1
|
//#region src/utils/dom.ts
|
|
2
2
|
/**
|
|
3
|
-
* Apply
|
|
3
|
+
* Apply color scheme to the DOM by updating `<html>` element.
|
|
4
4
|
*
|
|
5
5
|
* Updates both:
|
|
6
6
|
* - CSS class (for Tailwind's `dark:` variants)
|
|
7
7
|
* - `color-scheme` style (for native form controls and scrollbars)
|
|
8
8
|
*
|
|
9
|
-
* @param resolved - The resolved
|
|
9
|
+
* @param resolved - The resolved color scheme to apply ('light' or 'dark')
|
|
10
10
|
*
|
|
11
11
|
* @since 0.3.16-canary.0
|
|
12
12
|
*/
|
|
13
|
-
function
|
|
13
|
+
function applyColorScheme(resolved) {
|
|
14
14
|
const root = window.document.documentElement;
|
|
15
|
-
root.classList.remove("light", "dark", "
|
|
15
|
+
root.classList.remove("light", "dark", "automatic");
|
|
16
16
|
root.classList.add(resolved);
|
|
17
17
|
root.style.colorScheme = resolved;
|
|
18
18
|
}
|
|
19
19
|
/**
|
|
20
|
-
* Temporarily
|
|
20
|
+
* Temporarily suppress all CSS transitions during color scheme changes.
|
|
21
21
|
*
|
|
22
|
-
* Prevents jarring color animations when switching between light/dark
|
|
22
|
+
* Prevents jarring color animations when switching between light/dark appearances.
|
|
23
23
|
* Respects user's `prefers-reduced-motion` preference (does nothing if enabled).
|
|
24
24
|
*
|
|
25
25
|
* @param nonce - Optional CSP nonce for the injected style element
|
|
26
|
-
* @returns Cleanup function to re-enable transitions. Call after
|
|
26
|
+
* @returns Cleanup function to re-enable transitions. Call after color scheme is applied.
|
|
27
27
|
*
|
|
28
28
|
* @example
|
|
29
29
|
* ```tsx
|
|
30
|
-
* const enableTransitions =
|
|
31
|
-
*
|
|
30
|
+
* const enableTransitions = suppressTransitions();
|
|
31
|
+
* applyColorScheme('dark');
|
|
32
32
|
* enableTransitions(); // Re-enables CSS transitions
|
|
33
33
|
* ```
|
|
34
34
|
*
|
|
35
35
|
* @since 0.3.16-canary.0
|
|
36
36
|
*/
|
|
37
|
-
function
|
|
37
|
+
function suppressTransitions(nonce) {
|
|
38
38
|
if (typeof globalThis.window === "undefined") return () => {};
|
|
39
39
|
if (globalThis.window.matchMedia("(prefers-reduced-motion: reduce)").matches) return () => {};
|
|
40
40
|
const css = document.createElement("style");
|
|
41
41
|
if (nonce) css.setAttribute("nonce", nonce);
|
|
42
|
-
css.append(document.createTextNode(`*,*::before,*::after{
|
|
42
|
+
css.append(document.createTextNode(`*,*::before,*::after{transition:none!important}`));
|
|
43
43
|
document.head.append(css);
|
|
44
44
|
return () => {
|
|
45
45
|
window.getComputedStyle(document.body);
|
|
@@ -51,4 +51,4 @@ function disableAnimation(nonce) {
|
|
|
51
51
|
};
|
|
52
52
|
}
|
|
53
53
|
//#endregion
|
|
54
|
-
export {
|
|
54
|
+
export { applyColorScheme, suppressTransitions };
|
package/dist/utils/system.d.mts
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ColorScheme, ResolvedColorScheme } from "../types.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/utils/system.d.ts
|
|
4
4
|
/**
|
|
5
|
-
* Detect the user's OS
|
|
5
|
+
* Detect the user's OS color scheme preference.
|
|
6
6
|
*
|
|
7
7
|
* Uses `matchMedia()` to query the `prefers-color-scheme` media feature.
|
|
8
|
-
* Returns {@link
|
|
8
|
+
* Returns {@link DEFAULT_RESOLVED_COLOR_SCHEME} during SSR since `window` is unavailable.
|
|
9
9
|
*
|
|
10
10
|
* @returns 'light' or 'dark' based on OS preference
|
|
11
11
|
*
|
|
12
12
|
* @since 0.3.16-canary.0
|
|
13
13
|
*/
|
|
14
|
-
declare function
|
|
14
|
+
declare function getSystemColorScheme(): ResolvedColorScheme;
|
|
15
15
|
/**
|
|
16
|
-
* Resolve
|
|
16
|
+
* Resolve color scheme preference to actual light/dark value.
|
|
17
17
|
*
|
|
18
18
|
* - 'light' → 'light'
|
|
19
19
|
* - 'dark' → 'dark'
|
|
20
|
-
* - '
|
|
21
|
-
* `
|
|
20
|
+
* - 'automatic' → on the client, result of {@link getSystemColorScheme}; on the server,
|
|
21
|
+
* `ssrColorScheme` when provided, otherwise {@link DEFAULT_RESOLVED_COLOR_SCHEME}
|
|
22
22
|
*
|
|
23
|
-
* @param
|
|
24
|
-
* @param
|
|
23
|
+
* @param colorScheme - User's color scheme preference (`light`, `dark`, or `automatic`)
|
|
24
|
+
* @param ssrColorScheme - When `colorScheme` is `automatic` and this runs during SSR (no `window`),
|
|
25
25
|
* uses this as the resolved appearance—typically from Client Hints
|
|
26
26
|
* (`Sec-CH-Prefers-Color-Scheme`) so `<html class>` matches the real OS preference.
|
|
27
|
-
* Ignored for non-`
|
|
28
|
-
* @returns The resolved
|
|
27
|
+
* Ignored for non-`automatic` schemes and on the client (where {@link getSystemColorScheme} wins).
|
|
28
|
+
* @returns The resolved color scheme to apply (`light` or `dark`)
|
|
29
29
|
*
|
|
30
30
|
* @since 0.3.16-canary.0
|
|
31
31
|
*/
|
|
32
|
-
declare function
|
|
32
|
+
declare function resolveColorScheme(colorScheme: ColorScheme, ssrColorScheme?: ResolvedColorScheme): ResolvedColorScheme;
|
|
33
33
|
//#endregion
|
|
34
|
-
export {
|
|
34
|
+
export { getSystemColorScheme, resolveColorScheme };
|
package/dist/utils/system.mjs
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DEFAULT_RESOLVED_COLOR_SCHEME } from "../constants.mjs";
|
|
2
2
|
//#region src/utils/system.ts
|
|
3
3
|
/**
|
|
4
|
-
* Detect the user's OS
|
|
4
|
+
* Detect the user's OS color scheme preference.
|
|
5
5
|
*
|
|
6
6
|
* Uses `matchMedia()` to query the `prefers-color-scheme` media feature.
|
|
7
|
-
* Returns {@link
|
|
7
|
+
* Returns {@link DEFAULT_RESOLVED_COLOR_SCHEME} during SSR since `window` is unavailable.
|
|
8
8
|
*
|
|
9
9
|
* @returns 'light' or 'dark' based on OS preference
|
|
10
10
|
*
|
|
11
11
|
* @since 0.3.16-canary.0
|
|
12
12
|
*/
|
|
13
|
-
function
|
|
14
|
-
if (typeof globalThis.window === "undefined") return
|
|
13
|
+
function getSystemColorScheme() {
|
|
14
|
+
if (typeof globalThis.window === "undefined") return DEFAULT_RESOLVED_COLOR_SCHEME;
|
|
15
15
|
return globalThis.window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
|
-
* Resolve
|
|
18
|
+
* Resolve color scheme preference to actual light/dark value.
|
|
19
19
|
*
|
|
20
20
|
* - 'light' → 'light'
|
|
21
21
|
* - 'dark' → 'dark'
|
|
22
|
-
* - '
|
|
23
|
-
* `
|
|
22
|
+
* - 'automatic' → on the client, result of {@link getSystemColorScheme}; on the server,
|
|
23
|
+
* `ssrColorScheme` when provided, otherwise {@link DEFAULT_RESOLVED_COLOR_SCHEME}
|
|
24
24
|
*
|
|
25
|
-
* @param
|
|
26
|
-
* @param
|
|
25
|
+
* @param colorScheme - User's color scheme preference (`light`, `dark`, or `automatic`)
|
|
26
|
+
* @param ssrColorScheme - When `colorScheme` is `automatic` and this runs during SSR (no `window`),
|
|
27
27
|
* uses this as the resolved appearance—typically from Client Hints
|
|
28
28
|
* (`Sec-CH-Prefers-Color-Scheme`) so `<html class>` matches the real OS preference.
|
|
29
|
-
* Ignored for non-`
|
|
30
|
-
* @returns The resolved
|
|
29
|
+
* Ignored for non-`automatic` schemes and on the client (where {@link getSystemColorScheme} wins).
|
|
30
|
+
* @returns The resolved color scheme to apply (`light` or `dark`)
|
|
31
31
|
*
|
|
32
32
|
* @since 0.3.16-canary.0
|
|
33
33
|
*/
|
|
34
|
-
function
|
|
35
|
-
if (
|
|
36
|
-
if (typeof globalThis.window === "undefined") return
|
|
37
|
-
return
|
|
34
|
+
function resolveColorScheme(colorScheme, ssrColorScheme) {
|
|
35
|
+
if (colorScheme !== "automatic") return colorScheme;
|
|
36
|
+
if (typeof globalThis.window === "undefined") return ssrColorScheme ?? "dark";
|
|
37
|
+
return getSystemColorScheme();
|
|
38
38
|
}
|
|
39
39
|
//#endregion
|
|
40
|
-
export {
|
|
40
|
+
export { getSystemColorScheme, resolveColorScheme };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codefast/theme",
|
|
3
|
-
"version": "0.3.16-canary.
|
|
3
|
+
"version": "0.3.16-canary.3",
|
|
4
4
|
"description": "Theme management with React 19 features - optimistic updates, cross-tab sync, and SSR support",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dark-mode",
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
},
|
|
25
25
|
"files": [
|
|
26
26
|
"dist",
|
|
27
|
+
"src",
|
|
27
28
|
"CHANGELOG.md",
|
|
28
29
|
"README.md",
|
|
29
30
|
"LICENSE"
|
|
@@ -51,46 +52,57 @@
|
|
|
51
52
|
},
|
|
52
53
|
"exports": {
|
|
53
54
|
".": {
|
|
55
|
+
"source": "./src/index.ts",
|
|
54
56
|
"types": "./dist/index.d.mts",
|
|
55
57
|
"import": "./dist/index.mjs"
|
|
56
58
|
},
|
|
57
59
|
"./adapters/tanstack-start/server": {
|
|
60
|
+
"source": "./src/adapters/tanstack-start/server.ts",
|
|
58
61
|
"types": "./dist/adapters/tanstack-start/server.d.mts",
|
|
59
62
|
"import": "./dist/adapters/tanstack-start/server.mjs"
|
|
60
63
|
},
|
|
61
64
|
"./constants": {
|
|
65
|
+
"source": "./src/constants.ts",
|
|
62
66
|
"types": "./dist/constants.d.mts",
|
|
63
67
|
"import": "./dist/constants.mjs"
|
|
64
68
|
},
|
|
65
69
|
"./core/context": {
|
|
70
|
+
"source": "./src/core/context.tsx",
|
|
66
71
|
"types": "./dist/core/context.d.mts",
|
|
67
72
|
"import": "./dist/core/context.mjs"
|
|
68
73
|
},
|
|
69
74
|
"./core/provider": {
|
|
75
|
+
"source": "./src/core/provider.tsx",
|
|
70
76
|
"types": "./dist/core/provider.d.mts",
|
|
71
77
|
"import": "./dist/core/provider.mjs"
|
|
72
78
|
},
|
|
73
79
|
"./core/use-theme": {
|
|
80
|
+
"source": "./src/core/use-theme.ts",
|
|
74
81
|
"types": "./dist/core/use-theme.d.mts",
|
|
75
82
|
"import": "./dist/core/use-theme.mjs"
|
|
76
83
|
},
|
|
77
84
|
"./script/theme-script": {
|
|
85
|
+
"source": "./src/script/theme-script.tsx",
|
|
78
86
|
"types": "./dist/script/theme-script.d.mts",
|
|
79
87
|
"import": "./dist/script/theme-script.mjs"
|
|
80
88
|
},
|
|
81
89
|
"./start": {
|
|
90
|
+
"source": "./src/start.ts",
|
|
82
91
|
"types": "./dist/start.d.mts",
|
|
83
92
|
"import": "./dist/start.mjs"
|
|
84
93
|
},
|
|
85
94
|
"./types": {
|
|
95
|
+
"source": "./src/types.ts",
|
|
86
96
|
"types": "./dist/types.d.mts",
|
|
87
97
|
"import": "./dist/types.mjs"
|
|
88
98
|
},
|
|
89
99
|
"./utils/dom": {
|
|
100
|
+
"source": "./src/utils/dom.ts",
|
|
90
101
|
"types": "./dist/utils/dom.d.mts",
|
|
91
102
|
"import": "./dist/utils/dom.mjs"
|
|
92
103
|
},
|
|
93
104
|
"./utils/system": {
|
|
105
|
+
"source": "./src/utils/system.ts",
|
|
94
106
|
"types": "./dist/utils/system.d.mts",
|
|
95
107
|
"import": "./dist/utils/system.mjs"
|
|
96
108
|
},
|
|
@@ -103,32 +115,28 @@
|
|
|
103
115
|
"zod": "^4.4.3"
|
|
104
116
|
},
|
|
105
117
|
"devDependencies": {
|
|
106
|
-
"@tanstack/react-start": "^1.
|
|
118
|
+
"@tanstack/react-start": "^1.168.14",
|
|
107
119
|
"@testing-library/dom": "^10.4.1",
|
|
108
120
|
"@testing-library/jest-dom": "^6.9.1",
|
|
109
121
|
"@testing-library/react": "^16.3.2",
|
|
110
122
|
"@testing-library/user-event": "^14.6.1",
|
|
111
|
-
"@types/node": "^25.
|
|
112
|
-
"@typescript/native-preview": "7.0.0-dev.
|
|
113
|
-
"@vitejs/plugin-react": "^6.0.
|
|
114
|
-
"@vitest/coverage-v8": "^4.1.
|
|
123
|
+
"@types/node": "^25.9.1",
|
|
124
|
+
"@typescript/native-preview": "7.0.0-dev.20260526.1",
|
|
125
|
+
"@vitejs/plugin-react": "^6.0.2",
|
|
126
|
+
"@vitest/coverage-v8": "^4.1.7",
|
|
115
127
|
"jsdom": "^29.1.1",
|
|
116
128
|
"tsdown": "^0.22.0",
|
|
117
129
|
"typescript": "^6.0.3",
|
|
118
|
-
"vitest": "^4.1.
|
|
119
|
-
"@codefast/typescript-config": "0.3.16-canary.
|
|
130
|
+
"vitest": "^4.1.7",
|
|
131
|
+
"@codefast/typescript-config": "0.3.16-canary.3"
|
|
120
132
|
},
|
|
121
133
|
"peerDependencies": {
|
|
122
|
-
"@
|
|
123
|
-
"@types/react": "
|
|
124
|
-
"
|
|
125
|
-
"react": "
|
|
126
|
-
"react-dom": "^19.0"
|
|
134
|
+
"@types/react": ">=19.0.0",
|
|
135
|
+
"@types/react-dom": ">=19.0.0",
|
|
136
|
+
"react": ">=19.0.0",
|
|
137
|
+
"react-dom": ">=19.0.0"
|
|
127
138
|
},
|
|
128
139
|
"peerDependenciesMeta": {
|
|
129
|
-
"@tanstack/react-start": {
|
|
130
|
-
"optional": true
|
|
131
|
-
},
|
|
132
140
|
"@types/react": {
|
|
133
141
|
"optional": true
|
|
134
142
|
},
|
|
@@ -137,13 +145,12 @@
|
|
|
137
145
|
}
|
|
138
146
|
},
|
|
139
147
|
"engines": {
|
|
140
|
-
"node": ">=
|
|
148
|
+
"node": ">=24.0.0"
|
|
141
149
|
},
|
|
142
150
|
"scripts": {
|
|
143
151
|
"build": "tsdown",
|
|
144
152
|
"check-types": "tsgo --noEmit",
|
|
145
153
|
"clean": "rm -rf dist",
|
|
146
|
-
"dev": "tsdown --watch",
|
|
147
154
|
"test": "vitest run",
|
|
148
155
|
"test:coverage": "vitest run --coverage",
|
|
149
156
|
"test:e2e": "vitest run tests/e2e",
|