@codefast/theme 0.3.10

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 (46) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/LICENSE +21 -0
  3. package/README.md +102 -0
  4. package/dist/adapters/tanstack-start/index.cjs +42 -0
  5. package/dist/adapters/tanstack-start/index.d.ts +39 -0
  6. package/dist/adapters/tanstack-start/index.js +2 -0
  7. package/dist/adapters/tanstack-start/server.cjs +70 -0
  8. package/dist/adapters/tanstack-start/server.d.ts +37 -0
  9. package/dist/adapters/tanstack-start/server.js +30 -0
  10. package/dist/constants.cjs +52 -0
  11. package/dist/constants.d.ts +31 -0
  12. package/dist/constants.js +6 -0
  13. package/dist/core/context.cjs +37 -0
  14. package/dist/core/context.d.ts +13 -0
  15. package/dist/core/context.js +3 -0
  16. package/dist/core/index.cjs +44 -0
  17. package/dist/core/index.d.ts +3 -0
  18. package/dist/core/index.js +4 -0
  19. package/dist/core/provider.cjs +123 -0
  20. package/dist/core/provider.d.ts +47 -0
  21. package/dist/core/provider.js +89 -0
  22. package/dist/core/use-theme.cjs +42 -0
  23. package/dist/core/use-theme.d.ts +28 -0
  24. package/dist/core/use-theme.js +8 -0
  25. package/dist/index.cjs +76 -0
  26. package/dist/index.d.ts +16 -0
  27. package/dist/index.js +6 -0
  28. package/dist/script/index.cjs +36 -0
  29. package/dist/script/index.d.ts +1 -0
  30. package/dist/script/index.js +2 -0
  31. package/dist/script/theme-script.cjs +44 -0
  32. package/dist/script/theme-script.d.ts +32 -0
  33. package/dist/script/theme-script.js +10 -0
  34. package/dist/types.cjs +45 -0
  35. package/dist/types.d.ts +46 -0
  36. package/dist/types.js +8 -0
  37. package/dist/utils/dom.cjs +61 -0
  38. package/dist/utils/dom.d.ts +28 -0
  39. package/dist/utils/dom.js +24 -0
  40. package/dist/utils/index.cjs +46 -0
  41. package/dist/utils/index.d.ts +2 -0
  42. package/dist/utils/index.js +3 -0
  43. package/dist/utils/system.cjs +46 -0
  44. package/dist/utils/system.d.ts +21 -0
  45. package/dist/utils/system.js +9 -0
  46. package/package.json +164 -0
package/dist/types.js ADDED
@@ -0,0 +1,8 @@
1
+ import { z } from "zod";
2
+ const themeSchema = z["enum"]([
3
+ 'light',
4
+ 'dark',
5
+ 'system'
6
+ ]);
7
+ const themes = themeSchema.options;
8
+ export { themeSchema, themes };
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ applyTheme: ()=>applyTheme,
28
+ disableAnimation: ()=>disableAnimation
29
+ });
30
+ function applyTheme(resolved) {
31
+ const root = window.document.documentElement;
32
+ root.classList.remove('light', 'dark', 'system');
33
+ root.classList.add(resolved);
34
+ root.style.colorScheme = resolved;
35
+ }
36
+ function disableAnimation(nonce) {
37
+ if ("u" < typeof window) return ()=>{};
38
+ const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
39
+ if (prefersReducedMotion) return ()=>{};
40
+ const css = document.createElement('style');
41
+ if (nonce) css.setAttribute('nonce', nonce);
42
+ 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}"));
43
+ document.head.append(css);
44
+ return ()=>{
45
+ (()=>window.getComputedStyle(document.body))();
46
+ requestAnimationFrame(()=>{
47
+ requestAnimationFrame(()=>{
48
+ if (css.parentNode) css.remove();
49
+ });
50
+ });
51
+ };
52
+ }
53
+ exports.applyTheme = __webpack_exports__.applyTheme;
54
+ exports.disableAnimation = __webpack_exports__.disableAnimation;
55
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
56
+ "applyTheme",
57
+ "disableAnimation"
58
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
59
+ Object.defineProperty(exports, '__esModule', {
60
+ value: true
61
+ });
@@ -0,0 +1,28 @@
1
+ import type { ResolvedTheme } from '../types';
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
+ export declare function applyTheme(resolved: ResolvedTheme): void;
12
+ /**
13
+ * Temporarily disable all CSS transitions during theme changes.
14
+ *
15
+ * Prevents jarring color animations when switching between light/dark themes.
16
+ * Respects user's `prefers-reduced-motion` preference (does nothing if enabled).
17
+ *
18
+ * @param nonce - Optional CSP nonce for the injected style element
19
+ * @returns Cleanup function to re-enable transitions. Call after theme is applied.
20
+ *
21
+ * @example
22
+ * ```tsx
23
+ * const enableTransitions = disableAnimation();
24
+ * applyTheme('dark');
25
+ * enableTransitions(); // Re-enables CSS transitions
26
+ * ```
27
+ */
28
+ export declare function disableAnimation(nonce?: string): () => void;
@@ -0,0 +1,24 @@
1
+ function applyTheme(resolved) {
2
+ const root = window.document.documentElement;
3
+ root.classList.remove('light', 'dark', 'system');
4
+ root.classList.add(resolved);
5
+ root.style.colorScheme = resolved;
6
+ }
7
+ function disableAnimation(nonce) {
8
+ if ("u" < typeof window) return ()=>{};
9
+ const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
10
+ if (prefersReducedMotion) return ()=>{};
11
+ const css = document.createElement('style');
12
+ if (nonce) css.setAttribute('nonce', nonce);
13
+ 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}"));
14
+ document.head.append(css);
15
+ return ()=>{
16
+ (()=>window.getComputedStyle(document.body))();
17
+ requestAnimationFrame(()=>{
18
+ requestAnimationFrame(()=>{
19
+ if (css.parentNode) css.remove();
20
+ });
21
+ });
22
+ };
23
+ }
24
+ export { applyTheme, disableAnimation };
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ resolveTheme: ()=>external_system_cjs_namespaceObject.resolveTheme,
28
+ applyTheme: ()=>external_dom_cjs_namespaceObject.applyTheme,
29
+ disableAnimation: ()=>external_dom_cjs_namespaceObject.disableAnimation,
30
+ getSystemTheme: ()=>external_system_cjs_namespaceObject.getSystemTheme
31
+ });
32
+ const external_dom_cjs_namespaceObject = require("./dom.cjs");
33
+ const external_system_cjs_namespaceObject = require("./system.cjs");
34
+ exports.applyTheme = __webpack_exports__.applyTheme;
35
+ exports.disableAnimation = __webpack_exports__.disableAnimation;
36
+ exports.getSystemTheme = __webpack_exports__.getSystemTheme;
37
+ exports.resolveTheme = __webpack_exports__.resolveTheme;
38
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
39
+ "applyTheme",
40
+ "disableAnimation",
41
+ "getSystemTheme",
42
+ "resolveTheme"
43
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
44
+ Object.defineProperty(exports, '__esModule', {
45
+ value: true
46
+ });
@@ -0,0 +1,2 @@
1
+ export { applyTheme, disableAnimation } from './dom';
2
+ export { getSystemTheme, resolveTheme } from './system';
@@ -0,0 +1,3 @@
1
+ import { applyTheme, disableAnimation } from "./dom.js";
2
+ import { getSystemTheme, resolveTheme } from "./system.js";
3
+ export { applyTheme, disableAnimation, getSystemTheme, resolveTheme };
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ resolveTheme: ()=>resolveTheme,
28
+ getSystemTheme: ()=>getSystemTheme
29
+ });
30
+ const external_constants_cjs_namespaceObject = require("../constants.cjs");
31
+ function getSystemTheme() {
32
+ if ("u" < typeof window) return external_constants_cjs_namespaceObject.DEFAULT_RESOLVED_THEME;
33
+ return window.matchMedia(external_constants_cjs_namespaceObject.MEDIA).matches ? 'dark' : 'light';
34
+ }
35
+ function resolveTheme(theme) {
36
+ return 'system' === theme ? getSystemTheme() : theme;
37
+ }
38
+ exports.getSystemTheme = __webpack_exports__.getSystemTheme;
39
+ exports.resolveTheme = __webpack_exports__.resolveTheme;
40
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
41
+ "getSystemTheme",
42
+ "resolveTheme"
43
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
44
+ Object.defineProperty(exports, '__esModule', {
45
+ value: true
46
+ });
@@ -0,0 +1,21 @@
1
+ import type { ResolvedTheme, Theme } from '../types';
2
+ /**
3
+ * Detect the user's OS theme preference.
4
+ *
5
+ * Uses `matchMedia()` to query the `prefers-color-scheme` media feature.
6
+ * Returns {@link DEFAULT_RESOLVED_THEME} during SSR since `window` is unavailable.
7
+ *
8
+ * @returns 'light' or 'dark' based on OS preference
9
+ */
10
+ export declare function getSystemTheme(): ResolvedTheme;
11
+ /**
12
+ * Resolve theme preference to actual light/dark value.
13
+ *
14
+ * - 'light' → 'light'
15
+ * - 'dark' → 'dark'
16
+ * - 'system' → result of {@link getSystemTheme}
17
+ *
18
+ * @param theme - User's theme preference
19
+ * @returns The resolved theme to apply
20
+ */
21
+ export declare function resolveTheme(theme: Theme): ResolvedTheme;
@@ -0,0 +1,9 @@
1
+ import { DEFAULT_RESOLVED_THEME, MEDIA } from "../constants.js";
2
+ function getSystemTheme() {
3
+ if ("u" < typeof window) return DEFAULT_RESOLVED_THEME;
4
+ return window.matchMedia(MEDIA).matches ? 'dark' : 'light';
5
+ }
6
+ function resolveTheme(theme) {
7
+ return 'system' === theme ? getSystemTheme() : theme;
8
+ }
9
+ export { getSystemTheme, resolveTheme };
package/package.json ADDED
@@ -0,0 +1,164 @@
1
+ {
2
+ "name": "@codefast/theme",
3
+ "version": "0.3.10",
4
+ "description": "Theme management with React 19 features - optimistic updates, cross-tab sync, and SSR support",
5
+ "keywords": [
6
+ "theme",
7
+ "dark-mode",
8
+ "light-mode",
9
+ "react",
10
+ "react-19",
11
+ "ssr",
12
+ "typescript"
13
+ ],
14
+ "homepage": "https://github.com/codefastlabs/codefast/tree/main/packages/theme#readme",
15
+ "bugs": {
16
+ "url": "https://github.com/codefastlabs/codefast/issues"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/codefastlabs/codefast.git",
21
+ "directory": "packages/theme"
22
+ },
23
+ "license": "MIT",
24
+ "author": "Vuong Phan <mr.thevuong@gmail.com>",
25
+ "sideEffects": false,
26
+ "type": "module",
27
+ "exports": {
28
+ ".": {
29
+ "types": "./dist/index.d.ts",
30
+ "import": "./dist/index.js",
31
+ "require": "./dist/index.cjs"
32
+ },
33
+ "./core": {
34
+ "types": "./dist/core/index.d.ts",
35
+ "import": "./dist/core/index.js",
36
+ "require": "./dist/core/index.cjs"
37
+ },
38
+ "./core/context": {
39
+ "types": "./dist/core/context.d.ts",
40
+ "import": "./dist/core/context.js",
41
+ "require": "./dist/core/context.cjs"
42
+ },
43
+ "./core/provider": {
44
+ "types": "./dist/core/provider.d.ts",
45
+ "import": "./dist/core/provider.js",
46
+ "require": "./dist/core/provider.cjs"
47
+ },
48
+ "./core/use-theme": {
49
+ "types": "./dist/core/use-theme.d.ts",
50
+ "import": "./dist/core/use-theme.js",
51
+ "require": "./dist/core/use-theme.cjs"
52
+ },
53
+ "./utils": {
54
+ "types": "./dist/utils/index.d.ts",
55
+ "import": "./dist/utils/index.js",
56
+ "require": "./dist/utils/index.cjs"
57
+ },
58
+ "./utils/dom": {
59
+ "types": "./dist/utils/dom.d.ts",
60
+ "import": "./dist/utils/dom.js",
61
+ "require": "./dist/utils/dom.cjs"
62
+ },
63
+ "./utils/system": {
64
+ "types": "./dist/utils/system.d.ts",
65
+ "import": "./dist/utils/system.js",
66
+ "require": "./dist/utils/system.cjs"
67
+ },
68
+ "./adapters/tanstack-start": {
69
+ "types": "./dist/adapters/tanstack-start/index.d.ts",
70
+ "import": "./dist/adapters/tanstack-start/index.js",
71
+ "require": "./dist/adapters/tanstack-start/index.cjs"
72
+ },
73
+ "./adapters/tanstack-start/server": {
74
+ "types": "./dist/adapters/tanstack-start/server.d.ts",
75
+ "import": "./dist/adapters/tanstack-start/server.js",
76
+ "require": "./dist/adapters/tanstack-start/server.cjs"
77
+ },
78
+ "./script": {
79
+ "types": "./dist/script/index.d.ts",
80
+ "import": "./dist/script/index.js",
81
+ "require": "./dist/script/index.cjs"
82
+ },
83
+ "./script/theme-script": {
84
+ "types": "./dist/script/theme-script.d.ts",
85
+ "import": "./dist/script/theme-script.js",
86
+ "require": "./dist/script/theme-script.cjs"
87
+ },
88
+ "./constants": {
89
+ "types": "./dist/constants.d.ts",
90
+ "import": "./dist/constants.js",
91
+ "require": "./dist/constants.cjs"
92
+ },
93
+ "./types": {
94
+ "types": "./dist/types.d.ts",
95
+ "import": "./dist/types.js",
96
+ "require": "./dist/types.cjs"
97
+ },
98
+ "./package.json": "./package.json"
99
+ },
100
+ "main": "./dist/index.cjs",
101
+ "module": "./dist/index.js",
102
+ "types": "./dist/index.d.ts",
103
+ "files": [
104
+ "dist",
105
+ "CHANGELOG.md",
106
+ "README.md",
107
+ "LICENSE"
108
+ ],
109
+ "dependencies": {
110
+ "zod": "^4.3.6"
111
+ },
112
+ "devDependencies": {
113
+ "@rsbuild/plugin-react": "^1.4.5",
114
+ "@rslib/core": "^0.19.4",
115
+ "@swc/core": "^1.15.11",
116
+ "@swc/jest": "^0.2.39",
117
+ "@tanstack/react-start": "^1.158.0",
118
+ "@testing-library/dom": "^10.4.1",
119
+ "@testing-library/jest-dom": "^6.9.1",
120
+ "@testing-library/react": "^16.3.2",
121
+ "@testing-library/user-event": "^14.6.1",
122
+ "@types/jest": "^30.0.0",
123
+ "@types/node": "^25.2.0",
124
+ "eslint": "^9.39.2",
125
+ "jest": "^30.2.0",
126
+ "jest-environment-jsdom": "^30.2.0",
127
+ "typescript": "^5.9.3",
128
+ "@codefast/eslint-config": "0.3.10",
129
+ "@codefast/typescript-config": "0.3.10"
130
+ },
131
+ "peerDependencies": {
132
+ "@tanstack/react-start": "^1.0",
133
+ "@types/react": "^19.0",
134
+ "@types/react-dom": "^19.0",
135
+ "react": "^19.0",
136
+ "react-dom": "^19.0"
137
+ },
138
+ "peerDependenciesMeta": {
139
+ "@tanstack/react-start": {
140
+ "optional": true
141
+ },
142
+ "@types/react": {
143
+ "optional": true
144
+ },
145
+ "@types/react-dom": {
146
+ "optional": true
147
+ }
148
+ },
149
+ "publishConfig": {
150
+ "access": "public"
151
+ },
152
+ "scripts": {
153
+ "build": "rslib build",
154
+ "check-types": "tsc --noEmit",
155
+ "clean": "rm -rf dist",
156
+ "dev": "rslib build --watch",
157
+ "lint": "TIMING=1 eslint --max-warnings 0",
158
+ "lint:fix": "TIMING=1 eslint --max-warnings 0 --fix",
159
+ "test": "jest",
160
+ "test:coverage": "jest --coverage",
161
+ "test:coverage:ci": "jest --coverage --ci",
162
+ "test:watch": "jest --watch"
163
+ }
164
+ }