react-native-theme-transition 1.0.2 → 1.0.4
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 +111 -113
- package/README.npm.md +111 -0
- package/dist/hooks/useTheme.d.ts.map +1 -1
- package/dist/hooks/useTheme.js +4 -14
- package/dist/hooks/useTheme.js.map +1 -1
- package/dist/transitionEngine.d.ts +5 -0
- package/dist/transitionEngine.d.ts.map +1 -1
- package/dist/transitionEngine.js +77 -57
- package/dist/transitionEngine.js.map +1 -1
- package/package.json +90 -87
- package/src/createThemeTransition.ts +92 -92
- package/src/hooks/useTheme.ts +77 -88
- package/src/index.ts +29 -29
- package/src/transitionEngine.tsx +466 -445
- package/src/types.ts +304 -304
package/src/transitionEngine.tsx
CHANGED
|
@@ -1,445 +1,466 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Screenshot-overlay transition engine and provider.
|
|
3
|
-
*
|
|
4
|
-
* @
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
} from 'react-native
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
|
|
67
|
-
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
* @
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
const
|
|
169
|
-
|
|
170
|
-
const
|
|
171
|
-
//
|
|
172
|
-
const
|
|
173
|
-
const
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
//
|
|
188
|
-
const
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
await
|
|
243
|
-
if (!mountedRef.current) return;
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
if (
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
systemModeRef.current
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Screenshot-overlay transition engine and provider.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* View hierarchy: content root → shared-value touch blocker → animated overlay
|
|
6
|
+
* with decoded screenshot `Image`. The overlay is always mounted (no mount-time
|
|
7
|
+
* race with the shared opacity value); only the `Image` inside is conditional.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { createContext, useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
13
|
+
import { Appearance, AppState, Image, View } from 'react-native';
|
|
14
|
+
import Animated, {
|
|
15
|
+
useAnimatedProps,
|
|
16
|
+
useSharedValue,
|
|
17
|
+
withTiming,
|
|
18
|
+
} from 'react-native-reanimated';
|
|
19
|
+
import { captureRef } from 'react-native-view-shot';
|
|
20
|
+
import { scheduleOnRN } from 'react-native-worklets';
|
|
21
|
+
import type {
|
|
22
|
+
ThemeTransitionConfig,
|
|
23
|
+
UseThemeResult,
|
|
24
|
+
SetThemeOptions,
|
|
25
|
+
SystemThemeMap,
|
|
26
|
+
ThemeDefinition,
|
|
27
|
+
ThemeNames,
|
|
28
|
+
TokenNames,
|
|
29
|
+
} from './types';
|
|
30
|
+
import { TAG } from './constants';
|
|
31
|
+
|
|
32
|
+
const ABSOLUTE_FILL = { position: 'absolute', top: 0, left: 0, right: 0, bottom: 0 } as const;
|
|
33
|
+
// Avoids layout clipping on certain Android API levels.
|
|
34
|
+
const ROOT_STYLE = { flex: 1 } as const;
|
|
35
|
+
|
|
36
|
+
/** Calls `fn` and swallows errors with a console.warn tagged by `label`. */
|
|
37
|
+
function safeCall<A extends unknown[]>(label: string, fn: ((...args: A) => void) | undefined, ...args: A): void {
|
|
38
|
+
try { fn?.(...args); } catch (e) { console.warn(`${TAG} ${label} threw:`, e); }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Waits for `n` native animation frames to complete.
|
|
43
|
+
*
|
|
44
|
+
* @remarks
|
|
45
|
+
* React Native's rendering pipeline spans three phases:
|
|
46
|
+
* 1. **JS thread** — React reconciliation produces a virtual tree.
|
|
47
|
+
* 2. **Shadow Tree** — Yoga calculates layout from the virtual tree.
|
|
48
|
+
* 3. **Native UI thread** — platform views are painted on screen.
|
|
49
|
+
*
|
|
50
|
+
* Each phase runs on its own frame cadence (~16.67 ms at 60 fps).
|
|
51
|
+
* Waiting 1 frame lets React flush pending state or the compositor
|
|
52
|
+
* paint a decoded image.
|
|
53
|
+
*
|
|
54
|
+
* @param n - Number of animation frames to wait.
|
|
55
|
+
*/
|
|
56
|
+
function waitFrames(n: number): Promise<void> {
|
|
57
|
+
return new Promise((resolve) => {
|
|
58
|
+
let remaining = n;
|
|
59
|
+
const tick = () => {
|
|
60
|
+
if (--remaining <= 0) resolve();
|
|
61
|
+
else requestAnimationFrame(tick);
|
|
62
|
+
};
|
|
63
|
+
requestAnimationFrame(tick);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Normalizes an OS color scheme string to `'light'` or `'dark'`.
|
|
69
|
+
*
|
|
70
|
+
* @remarks
|
|
71
|
+
* Only accepts the two known values. Any other input — including `null`,
|
|
72
|
+
* `undefined`, and `'unspecified'` (returned by RN after
|
|
73
|
+
* `Appearance.setColorScheme('unspecified')`) — defaults to `'light'`.
|
|
74
|
+
*
|
|
75
|
+
* @internal
|
|
76
|
+
*/
|
|
77
|
+
function normalizeScheme(colorScheme?: string | null, fallback: 'light' | 'dark' = 'light'): 'light' | 'dark' {
|
|
78
|
+
if (colorScheme === 'dark') return 'dark';
|
|
79
|
+
if (colorScheme === 'light') return 'light';
|
|
80
|
+
return fallback;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Maps an OS color scheme to a theme name via `systemThemeMap`.
|
|
85
|
+
*
|
|
86
|
+
* @remarks
|
|
87
|
+
* The fallback `scheme as Names` is only safe when the themes are named `'light'` and
|
|
88
|
+
* `'dark'`. Callers that use custom theme names must validate the result against `themes` —
|
|
89
|
+
* both `ThemeTransitionProvider` (for `initialTheme="system"`) and `setTheme`
|
|
90
|
+
* (for `setTheme('system')`) perform this validation and throw a clear error if the
|
|
91
|
+
* resolved name is not a registered theme.
|
|
92
|
+
*
|
|
93
|
+
* @internal
|
|
94
|
+
*/
|
|
95
|
+
function mapSchemeToTheme<Names extends string>(
|
|
96
|
+
scheme: 'light' | 'dark',
|
|
97
|
+
mapping?: SystemThemeMap<Names>,
|
|
98
|
+
): Names {
|
|
99
|
+
return mapping?.[scheme] ?? (scheme as Names);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Reads the current OS appearance and maps it to a theme name.
|
|
104
|
+
*
|
|
105
|
+
* @internal Convenience wrapper combining {@link normalizeScheme} and {@link mapSchemeToTheme}.
|
|
106
|
+
*/
|
|
107
|
+
function resolveSystemTheme<Names extends string>(
|
|
108
|
+
mapping?: SystemThemeMap<Names>,
|
|
109
|
+
fallbackScheme?: 'light' | 'dark',
|
|
110
|
+
): Names {
|
|
111
|
+
return mapSchemeToTheme<Names>(normalizeScheme(Appearance.getColorScheme(), fallbackScheme), mapping);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Creates the React Context and provider component for a given theme configuration.
|
|
116
|
+
*
|
|
117
|
+
* @internal Used by {@link createThemeTransition}; not part of the public API.
|
|
118
|
+
*/
|
|
119
|
+
export function createProviderAndContext<
|
|
120
|
+
T extends Record<string, ThemeDefinition>,
|
|
121
|
+
>(config: ThemeTransitionConfig<T>) {
|
|
122
|
+
const {
|
|
123
|
+
themes, duration = 350, systemThemeMap, darkThemes: darkThemesConfig,
|
|
124
|
+
onTransitionStart: configOnTransitionStart,
|
|
125
|
+
onTransitionEnd: configOnTransitionEnd,
|
|
126
|
+
onThemeChange,
|
|
127
|
+
} = config;
|
|
128
|
+
|
|
129
|
+
const darkThemeSet = new Set<string>(
|
|
130
|
+
darkThemesConfig ?? (systemThemeMap ? [systemThemeMap.dark] : ['dark']),
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
const schemeOf = (name: string): 'light' | 'dark' => darkThemeSet.has(name) ? 'dark' : 'light';
|
|
134
|
+
|
|
135
|
+
type Names = ThemeNames<T>;
|
|
136
|
+
type Tokens = TokenNames<T>;
|
|
137
|
+
|
|
138
|
+
const Context = createContext<UseThemeResult<Tokens, Names> | null>(null);
|
|
139
|
+
|
|
140
|
+
function getColors(name: Names): Record<Tokens, string> {
|
|
141
|
+
return { ...themes[name] } as Record<Tokens, string>;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function ThemeTransitionProvider({
|
|
145
|
+
children,
|
|
146
|
+
initialTheme,
|
|
147
|
+
}: {
|
|
148
|
+
children: React.ReactNode;
|
|
149
|
+
initialTheme: Names | 'system';
|
|
150
|
+
}) {
|
|
151
|
+
const [activeTheme, setActiveTheme] = useState<{ colors: Record<Tokens, string>; name: Names }>(() => {
|
|
152
|
+
const isInitialSystem = initialTheme === 'system';
|
|
153
|
+
const startTheme = isInitialSystem
|
|
154
|
+
? resolveSystemTheme<Names>(systemThemeMap)
|
|
155
|
+
: initialTheme;
|
|
156
|
+
|
|
157
|
+
if (!(startTheme in themes)) {
|
|
158
|
+
throw new Error(
|
|
159
|
+
`${TAG} initialTheme resolved to "${startTheme}" which does not exist in themes.${
|
|
160
|
+
isInitialSystem ? ' Provide `systemThemeMap` in the config to map OS appearance to your theme names.' : ''
|
|
161
|
+
}`,
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return { colors: getColors(startTheme), name: startTheme };
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
const targetThemeRef = useRef(activeTheme.name);
|
|
169
|
+
const rootRef = useRef<View>(null);
|
|
170
|
+
const transitioningRef = useRef(false);
|
|
171
|
+
// Guards async callbacks against running after unmount.
|
|
172
|
+
const mountedRef = useRef(true);
|
|
173
|
+
const [isTransitioning, setIsTransitioning] = useState(false);
|
|
174
|
+
|
|
175
|
+
const systemModeRef = useRef(initialTheme === 'system');
|
|
176
|
+
// State mirror of systemModeRef — drives the Appearance.setColorScheme effect.
|
|
177
|
+
const [isSystemMode, setIsSystemMode] = useState(initialTheme === 'system');
|
|
178
|
+
const appStateRef = useRef(AppState.currentState);
|
|
179
|
+
// Pending OS scheme change that arrived mid-transition. Applied by finishTransition.
|
|
180
|
+
const pendingSchemeRef = useRef<'light' | 'dark' | null>(null);
|
|
181
|
+
// Last observed OS scheme — lets manual→system resolve without calling
|
|
182
|
+
// setColorScheme('unspecified') (which flashes the Android status bar).
|
|
183
|
+
const lastKnownOsSchemeRef = useRef<'light' | 'dark'>(
|
|
184
|
+
normalizeScheme(Appearance.getColorScheme()),
|
|
185
|
+
);
|
|
186
|
+
// Manual override scheme while a deferred setColorScheme('unspecified') is pending.
|
|
187
|
+
// finishTransition uses this to correct the theme if the OS scheme diverged.
|
|
188
|
+
const deferredSystemRestoreRef = useRef<'light' | 'dark' | null>(null);
|
|
189
|
+
|
|
190
|
+
// SharedValue (not state) so touch blocking takes effect on the native thread
|
|
191
|
+
// within one frame, without waiting for a React re-render cycle.
|
|
192
|
+
const isBlocking = useSharedValue(false);
|
|
193
|
+
const blockerProps = useAnimatedProps(() => ({
|
|
194
|
+
pointerEvents: isBlocking.value ? 'auto' as const : 'none' as const,
|
|
195
|
+
}));
|
|
196
|
+
|
|
197
|
+
// State (not ref) so unsetting it unmounts the Image, freeing the captured bitmap.
|
|
198
|
+
const [overlayUri, setOverlayUri] = useState<string | null>(null);
|
|
199
|
+
const overlayOpacity = useSharedValue(0);
|
|
200
|
+
// Stable ref so Reanimated doesn't re-process the style on every render.
|
|
201
|
+
const overlayStyle = useMemo(() => ({ opacity: overlayOpacity }), [overlayOpacity]);
|
|
202
|
+
const overlayDecodeRef = useRef<{ resolve: () => void; reject: () => void } | null>(null);
|
|
203
|
+
const overlaySource = useMemo(() => overlayUri ? { uri: overlayUri } : undefined, [overlayUri]);
|
|
204
|
+
const onOverlayLoad = useCallback(() => {
|
|
205
|
+
overlayDecodeRef.current?.resolve();
|
|
206
|
+
}, []);
|
|
207
|
+
const onOverlayError = useCallback(() => {
|
|
208
|
+
overlayDecodeRef.current?.reject();
|
|
209
|
+
}, []);
|
|
210
|
+
|
|
211
|
+
useEffect(() => {
|
|
212
|
+
mountedRef.current = true;
|
|
213
|
+
return () => {
|
|
214
|
+
mountedRef.current = false;
|
|
215
|
+
};
|
|
216
|
+
}, []);
|
|
217
|
+
|
|
218
|
+
// Sync native UI (alerts, keyboards) with theme. Skipped in system mode
|
|
219
|
+
// where the OS drives appearance via 'unspecified'.
|
|
220
|
+
useEffect(() => {
|
|
221
|
+
if (isSystemMode) return;
|
|
222
|
+
if (isTransitioning) return;
|
|
223
|
+
Appearance.setColorScheme(schemeOf(activeTheme.name));
|
|
224
|
+
}, [isSystemMode, activeTheme.name, isTransitioning]);
|
|
225
|
+
|
|
226
|
+
const resetTransition = useCallback(() => {
|
|
227
|
+
transitioningRef.current = false;
|
|
228
|
+
isBlocking.value = false;
|
|
229
|
+
setIsTransitioning(false);
|
|
230
|
+
setOverlayUri(null);
|
|
231
|
+
}, [isBlocking]);
|
|
232
|
+
|
|
233
|
+
const runTransition = useCallback(async (initialName: Names, options?: SetThemeOptions<Names>) => {
|
|
234
|
+
let name: Names = initialName;
|
|
235
|
+
try {
|
|
236
|
+
// 2 frames for JS → Shadow Tree → Native UI to paint before capture.
|
|
237
|
+
// useTheme's requestAnimationFrame adds a 3rd — enough for 120Hz.
|
|
238
|
+
await waitFrames(2);
|
|
239
|
+
if (!mountedRef.current) return;
|
|
240
|
+
|
|
241
|
+
// jpg 0.8: ~30% smaller than png, no visible degradation during a cross-fade.
|
|
242
|
+
const uri = await captureRef(rootRef, { format: 'jpg', quality: 0.8 });
|
|
243
|
+
if (!mountedRef.current) return;
|
|
244
|
+
|
|
245
|
+
overlayOpacity.set(1);
|
|
246
|
+
|
|
247
|
+
await new Promise<void>((resolve, reject) => {
|
|
248
|
+
overlayDecodeRef.current = { resolve, reject };
|
|
249
|
+
setOverlayUri(uri);
|
|
250
|
+
});
|
|
251
|
+
if (!mountedRef.current) return;
|
|
252
|
+
|
|
253
|
+
await waitFrames(1);
|
|
254
|
+
if (!mountedRef.current) return;
|
|
255
|
+
|
|
256
|
+
// Deferred: setting this earlier would re-render under the overlay,
|
|
257
|
+
// flashing state-dependent visuals before the screenshot hides them.
|
|
258
|
+
setIsTransitioning(true);
|
|
259
|
+
|
|
260
|
+
setActiveTheme({ colors: getColors(name), name });
|
|
261
|
+
|
|
262
|
+
// Deferred setColorScheme('unspecified') — done now while the overlay is
|
|
263
|
+
// opaque so any theme correction (OS scheme diverged) is invisible to the user.
|
|
264
|
+
if (deferredSystemRestoreRef.current !== null) {
|
|
265
|
+
const manualScheme = deferredSystemRestoreRef.current;
|
|
266
|
+
|
|
267
|
+
let resolved = false;
|
|
268
|
+
const realScheme = await new Promise<'light' | 'dark'>((resolve) => {
|
|
269
|
+
const sub = Appearance.addChangeListener(({ colorScheme }) => {
|
|
270
|
+
if (resolved) return;
|
|
271
|
+
resolved = true;
|
|
272
|
+
sub.remove();
|
|
273
|
+
resolve(normalizeScheme(colorScheme));
|
|
274
|
+
});
|
|
275
|
+
Appearance.setColorScheme('unspecified');
|
|
276
|
+
// No event → OS scheme already matches the manual override.
|
|
277
|
+
requestAnimationFrame(() => {
|
|
278
|
+
if (resolved) return;
|
|
279
|
+
resolved = true;
|
|
280
|
+
sub.remove();
|
|
281
|
+
resolve(manualScheme);
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
if (!mountedRef.current) return;
|
|
285
|
+
|
|
286
|
+
deferredSystemRestoreRef.current = null;
|
|
287
|
+
lastKnownOsSchemeRef.current = realScheme;
|
|
288
|
+
|
|
289
|
+
const corrected = mapSchemeToTheme<Names>(realScheme, systemThemeMap);
|
|
290
|
+
if (corrected in themes && corrected !== name) {
|
|
291
|
+
name = corrected as Names;
|
|
292
|
+
targetThemeRef.current = corrected;
|
|
293
|
+
setActiveTheme({ colors: getColors(corrected), name: corrected });
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
await waitFrames(1);
|
|
298
|
+
if (!mountedRef.current) return;
|
|
299
|
+
|
|
300
|
+
const finishTransition = () => {
|
|
301
|
+
if (!mountedRef.current) return;
|
|
302
|
+
resetTransition();
|
|
303
|
+
safeCall('config onTransitionEnd', configOnTransitionEnd, name);
|
|
304
|
+
safeCall('per-call onTransitionEnd', options?.onTransitionEnd, name);
|
|
305
|
+
safeCall('config onThemeChange', onThemeChange, name);
|
|
306
|
+
|
|
307
|
+
// Apply any OS scheme change that arrived mid-transition.
|
|
308
|
+
if (systemModeRef.current && pendingSchemeRef.current !== null) {
|
|
309
|
+
const resolved = mapSchemeToTheme<Names>(pendingSchemeRef.current, systemThemeMap);
|
|
310
|
+
pendingSchemeRef.current = null;
|
|
311
|
+
if (resolved in themes && resolved !== name) {
|
|
312
|
+
targetThemeRef.current = resolved;
|
|
313
|
+
setActiveTheme({ colors: getColors(resolved), name: resolved });
|
|
314
|
+
safeCall('config onThemeChange', onThemeChange, resolved);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
overlayOpacity.set(withTiming(0, { duration }, (finished) => {
|
|
320
|
+
'worklet';
|
|
321
|
+
if (finished) scheduleOnRN(finishTransition);
|
|
322
|
+
}));
|
|
323
|
+
} catch (error) {
|
|
324
|
+
// If capture fails, apply theme instantly — no animation beats a stuck overlay.
|
|
325
|
+
console.warn(
|
|
326
|
+
`${TAG} Failed to capture screenshot. Falling back to instant theme switch.`,
|
|
327
|
+
error,
|
|
328
|
+
);
|
|
329
|
+
if (!mountedRef.current) return;
|
|
330
|
+
setActiveTheme({ colors: getColors(name), name });
|
|
331
|
+
overlayOpacity.set(0);
|
|
332
|
+
resetTransition();
|
|
333
|
+
safeCall('config onThemeChange', onThemeChange, name);
|
|
334
|
+
}
|
|
335
|
+
}, [overlayOpacity, resetTransition]);
|
|
336
|
+
|
|
337
|
+
const setTheme = useCallback((name: Names | 'system', options?: SetThemeOptions<Names>): boolean => {
|
|
338
|
+
// Manual→system: resolve from cached OS scheme to avoid the Android
|
|
339
|
+
// status bar flash that setColorScheme('unspecified') causes pre-overlay.
|
|
340
|
+
if (name === 'system' && !systemModeRef.current) {
|
|
341
|
+
if (transitioningRef.current) return false;
|
|
342
|
+
|
|
343
|
+
setIsSystemMode(true);
|
|
344
|
+
systemModeRef.current = true;
|
|
345
|
+
deferredSystemRestoreRef.current = schemeOf(targetThemeRef.current);
|
|
346
|
+
return setTheme('system', options);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
const resolvedTheme = name === 'system'
|
|
350
|
+
? mapSchemeToTheme<Names>(lastKnownOsSchemeRef.current, systemThemeMap)
|
|
351
|
+
: name;
|
|
352
|
+
if (!(resolvedTheme in themes)) {
|
|
353
|
+
const resolutionHint = name === 'system'
|
|
354
|
+
? ' Provide `systemThemeMap` in the config to map OS appearance to your theme names.'
|
|
355
|
+
: '';
|
|
356
|
+
throw new Error(
|
|
357
|
+
`${TAG} setTheme("${name}") resolved to "${resolvedTheme}" which does not exist in themes.${resolutionHint}`,
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (transitioningRef.current) return false;
|
|
362
|
+
|
|
363
|
+
// After transition guard — a rejected call must not activate system mode.
|
|
364
|
+
const wasSystemMode = systemModeRef.current;
|
|
365
|
+
systemModeRef.current = name === 'system';
|
|
366
|
+
|
|
367
|
+
if (systemModeRef.current !== wasSystemMode) {
|
|
368
|
+
setIsSystemMode(systemModeRef.current);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
if (resolvedTheme === targetThemeRef.current) {
|
|
372
|
+
// Manual↔system mode switch is valid even when the resolved theme matches —
|
|
373
|
+
// returning true prevents select() from reverting the user's selection.
|
|
374
|
+
return wasSystemMode !== systemModeRef.current;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
targetThemeRef.current = resolvedTheme;
|
|
378
|
+
|
|
379
|
+
if (options?.animated === false) {
|
|
380
|
+
setActiveTheme({ colors: getColors(resolvedTheme), name: resolvedTheme });
|
|
381
|
+
safeCall('config onThemeChange', onThemeChange, resolvedTheme);
|
|
382
|
+
return true;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// Block before user callbacks to prevent re-entrant setTheme.
|
|
386
|
+
transitioningRef.current = true;
|
|
387
|
+
isBlocking.value = true;
|
|
388
|
+
|
|
389
|
+
try {
|
|
390
|
+
configOnTransitionStart?.(resolvedTheme);
|
|
391
|
+
options?.onTransitionStart?.(resolvedTheme);
|
|
392
|
+
} catch (e) {
|
|
393
|
+
// Reset guards so a callback error doesn't permanently block transitions.
|
|
394
|
+
resetTransition();
|
|
395
|
+
throw e;
|
|
396
|
+
}
|
|
397
|
+
runTransition(resolvedTheme, options).catch(() => {});
|
|
398
|
+
return true;
|
|
399
|
+
}, [isBlocking, resetTransition, runTransition]);
|
|
400
|
+
|
|
401
|
+
useEffect(() => {
|
|
402
|
+
const appearanceSub = Appearance.addChangeListener(({ colorScheme }) => {
|
|
403
|
+
if (!systemModeRef.current) return;
|
|
404
|
+
lastKnownOsSchemeRef.current = normalizeScheme(colorScheme);
|
|
405
|
+
// Deferred restore listener in runTransition handles these.
|
|
406
|
+
if (deferredSystemRestoreRef.current !== null) return;
|
|
407
|
+
if (transitioningRef.current) {
|
|
408
|
+
pendingSchemeRef.current = normalizeScheme(colorScheme);
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
if (appStateRef.current === 'active') {
|
|
412
|
+
setTheme('system');
|
|
413
|
+
} else {
|
|
414
|
+
// Background: apply directly. iOS may defer — AppState handler re-applies on foreground.
|
|
415
|
+
const resolved = mapSchemeToTheme<Names>(normalizeScheme(colorScheme), systemThemeMap);
|
|
416
|
+
if (!(resolved in themes) || resolved === targetThemeRef.current) return;
|
|
417
|
+
targetThemeRef.current = resolved;
|
|
418
|
+
setActiveTheme({ colors: getColors(resolved), name: resolved });
|
|
419
|
+
safeCall('config onThemeChange', onThemeChange, resolved);
|
|
420
|
+
}
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
// Re-read OS scheme on foreground return — iOS may not deliver
|
|
424
|
+
// Appearance events while suspended, or may defer the React commit.
|
|
425
|
+
const appSub = AppState.addEventListener('change', (next) => {
|
|
426
|
+
if (appStateRef.current !== 'active' && next === 'active' && systemModeRef.current) {
|
|
427
|
+
const resolved = resolveSystemTheme<Names>(systemThemeMap, schemeOf(targetThemeRef.current));
|
|
428
|
+
if (resolved in themes) {
|
|
429
|
+
const changed = resolved !== targetThemeRef.current;
|
|
430
|
+
targetThemeRef.current = resolved;
|
|
431
|
+
setActiveTheme({ colors: getColors(resolved), name: resolved });
|
|
432
|
+
if (changed) safeCall('config onThemeChange', onThemeChange, resolved);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
appStateRef.current = next;
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
return () => {
|
|
439
|
+
appearanceSub.remove();
|
|
440
|
+
appSub.remove();
|
|
441
|
+
};
|
|
442
|
+
}, [setTheme]);
|
|
443
|
+
|
|
444
|
+
const contextValue = useMemo(() => ({
|
|
445
|
+
colors: activeTheme.colors, name: activeTheme.name, setTheme, isTransitioning,
|
|
446
|
+
}), [activeTheme, setTheme, isTransitioning]);
|
|
447
|
+
|
|
448
|
+
return (
|
|
449
|
+
<Context.Provider value={contextValue}>
|
|
450
|
+
{/* collapsable={false}: Android flattens views by default, breaking captureRef. */}
|
|
451
|
+
<View ref={rootRef} style={ROOT_STYLE} collapsable={false}>
|
|
452
|
+
{children}
|
|
453
|
+
<Animated.View style={ABSOLUTE_FILL} animatedProps={blockerProps} />
|
|
454
|
+
{/* Always mounted — avoids shared-value race on conditional mount. */}
|
|
455
|
+
<Animated.View style={[ABSOLUTE_FILL, overlayStyle]} pointerEvents="none">
|
|
456
|
+
{overlayUri != null ? (
|
|
457
|
+
<Image source={overlaySource} style={ABSOLUTE_FILL} resizeMode="cover" fadeDuration={0} onLoad={onOverlayLoad} onError={onOverlayError} />
|
|
458
|
+
) : null}
|
|
459
|
+
</Animated.View>
|
|
460
|
+
</View>
|
|
461
|
+
</Context.Provider>
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
return { Context, ThemeTransitionProvider };
|
|
466
|
+
}
|