@codefast/theme 0.3.10 → 0.3.11-canary.1
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 +22 -0
- package/README.md +231 -39
- package/dist/adapters/tanstack-start/index.d.ts +1 -1
- package/dist/adapters/tanstack-start/server.d.ts +1 -1
- package/dist/adapters/tanstack-start/server.js +4 -4
- package/dist/constants.d.ts +1 -1
- package/dist/constants.js +5 -5
- package/dist/core/context.d.ts +1 -1
- package/dist/core/index.d.ts +3 -3
- package/dist/core/provider.d.ts +2 -2
- package/dist/core/provider.js +6 -6
- package/dist/core/use-theme.d.ts +1 -1
- package/dist/core/use-theme.js +1 -1
- package/dist/index.d.ts +6 -6
- package/dist/script/index.d.ts +1 -1
- package/dist/script/theme-script.d.ts +2 -2
- package/dist/types.d.ts +2 -2
- package/dist/types.js +3 -3
- package/dist/utils/dom.d.ts +1 -1
- package/dist/utils/dom.js +4 -4
- package/dist/utils/index.d.ts +2 -2
- package/dist/utils/system.d.ts +1 -1
- package/dist/utils/system.js +2 -2
- package/package.json +41 -60
- package/dist/adapters/tanstack-start/index.cjs +0 -42
- package/dist/adapters/tanstack-start/server.cjs +0 -70
- package/dist/constants.cjs +0 -52
- package/dist/core/context.cjs +0 -37
- package/dist/core/index.cjs +0 -44
- package/dist/core/provider.cjs +0 -123
- package/dist/core/use-theme.cjs +0 -42
- package/dist/index.cjs +0 -76
- package/dist/script/index.cjs +0 -36
- package/dist/script/theme-script.cjs +0 -44
- package/dist/types.cjs +0 -45
- package/dist/utils/dom.cjs +0 -61
- package/dist/utils/index.cjs +0 -46
- package/dist/utils/system.cjs +0 -46
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# @codefast/theme
|
|
2
2
|
|
|
3
|
+
## 0.3.11-canary.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [`dd5130a`](https://github.com/codefastlabs/codefast/commit/dd5130adb5649dca6773614ffda262cdde732d8f) Thanks [@thevuong](https://github.com/thevuong)! - chore(deps): downgrade @rslib/core to 0.19.1 in package.json and pnpm-lock.yaml
|
|
8
|
+
|
|
9
|
+
- [`42758ab`](https://github.com/codefastlabs/codefast/commit/42758ab9d8f548ea9f4a806a7dbb7ed295fe2390) Thanks [@thevuong](https://github.com/thevuong)! - chore: update package configurations and TypeScript settings
|
|
10
|
+
|
|
11
|
+
- [`d2abc63`](https://github.com/codefastlabs/codefast/commit/d2abc636245526ec402811d8585d93684865dde6) Thanks [@thevuong](https://github.com/thevuong)! - chore(deps): update package versions and configurations
|
|
12
|
+
|
|
13
|
+
## 0.3.11-canary.0
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- [`135c6be`](https://github.com/codefastlabs/codefast/commit/135c6bed9d1bc1e96bad3173adab1bf32d74322f) Thanks [@thevuong](https://github.com/thevuong)! - chore: update linting and formatting scripts in package.json
|
|
18
|
+
|
|
19
|
+
- [`610cf7a`](https://github.com/codefastlabs/codefast/commit/610cf7a7f9b6e5b37be21e7675ea363ef1020639) Thanks [@thevuong](https://github.com/thevuong)! - fix(hooks): handle missing `navigator.clipboard.writeText` gracefully
|
|
20
|
+
|
|
21
|
+
- [`3e90474`](https://github.com/codefastlabs/codefast/commit/3e90474fbbb9f8ac626c5949bfdbac2eaf1cb3e8) Thanks [@thevuong](https://github.com/thevuong)! - chore: update project configuration and dependencies
|
|
22
|
+
|
|
23
|
+
- [`5c09d4c`](https://github.com/codefastlabs/codefast/commit/5c09d4ce2df564dc7ac6727b00cfa668164a1af4) Thanks [@thevuong](https://github.com/thevuong)! - refactor(config): rename `check-types` to `typecheck` and update usage
|
|
24
|
+
|
|
3
25
|
## 0.3.10
|
|
4
26
|
|
|
5
27
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -1,15 +1,45 @@
|
|
|
1
1
|
# @codefast/theme
|
|
2
2
|
|
|
3
|
-
Theme management
|
|
3
|
+
Theme management for React 19 applications with optimistic updates, cross-tab synchronization, and SSR support.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
[](https://github.com/codefastlabs/codefast/actions/workflows/release.yml)
|
|
6
|
+
[](https://www.npmjs.com/package/@codefast/theme)
|
|
7
|
+
[](https://www.npmjs.com/package/@codefast/theme)
|
|
8
|
+
[](https://bundlephobia.com/package/@codefast/theme)
|
|
9
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
10
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
11
|
+
## Table of Contents
|
|
12
|
+
|
|
13
|
+
- [Overview](#overview)
|
|
14
|
+
- [Installation](#installation)
|
|
15
|
+
- [Quick Start](#quick-start)
|
|
16
|
+
- [Usage](#usage)
|
|
17
|
+
- [TanStack Start](#tanstack-start)
|
|
18
|
+
- [Generic React (Client-Side)](#generic-react-client-side)
|
|
19
|
+
- [Theme Toggle Component](#theme-toggle-component)
|
|
20
|
+
- [SSR and FOUC Prevention](#ssr-and-fouc-prevention)
|
|
21
|
+
- [API Reference](#api-reference)
|
|
22
|
+
- [ThemeProvider](#themeprovider)
|
|
23
|
+
- [useTheme](#usetheme)
|
|
24
|
+
- [ThemeScript](#themescript)
|
|
25
|
+
- [resolveTheme](#resolvetheme)
|
|
26
|
+
- [themes](#themes)
|
|
27
|
+
- [Contributing](#contributing)
|
|
28
|
+
- [License](#license)
|
|
29
|
+
- [Changelog](#changelog)
|
|
30
|
+
|
|
31
|
+
## Overview
|
|
32
|
+
|
|
33
|
+
`@codefast/theme` provides a complete theme management solution built specifically for React 19. It leverages modern React APIs for the best possible user experience.
|
|
34
|
+
|
|
35
|
+
**Key features:**
|
|
36
|
+
|
|
37
|
+
- **React 19 Optimized** -- Uses `useOptimistic`, `useSyncExternalStore`, and `useEffectEvent` for seamless transitions.
|
|
38
|
+
- **System Theme Detection** -- Automatically detects and follows the operating system preference.
|
|
39
|
+
- **Cross-tab Sync** -- Theme changes propagate instantly across all open browser tabs.
|
|
40
|
+
- **Optimistic Updates** -- Immediate UI feedback during asynchronous theme persistence.
|
|
41
|
+
- **SSR Ready** -- Inline script prevents flash of unstyled content (FOUC) on server-rendered pages.
|
|
42
|
+
- **Framework Adapters** -- Built-in TanStack Start adapter with an extensible architecture for other frameworks.
|
|
13
43
|
|
|
14
44
|
## Installation
|
|
15
45
|
|
|
@@ -17,14 +47,64 @@ Theme management with React 19 features - optimistic updates, cross-tab sync, an
|
|
|
17
47
|
pnpm add @codefast/theme
|
|
18
48
|
```
|
|
19
49
|
|
|
50
|
+
Or using npm:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
npm install @codefast/theme
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
**Peer dependencies:**
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
pnpm add react react-dom
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
**Optional peer dependencies (for TanStack Start):**
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
pnpm add @tanstack/react-start
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
**Requirements:**
|
|
69
|
+
|
|
70
|
+
- Node.js >= 24.0.0 (LTS)
|
|
71
|
+
- React >= 19.0.0
|
|
72
|
+
- TypeScript >= 5.9.2 (recommended)
|
|
73
|
+
|
|
74
|
+
## Quick Start
|
|
75
|
+
|
|
76
|
+
```tsx
|
|
77
|
+
import { ThemeProvider, useTheme } from "@codefast/theme";
|
|
78
|
+
|
|
79
|
+
function App() {
|
|
80
|
+
return (
|
|
81
|
+
<ThemeProvider theme="system">
|
|
82
|
+
<Page />
|
|
83
|
+
</ThemeProvider>
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function Page() {
|
|
88
|
+
const { theme, setTheme } = useTheme();
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<button onClick={() => setTheme(theme === "dark" ? "light" : "dark")}>
|
|
92
|
+
Current theme: {theme}
|
|
93
|
+
</button>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
20
98
|
## Usage
|
|
21
99
|
|
|
22
100
|
### TanStack Start
|
|
23
101
|
|
|
102
|
+
The built-in TanStack Start adapter handles server-side theme persistence via cookies.
|
|
103
|
+
|
|
24
104
|
```tsx
|
|
25
105
|
// routes/__root.tsx
|
|
26
|
-
import { ThemeProvider, ThemeScript, resolveTheme } from
|
|
27
|
-
import { getThemeServerFn, setThemeServerFn } from
|
|
106
|
+
import { ThemeProvider, ThemeScript, resolveTheme } from "@codefast/theme";
|
|
107
|
+
import { getThemeServerFn, setThemeServerFn } from "@codefast/theme/tanstack-start";
|
|
28
108
|
|
|
29
109
|
export const Route = createRootRoute({
|
|
30
110
|
loader: async () => {
|
|
@@ -33,70 +113,182 @@ export const Route = createRootRoute({
|
|
|
33
113
|
},
|
|
34
114
|
component: RootComponent,
|
|
35
115
|
head: ({ loaderData }) => ({
|
|
36
|
-
scripts: [
|
|
37
|
-
{ tag: ThemeScript, props: { theme: loaderData?.theme ?? 'system' } },
|
|
38
|
-
],
|
|
116
|
+
scripts: [{ tag: ThemeScript, props: { theme: loaderData?.theme ?? "system" } }],
|
|
39
117
|
}),
|
|
40
118
|
});
|
|
41
119
|
|
|
42
120
|
function RootComponent() {
|
|
43
121
|
const { theme } = Route.useLoaderData();
|
|
44
122
|
|
|
123
|
+
return (
|
|
124
|
+
<ThemeProvider theme={theme} persistTheme={(value) => setThemeServerFn({ data: value })}>
|
|
125
|
+
<Outlet />
|
|
126
|
+
</ThemeProvider>
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Generic React (Client-Side)
|
|
132
|
+
|
|
133
|
+
For client-side React applications without a server framework, persist the theme to `localStorage`:
|
|
134
|
+
|
|
135
|
+
```tsx
|
|
136
|
+
import { ThemeProvider } from "@codefast/theme";
|
|
137
|
+
|
|
138
|
+
function App() {
|
|
139
|
+
const savedTheme = localStorage.getItem("theme") ?? "system";
|
|
140
|
+
|
|
45
141
|
return (
|
|
46
142
|
<ThemeProvider
|
|
47
|
-
theme={
|
|
48
|
-
persistTheme={(value) =>
|
|
143
|
+
theme={savedTheme}
|
|
144
|
+
persistTheme={async (value) => {
|
|
145
|
+
localStorage.setItem("theme", value);
|
|
146
|
+
}}
|
|
49
147
|
>
|
|
50
|
-
<
|
|
148
|
+
<YourApp />
|
|
51
149
|
</ThemeProvider>
|
|
52
150
|
);
|
|
53
151
|
}
|
|
54
152
|
```
|
|
55
153
|
|
|
56
|
-
###
|
|
154
|
+
### Theme Toggle Component
|
|
155
|
+
|
|
156
|
+
Build a theme switcher using the `useTheme` hook and the `themes` constant:
|
|
57
157
|
|
|
58
158
|
```tsx
|
|
59
|
-
import { useTheme, themes } from
|
|
159
|
+
import { useTheme, themes } from "@codefast/theme";
|
|
60
160
|
|
|
61
161
|
function ThemeToggle() {
|
|
62
162
|
const { theme, setTheme, isPending } = useTheme();
|
|
63
163
|
|
|
64
164
|
return (
|
|
65
|
-
<select
|
|
66
|
-
value={theme}
|
|
67
|
-
onChange={(e) => setTheme(e.target.value)}
|
|
68
|
-
disabled={isPending}
|
|
69
|
-
>
|
|
165
|
+
<select value={theme} onChange={(e) => setTheme(e.target.value)} disabled={isPending}>
|
|
70
166
|
{themes.map((t) => (
|
|
71
|
-
<option key={t} value={t}>
|
|
167
|
+
<option key={t} value={t}>
|
|
168
|
+
{t.charAt(0).toUpperCase() + t.slice(1)}
|
|
169
|
+
</option>
|
|
72
170
|
))}
|
|
73
171
|
</select>
|
|
74
172
|
);
|
|
75
173
|
}
|
|
76
174
|
```
|
|
77
175
|
|
|
78
|
-
##
|
|
176
|
+
## SSR and FOUC Prevention
|
|
177
|
+
|
|
178
|
+
When server-rendering your application, include `ThemeScript` in the `<head>` to prevent a flash of unstyled content. The script runs synchronously before the page paints, applying the correct theme class to the `<html>` element.
|
|
179
|
+
|
|
180
|
+
```tsx
|
|
181
|
+
import { ThemeScript } from "@codefast/theme";
|
|
182
|
+
|
|
183
|
+
// In your HTML head (framework-specific)
|
|
184
|
+
<ThemeScript theme="system" />;
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
For Content Security Policy (CSP) environments, pass a `nonce` to both `ThemeScript` and `ThemeProvider`:
|
|
188
|
+
|
|
189
|
+
```tsx
|
|
190
|
+
<ThemeScript theme="system" nonce={cspNonce} />
|
|
191
|
+
<ThemeProvider theme="system" nonce={cspNonce}>
|
|
192
|
+
<App />
|
|
193
|
+
</ThemeProvider>
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## API Reference
|
|
79
197
|
|
|
80
198
|
### ThemeProvider
|
|
81
199
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
200
|
+
Root provider component that manages theme state and context.
|
|
201
|
+
|
|
202
|
+
```tsx
|
|
203
|
+
<ThemeProvider
|
|
204
|
+
theme="system"
|
|
205
|
+
persistTheme={async (value) => {
|
|
206
|
+
/* save theme */
|
|
207
|
+
}}
|
|
208
|
+
disableTransitionOnChange={false}
|
|
209
|
+
nonce="abc123"
|
|
210
|
+
>
|
|
211
|
+
{children}
|
|
212
|
+
</ThemeProvider>
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
| Prop | Type | Default | Description |
|
|
216
|
+
| --------------------------- | --------------------------------- | ------- | -------------------------------------------------------------------- |
|
|
217
|
+
| `theme` | `Theme` | -- | Initial theme value (`'light'`, `'dark'`, or `'system'`) |
|
|
218
|
+
| `persistTheme` | `(value: Theme) => Promise<void>` | -- | Callback invoked when the user changes the theme |
|
|
219
|
+
| `disableTransitionOnChange` | `boolean` | `false` | Disable CSS transitions during theme changes to avoid visual flicker |
|
|
220
|
+
| `nonce` | `string` | -- | CSP nonce for inline `<style>` elements |
|
|
221
|
+
| `children` | `React.ReactNode` | -- | Application content |
|
|
88
222
|
|
|
89
223
|
### useTheme
|
|
90
224
|
|
|
91
|
-
|
|
225
|
+
Hook that returns the current theme state and a setter function.
|
|
226
|
+
|
|
227
|
+
```tsx
|
|
228
|
+
const { theme, resolvedTheme, setTheme, isPending } = useTheme();
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
| Property | Type | Description |
|
|
232
|
+
| --------------- | --------------------------------- | ------------------------------------------------------------------------- |
|
|
233
|
+
| `theme` | `Theme` | Current theme preference (`'light'`, `'dark'`, or `'system'`) |
|
|
234
|
+
| `resolvedTheme` | `ResolvedTheme` | Resolved theme after evaluating system preference (`'light'` or `'dark'`) |
|
|
235
|
+
| `setTheme` | `(value: Theme) => Promise<void>` | Function to update the theme |
|
|
236
|
+
| `isPending` | `boolean` | `true` while an optimistic theme update is in progress |
|
|
237
|
+
|
|
238
|
+
### ThemeScript
|
|
239
|
+
|
|
240
|
+
Inline script component for SSR that prevents FOUC by applying the theme before first paint.
|
|
92
241
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
|
98
|
-
|
|
|
242
|
+
```tsx
|
|
243
|
+
<ThemeScript theme="system" nonce="optional-csp-nonce" />
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
| Prop | Type | Description |
|
|
247
|
+
| ------- | -------- | ---------------------------------- |
|
|
248
|
+
| `theme` | `Theme` | The theme to apply on initial load |
|
|
249
|
+
| `nonce` | `string` | Optional CSP nonce |
|
|
250
|
+
|
|
251
|
+
### resolveTheme
|
|
252
|
+
|
|
253
|
+
Utility function that resolves a theme value to either `'light'` or `'dark'`.
|
|
254
|
+
|
|
255
|
+
```typescript
|
|
256
|
+
import { resolveTheme } from "@codefast/theme";
|
|
257
|
+
|
|
258
|
+
const resolved = resolveTheme("system"); // 'light' or 'dark' based on OS preference
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### themes
|
|
262
|
+
|
|
263
|
+
A constant array containing all available theme values.
|
|
264
|
+
|
|
265
|
+
```typescript
|
|
266
|
+
import { themes } from "@codefast/theme";
|
|
267
|
+
|
|
268
|
+
console.log(themes); // ['light', 'dark', 'system']
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## Contributing
|
|
272
|
+
|
|
273
|
+
We welcome contributions! Please see the [contributing guide](../../README.md#contributing) in the root of this repository for detailed instructions on how to get started.
|
|
274
|
+
|
|
275
|
+
For package-specific development:
|
|
276
|
+
|
|
277
|
+
```bash
|
|
278
|
+
# Development mode with watch
|
|
279
|
+
pnpm dev --filter=@codefast/theme
|
|
280
|
+
|
|
281
|
+
# Run tests
|
|
282
|
+
pnpm test --filter=@codefast/theme
|
|
283
|
+
|
|
284
|
+
# Run tests with coverage
|
|
285
|
+
pnpm test:coverage --filter=@codefast/theme
|
|
286
|
+
```
|
|
99
287
|
|
|
100
288
|
## License
|
|
101
289
|
|
|
102
|
-
MIT
|
|
290
|
+
Distributed under the MIT License. See [LICENSE](../../LICENSE) for more details.
|
|
291
|
+
|
|
292
|
+
## Changelog
|
|
293
|
+
|
|
294
|
+
See [CHANGELOG.md](./CHANGELOG.md) for a complete list of changes and version history.
|
|
@@ -9,14 +9,14 @@ const getThemeServerFn = createServerFn().handler(()=>{
|
|
|
9
9
|
return DEFAULT_THEME;
|
|
10
10
|
});
|
|
11
11
|
const setThemeServerFn = createServerFn({
|
|
12
|
-
method:
|
|
12
|
+
method: "POST"
|
|
13
13
|
}).inputValidator(themeSchema).handler(({ data })=>{
|
|
14
14
|
setCookie(THEME_STORAGE_KEY, data, {
|
|
15
15
|
httpOnly: true,
|
|
16
16
|
maxAge: 31536000,
|
|
17
|
-
path:
|
|
18
|
-
sameSite:
|
|
19
|
-
secure:
|
|
17
|
+
path: "/",
|
|
18
|
+
sameSite: "lax",
|
|
19
|
+
secure: "production" === process.env.NODE_ENV
|
|
20
20
|
});
|
|
21
21
|
});
|
|
22
22
|
const persistThemeHandler = async (value)=>{
|
package/dist/constants.d.ts
CHANGED
package/dist/constants.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
const DEFAULT_THEME =
|
|
2
|
-
const DEFAULT_RESOLVED_THEME =
|
|
3
|
-
const THEME_STORAGE_KEY =
|
|
4
|
-
const MEDIA =
|
|
5
|
-
const THEME_CHANNEL =
|
|
1
|
+
const DEFAULT_THEME = "system";
|
|
2
|
+
const DEFAULT_RESOLVED_THEME = "dark";
|
|
3
|
+
const THEME_STORAGE_KEY = "ui-theme";
|
|
4
|
+
const MEDIA = "(prefers-color-scheme: dark)";
|
|
5
|
+
const THEME_CHANNEL = "theme-sync";
|
|
6
6
|
export { DEFAULT_RESOLVED_THEME, DEFAULT_THEME, MEDIA, THEME_CHANNEL, THEME_STORAGE_KEY };
|
package/dist/core/context.d.ts
CHANGED
package/dist/core/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { ThemeContext } from
|
|
2
|
-
export { ThemeProvider } from
|
|
3
|
-
export { useTheme } from
|
|
1
|
+
export { ThemeContext } from "./context";
|
|
2
|
+
export { ThemeProvider } from "./provider";
|
|
3
|
+
export { useTheme } from "./use-theme";
|
package/dist/core/provider.d.ts
CHANGED
package/dist/core/provider.js
CHANGED
|
@@ -5,9 +5,9 @@ import { ThemeContext } from "./context.js";
|
|
|
5
5
|
import { applyTheme, disableAnimation, getSystemTheme } from "../utils/index.js";
|
|
6
6
|
function subscribeToSystemTheme(callback) {
|
|
7
7
|
const mediaQuery = window.matchMedia(MEDIA);
|
|
8
|
-
mediaQuery.addEventListener(
|
|
8
|
+
mediaQuery.addEventListener("change", callback);
|
|
9
9
|
return ()=>{
|
|
10
|
-
mediaQuery.removeEventListener(
|
|
10
|
+
mediaQuery.removeEventListener("change", callback);
|
|
11
11
|
};
|
|
12
12
|
}
|
|
13
13
|
function getSystemThemeSnapshot() {
|
|
@@ -21,7 +21,7 @@ function ThemeProvider({ children, disableTransitionOnChange = false, nonce, per
|
|
|
21
21
|
const [optimisticTheme, setOptimisticTheme] = useOptimistic(theme);
|
|
22
22
|
const isPending = optimisticTheme !== theme;
|
|
23
23
|
const systemTheme = useSyncExternalStore(subscribeToSystemTheme, getSystemThemeSnapshot, getServerSnapshot);
|
|
24
|
-
const resolvedTheme = useMemo(()=>
|
|
24
|
+
const resolvedTheme = useMemo(()=>"system" === optimisticTheme ? systemTheme : optimisticTheme, [
|
|
25
25
|
optimisticTheme,
|
|
26
26
|
systemTheme
|
|
27
27
|
]);
|
|
@@ -39,9 +39,9 @@ function ThemeProvider({ children, disableTransitionOnChange = false, nonce, per
|
|
|
39
39
|
});
|
|
40
40
|
useEffect(()=>{
|
|
41
41
|
const channel = new BroadcastChannel(THEME_CHANNEL);
|
|
42
|
-
channel.addEventListener(
|
|
42
|
+
channel.addEventListener("message", handleCrossTabMessage);
|
|
43
43
|
return ()=>{
|
|
44
|
-
channel.removeEventListener(
|
|
44
|
+
channel.removeEventListener("message", handleCrossTabMessage);
|
|
45
45
|
channel.close();
|
|
46
46
|
};
|
|
47
47
|
}, []);
|
|
@@ -58,7 +58,7 @@ function ThemeProvider({ children, disableTransitionOnChange = false, nonce, per
|
|
|
58
58
|
channel.postMessage(value);
|
|
59
59
|
channel.close();
|
|
60
60
|
} catch (error) {
|
|
61
|
-
console.error(
|
|
61
|
+
console.error("Failed to set theme:", error);
|
|
62
62
|
} finally{
|
|
63
63
|
enable?.();
|
|
64
64
|
}
|
package/dist/core/use-theme.d.ts
CHANGED
package/dist/core/use-theme.js
CHANGED
|
@@ -2,7 +2,7 @@ import { use } from "react";
|
|
|
2
2
|
import { ThemeContext } from "./context.js";
|
|
3
3
|
function useTheme() {
|
|
4
4
|
const value = use(ThemeContext);
|
|
5
|
-
if (!value) throw new Error(
|
|
5
|
+
if (!value) throw new Error("useTheme must be used within a ThemeProvider");
|
|
6
6
|
return value;
|
|
7
7
|
}
|
|
8
8
|
export { useTheme };
|
package/dist/index.d.ts
CHANGED
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
* @see {@link useTheme} - Hook for consuming theme context
|
|
9
9
|
* @see {@link ThemeScript} - FOUC prevention script
|
|
10
10
|
*/
|
|
11
|
-
export type { ResolvedTheme, Theme, ThemeContextType } from
|
|
12
|
-
export { themes, themeSchema } from
|
|
13
|
-
export { DEFAULT_RESOLVED_THEME, DEFAULT_THEME, THEME_STORAGE_KEY } from
|
|
14
|
-
export { ThemeContext, ThemeProvider, useTheme } from
|
|
15
|
-
export { ThemeScript } from
|
|
16
|
-
export { applyTheme, disableAnimation, getSystemTheme, resolveTheme } from
|
|
11
|
+
export type { ResolvedTheme, Theme, ThemeContextType } from "./types";
|
|
12
|
+
export { themes, themeSchema } from "./types";
|
|
13
|
+
export { DEFAULT_RESOLVED_THEME, DEFAULT_THEME, THEME_STORAGE_KEY } from "./constants";
|
|
14
|
+
export { ThemeContext, ThemeProvider, useTheme } from "./core";
|
|
15
|
+
export { ThemeScript } from "./script";
|
|
16
|
+
export { applyTheme, disableAnimation, getSystemTheme, resolveTheme } from "./utils";
|
package/dist/script/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { ThemeScript } from
|
|
1
|
+
export { ThemeScript } from "./theme-script";
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { z } from
|
|
1
|
+
import { z } from "zod";
|
|
2
2
|
/**
|
|
3
3
|
* Zod schema for theme validation.
|
|
4
4
|
*
|
|
@@ -23,7 +23,7 @@ export type Theme = z.infer<typeof themeSchema>;
|
|
|
23
23
|
*
|
|
24
24
|
* When theme is 'system', this reflects the OS preference (light/dark).
|
|
25
25
|
*/
|
|
26
|
-
export type ResolvedTheme = Exclude<Theme,
|
|
26
|
+
export type ResolvedTheme = Exclude<Theme, "system">;
|
|
27
27
|
/**
|
|
28
28
|
* Array of all available theme options: `['light', 'dark', 'system']`.
|
|
29
29
|
*
|
package/dist/types.js
CHANGED
package/dist/utils/dom.d.ts
CHANGED
package/dist/utils/dom.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
function applyTheme(resolved) {
|
|
2
2
|
const root = window.document.documentElement;
|
|
3
|
-
root.classList.remove(
|
|
3
|
+
root.classList.remove("light", "dark", "system");
|
|
4
4
|
root.classList.add(resolved);
|
|
5
5
|
root.style.colorScheme = resolved;
|
|
6
6
|
}
|
|
7
7
|
function disableAnimation(nonce) {
|
|
8
8
|
if ("u" < typeof window) return ()=>{};
|
|
9
|
-
const prefersReducedMotion = window.matchMedia(
|
|
9
|
+
const prefersReducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
10
10
|
if (prefersReducedMotion) return ()=>{};
|
|
11
|
-
const css = document.createElement(
|
|
12
|
-
if (nonce) css.setAttribute(
|
|
11
|
+
const css = document.createElement("style");
|
|
12
|
+
if (nonce) css.setAttribute("nonce", nonce);
|
|
13
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
14
|
document.head.append(css);
|
|
15
15
|
return ()=>{
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { applyTheme, disableAnimation } from
|
|
2
|
-
export { getSystemTheme, resolveTheme } from
|
|
1
|
+
export { applyTheme, disableAnimation } from "./dom";
|
|
2
|
+
export { getSystemTheme, resolveTheme } from "./system";
|
package/dist/utils/system.d.ts
CHANGED
package/dist/utils/system.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { DEFAULT_RESOLVED_THEME, MEDIA } from "../constants.js";
|
|
2
2
|
function getSystemTheme() {
|
|
3
3
|
if ("u" < typeof window) return DEFAULT_RESOLVED_THEME;
|
|
4
|
-
return window.matchMedia(MEDIA).matches ?
|
|
4
|
+
return window.matchMedia(MEDIA).matches ? "dark" : "light";
|
|
5
5
|
}
|
|
6
6
|
function resolveTheme(theme) {
|
|
7
|
-
return
|
|
7
|
+
return "system" === theme ? getSystemTheme() : theme;
|
|
8
8
|
}
|
|
9
9
|
export { getSystemTheme, resolveTheme };
|