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.
@@ -1,88 +1,88 @@
1
- import { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
2
- import type { Context } from 'react';
3
- import type {
4
- UseThemeResult,
5
- ThemeSelectionResult,
6
- ThemeDefinition,
7
- ThemeNames,
8
- TokenNames,
9
- } from '../types';
10
- import { TAG } from '../constants';
11
-
12
- /**
13
- * Factory that produces the `useTheme` hook bound to a specific context.
14
- *
15
- * @internal Used by {@link createThemeTransition}; not part of the public API.
16
- */
17
- export function createUseTheme<T extends Record<string, ThemeDefinition>>(
18
- Ctx: Context<UseThemeResult<TokenNames<T>, ThemeNames<T>> | null>,
19
- ) {
20
- type Tokens = TokenNames<T>;
21
- type Names = ThemeNames<T>;
22
- type BaseResult = UseThemeResult<Tokens, Names>;
23
- type FullResult = BaseResult & ThemeSelectionResult<Names>;
24
-
25
- function useTheme(): BaseResult;
26
- function useTheme(options: { initialSelection?: Names | 'system' }): FullResult;
27
- function useTheme(options?: { initialSelection?: Names | 'system' }): BaseResult | FullResult {
28
- const ctx = useContext(Ctx);
29
- if (!ctx) {
30
- throw new Error(
31
- `${TAG} \`useTheme\` must be used inside a \`ThemeTransitionProvider\`.`,
32
- );
33
- }
34
-
35
- const { setTheme, isTransitioning } = ctx;
36
-
37
- // Selection state — hooks are always called to satisfy React's rules of hooks.
38
- // Only exposed in the return value when options are provided.
39
- const [selected, setSelected] = useState<Names | 'system'>(
40
- () => options?.initialSelection ?? ctx.name,
41
- );
42
- const pressLockRef = useRef(false);
43
- // Ref tracks latest `selected` so the callback can read it without
44
- // `selected` being a dependency — avoids recreating `select` on every change.
45
- const selectedRef = useRef(selected);
46
- selectedRef.current = selected;
47
-
48
- useEffect(() => {
49
- if (!isTransitioning) pressLockRef.current = false;
50
- }, [isTransitioning]);
51
-
52
- const select = useCallback(
53
- (option: Names | 'system') => {
54
- if (pressLockRef.current) return;
55
- pressLockRef.current = true;
56
- const previousSelected = selectedRef.current;
57
- // Paint the new highlight FIRST. Deferring setTheme to the next frame
58
- // guarantees React commits + native paint before the library captures.
59
- setSelected(option);
60
- requestAnimationFrame(() => {
61
- if (!setTheme(option)) {
62
- // Rejected (same theme or already transitioning) — restore the prior selection.
63
- setSelected(previousSelected);
64
- }
65
- // Always release: the blocker view (isBlocking) handles touch
66
- // prevention during transitions at the native level.
67
- pressLockRef.current = false;
68
- });
69
- },
70
- [setTheme],
71
- );
72
-
73
- // Memoize the extended result so consumers using selection tracking
74
- // don't get a new object reference on every render (which would defeat
75
- // the context-level useMemo in the provider).
76
- const selectionResult = useMemo(
77
- () => ({ ...ctx, selected, select }),
78
- [ctx, selected, select],
79
- );
80
-
81
- if (options != null) {
82
- return selectionResult;
83
- }
84
- return ctx;
85
- }
86
-
87
- return useTheme;
88
- }
1
+ import { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
2
+ import type { Context } from 'react';
3
+ import type {
4
+ UseThemeResult,
5
+ ThemeSelectionResult,
6
+ ThemeDefinition,
7
+ ThemeNames,
8
+ TokenNames,
9
+ } from '../types';
10
+ import { TAG } from '../constants';
11
+
12
+ /**
13
+ * Factory that produces the `useTheme` hook bound to a specific context.
14
+ *
15
+ * @internal Used by {@link createThemeTransition}; not part of the public API.
16
+ */
17
+ export function createUseTheme<T extends Record<string, ThemeDefinition>>(
18
+ Ctx: Context<UseThemeResult<TokenNames<T>, ThemeNames<T>> | null>,
19
+ ) {
20
+ type Tokens = TokenNames<T>;
21
+ type Names = ThemeNames<T>;
22
+ type BaseResult = UseThemeResult<Tokens, Names>;
23
+ type FullResult = BaseResult & ThemeSelectionResult<Names>;
24
+
25
+ function useTheme(): BaseResult;
26
+ function useTheme(options: { initialSelection?: Names | 'system' }): FullResult;
27
+ function useTheme(options?: { initialSelection?: Names | 'system' }): BaseResult | FullResult {
28
+ const ctx = useContext(Ctx);
29
+ if (!ctx) {
30
+ throw new Error(
31
+ `${TAG} \`useTheme\` must be used inside a \`ThemeTransitionProvider\`.`,
32
+ );
33
+ }
34
+
35
+ const { setTheme, isTransitioning } = ctx;
36
+
37
+ // Selection state — hooks are always called to satisfy React's rules of hooks.
38
+ // Only exposed in the return value when options are provided.
39
+ const [selected, setSelected] = useState<Names | 'system'>(
40
+ () => options?.initialSelection ?? ctx.name,
41
+ );
42
+ const pressLockRef = useRef(false);
43
+ // Ref tracks latest `selected` so the callback can read it without
44
+ // `selected` being a dependency — avoids recreating `select` on every change.
45
+ const selectedRef = useRef(selected);
46
+ selectedRef.current = selected;
47
+
48
+ useEffect(() => {
49
+ if (!isTransitioning) pressLockRef.current = false;
50
+ }, [isTransitioning]);
51
+
52
+ const select = useCallback(
53
+ (option: Names | 'system') => {
54
+ if (pressLockRef.current) return;
55
+ pressLockRef.current = true;
56
+ const previousSelected = selectedRef.current;
57
+ // Paint the new highlight FIRST. Deferring setTheme to the next frame
58
+ // guarantees React commits + native paint before the library captures.
59
+ setSelected(option);
60
+ requestAnimationFrame(() => {
61
+ if (!setTheme(option)) {
62
+ // Rejected (same theme or already transitioning) — restore the prior selection.
63
+ setSelected(previousSelected);
64
+ }
65
+ // Always release: the blocker view (isBlocking) handles touch
66
+ // prevention during transitions at the native level.
67
+ pressLockRef.current = false;
68
+ });
69
+ },
70
+ [setTheme],
71
+ );
72
+
73
+ // Memoize the extended result so consumers using selection tracking
74
+ // don't get a new object reference on every render (which would defeat
75
+ // the context-level useMemo in the provider).
76
+ const selectionResult = useMemo(
77
+ () => ({ ...ctx, selected, select }),
78
+ [ctx, selected, select],
79
+ );
80
+
81
+ if (options != null) {
82
+ return selectionResult;
83
+ }
84
+ return ctx;
85
+ }
86
+
87
+ return useTheme;
88
+ }
package/src/index.ts CHANGED
@@ -1,29 +1,29 @@
1
- /**
2
- * Public entry point for react-native-theme-transition.
3
- *
4
- * @remarks
5
- * The transition mechanism works in five steps:
6
- * 1. Blocks touch input via a Reanimated shared value (instant, no React re-render).
7
- * 2. Captures the current native view via `react-native-view-shot`.
8
- * 3. Overlays the snapshot as an absolute-positioned `Image` (`onLoad` confirms decode).
9
- * 4. Mutates context to inject new theme colors underneath.
10
- * 5. Fades the overlay to zero via `react-native-reanimated` — the native repaint
11
- * completes during the first frames while the overlay is still near-opaque.
12
- *
13
- * @see {@link createThemeTransition} for the main API surface.
14
- * @packageDocumentation
15
- */
16
-
17
- export { createThemeTransition } from './createThemeTransition';
18
-
19
- export type {
20
- ThemeTransitionConfig,
21
- ThemeTransitionAPI,
22
- UseThemeResult,
23
- ThemeSelectionResult,
24
- SystemThemeMap,
25
- SetThemeOptions,
26
- ThemeDefinition,
27
- ThemeNames,
28
- TokenNames,
29
- } from './types';
1
+ /**
2
+ * Public entry point for react-native-theme-transition.
3
+ *
4
+ * @remarks
5
+ * The transition mechanism works in five steps:
6
+ * 1. Blocks touch input via a Reanimated shared value (instant, no React re-render).
7
+ * 2. Captures the current native view via `react-native-view-shot`.
8
+ * 3. Overlays the snapshot as an absolute-positioned `Image` (`onLoad` confirms decode).
9
+ * 4. Mutates context to inject new theme colors underneath.
10
+ * 5. Fades the overlay to zero via `react-native-reanimated` — the native repaint
11
+ * completes during the first frames while the overlay is still near-opaque.
12
+ *
13
+ * @see {@link createThemeTransition} for the main API surface.
14
+ * @packageDocumentation
15
+ */
16
+
17
+ export { createThemeTransition } from './createThemeTransition';
18
+
19
+ export type {
20
+ ThemeTransitionConfig,
21
+ ThemeTransitionAPI,
22
+ UseThemeResult,
23
+ ThemeSelectionResult,
24
+ SystemThemeMap,
25
+ SetThemeOptions,
26
+ ThemeDefinition,
27
+ ThemeNames,
28
+ TokenNames,
29
+ } from './types';