react-native-theme-transition 1.0.0 → 1.0.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/LICENSE +9 -9
- package/README.md +12 -8
- package/README.npm.md +111 -0
- package/package.json +90 -87
- package/src/createThemeTransition.ts +92 -92
- package/src/hooks/useTheme.ts +88 -88
- package/src/index.ts +29 -29
- package/src/transitionEngine.tsx +445 -445
- package/src/types.ts +304 -304
package/LICENSE
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Mario Prieta
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
-
|
|
7
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
-
|
|
9
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mario Prieta
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
<h1 align="center">
|
|
1
|
+
<h1 align="center">React Native Theme Transition</h1>
|
|
2
2
|
|
|
3
3
|
<p align="center">
|
|
4
4
|
Smooth, animated theme transitions for React Native. Expo Go compatible, powered by Reanimated.
|
|
5
5
|
</p>
|
|
6
6
|
|
|
7
7
|
<p align="center">
|
|
8
|
-
<a href="https://www.npmjs.com/package/react-native-theme-transition"><img src="https://img.shields.io/npm/v/react-native-theme-transition.svg" alt="npm version" /></a>
|
|
9
|
-
<a href="https://bundlephobia.com/package/react-native-theme-transition"><img src="https://img.shields.io/
|
|
8
|
+
<a href="https://www.npmjs.com/package/react-native-theme-transition"><img src="https://img.shields.io/npm/v/react-native-theme-transition.svg?t=1" alt="npm version" /></a>
|
|
9
|
+
<a href="https://bundlephobia.com/package/react-native-theme-transition"><img src="https://img.shields.io/badge/gzip%20size-2.5%20kB-brightgreen" alt="bundle size" /></a>
|
|
10
10
|
<img src="https://img.shields.io/badge/Expo_Go-compatible-000.svg?logo=expo&logoColor=white" alt="expo compatible" />
|
|
11
|
-
<img src="https://img.shields.io/badge/React_Compiler-compatible-
|
|
12
|
-
<a href="https://github.com/marioprieta/react-native-theme-transition/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/react-native-theme-transition" alt="license" /></a>
|
|
11
|
+
<img src="https://img.shields.io/badge/React_Compiler-compatible-blueviolet.svg" alt="react compiler" />
|
|
12
|
+
<a href="https://github.com/marioprieta/react-native-theme-transition/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/react-native-theme-transition?t=1" alt="license" /></a>
|
|
13
13
|
</p>
|
|
14
14
|
|
|
15
15
|
<table align="center">
|
|
@@ -18,8 +18,12 @@
|
|
|
18
18
|
<td align="center"><b>Android</b></td>
|
|
19
19
|
</tr>
|
|
20
20
|
<tr>
|
|
21
|
-
<td
|
|
22
|
-
|
|
21
|
+
<td>
|
|
22
|
+
<img src="https://raw.githubusercontent.com/marioprieta/react-native-theme-transition/main/assets/videos/iOS_demo.gif" width="260" alt="iOS demo" />
|
|
23
|
+
</td>
|
|
24
|
+
<td>
|
|
25
|
+
<img src="https://raw.githubusercontent.com/marioprieta/react-native-theme-transition/main/assets/videos/Android_demo.gif" width="260" alt="Android demo" />
|
|
26
|
+
</td>
|
|
23
27
|
</tr>
|
|
24
28
|
</table>
|
|
25
29
|
|
|
@@ -32,7 +36,7 @@
|
|
|
32
36
|
- **Native UI sync** — alerts, keyboards, and date pickers automatically match your active theme
|
|
33
37
|
- **Transition safe** — blocks concurrent transitions and exposes `isTransitioning`
|
|
34
38
|
- **React Compiler ready** — all hooks follow the [Rules of React](https://react.dev/reference/rules)
|
|
35
|
-
- **Lightweight** — zero runtime dependencies
|
|
39
|
+
- **Lightweight** — ~2.5 kB gzipped, zero runtime dependencies
|
|
36
40
|
|
|
37
41
|
## Documentation
|
|
38
42
|
|
package/README.npm.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
<h1 align="center">React Native Theme Transition</h1>
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
Smooth, animated theme transitions for React Native. Expo Go compatible, powered by Reanimated.
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
<a href="https://www.npmjs.com/package/react-native-theme-transition"><img src="https://img.shields.io/npm/v/react-native-theme-transition.svg?t=1" alt="npm version" /></a>
|
|
9
|
+
<a href="https://bundlephobia.com/package/react-native-theme-transition"><img src="https://img.shields.io/badge/gzip%20size-2.5%20kB-brightgreen" alt="bundle size" /></a>
|
|
10
|
+
<img src="https://img.shields.io/badge/Expo_Go-compatible-000.svg?logo=expo&logoColor=white" alt="expo compatible" />
|
|
11
|
+
<img src="https://img.shields.io/badge/React_Compiler-compatible-blueviolet.svg" alt="react compiler" />
|
|
12
|
+
<a href="https://github.com/marioprieta/react-native-theme-transition/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/react-native-theme-transition?t=1" alt="license" /></a>
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
<table align="center">
|
|
16
|
+
<tr>
|
|
17
|
+
<td align="center"><b>iOS</b></td>
|
|
18
|
+
<td align="center"><b>Android</b></td>
|
|
19
|
+
</tr>
|
|
20
|
+
<tr>
|
|
21
|
+
<td>
|
|
22
|
+
<img src="https://raw.githubusercontent.com/marioprieta/react-native-theme-transition/main/assets/videos/iOS_demo.gif" width="260" alt="iOS demo" />
|
|
23
|
+
</td>
|
|
24
|
+
<td>
|
|
25
|
+
<img src="https://raw.githubusercontent.com/marioprieta/react-native-theme-transition/main/assets/videos/Android_demo.gif" width="260" alt="Android demo" />
|
|
26
|
+
</td>
|
|
27
|
+
</tr>
|
|
28
|
+
</table>
|
|
29
|
+
|
|
30
|
+
## Features
|
|
31
|
+
|
|
32
|
+
- **Smooth cross-fade transitions** — powered by Reanimated on the native UI thread (60-120 FPS)
|
|
33
|
+
- **Expo Go compatible** — no native code, no prebuilds required
|
|
34
|
+
- **Multi-theme support** — define as many themes as you need with full TypeScript inference
|
|
35
|
+
- **System theme sync** — follows OS appearance automatically with zero-flash startup
|
|
36
|
+
- **Native UI sync** — alerts, keyboards, and date pickers automatically match your active theme
|
|
37
|
+
- **Transition safe** — blocks concurrent transitions and exposes `isTransitioning`
|
|
38
|
+
- **React Compiler ready** — all hooks follow the [Rules of React](https://react.dev/reference/rules)
|
|
39
|
+
- **Lightweight** — ~2.5 kB gzipped, zero runtime dependencies
|
|
40
|
+
|
|
41
|
+
## Documentation
|
|
42
|
+
|
|
43
|
+
For full docs, API reference, examples, and recipes, visit **[react-native-theme-transition.vercel.app](https://react-native-theme-transition.vercel.app)**.
|
|
44
|
+
|
|
45
|
+
## Installation
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
# Expo (SDK 54+ already has reanimated and view-shot)
|
|
49
|
+
npx expo install react-native-theme-transition react-native-worklets
|
|
50
|
+
|
|
51
|
+
# React Native CLI
|
|
52
|
+
npm install react-native-theme-transition react-native-reanimated react-native-view-shot react-native-worklets
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Add `react-native-worklets/plugin` as the **last plugin** in your `babel.config.js`.
|
|
56
|
+
|
|
57
|
+
## Quick start
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
// theme.ts
|
|
61
|
+
import { createThemeTransition } from 'react-native-theme-transition';
|
|
62
|
+
|
|
63
|
+
export const { ThemeTransitionProvider, useTheme } = createThemeTransition({
|
|
64
|
+
themes: {
|
|
65
|
+
light: { background: '#ffffff', text: '#000000', primary: '#007AFF' },
|
|
66
|
+
dark: { background: '#000000', text: '#ffffff', primary: '#0A84FF' },
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
```tsx
|
|
72
|
+
// App.tsx
|
|
73
|
+
import { ThemeTransitionProvider } from './theme';
|
|
74
|
+
|
|
75
|
+
export default function App() {
|
|
76
|
+
return (
|
|
77
|
+
<ThemeTransitionProvider initialTheme="system">
|
|
78
|
+
<MyApp />
|
|
79
|
+
</ThemeTransitionProvider>
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
```tsx
|
|
85
|
+
// MyScreen.tsx
|
|
86
|
+
import { useTheme } from './theme';
|
|
87
|
+
|
|
88
|
+
function MyScreen() {
|
|
89
|
+
const { colors, name, setTheme, isTransitioning } = useTheme();
|
|
90
|
+
|
|
91
|
+
return (
|
|
92
|
+
<View style={{ flex: 1, backgroundColor: colors.background }}>
|
|
93
|
+
<Text style={{ color: colors.text }}>Current: {name}</Text>
|
|
94
|
+
<Pressable
|
|
95
|
+
onPress={() => setTheme(name === 'light' ? 'dark' : 'light')}
|
|
96
|
+
disabled={isTransitioning}
|
|
97
|
+
>
|
|
98
|
+
<Text style={{ color: colors.primary }}>Toggle theme</Text>
|
|
99
|
+
</Pressable>
|
|
100
|
+
</View>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Contributing
|
|
106
|
+
|
|
107
|
+
Contributions are welcome! Please read the [contributing guide](./CONTRIBUTING.md) and open an issue first to discuss what you'd like to change.
|
|
108
|
+
|
|
109
|
+
## License
|
|
110
|
+
|
|
111
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,87 +1,90 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "react-native-theme-transition",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "Smooth, animated theme and dark mode transitions for React Native. Expo Go compatible, powered by Reanimated.",
|
|
5
|
-
"source": "./src/index.ts",
|
|
6
|
-
"main": "./dist/index.js",
|
|
7
|
-
"module": "./dist/index.js",
|
|
8
|
-
"types": "./dist/index.d.ts",
|
|
9
|
-
"react-native": "./src/index.ts",
|
|
10
|
-
"exports": {
|
|
11
|
-
".": {
|
|
12
|
-
"react-native": "./src/index.ts",
|
|
13
|
-
"types": "./dist/index.d.ts",
|
|
14
|
-
"import": "./dist/index.js",
|
|
15
|
-
"default": "./dist/index.js"
|
|
16
|
-
},
|
|
17
|
-
"./package.json": "./package.json"
|
|
18
|
-
},
|
|
19
|
-
"sideEffects": false,
|
|
20
|
-
"files": [
|
|
21
|
-
"src",
|
|
22
|
-
"dist",
|
|
23
|
-
"README.md",
|
|
24
|
-
"LICENSE",
|
|
25
|
-
"!**/__tests__",
|
|
26
|
-
"!**/.*"
|
|
27
|
-
],
|
|
28
|
-
"scripts": {
|
|
29
|
-
"prebuild": "node -e \"require('fs').rmSync('dist', { recursive: true, force: true })\"",
|
|
30
|
-
"build": "tsc",
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
"url": "https://github.com/marioprieta/react-native-theme-transition
|
|
67
|
-
},
|
|
68
|
-
"
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
"react
|
|
75
|
-
"react-native
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
"
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
"
|
|
82
|
-
"react
|
|
83
|
-
"react
|
|
84
|
-
"react-native
|
|
85
|
-
"
|
|
86
|
-
|
|
87
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "react-native-theme-transition",
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"description": "Smooth, animated theme and dark mode transitions for React Native. Expo Go compatible, powered by Reanimated.",
|
|
5
|
+
"source": "./src/index.ts",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"react-native": "./src/index.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"react-native": "./src/index.ts",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./package.json": "./package.json"
|
|
18
|
+
},
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"files": [
|
|
21
|
+
"src",
|
|
22
|
+
"dist",
|
|
23
|
+
"README.md",
|
|
24
|
+
"LICENSE",
|
|
25
|
+
"!**/__tests__",
|
|
26
|
+
"!**/.*"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"prebuild": "node -e \"require('fs').rmSync('dist', { recursive: true, force: true })\"",
|
|
30
|
+
"build": "tsc",
|
|
31
|
+
"prepack": "cp README.npm.md README.md",
|
|
32
|
+
"postpack": "git checkout README.md",
|
|
33
|
+
"prepublishOnly": "npm run build",
|
|
34
|
+
"release": "npm publish --provenance --access public",
|
|
35
|
+
"release:dry": "npm publish --provenance --access public --dry-run"
|
|
36
|
+
},
|
|
37
|
+
"keywords": [
|
|
38
|
+
"react-native",
|
|
39
|
+
"react",
|
|
40
|
+
"ios",
|
|
41
|
+
"android",
|
|
42
|
+
"theme",
|
|
43
|
+
"theming",
|
|
44
|
+
"dark-mode",
|
|
45
|
+
"light-mode",
|
|
46
|
+
"switch",
|
|
47
|
+
"toggle",
|
|
48
|
+
"appearance",
|
|
49
|
+
"design-system",
|
|
50
|
+
"animation",
|
|
51
|
+
"transition",
|
|
52
|
+
"view-transition",
|
|
53
|
+
"react-native-reanimated",
|
|
54
|
+
"reanimated",
|
|
55
|
+
"screenshot",
|
|
56
|
+
"overlay",
|
|
57
|
+
"expo",
|
|
58
|
+
"expo-go",
|
|
59
|
+
"fabric",
|
|
60
|
+
"new-architecture",
|
|
61
|
+
"ui"
|
|
62
|
+
],
|
|
63
|
+
"license": "MIT",
|
|
64
|
+
"repository": {
|
|
65
|
+
"type": "git",
|
|
66
|
+
"url": "git+https://github.com/marioprieta/react-native-theme-transition.git"
|
|
67
|
+
},
|
|
68
|
+
"bugs": {
|
|
69
|
+
"url": "https://github.com/marioprieta/react-native-theme-transition/issues"
|
|
70
|
+
},
|
|
71
|
+
"homepage": "https://react-native-theme-transition.vercel.app",
|
|
72
|
+
"author": "Mario Prieta",
|
|
73
|
+
"peerDependencies": {
|
|
74
|
+
"react": ">=18.0.0",
|
|
75
|
+
"react-native": ">=0.76.0",
|
|
76
|
+
"react-native-reanimated": ">=4.0.0",
|
|
77
|
+
"react-native-view-shot": ">=3.0.0",
|
|
78
|
+
"react-native-worklets": ">=0.5.0"
|
|
79
|
+
},
|
|
80
|
+
"devDependencies": {
|
|
81
|
+
"@changesets/cli": "^2.30.0",
|
|
82
|
+
"@types/react": "~19.2.2",
|
|
83
|
+
"react": "19.2.0",
|
|
84
|
+
"react-native": "0.83.2",
|
|
85
|
+
"react-native-reanimated": "4.2.1",
|
|
86
|
+
"react-native-view-shot": "4.0.3",
|
|
87
|
+
"react-native-worklets": "0.7.2",
|
|
88
|
+
"typescript": "~5.9.2"
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -1,92 +1,92 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Factory that wires the theme transition provider, context, and hooks.
|
|
3
|
-
*
|
|
4
|
-
* @remarks
|
|
5
|
-
* Validates theme configuration at initialization: ensures at least one theme
|
|
6
|
-
* exists and that all themes share identical token keys.
|
|
7
|
-
* Returns a self-contained API ({@link ThemeTransitionAPI}) with no singletons,
|
|
8
|
-
* so multiple theme scopes can coexist in the same app.
|
|
9
|
-
*
|
|
10
|
-
* @module
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import type {
|
|
14
|
-
ThemeTransitionAPI,
|
|
15
|
-
ThemeTransitionConfig,
|
|
16
|
-
ThemeDefinition,
|
|
17
|
-
ThemeNames,
|
|
18
|
-
} from './types';
|
|
19
|
-
import { createProviderAndContext } from './transitionEngine';
|
|
20
|
-
import { createUseTheme } from './hooks/useTheme';
|
|
21
|
-
import { TAG } from './constants';
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Creates a fully typed theme transition system.
|
|
25
|
-
*
|
|
26
|
-
* @typeParam T - Your application's theme map, keyed by theme name.
|
|
27
|
-
* @param config - Theme configuration including available themes and defaults.
|
|
28
|
-
* @returns A provider component and hooks scoped to the supplied themes.
|
|
29
|
-
*
|
|
30
|
-
* @example
|
|
31
|
-
* ```tsx
|
|
32
|
-
* const { ThemeTransitionProvider, useTheme } = createThemeTransition({
|
|
33
|
-
* themes: { light: { bg: '#fff' }, dark: { bg: '#000' } },
|
|
34
|
-
* });
|
|
35
|
-
* ```
|
|
36
|
-
*/
|
|
37
|
-
export function createThemeTransition<
|
|
38
|
-
T extends Record<string, ThemeDefinition>,
|
|
39
|
-
>(config: ThemeTransitionConfig<T>): ThemeTransitionAPI<T> {
|
|
40
|
-
const themeNames = Object.keys(config.themes) as ThemeNames<T>[];
|
|
41
|
-
|
|
42
|
-
if (themeNames.length === 0) {
|
|
43
|
-
throw new Error(`${TAG} \`themes\` must contain at least one theme.`);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if ('system' in config.themes) {
|
|
47
|
-
throw new Error(
|
|
48
|
-
`${TAG} \`"system"\` is a reserved name and cannot be used as a theme key. Rename the theme and use \`systemThemeMap\` to map OS appearance to it.`,
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const referenceTheme = themeNames[0];
|
|
53
|
-
const referenceKeys = Object.keys(config.themes[referenceTheme]).sort();
|
|
54
|
-
for (const name of themeNames) {
|
|
55
|
-
if (name === referenceTheme) continue;
|
|
56
|
-
const keys = Object.keys(config.themes[name]).sort();
|
|
57
|
-
if (keys.length !== referenceKeys.length || keys.some((k, i) => k !== referenceKeys[i])) {
|
|
58
|
-
throw new Error(
|
|
59
|
-
`${TAG} Theme "${name}" has different token keys than "${referenceTheme}". All themes must share identical keys.`,
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (config.duration != null && (typeof config.duration !== 'number' || !isFinite(config.duration) || config.duration < 0)) {
|
|
65
|
-
throw new Error(
|
|
66
|
-
`${TAG} \`duration\` must be a finite non-negative number.`,
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (config.systemThemeMap) {
|
|
71
|
-
if (!('light' in config.systemThemeMap) || !('dark' in config.systemThemeMap)) {
|
|
72
|
-
throw new Error(
|
|
73
|
-
`${TAG} \`systemThemeMap\` must provide both \`light\` and \`dark\` keys.`,
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
for (const [scheme, name] of Object.entries(config.systemThemeMap)) {
|
|
78
|
-
if (!(name in config.themes)) {
|
|
79
|
-
throw new Error(
|
|
80
|
-
`${TAG} \`systemThemeMap.${scheme}\` maps to "${name}" which does not exist in themes.`,
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const { Context, ThemeTransitionProvider } = createProviderAndContext(config);
|
|
87
|
-
|
|
88
|
-
return {
|
|
89
|
-
ThemeTransitionProvider,
|
|
90
|
-
useTheme: createUseTheme<T>(Context),
|
|
91
|
-
};
|
|
92
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Factory that wires the theme transition provider, context, and hooks.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* Validates theme configuration at initialization: ensures at least one theme
|
|
6
|
+
* exists and that all themes share identical token keys.
|
|
7
|
+
* Returns a self-contained API ({@link ThemeTransitionAPI}) with no singletons,
|
|
8
|
+
* so multiple theme scopes can coexist in the same app.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type {
|
|
14
|
+
ThemeTransitionAPI,
|
|
15
|
+
ThemeTransitionConfig,
|
|
16
|
+
ThemeDefinition,
|
|
17
|
+
ThemeNames,
|
|
18
|
+
} from './types';
|
|
19
|
+
import { createProviderAndContext } from './transitionEngine';
|
|
20
|
+
import { createUseTheme } from './hooks/useTheme';
|
|
21
|
+
import { TAG } from './constants';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Creates a fully typed theme transition system.
|
|
25
|
+
*
|
|
26
|
+
* @typeParam T - Your application's theme map, keyed by theme name.
|
|
27
|
+
* @param config - Theme configuration including available themes and defaults.
|
|
28
|
+
* @returns A provider component and hooks scoped to the supplied themes.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```tsx
|
|
32
|
+
* const { ThemeTransitionProvider, useTheme } = createThemeTransition({
|
|
33
|
+
* themes: { light: { bg: '#fff' }, dark: { bg: '#000' } },
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export function createThemeTransition<
|
|
38
|
+
T extends Record<string, ThemeDefinition>,
|
|
39
|
+
>(config: ThemeTransitionConfig<T>): ThemeTransitionAPI<T> {
|
|
40
|
+
const themeNames = Object.keys(config.themes) as ThemeNames<T>[];
|
|
41
|
+
|
|
42
|
+
if (themeNames.length === 0) {
|
|
43
|
+
throw new Error(`${TAG} \`themes\` must contain at least one theme.`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if ('system' in config.themes) {
|
|
47
|
+
throw new Error(
|
|
48
|
+
`${TAG} \`"system"\` is a reserved name and cannot be used as a theme key. Rename the theme and use \`systemThemeMap\` to map OS appearance to it.`,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const referenceTheme = themeNames[0];
|
|
53
|
+
const referenceKeys = Object.keys(config.themes[referenceTheme]).sort();
|
|
54
|
+
for (const name of themeNames) {
|
|
55
|
+
if (name === referenceTheme) continue;
|
|
56
|
+
const keys = Object.keys(config.themes[name]).sort();
|
|
57
|
+
if (keys.length !== referenceKeys.length || keys.some((k, i) => k !== referenceKeys[i])) {
|
|
58
|
+
throw new Error(
|
|
59
|
+
`${TAG} Theme "${name}" has different token keys than "${referenceTheme}". All themes must share identical keys.`,
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (config.duration != null && (typeof config.duration !== 'number' || !isFinite(config.duration) || config.duration < 0)) {
|
|
65
|
+
throw new Error(
|
|
66
|
+
`${TAG} \`duration\` must be a finite non-negative number.`,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (config.systemThemeMap) {
|
|
71
|
+
if (!('light' in config.systemThemeMap) || !('dark' in config.systemThemeMap)) {
|
|
72
|
+
throw new Error(
|
|
73
|
+
`${TAG} \`systemThemeMap\` must provide both \`light\` and \`dark\` keys.`,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
for (const [scheme, name] of Object.entries(config.systemThemeMap)) {
|
|
78
|
+
if (!(name in config.themes)) {
|
|
79
|
+
throw new Error(
|
|
80
|
+
`${TAG} \`systemThemeMap.${scheme}\` maps to "${name}" which does not exist in themes.`,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const { Context, ThemeTransitionProvider } = createProviderAndContext(config);
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
ThemeTransitionProvider,
|
|
90
|
+
useTheme: createUseTheme<T>(Context),
|
|
91
|
+
};
|
|
92
|
+
}
|