@rootnative/core 0.0.0-alpha.8 → 0.0.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/LICENSE +1 -1
- package/README.md +116 -45
- package/dist/chunk-OK7DBCK4.mjs +12 -0
- package/dist/{chunk-QQ3ME6R7.mjs → chunk-Y5CU4C2C.mjs} +29 -7
- package/dist/chunk-Y6FXYEAI.mjs +10 -0
- package/dist/create-theme/index.d.mts +1 -1
- package/dist/create-theme/index.mjs +5152 -19
- package/dist/index.d.mts +276 -20
- package/dist/index.mjs +214 -13
- package/dist/static-export/index.d.mts +119 -0
- package/dist/static-export/index.mjs +55 -0
- package/dist/{types-NiRbh4dM.d.mts → types-DbJZ6AOd.d.mts} +43 -9
- package/llms.txt +216 -26
- package/package.json +21 -17
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { T as Theme, a as TopAppBarTokens, B as BaseTheme, M as Motion, S as Shape } from './types-
|
|
2
|
-
export { C as Colors, E as Elevation, b as ElevationLevel, F as FontWeight, c as MaterialTheme, d as MotionSpring, e as ShadowOffset, f as Spacing, g as StateLayer, h as Typography, i as TypographyToken } from './types-
|
|
3
|
-
import { TransitionConfig, NamedTransitions } from '@rootnative/inertia';
|
|
1
|
+
import { T as Theme, a as TopAppBarTokens, B as BaseTheme, M as Motion, S as Shape } from './types-DbJZ6AOd.mjs';
|
|
2
|
+
export { C as Colors, E as Elevation, b as ElevationLevel, F as FontWeight, c as MaterialTheme, d as MotionSpring, e as ShadowOffset, f as Spacing, g as StateLayer, h as Typography, i as TypographyToken } from './types-DbJZ6AOd.mjs';
|
|
3
|
+
import { TransitionConfig, NamedTransitions, MotionConfigValue } from '@rootnative/inertia';
|
|
4
4
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
5
5
|
import * as React from 'react';
|
|
6
|
+
import { ScaledSize } from 'react-native';
|
|
6
7
|
|
|
7
8
|
declare const lightTheme: Theme;
|
|
8
9
|
|
|
@@ -11,18 +12,26 @@ declare const darkTheme: Theme;
|
|
|
11
12
|
declare const defaultTopAppBarTokens: TopAppBarTokens;
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
|
-
* Identity function that validates a
|
|
15
|
-
* Provides type-checking and autocompletion
|
|
15
|
+
* Identity function that validates a theme object against `BaseTheme`.
|
|
16
|
+
* Provides type-checking and autocompletion for a theme with your own roles.
|
|
17
|
+
*
|
|
18
|
+
* A theme with your own role names is for your own components. The
|
|
19
|
+
* components of `@rootnative/components` read the MD3 roles, so they need a
|
|
20
|
+
* theme that keeps them: spread `lightTheme`, and add your tokens beside it.
|
|
16
21
|
*
|
|
17
22
|
* @example
|
|
23
|
+
* // Your own roles, for your own components
|
|
18
24
|
* const myTheme = defineTheme({
|
|
19
25
|
* colors: { brand: '#FF6B00', background: '#FFFFFF', text: '#1A1A1A' },
|
|
20
26
|
* typography: { heading: { fontFamily: 'Inter', fontSize: 24, fontWeight: '700', lineHeight: 32, letterSpacing: 0 } },
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
27
|
+
* radius: { control: 8, card: 16 },
|
|
28
|
+
* })
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* // The MD3 roles plus your own tokens, for the library components too
|
|
32
|
+
* const appTheme = defineTheme({
|
|
33
|
+
* ...lightTheme,
|
|
34
|
+
* status: { success: '#388E3C', onSuccess: '#FFFFFF' },
|
|
26
35
|
* })
|
|
27
36
|
*/
|
|
28
37
|
declare function defineTheme<T extends BaseTheme>(theme: T): T;
|
|
@@ -40,6 +49,8 @@ declare module '@rootnative/inertia' {
|
|
|
40
49
|
'spring-slow-effects': TransitionConfig;
|
|
41
50
|
}
|
|
42
51
|
}
|
|
52
|
+
declare const SPRING_TOKENS: readonly ["springFastSpatial", "springDefaultSpatial", "springSlowSpatial", "springFastEffects", "springDefaultEffects", "springSlowEffects"];
|
|
53
|
+
type MotionSprings = Pick<Motion, (typeof SPRING_TOKENS)[number]>;
|
|
43
54
|
/**
|
|
44
55
|
* Map a theme's motion tokens into inertia's named-transition registry.
|
|
45
56
|
* `ThemeProvider` feeds the result to `<MotionConfig transitions>`, making
|
|
@@ -52,7 +63,7 @@ declare module '@rootnative/inertia' {
|
|
|
52
63
|
* fast-effects spring (small-element effects, critically damped). The
|
|
53
64
|
* spring names expose the theme's `MotionSpring` tokens verbatim.
|
|
54
65
|
*/
|
|
55
|
-
declare function motionTransitions(motion:
|
|
66
|
+
declare function motionTransitions(motion: MotionSprings): NamedTransitions;
|
|
56
67
|
|
|
57
68
|
/**
|
|
58
69
|
* Scales the intermediate corner radius tokens by a `roundness` multiplier.
|
|
@@ -88,8 +99,8 @@ type IconResolver = (name: string, props: IconRenderProps) => React.ReactNode;
|
|
|
88
99
|
* Anything a component will accept for an icon prop:
|
|
89
100
|
*
|
|
90
101
|
* - **string** — a name resolved via the `iconResolver` registered on
|
|
91
|
-
* `ThemeProvider`.
|
|
92
|
-
* resolver is set
|
|
102
|
+
* `ThemeProvider`. Renders nothing, with one warning, when no
|
|
103
|
+
* resolver is set.
|
|
93
104
|
* - **ReactElement** — a pre-rendered icon. The caller is responsible for
|
|
94
105
|
* passing size and color; the component will not override them.
|
|
95
106
|
* - **(props) => ReactNode** — a render function that receives the
|
|
@@ -98,17 +109,128 @@ type IconResolver = (name: string, props: IconRenderProps) => React.ReactNode;
|
|
|
98
109
|
*/
|
|
99
110
|
type IconSource = string | React.ReactElement | ((props: IconRenderProps) => React.ReactNode);
|
|
100
111
|
|
|
112
|
+
/**
|
|
113
|
+
* What the app asks for. `'system'` follows the OS setting and keeps
|
|
114
|
+
* following it as the OS changes; `'light'` / `'dark'` pin the scheme.
|
|
115
|
+
*/
|
|
116
|
+
type ThemeMode = 'system' | 'light' | 'dark';
|
|
117
|
+
/** What the app actually renders. `'system'` is always resolved away. */
|
|
118
|
+
type ColorScheme = 'light' | 'dark';
|
|
119
|
+
/**
|
|
120
|
+
* Persists the user's mode choice across launches. Deliberately structural
|
|
121
|
+
* rather than a dependency on any one storage library: pass an
|
|
122
|
+
* AsyncStorage-shaped object, MMKV, `localStorage`, SecureStore, or your own
|
|
123
|
+
* wrapper. `@rootnative/core` takes no storage dependency of its own.
|
|
124
|
+
*
|
|
125
|
+
* Both methods may be sync or async — the provider awaits either.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* import AsyncStorage from '@react-native-async-storage/async-storage'
|
|
129
|
+
*
|
|
130
|
+
* <ThemeProvider theme={{ light, dark }} storage={AsyncStorage}>
|
|
131
|
+
*/
|
|
132
|
+
interface ThemeModeStorage {
|
|
133
|
+
getItem: (key: string) => string | null | Promise<string | null>;
|
|
134
|
+
setItem: (key: string, value: string) => unknown | Promise<unknown>;
|
|
135
|
+
}
|
|
136
|
+
interface ThemeModeContextValue {
|
|
137
|
+
/** The requested mode, including `'system'`. */
|
|
138
|
+
mode: ThemeMode;
|
|
139
|
+
/**
|
|
140
|
+
* The resolved scheme actually in use. `mode: 'system'` resolves this from
|
|
141
|
+
* the OS; `'light'` / `'dark'` pass straight through. Use this to sync
|
|
142
|
+
* anything outside the theme — most commonly the status bar.
|
|
143
|
+
*/
|
|
144
|
+
scheme: ColorScheme;
|
|
145
|
+
/** Sets the mode, and persists it when a `storage` is configured. */
|
|
146
|
+
setMode: (mode: ThemeMode) => void;
|
|
147
|
+
/**
|
|
148
|
+
* `false` until a persisted mode has been read back from `storage`. Always
|
|
149
|
+
* `true` when no `storage` is configured, since there is nothing to wait
|
|
150
|
+
* for. Gate a splash screen on this to avoid a light-to-dark flash on
|
|
151
|
+
* launch.
|
|
152
|
+
*/
|
|
153
|
+
isReady: boolean;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* A light/dark pair. Matches what `createMaterialTheme()` returns, so its
|
|
158
|
+
* result can be renamed straight into the prop:
|
|
159
|
+
* `const { lightTheme: light, darkTheme: dark } = createMaterialTheme(seed)`.
|
|
160
|
+
*/
|
|
161
|
+
interface ThemePair {
|
|
162
|
+
light: BaseTheme;
|
|
163
|
+
dark: BaseTheme;
|
|
164
|
+
}
|
|
101
165
|
interface ThemeProviderProps {
|
|
102
166
|
/**
|
|
103
167
|
* Theme object to provide to all child components via context.
|
|
104
|
-
* Accepts any theme extending `BaseTheme
|
|
168
|
+
* Accepts any theme extending `BaseTheme`: Material Design 3, or a theme
|
|
169
|
+
* with your own roles for your own components.
|
|
170
|
+
*
|
|
171
|
+
* Pass a `{ light, dark }` pair to enable light/dark switching: the provider
|
|
172
|
+
* then resolves which one to use from `mode` plus the OS setting, and
|
|
173
|
+
* `useThemeMode()` becomes available to descendants.
|
|
174
|
+
*
|
|
105
175
|
* @default lightTheme (Material Design 3)
|
|
106
176
|
*/
|
|
107
|
-
theme?: BaseTheme;
|
|
177
|
+
theme?: BaseTheme | ThemePair;
|
|
178
|
+
/**
|
|
179
|
+
* Which theme of the pair to use. `'system'` follows the OS and keeps
|
|
180
|
+
* following it; `'light'` / `'dark'` pin it. Ignored when `theme` is a
|
|
181
|
+
* single theme.
|
|
182
|
+
*
|
|
183
|
+
* Leave uncontrolled (the default) and drive it with `setMode()` from
|
|
184
|
+
* `useThemeMode()`. Pass it to control the mode from your own state — the
|
|
185
|
+
* provider then never changes it on its own, so pair it with `onModeChange`.
|
|
186
|
+
*
|
|
187
|
+
* On a static web export, `'system'` renders light on the server and while
|
|
188
|
+
* the client hydrates, then switches to the OS scheme on the render after
|
|
189
|
+
* hydration. The server cannot know the scheme, so a dark-scheme reader sees
|
|
190
|
+
* the light theme until hydration ends. `schemeGuardCss` from
|
|
191
|
+
* `@rootnative/core/static-export` hides that light paint.
|
|
192
|
+
*
|
|
193
|
+
* A nested provider with a pair, under a parent provider with a pair,
|
|
194
|
+
* follows the mode of the parent when it gets none of `mode`, `defaultMode`
|
|
195
|
+
* and `storage`. Its `setMode()` then sets the mode of the parent.
|
|
196
|
+
*
|
|
197
|
+
* @default 'system'
|
|
198
|
+
*/
|
|
199
|
+
mode?: ThemeMode;
|
|
200
|
+
/**
|
|
201
|
+
* Initial mode for the uncontrolled case. A persisted value from `storage`
|
|
202
|
+
* wins over this once it loads. On a nested provider, setting it gives the
|
|
203
|
+
* provider a mode of its own.
|
|
204
|
+
*
|
|
205
|
+
* @default 'system'
|
|
206
|
+
*/
|
|
207
|
+
defaultMode?: ThemeMode;
|
|
208
|
+
/** Called whenever `setMode()` runs, controlled or not. */
|
|
209
|
+
onModeChange?: (mode: ThemeMode) => void;
|
|
210
|
+
/**
|
|
211
|
+
* Persists the mode across launches. Any object with `getItem`/`setItem` —
|
|
212
|
+
* AsyncStorage, MMKV, `localStorage`. `@rootnative/core` takes no storage
|
|
213
|
+
* dependency of its own, so nothing is persisted unless you pass this.
|
|
214
|
+
*
|
|
215
|
+
* Ignored when `mode` is controlled: your own state is then the source of
|
|
216
|
+
* truth and persisting behind it would fight it.
|
|
217
|
+
*
|
|
218
|
+
* On a nested provider, setting it gives the provider a mode of its own.
|
|
219
|
+
* Every provider uses the same storage key, so a nested provider with
|
|
220
|
+
* `storage` overwrites the mode that the root provider stored.
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* import AsyncStorage from '@react-native-async-storage/async-storage'
|
|
224
|
+
*
|
|
225
|
+
* <ThemeProvider theme={{ light, dark }} storage={AsyncStorage}>
|
|
226
|
+
*/
|
|
227
|
+
storage?: ThemeModeStorage;
|
|
108
228
|
/**
|
|
109
229
|
* Resolves string icon names (e.g. `leadingIcon="check"`) to icon nodes.
|
|
110
230
|
* Set this once at the app root to use SF Symbols, Lucide, custom SVGs,
|
|
111
|
-
* etc.
|
|
231
|
+
* etc. `mdiResolver` from `@rootnative/components/mdi` gives MaterialDesignIcons.
|
|
232
|
+
*
|
|
233
|
+
* A nested provider without it uses the resolver of its parent.
|
|
112
234
|
*
|
|
113
235
|
* @example
|
|
114
236
|
* import { Check, ArrowRight } from 'lucide-react-native'
|
|
@@ -140,14 +262,34 @@ interface ThemeProviderProps {
|
|
|
140
262
|
* </ThemeProvider>
|
|
141
263
|
*
|
|
142
264
|
* @example
|
|
265
|
+
* // Light/dark, following the OS
|
|
266
|
+
* import { ThemeProvider, lightTheme, darkTheme } from '@rootnative/core'
|
|
267
|
+
*
|
|
268
|
+
* <ThemeProvider theme={{ light: lightTheme, dark: darkTheme }}>
|
|
269
|
+
* <App />
|
|
270
|
+
* </ThemeProvider>
|
|
271
|
+
*
|
|
272
|
+
* @example
|
|
143
273
|
* // Custom or Apple theme
|
|
144
274
|
* import { ThemeProvider } from '@rootnative/core'
|
|
145
275
|
*
|
|
146
276
|
* <ThemeProvider theme={myTheme}>
|
|
147
277
|
* <App />
|
|
148
278
|
* </ThemeProvider>
|
|
279
|
+
*
|
|
280
|
+
* @example
|
|
281
|
+
* // A nested pair for one screen. It follows the mode and the icon resolver
|
|
282
|
+
* // of the root provider.
|
|
283
|
+
* const { lightTheme: light, darkTheme: dark } = createMaterialTheme(seed)
|
|
284
|
+
*
|
|
285
|
+
* <ThemeProvider theme={{ light, dark }}>
|
|
286
|
+
* <Screen />
|
|
287
|
+
* </ThemeProvider>
|
|
149
288
|
*/
|
|
150
|
-
declare function ThemeProvider({ theme, iconResolver, children, }: ThemeProviderProps): react_jsx_runtime.JSX.Element;
|
|
289
|
+
declare function ThemeProvider({ theme, mode: controlledMode, defaultMode, onModeChange, storage, iconResolver, children, }: ThemeProviderProps): react_jsx_runtime.JSX.Element;
|
|
290
|
+
|
|
291
|
+
/** Storage key for the persisted mode. Namespaced to avoid app collisions. */
|
|
292
|
+
declare const THEME_MODE_STORAGE_KEY = "rootnative.theme-mode";
|
|
151
293
|
|
|
152
294
|
/**
|
|
153
295
|
* Returns the current theme from the nearest `ThemeProvider`.
|
|
@@ -166,13 +308,75 @@ declare function ThemeProvider({ theme, iconResolver, children, }: ThemeProvider
|
|
|
166
308
|
*/
|
|
167
309
|
declare function useTheme<T extends BaseTheme = Theme>(): T;
|
|
168
310
|
|
|
311
|
+
/**
|
|
312
|
+
* Reads and controls the light/dark mode of the nearest `ThemeProvider`.
|
|
313
|
+
*
|
|
314
|
+
* Requires a provider configured with a theme *pair* — `theme={{ light, dark }}`.
|
|
315
|
+
* A provider given a single theme has no mode to report, so this throws rather
|
|
316
|
+
* than inventing one.
|
|
317
|
+
*
|
|
318
|
+
* @example
|
|
319
|
+
* const { mode, scheme, setMode } = useThemeMode()
|
|
320
|
+
*
|
|
321
|
+
* <StatusBar style={scheme === 'dark' ? 'light' : 'dark'} />
|
|
322
|
+
* <Button onPress={() => setMode(scheme === 'dark' ? 'light' : 'dark')} />
|
|
323
|
+
*/
|
|
324
|
+
declare function useThemeMode(): ThemeModeContextValue;
|
|
325
|
+
|
|
169
326
|
/**
|
|
170
327
|
* Returns the icon resolver registered on the nearest `ThemeProvider`,
|
|
171
|
-
* or `null` if none was provided.
|
|
172
|
-
*
|
|
328
|
+
* or `null` if none was provided. A string icon renders nothing and logs one
|
|
329
|
+
* warning when no resolver is set.
|
|
173
330
|
*/
|
|
174
331
|
declare function useIconResolver(): IconResolver | null;
|
|
175
332
|
|
|
333
|
+
/**
|
|
334
|
+
* Everything a `ThemeProvider` gives its descendants, read at one place in
|
|
335
|
+
* the tree so that it can be provided again at another. Treat it as opaque:
|
|
336
|
+
* read it with `useThemeScope()` and hand it to `ThemeScopeProvider`.
|
|
337
|
+
*/
|
|
338
|
+
interface ThemeScope {
|
|
339
|
+
/** The resolved theme at the call site. */
|
|
340
|
+
readonly theme: BaseTheme;
|
|
341
|
+
/** The mode value at the call site; `null` under a single-theme provider. */
|
|
342
|
+
readonly mode: ThemeModeContextValue | null;
|
|
343
|
+
/** The icon resolver at the call site; `null` when none is set. */
|
|
344
|
+
readonly iconResolver: IconResolver | null;
|
|
345
|
+
/** The merged motion config at the call site. */
|
|
346
|
+
readonly motion: MotionConfigValue;
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Reads the theme, the mode, the icon resolver and the motion config of the
|
|
350
|
+
* nearest providers, as one value for `ThemeScopeProvider`.
|
|
351
|
+
*
|
|
352
|
+
* Use the pair when you render content at another place in the tree, for
|
|
353
|
+
* example through a portal. The content then reads the theme of the place
|
|
354
|
+
* where you wrote it, not the theme of the place where it renders.
|
|
355
|
+
* `@rootnative/components` does this in `Portal`, so every overlay follows
|
|
356
|
+
* the provider it is written under.
|
|
357
|
+
*
|
|
358
|
+
* @example
|
|
359
|
+
* function MyPortal({ children }) {
|
|
360
|
+
* const scope = useThemeScope()
|
|
361
|
+
* return createPortal(
|
|
362
|
+
* <ThemeScopeProvider scope={scope}>{children}</ThemeScopeProvider>,
|
|
363
|
+
* host,
|
|
364
|
+
* )
|
|
365
|
+
* }
|
|
366
|
+
*/
|
|
367
|
+
declare function useThemeScope(): ThemeScope;
|
|
368
|
+
interface ThemeScopeProviderProps {
|
|
369
|
+
/** The value that `useThemeScope()` returned at the place to copy. */
|
|
370
|
+
scope: ThemeScope;
|
|
371
|
+
children: React.ReactNode;
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Provides a `ThemeScope` to its descendants, as if the providers at the
|
|
375
|
+
* place that read it stood here. A `ThemeProvider` below it inherits from it
|
|
376
|
+
* the same way it inherits from a real parent provider.
|
|
377
|
+
*/
|
|
378
|
+
declare function ThemeScopeProvider({ scope, children, }: ThemeScopeProviderProps): react_jsx_runtime.JSX.Element;
|
|
379
|
+
|
|
176
380
|
/**
|
|
177
381
|
* Material Design 3 window size classes.
|
|
178
382
|
* @see https://m3.material.io/foundations/layout/applying-layout/window-size-classes
|
|
@@ -189,6 +393,30 @@ declare const breakpoints: {
|
|
|
189
393
|
* Returns the current Material Design 3 window size class based on viewport width.
|
|
190
394
|
* Reactively updates when the window is resized.
|
|
191
395
|
*
|
|
396
|
+
* **Hydration-safe by construction.** A static export renders on a server with
|
|
397
|
+
* no DOM, where react-native-web's `Dimensions` is hard-wired to `width: 0`,
|
|
398
|
+
* so every breakpoint resolves to `compact` and that is what ships in the
|
|
399
|
+
* HTML. On the client `Dimensions` reports the real width immediately, so
|
|
400
|
+
* without care the first client render disagrees with the markup it is
|
|
401
|
+
* hydrating — and **React does not repair that**. Measured against React
|
|
402
|
+
* 19.2.3: a `className` / `style` mismatch is adopted from the server DOM,
|
|
403
|
+
* left on screen, and reported through *zero* recoverable errors. A tablet
|
|
404
|
+
* gets the phone layout, permanently, with no console signal.
|
|
405
|
+
*
|
|
406
|
+
* So this hook reports `compact` for as long as the client is hydrating,
|
|
407
|
+
* matching the markup exactly, and switches to the measured breakpoint on the
|
|
408
|
+
* re-render React schedules once hydration finishes. The correction is an
|
|
409
|
+
* ordinary update, which *does* patch the DOM. The guard lives in this
|
|
410
|
+
* package's `useWindowDimensions`, which this hook reads. Use that hook, not
|
|
411
|
+
* the `react-native` one, when a layout needs the raw width.
|
|
412
|
+
*
|
|
413
|
+
* **It costs nothing where there is no hydration.** On native, and on a
|
|
414
|
+
* single-page web build, `getServerSnapshot` is never called, so the measured
|
|
415
|
+
* breakpoint is returned from the first render with no extra pass. Consumers
|
|
416
|
+
* need no `useHydrated` gate of their own, and neither does `Grid`, which
|
|
417
|
+
* resolves a breakpoint map internally and gives a consumer nowhere to put
|
|
418
|
+
* one.
|
|
419
|
+
*
|
|
192
420
|
* @example
|
|
193
421
|
* const breakpoint = useBreakpoint()
|
|
194
422
|
* const columns = breakpoint === 'compact' ? 2 : 4
|
|
@@ -207,6 +435,34 @@ type BreakpointValues<T> = Partial<Record<Breakpoint, T>> & Record<'compact', T>
|
|
|
207
435
|
*/
|
|
208
436
|
declare function useBreakpointValue<T>(values: BreakpointValues<T>): T;
|
|
209
437
|
|
|
438
|
+
/**
|
|
439
|
+
* Returns the window size, the same as `useWindowDimensions` from
|
|
440
|
+
* `react-native`, but safe to use in a static web export.
|
|
441
|
+
*
|
|
442
|
+
* **Use this instead of the `react-native` hook for any value that reaches a
|
|
443
|
+
* style or the tree.** A static export (`web.output: 'static'`) renders on a
|
|
444
|
+
* server with no DOM, where react-native-web measures a 0 by 0 window. The
|
|
445
|
+
* client measures the real window on its first render, so the `react-native`
|
|
446
|
+
* hook disagrees with the markup it hydrates, and React 19 keeps the server's
|
|
447
|
+
* style with no error. A carousel slot sized from the width, or a hero height
|
|
448
|
+
* set as a fraction of it, then keeps the layout of a 0 wide window until the
|
|
449
|
+
* reader resizes the window.
|
|
450
|
+
*
|
|
451
|
+
* This hook returns the server's 0 by 0 window while the client hydrates, and
|
|
452
|
+
* the measured window on the re-render React schedules after hydration. That
|
|
453
|
+
* re-render is an ordinary update, which does patch the DOM. On native, and on
|
|
454
|
+
* a single-page web build, nothing hydrates, so the measured window comes back
|
|
455
|
+
* from the first render with no extra pass.
|
|
456
|
+
*
|
|
457
|
+
* `useBreakpoint` reads this hook, so a breakpoint and a raw width cannot
|
|
458
|
+
* disagree during hydration.
|
|
459
|
+
*
|
|
460
|
+
* @example
|
|
461
|
+
* const { width } = useWindowDimensions()
|
|
462
|
+
* const heroHeight = width * 0.56
|
|
463
|
+
*/
|
|
464
|
+
declare function useWindowDimensions(): ScaledSize;
|
|
465
|
+
|
|
210
466
|
/**
|
|
211
467
|
* Material Design 3 preset — groups all MD3-specific theme values.
|
|
212
468
|
*
|
|
@@ -223,4 +479,4 @@ declare const material: {
|
|
|
223
479
|
readonly defaultTopAppBarTokens: TopAppBarTokens;
|
|
224
480
|
};
|
|
225
481
|
|
|
226
|
-
export { BaseTheme, type Breakpoint, type BreakpointValues, type IconRenderProps, type IconResolver, type IconSource, Motion, Shape, Theme, ThemeProvider, type ThemeProviderProps, TopAppBarTokens, applyRoundness, breakpoints, darkTheme, defaultTopAppBarTokens, defineTheme, lightTheme, material, motionTransitions, useBreakpoint, useBreakpointValue, useIconResolver, useTheme };
|
|
482
|
+
export { BaseTheme, type Breakpoint, type BreakpointValues, type ColorScheme, type IconRenderProps, type IconResolver, type IconSource, Motion, Shape, THEME_MODE_STORAGE_KEY, Theme, type ThemeMode, type ThemeModeContextValue, type ThemeModeStorage, type ThemePair, ThemeProvider, type ThemeProviderProps, type ThemeScope, ThemeScopeProvider, type ThemeScopeProviderProps, TopAppBarTokens, applyRoundness, breakpoints, darkTheme, defaultTopAppBarTokens, defineTheme, lightTheme, material, motionTransitions, useBreakpoint, useBreakpointValue, useIconResolver, useTheme, useThemeMode, useThemeScope, useWindowDimensions };
|
package/dist/index.mjs
CHANGED
|
@@ -3,7 +3,12 @@ import {
|
|
|
3
3
|
defaultTopAppBarTokens,
|
|
4
4
|
defaultTypography,
|
|
5
5
|
lightTheme
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-Y5CU4C2C.mjs";
|
|
7
|
+
import {
|
|
8
|
+
THEME_MODE_STORAGE_KEY,
|
|
9
|
+
THEME_READY_ATTRIBUTE
|
|
10
|
+
} from "./chunk-OK7DBCK4.mjs";
|
|
11
|
+
import "./chunk-Y6FXYEAI.mjs";
|
|
7
12
|
|
|
8
13
|
// src/theme/dark.ts
|
|
9
14
|
var darkTheme = {
|
|
@@ -188,6 +193,19 @@ function defineTheme(theme) {
|
|
|
188
193
|
}
|
|
189
194
|
|
|
190
195
|
// src/theme/motionAdapter.ts
|
|
196
|
+
var SPRING_TOKENS = [
|
|
197
|
+
"springFastSpatial",
|
|
198
|
+
"springDefaultSpatial",
|
|
199
|
+
"springSlowSpatial",
|
|
200
|
+
"springFastEffects",
|
|
201
|
+
"springDefaultEffects",
|
|
202
|
+
"springSlowEffects"
|
|
203
|
+
];
|
|
204
|
+
function hasMotionSprings(motion) {
|
|
205
|
+
if (typeof motion !== "object" || motion === null) return false;
|
|
206
|
+
const tokens = motion;
|
|
207
|
+
return SPRING_TOKENS.every((name) => typeof tokens[name] === "object");
|
|
208
|
+
}
|
|
191
209
|
function motionTransitions(motion) {
|
|
192
210
|
return {
|
|
193
211
|
"state-hover": { type: "spring", ...motion.springFastEffects },
|
|
@@ -210,7 +228,31 @@ function motionTransitions(motion) {
|
|
|
210
228
|
|
|
211
229
|
// src/provider/ThemeProvider.tsx
|
|
212
230
|
import { MotionConfig } from "@rootnative/inertia";
|
|
213
|
-
import * as
|
|
231
|
+
import * as React4 from "react";
|
|
232
|
+
import { useColorScheme } from "react-native";
|
|
233
|
+
|
|
234
|
+
// src/internal/useIsHydrating.ts
|
|
235
|
+
import { useSyncExternalStore } from "react";
|
|
236
|
+
var subscribeToNothing = () => () => {
|
|
237
|
+
};
|
|
238
|
+
var notHydrating = () => false;
|
|
239
|
+
var isHydratingOnServer = () => true;
|
|
240
|
+
function useIsHydrating() {
|
|
241
|
+
return useSyncExternalStore(
|
|
242
|
+
subscribeToNothing,
|
|
243
|
+
notHydrating,
|
|
244
|
+
isHydratingOnServer
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// src/internal/useMarkThemeReady.ts
|
|
249
|
+
import { useEffect } from "react";
|
|
250
|
+
function useMarkThemeReady(isFinal) {
|
|
251
|
+
useEffect(() => {
|
|
252
|
+
if (!isFinal || typeof document === "undefined") return;
|
|
253
|
+
document.documentElement.setAttribute(THEME_READY_ATTRIBUTE, "");
|
|
254
|
+
}, [isFinal]);
|
|
255
|
+
}
|
|
214
256
|
|
|
215
257
|
// src/provider/IconResolverContext.ts
|
|
216
258
|
import * as React from "react";
|
|
@@ -222,35 +264,189 @@ var IconResolverContext = React.createContext(
|
|
|
222
264
|
import * as React2 from "react";
|
|
223
265
|
var ThemeContext = React2.createContext(lightTheme);
|
|
224
266
|
|
|
267
|
+
// src/provider/ThemeModeContext.ts
|
|
268
|
+
import * as React3 from "react";
|
|
269
|
+
var ThemeModeContext = React3.createContext(null);
|
|
270
|
+
|
|
225
271
|
// src/provider/ThemeProvider.tsx
|
|
226
272
|
import { jsx } from "react/jsx-runtime";
|
|
273
|
+
function isThemePair(theme) {
|
|
274
|
+
return !("colors" in theme);
|
|
275
|
+
}
|
|
276
|
+
function isThemeMode(value) {
|
|
277
|
+
return value === "system" || value === "light" || value === "dark";
|
|
278
|
+
}
|
|
227
279
|
function ThemeProvider({
|
|
228
280
|
theme,
|
|
281
|
+
mode: controlledMode,
|
|
282
|
+
defaultMode,
|
|
283
|
+
onModeChange,
|
|
284
|
+
storage,
|
|
229
285
|
iconResolver,
|
|
230
286
|
children
|
|
231
287
|
}) {
|
|
232
|
-
const
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
|
|
288
|
+
const pair = theme && isThemePair(theme) ? theme : null;
|
|
289
|
+
const parentModeValue = React4.useContext(ThemeModeContext);
|
|
290
|
+
const parentIconResolver = React4.useContext(IconResolverContext);
|
|
291
|
+
const inheritedMode = pair && controlledMode === void 0 && defaultMode === void 0 && storage === void 0 ? parentModeValue : null;
|
|
292
|
+
const measuredScheme = useColorScheme();
|
|
293
|
+
const isHydrating = useIsHydrating();
|
|
294
|
+
const systemScheme = isHydrating ? "light" : measuredScheme;
|
|
295
|
+
const [uncontrolledMode, setUncontrolledMode] = React4.useState(
|
|
296
|
+
defaultMode != null ? defaultMode : "system"
|
|
297
|
+
);
|
|
298
|
+
const isControlled = controlledMode !== void 0;
|
|
299
|
+
const ownMode = isControlled ? controlledMode : uncontrolledMode;
|
|
300
|
+
const [isReady, setIsReady] = React4.useState(
|
|
301
|
+
() => !storage || isControlled || !pair
|
|
302
|
+
);
|
|
303
|
+
const storageRef = React4.useRef(storage);
|
|
304
|
+
React4.useEffect(() => {
|
|
305
|
+
storageRef.current = storage;
|
|
306
|
+
});
|
|
307
|
+
const shouldLoad = Boolean(storage) && !isControlled && Boolean(pair);
|
|
308
|
+
React4.useEffect(() => {
|
|
309
|
+
if (!shouldLoad) {
|
|
310
|
+
setIsReady(true);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
let cancelled = false;
|
|
314
|
+
const load = async () => {
|
|
315
|
+
var _a;
|
|
316
|
+
try {
|
|
317
|
+
const stored = await ((_a = storageRef.current) == null ? void 0 : _a.getItem(THEME_MODE_STORAGE_KEY));
|
|
318
|
+
if (!cancelled && isThemeMode(stored)) {
|
|
319
|
+
setUncontrolledMode(stored);
|
|
320
|
+
}
|
|
321
|
+
} catch {
|
|
322
|
+
} finally {
|
|
323
|
+
if (!cancelled) {
|
|
324
|
+
setIsReady(true);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
void load();
|
|
329
|
+
return () => {
|
|
330
|
+
cancelled = true;
|
|
331
|
+
};
|
|
332
|
+
}, [shouldLoad]);
|
|
333
|
+
const setParentMode = inheritedMode == null ? void 0 : inheritedMode.setMode;
|
|
334
|
+
const setMode = React4.useCallback(
|
|
335
|
+
(nextMode) => {
|
|
336
|
+
var _a;
|
|
337
|
+
if (setParentMode) {
|
|
338
|
+
setParentMode(nextMode);
|
|
339
|
+
onModeChange == null ? void 0 : onModeChange(nextMode);
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
if (!isControlled) {
|
|
343
|
+
setUncontrolledMode(nextMode);
|
|
344
|
+
}
|
|
345
|
+
onModeChange == null ? void 0 : onModeChange(nextMode);
|
|
346
|
+
if (!isControlled) {
|
|
347
|
+
try {
|
|
348
|
+
const result = (_a = storageRef.current) == null ? void 0 : _a.setItem(
|
|
349
|
+
THEME_MODE_STORAGE_KEY,
|
|
350
|
+
nextMode
|
|
351
|
+
);
|
|
352
|
+
void Promise.resolve(result).catch(() => {
|
|
353
|
+
});
|
|
354
|
+
} catch {
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
},
|
|
358
|
+
[setParentMode, isControlled, onModeChange]
|
|
236
359
|
);
|
|
237
|
-
|
|
360
|
+
const ownScheme = ownMode === "system" ? systemScheme === "dark" ? "dark" : "light" : ownMode;
|
|
361
|
+
const mode = inheritedMode ? inheritedMode.mode : ownMode;
|
|
362
|
+
const scheme = inheritedMode ? inheritedMode.scheme : ownScheme;
|
|
363
|
+
const ready = inheritedMode ? inheritedMode.isReady : isReady;
|
|
364
|
+
useMarkThemeReady(!isHydrating && ready);
|
|
365
|
+
const resolvedTheme = pair ? scheme === "dark" ? pair.dark : pair.light : theme != null ? theme : lightTheme;
|
|
366
|
+
const modeValue = React4.useMemo(
|
|
367
|
+
() => pair ? { mode, scheme, setMode, isReady: ready } : null,
|
|
368
|
+
[pair, mode, scheme, setMode, ready]
|
|
369
|
+
);
|
|
370
|
+
const motion = resolvedTheme.motion;
|
|
371
|
+
const transitions = React4.useMemo(
|
|
372
|
+
() => hasMotionSprings(motion) ? motionTransitions(motion) : void 0,
|
|
373
|
+
[motion]
|
|
374
|
+
);
|
|
375
|
+
return /* @__PURE__ */ jsx(ThemeContext.Provider, { value: resolvedTheme, children: /* @__PURE__ */ jsx(ThemeModeContext.Provider, { value: modeValue, children: /* @__PURE__ */ jsx(
|
|
376
|
+
IconResolverContext.Provider,
|
|
377
|
+
{
|
|
378
|
+
value: iconResolver != null ? iconResolver : parentIconResolver,
|
|
379
|
+
children: /* @__PURE__ */ jsx(MotionConfig, { transitions, children })
|
|
380
|
+
}
|
|
381
|
+
) }) });
|
|
238
382
|
}
|
|
239
383
|
|
|
240
384
|
// src/provider/useTheme.ts
|
|
241
|
-
import { useContext } from "react";
|
|
385
|
+
import { useContext as useContext2 } from "react";
|
|
242
386
|
function useTheme() {
|
|
243
|
-
return
|
|
387
|
+
return useContext2(ThemeContext);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// src/provider/useThemeMode.ts
|
|
391
|
+
import { useContext as useContext3 } from "react";
|
|
392
|
+
function useThemeMode() {
|
|
393
|
+
const value = useContext3(ThemeModeContext);
|
|
394
|
+
if (value === null) {
|
|
395
|
+
throw new Error(
|
|
396
|
+
"useThemeMode() requires a <ThemeProvider theme={{ light, dark }}>. A provider with a single theme has no light/dark mode to control."
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
return value;
|
|
244
400
|
}
|
|
245
401
|
|
|
246
402
|
// src/provider/useIconResolver.ts
|
|
247
|
-
import { useContext as
|
|
403
|
+
import { useContext as useContext4 } from "react";
|
|
248
404
|
function useIconResolver() {
|
|
249
|
-
return
|
|
405
|
+
return useContext4(IconResolverContext);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// src/provider/ThemeScope.tsx
|
|
409
|
+
import { MotionConfig as MotionConfig2, useMotionConfig } from "@rootnative/inertia";
|
|
410
|
+
import * as React5 from "react";
|
|
411
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
412
|
+
function useThemeScope() {
|
|
413
|
+
const theme = React5.useContext(ThemeContext);
|
|
414
|
+
const mode = React5.useContext(ThemeModeContext);
|
|
415
|
+
const iconResolver = React5.useContext(IconResolverContext);
|
|
416
|
+
const motion = useMotionConfig();
|
|
417
|
+
return React5.useMemo(
|
|
418
|
+
() => ({ theme, mode, iconResolver, motion }),
|
|
419
|
+
[theme, mode, iconResolver, motion]
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
function ThemeScopeProvider({
|
|
423
|
+
scope,
|
|
424
|
+
children
|
|
425
|
+
}) {
|
|
426
|
+
return /* @__PURE__ */ jsx2(ThemeContext.Provider, { value: scope.theme, children: /* @__PURE__ */ jsx2(ThemeModeContext.Provider, { value: scope.mode, children: /* @__PURE__ */ jsx2(IconResolverContext.Provider, { value: scope.iconResolver, children: /* @__PURE__ */ jsx2(
|
|
427
|
+
MotionConfig2,
|
|
428
|
+
{
|
|
429
|
+
reducedMotion: scope.motion.reducedMotion,
|
|
430
|
+
transitions: scope.motion.transitions,
|
|
431
|
+
children
|
|
432
|
+
}
|
|
433
|
+
) }) }) });
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// src/responsive/useWindowDimensions.ts
|
|
437
|
+
import { useWindowDimensions as useMeasuredWindowDimensions } from "react-native";
|
|
438
|
+
var SERVER_WINDOW = Object.freeze({
|
|
439
|
+
width: 0,
|
|
440
|
+
height: 0,
|
|
441
|
+
scale: 1,
|
|
442
|
+
fontScale: 1
|
|
443
|
+
});
|
|
444
|
+
function useWindowDimensions() {
|
|
445
|
+
const measured = useMeasuredWindowDimensions();
|
|
446
|
+
return useIsHydrating() ? SERVER_WINDOW : measured;
|
|
250
447
|
}
|
|
251
448
|
|
|
252
449
|
// src/responsive/useBreakpoint.ts
|
|
253
|
-
import { useWindowDimensions } from "react-native";
|
|
254
450
|
var breakpoints = {
|
|
255
451
|
compact: 0,
|
|
256
452
|
medium: 600,
|
|
@@ -295,7 +491,9 @@ var material = {
|
|
|
295
491
|
defaultTopAppBarTokens
|
|
296
492
|
};
|
|
297
493
|
export {
|
|
494
|
+
THEME_MODE_STORAGE_KEY,
|
|
298
495
|
ThemeProvider,
|
|
496
|
+
ThemeScopeProvider,
|
|
299
497
|
applyRoundness,
|
|
300
498
|
breakpoints,
|
|
301
499
|
darkTheme,
|
|
@@ -307,5 +505,8 @@ export {
|
|
|
307
505
|
useBreakpoint,
|
|
308
506
|
useBreakpointValue,
|
|
309
507
|
useIconResolver,
|
|
310
|
-
useTheme
|
|
508
|
+
useTheme,
|
|
509
|
+
useThemeMode,
|
|
510
|
+
useThemeScope,
|
|
511
|
+
useWindowDimensions
|
|
311
512
|
};
|