react-native-theme-transition 2.0.0 → 2.0.2

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.
@@ -14,108 +14,38 @@
14
14
  * @internal
15
15
  */
16
16
 
17
- import type { SkImage, SkPath } from '@shopify/react-native-skia'
17
+ import type { CanvasRef, SkImage, SkPath } from '@shopify/react-native-skia'
18
18
  import {
19
19
  Canvas,
20
20
  Circle,
21
21
  Fill,
22
22
  Group,
23
23
  ImageShader,
24
+ interpolatePaths,
24
25
  Rect,
25
26
  Shader,
26
27
  Skia,
27
28
  Image as SkiaImage,
28
- usePathInterpolation,
29
29
  } from '@shopify/react-native-skia'
30
30
  import { useMemo } from 'react'
31
31
  import type { SharedValue } from 'react-native-reanimated'
32
32
  import { useDerivedValue } from 'react-native-reanimated'
33
33
  import { ABSOLUTE_FILL } from '../constants'
34
34
  import type { WipeDirection } from '../transitionMeta'
35
+ import {
36
+ HEART_REACH,
37
+ makeHeartPath,
38
+ makeStarPath,
39
+ orderKeyframes,
40
+ STAR_GEOMETRY,
41
+ } from './shapePaths'
35
42
  import type { OverlayParams } from './types'
36
43
 
37
44
  /** @internal Frozen style for the Canvas. Hoisted so JSX never reallocates. */
38
45
  const CANVAS_STYLE = [ABSOLUTE_FILL, { pointerEvents: 'none' as const }]
39
- /** @internal Static input domain for `usePathInterpolation`. */
46
+ /** @internal Static input domain for `interpolatePaths`. */
40
47
  const KEYFRAME_INPUT = [0, 1]
41
48
 
42
- // ───────────────────────────── Path builders ──────────────────────────────
43
-
44
- /**
45
- * @internal Empirically tuned heart bezier reach factors. The bounding
46
- * math in `useHeartSize` reads these to compute the smallest scale at
47
- * which the path covers every screen corner. Kept next to the path
48
- * builder so the bounds and the curve stay in sync when either is
49
- * tweaked.
50
- */
51
- const HEART_REACH = {
52
- /** Bottom tip y-distance from `cy`, in heart-units. */
53
- bottomTip: 0.7,
54
- /** Top notch dip from `cy`, in heart-units (negative y). */
55
- topNotch: 0.55,
56
- /** Outer lobe x-reach in heart-units. */
57
- lobeX: 1.2,
58
- /** Outer lobe y-reach above `cy`, in heart-units. */
59
- lobeY: 0.9,
60
- /** Bezier control x for the inner curve into the bottom tip. */
61
- innerLobeX: 0.7,
62
- /** Y-reach factor for the bottom corners (`+y` direction). */
63
- bottomCornerY: 0.65,
64
- /** Y-reach factor for the top corners (`-y` direction, lobe rises higher). */
65
- topCornerY: 0.75,
66
- } as const
67
-
68
- function makeHeartPath(cx: number, cy: number, w: number, h: number): SkPath {
69
- const p = Skia.Path.Make()
70
- p.moveTo(cx, cy + h * HEART_REACH.bottomTip)
71
- p.cubicTo(
72
- cx - w * HEART_REACH.lobeX,
73
- cy + h * 0.1,
74
- cx - w * HEART_REACH.innerLobeX,
75
- cy - h * HEART_REACH.lobeY,
76
- cx,
77
- cy - h * HEART_REACH.topNotch,
78
- )
79
- p.cubicTo(
80
- cx + w * HEART_REACH.innerLobeX,
81
- cy - h * HEART_REACH.lobeY,
82
- cx + w * HEART_REACH.lobeX,
83
- cy + h * 0.1,
84
- cx,
85
- cy + h * HEART_REACH.bottomTip,
86
- )
87
- p.close()
88
- return p
89
- }
90
-
91
- /**
92
- * @internal Star geometry. `INNER_RATIO` is the valley-to-tip ratio
93
- * (5-point star with deep valleys). `OUTER_PADDING` over-extends the
94
- * tips past the screen corner so the inner valleys still cover them.
95
- */
96
- const STAR_GEOMETRY = { points: 5, innerRatio: 0.4, outerPadding: 1.05 } as const
97
-
98
- function makeStarPath(cx: number, cy: number, rOuter: number, rInner: number): SkPath {
99
- const p = Skia.Path.Make()
100
- const step = Math.PI / STAR_GEOMETRY.points
101
- const start = -Math.PI / 2
102
- for (let i = 0; i < STAR_GEOMETRY.points * 2; i++) {
103
- const r = i % 2 === 0 ? rOuter : rInner
104
- const a = start + i * step
105
- const x = cx + Math.cos(a) * r
106
- const y = cy + Math.sin(a) * r
107
- if (i === 0) p.moveTo(x, y)
108
- else p.lineTo(x, y)
109
- }
110
- p.close()
111
- return p
112
- }
113
-
114
- /** @internal Order keyframes so `progress` 0 → 1 grows the shape (or shrinks if inverted). */
115
- function orderKeyframes(inverted: boolean, tiny: SkPath, full: SkPath): SkPath[] {
116
- return inverted ? [full, tiny] : [tiny, full]
117
- }
118
-
119
49
  // ──────────────────────────── Runtime shaders ─────────────────────────────
120
50
 
121
51
  /**
@@ -177,12 +107,13 @@ interface OverlayProps {
177
107
  imageNew: SkImage | null
178
108
  progress: SharedValue<number>
179
109
  params: OverlayParams
110
+ canvasRef?: React.RefObject<CanvasRef | null>
180
111
  }
181
112
 
182
113
  /** @internal Renders the active-mode subtree. Hooks are scoped per mode. */
183
- export function SkiaOverlay({ image, imageNew, progress, params }: OverlayProps) {
114
+ export function SkiaOverlay({ image, imageNew, progress, params, canvasRef }: OverlayProps) {
184
115
  return (
185
- <Canvas style={CANVAS_STYLE}>
116
+ <Canvas ref={canvasRef} style={CANVAS_STYLE}>
186
117
  {image && (
187
118
  <ActiveMode image={image} imageNew={imageNew} progress={progress} params={params} />
188
119
  )}
@@ -510,7 +441,9 @@ function useHeartPath(
510
441
  ),
511
442
  [originX, originY, heartSize, inverted],
512
443
  )
513
- return usePathInterpolation(progress, KEYFRAME_INPUT, keyframes)
444
+ return useDerivedValue(() => {
445
+ return interpolatePaths(progress.value, KEYFRAME_INPUT, keyframes)
446
+ })
514
447
  }
515
448
 
516
449
  function useStarPath(
@@ -533,7 +466,9 @@ function useStarPath(
533
466
  ),
534
467
  [originX, originY, outer, inner, inverted],
535
468
  )
536
- return usePathInterpolation(progress, KEYFRAME_INPUT, keyframes)
469
+ return useDerivedValue(() => {
470
+ return interpolatePaths(progress.value, KEYFRAME_INPUT, keyframes)
471
+ })
537
472
  }
538
473
 
539
474
  function PixelizeMode({ image, imageNew, progress, params }: DualModeProps) {
@@ -7,20 +7,29 @@
7
7
  import type { SkImage } from '@shopify/react-native-skia'
8
8
  import { makeImageFromView } from '@shopify/react-native-skia'
9
9
  import type { RefObject } from 'react'
10
- import type { View } from 'react-native'
10
+ import type { ViewRef } from '../types'
11
+
12
+ // Skia 2.14 still types `makeImageFromView` for class-component refs
13
+ // (React Native <= 0.87). React Native 0.88 types view refs as host
14
+ // instances. Both are valid at runtime because the implementation only
15
+ // reads `ref.current` and resolves its native node handle, so the
16
+ // published parameter type is narrowed to the shape we actually pass.
17
+ const captureFromViewRef = makeImageFromView as unknown as (
18
+ viewRef: RefObject<ViewRef | null>,
19
+ callback?: null | ((viewRef: RefObject<ViewRef | null>) => Promise<SkImage | null>),
20
+ ) => Promise<SkImage | null>
11
21
 
12
22
  /**
13
23
  * Captures a CPU-backed snapshot of the given view. Returns `null` if the
14
24
  * capture fails (e.g. the view has unmounted or Skia is unavailable).
15
25
  * @internal
16
26
  */
17
- export async function captureView(ref: RefObject<View | null>): Promise<SkImage | null> {
27
+ export async function captureView(ref: RefObject<ViewRef | null>): Promise<SkImage | null> {
18
28
  try {
19
- const gpuImage = await makeImageFromView(ref)
29
+ const gpuImage = await captureFromViewRef(ref)
20
30
  if (!gpuImage) return null
21
- // Convert to CPU-backed so the snapshot renders in a separate GL
22
- // context (Skia Canvas). `finally` guarantees the GPU image is
23
- // released even if the conversion throws.
31
+ // Normalize to raster for cross-canvas use (Android view captures are
32
+ // already raster). Release the source handle even if conversion throws.
24
33
  try {
25
34
  return gpuImage.makeNonTextureImage()
26
35
  } finally {
@@ -5,8 +5,7 @@
5
5
  * @internal
6
6
  */
7
7
 
8
- import type { View } from 'react-native'
9
- import type { OriginSpec, TransitionOrigin } from '../types'
8
+ import type { OriginSpec, TransitionOrigin, ViewRef } from '../types'
10
9
 
11
10
  /**
12
11
  * Measures the center point of a View ref. Returns `null` if the ref is
@@ -14,14 +13,14 @@ import type { OriginSpec, TransitionOrigin } from '../types'
14
13
  *
15
14
  * @remarks
16
15
  * `measure` invokes its callback asynchronously on Paper but synchronously on
17
- * Fabric, because Fabric reaches the Shadow Tree without a bridge hop. v2's
18
- * peer deps (`react-native >= 0.78.0`, `@shopify/react-native-skia >= 2.0.0`)
19
- * both require Fabric, so the callback runs before `measure` returns and
20
- * `result` is already set at the return site. Paper is not a supported target.
16
+ * Fabric on the locked RN 0.88 baseline: `NativeDOM.measure` reads the Shadow
17
+ * Tree and invokes the callback before returning. This helper relies on
18
+ * that synchronous path; it is not an asynchronous measurement adapter.
19
+ * Reanimated 4 requires the New Architecture; Paper is unsupported.
21
20
  *
22
21
  * @internal
23
22
  */
24
- export function measureCenter(ref: React.RefObject<View | null>): TransitionOrigin | null {
23
+ export function measureCenter(ref: React.RefObject<ViewRef | null>): TransitionOrigin | null {
25
24
  if (!ref.current) return null
26
25
  try {
27
26
  let result: TransitionOrigin | null = null
@@ -37,7 +36,7 @@ export function measureCenter(ref: React.RefObject<View | null>): TransitionOrig
37
36
  }
38
37
 
39
38
  /** Type guard: is this `OriginSpec` a ref rather than a `{x,y}` point? @internal */
40
- export function isOriginRef(value: OriginSpec): value is React.RefObject<View | null> {
39
+ export function isOriginRef(value: OriginSpec): value is React.RefObject<ViewRef | null> {
41
40
  return 'current' in value
42
41
  }
43
42
 
@@ -0,0 +1,138 @@
1
+ /**
2
+ * Immutable Skia path commands for the heart and star reveal overlays.
3
+ *
4
+ * @remarks
5
+ * Shape modes animate between two keyframes of the same contour with
6
+ * `interpolatePaths`, which requires both keyframes to share the
7
+ * exact same verb topology. `@shopify/react-native-skia` 2.14 removed
8
+ * the mutable `SkPath` API (`moveTo`, `cubicTo`, `close`), so every
9
+ * contour is built as a list of per-verb `PathCommand` arrays and
10
+ * handed to `Skia.Path.MakeFromCmds` in a single call. The command
11
+ * builders are pure functions so the geometry can be regression tested
12
+ * without a native Skia runtime.
13
+ *
14
+ * @module
15
+ * @internal
16
+ */
17
+
18
+ import type { PathCommand, SkPath } from '@shopify/react-native-skia'
19
+ import { PathVerb, Skia } from '@shopify/react-native-skia'
20
+
21
+ /**
22
+ * @internal Empirically tuned heart bezier reach factors. The bounding
23
+ * math in `useHeartPath` reads these to compute the smallest scale at
24
+ * which the path covers every screen corner. Kept next to the path
25
+ * builder so the bounds and the curve stay in sync when either is
26
+ * tweaked.
27
+ */
28
+ export const HEART_REACH = {
29
+ /** Bottom tip y-distance from `cy`, in heart-units. */
30
+ bottomTip: 0.7,
31
+ /** Top notch dip from `cy`, in heart-units (negative y). */
32
+ topNotch: 0.55,
33
+ /** Outer lobe x-reach in heart-units. */
34
+ lobeX: 1.2,
35
+ /** Outer lobe y-reach above `cy`, in heart-units. */
36
+ lobeY: 0.9,
37
+ /** Bezier control x for the inner curve into the bottom tip. */
38
+ innerLobeX: 0.7,
39
+ /** Y-reach factor for the bottom corners (`+y` direction). */
40
+ bottomCornerY: 0.65,
41
+ /** Y-reach factor for the top corners (`-y` direction, lobe rises higher). */
42
+ topCornerY: 0.75,
43
+ } as const
44
+
45
+ /**
46
+ * @internal Star geometry. `innerRatio` is the valley-to-tip ratio
47
+ * (5-point star with deep valleys). `outerPadding` over-extends the
48
+ * tips past the screen corner so the inner valleys still cover them.
49
+ */
50
+ export const STAR_GEOMETRY = { points: 5, innerRatio: 0.4, outerPadding: 1.05 } as const
51
+
52
+ /**
53
+ * @internal Heart contour as immutable Skia commands: one `Move` to
54
+ * the bottom tip, two `Cubic` lobes, one `Close`. Both keyframes must
55
+ * use this exact verb sequence so they stay interpolatable.
56
+ */
57
+ export function heartCommands(cx: number, cy: number, w: number, h: number): PathCommand[] {
58
+ return [
59
+ [PathVerb.Move, cx, cy + h * HEART_REACH.bottomTip],
60
+ [
61
+ PathVerb.Cubic,
62
+ cx - w * HEART_REACH.lobeX,
63
+ cy + h * 0.1,
64
+ cx - w * HEART_REACH.innerLobeX,
65
+ cy - h * HEART_REACH.lobeY,
66
+ cx,
67
+ cy - h * HEART_REACH.topNotch,
68
+ ],
69
+ [
70
+ PathVerb.Cubic,
71
+ cx + w * HEART_REACH.innerLobeX,
72
+ cy - h * HEART_REACH.lobeY,
73
+ cx + w * HEART_REACH.lobeX,
74
+ cy + h * 0.1,
75
+ cx,
76
+ cy + h * HEART_REACH.bottomTip,
77
+ ],
78
+ [PathVerb.Close],
79
+ ]
80
+ }
81
+
82
+ /**
83
+ * @internal Star contour as immutable Skia commands: one `Move`, nine
84
+ * `Line`s alternating outer tip and inner valley, one `Close`.
85
+ */
86
+ export function starCommands(
87
+ cx: number,
88
+ cy: number,
89
+ rOuter: number,
90
+ rInner: number,
91
+ ): PathCommand[] {
92
+ const commands: PathCommand[] = []
93
+ const step = Math.PI / STAR_GEOMETRY.points
94
+ const start = -Math.PI / 2
95
+ for (let i = 0; i < STAR_GEOMETRY.points * 2; i++) {
96
+ const r = i % 2 === 0 ? rOuter : rInner
97
+ const a = start + i * step
98
+ commands.push([
99
+ i === 0 ? PathVerb.Move : PathVerb.Line,
100
+ cx + Math.cos(a) * r,
101
+ cy + Math.sin(a) * r,
102
+ ])
103
+ }
104
+ commands.push([PathVerb.Close])
105
+ return commands
106
+ }
107
+
108
+ /**
109
+ * @internal Builds an `SkPath` from immutable commands. `MakeFromCmds`
110
+ * returns `null` instead of throwing when Skia rejects the list; that
111
+ * would leave the overlay with no clip, so fail loudly with the shape
112
+ * name instead.
113
+ */
114
+ function makeShapePath(shape: 'heart' | 'star', commands: PathCommand[]): SkPath {
115
+ const path = Skia.Path.MakeFromCmds(commands)
116
+ if (path === null) {
117
+ throw new Error(
118
+ `[react-native-theme-transition] Skia.Path.MakeFromCmds returned null while building the ${shape} reveal path. ` +
119
+ 'Check that @shopify/react-native-skia supports this path command list.',
120
+ )
121
+ }
122
+ return path
123
+ }
124
+
125
+ /** @internal Immutable heart path at the given center and size. */
126
+ export function makeHeartPath(cx: number, cy: number, w: number, h: number): SkPath {
127
+ return makeShapePath('heart', heartCommands(cx, cy, w, h))
128
+ }
129
+
130
+ /** @internal Immutable star path at the given center and radii. */
131
+ export function makeStarPath(cx: number, cy: number, rOuter: number, rInner: number): SkPath {
132
+ return makeShapePath('star', starCommands(cx, cy, rOuter, rInner))
133
+ }
134
+
135
+ /** @internal Order keyframes so `progress` 0 → 1 grows the shape (or shrinks if inverted). */
136
+ export function orderKeyframes(inverted: boolean, tiny: SkPath, full: SkPath): SkPath[] {
137
+ return inverted ? [full, tiny] : [tiny, full]
138
+ }
@@ -3,6 +3,8 @@
3
3
  * and the requested change, returns what the engine should do: ignore
4
4
  * the call, accept it as a no-op mode flip (only `preference` updates,
5
5
  * no animated transition), or proceed with a real theme swap.
6
+ * `preferenceWriteTiming` covers the follow-up question: which commit
7
+ * writes the user's pick.
6
8
  *
7
9
  * Extracted from the `setTheme` closure in `transitionEngine.tsx` so
8
10
  * the guard rules can be unit-tested without mounting the provider.
@@ -46,3 +48,19 @@ export function decideSetTheme(input: {
46
48
  }
47
49
  return { status: 'accepted', path: 'proceed' }
48
50
  }
51
+
52
+ /**
53
+ * Which commit writes the user's `preference` for an accepted call.
54
+ * `'deferred'` stashes the pick and applies it in the commit that swaps
55
+ * the colour tokens, avoiding a preference-driven update before the
56
+ * snapshot; other state/callback updates are independent. The
57
+ * `'system'`-mode no-op flip and `animated: false` have nothing to defer
58
+ * to and write immediately.
59
+ * @internal
60
+ */
61
+ export function preferenceWriteTiming(
62
+ path: 'mode-flip-noop' | 'proceed',
63
+ animated: boolean,
64
+ ): 'eager' | 'deferred' {
65
+ return animated && path === 'proceed' ? 'deferred' : 'eager'
66
+ }