@pgy-eu/react-native-tour-guide 0.0.0-stage → 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.
Files changed (161) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +993 -2
  3. package/lib/commonjs/SpotlightOverlay.js +712 -0
  4. package/lib/commonjs/SpotlightOverlay.js.map +1 -0
  5. package/lib/commonjs/Tooltip.js +593 -0
  6. package/lib/commonjs/Tooltip.js.map +1 -0
  7. package/lib/commonjs/TourGuideContext.js +535 -0
  8. package/lib/commonjs/TourGuideContext.js.map +1 -0
  9. package/lib/commonjs/TourGuideOverlay.js +1173 -0
  10. package/lib/commonjs/TourGuideOverlay.js.map +1 -0
  11. package/lib/commonjs/TourTarget.js +80 -0
  12. package/lib/commonjs/TourTarget.js.map +1 -0
  13. package/lib/commonjs/accessibility.js +49 -0
  14. package/lib/commonjs/accessibility.js.map +1 -0
  15. package/lib/commonjs/dev.js +48 -0
  16. package/lib/commonjs/dev.js.map +1 -0
  17. package/lib/commonjs/events.js +56 -0
  18. package/lib/commonjs/events.js.map +1 -0
  19. package/lib/commonjs/index.js +169 -0
  20. package/lib/commonjs/index.js.map +1 -0
  21. package/lib/commonjs/package.json +1 -0
  22. package/lib/commonjs/shapes.js +158 -0
  23. package/lib/commonjs/shapes.js.map +1 -0
  24. package/lib/commonjs/storage.js +116 -0
  25. package/lib/commonjs/storage.js.map +1 -0
  26. package/lib/commonjs/themes.js +86 -0
  27. package/lib/commonjs/themes.js.map +1 -0
  28. package/lib/commonjs/types.js +6 -0
  29. package/lib/commonjs/types.js.map +1 -0
  30. package/lib/commonjs/useTourPersistence.js +135 -0
  31. package/lib/commonjs/useTourPersistence.js.map +1 -0
  32. package/lib/commonjs/useTourScroll.js +46 -0
  33. package/lib/commonjs/useTourScroll.js.map +1 -0
  34. package/lib/commonjs/utils.js +372 -0
  35. package/lib/commonjs/utils.js.map +1 -0
  36. package/lib/commonjs/validation.js +143 -0
  37. package/lib/commonjs/validation.js.map +1 -0
  38. package/lib/module/SpotlightOverlay.js +707 -0
  39. package/lib/module/SpotlightOverlay.js.map +1 -0
  40. package/lib/module/Tooltip.js +588 -0
  41. package/lib/module/Tooltip.js.map +1 -0
  42. package/lib/module/TourGuideContext.js +529 -0
  43. package/lib/module/TourGuideContext.js.map +1 -0
  44. package/lib/module/TourGuideOverlay.js +1167 -0
  45. package/lib/module/TourGuideOverlay.js.map +1 -0
  46. package/lib/module/TourTarget.js +74 -0
  47. package/lib/module/TourTarget.js.map +1 -0
  48. package/lib/module/accessibility.js +43 -0
  49. package/lib/module/accessibility.js.map +1 -0
  50. package/lib/module/dev.js +40 -0
  51. package/lib/module/dev.js.map +1 -0
  52. package/lib/module/events.js +51 -0
  53. package/lib/module/events.js.map +1 -0
  54. package/lib/module/index.js +25 -0
  55. package/lib/module/index.js.map +1 -0
  56. package/lib/module/package.json +1 -0
  57. package/lib/module/shapes.js +152 -0
  58. package/lib/module/shapes.js.map +1 -0
  59. package/lib/module/storage.js +111 -0
  60. package/lib/module/storage.js.map +1 -0
  61. package/lib/module/themes.js +81 -0
  62. package/lib/module/themes.js.map +1 -0
  63. package/lib/module/types.js +4 -0
  64. package/lib/module/types.js.map +1 -0
  65. package/lib/module/useTourPersistence.js +130 -0
  66. package/lib/module/useTourPersistence.js.map +1 -0
  67. package/lib/module/useTourScroll.js +41 -0
  68. package/lib/module/useTourScroll.js.map +1 -0
  69. package/lib/module/utils.js +361 -0
  70. package/lib/module/utils.js.map +1 -0
  71. package/lib/module/validation.js +138 -0
  72. package/lib/module/validation.js.map +1 -0
  73. package/lib/typescript/commonjs/package.json +1 -0
  74. package/lib/typescript/commonjs/src/SpotlightOverlay.d.ts +46 -0
  75. package/lib/typescript/commonjs/src/SpotlightOverlay.d.ts.map +1 -0
  76. package/lib/typescript/commonjs/src/Tooltip.d.ts +9 -0
  77. package/lib/typescript/commonjs/src/Tooltip.d.ts.map +1 -0
  78. package/lib/typescript/commonjs/src/TourGuideContext.d.ts +46 -0
  79. package/lib/typescript/commonjs/src/TourGuideContext.d.ts.map +1 -0
  80. package/lib/typescript/commonjs/src/TourGuideOverlay.d.ts +22 -0
  81. package/lib/typescript/commonjs/src/TourGuideOverlay.d.ts.map +1 -0
  82. package/lib/typescript/commonjs/src/TourTarget.d.ts +36 -0
  83. package/lib/typescript/commonjs/src/TourTarget.d.ts.map +1 -0
  84. package/lib/typescript/commonjs/src/accessibility.d.ts +22 -0
  85. package/lib/typescript/commonjs/src/accessibility.d.ts.map +1 -0
  86. package/lib/typescript/commonjs/src/dev.d.ts +22 -0
  87. package/lib/typescript/commonjs/src/dev.d.ts.map +1 -0
  88. package/lib/typescript/commonjs/src/events.d.ts +57 -0
  89. package/lib/typescript/commonjs/src/events.d.ts.map +1 -0
  90. package/lib/typescript/commonjs/src/index.d.ts +21 -0
  91. package/lib/typescript/commonjs/src/index.d.ts.map +1 -0
  92. package/lib/typescript/commonjs/src/shapes.d.ts +50 -0
  93. package/lib/typescript/commonjs/src/shapes.d.ts.map +1 -0
  94. package/lib/typescript/commonjs/src/storage.d.ts +15 -0
  95. package/lib/typescript/commonjs/src/storage.d.ts.map +1 -0
  96. package/lib/typescript/commonjs/src/themes.d.ts +10 -0
  97. package/lib/typescript/commonjs/src/themes.d.ts.map +1 -0
  98. package/lib/typescript/commonjs/src/types.d.ts +599 -0
  99. package/lib/typescript/commonjs/src/types.d.ts.map +1 -0
  100. package/lib/typescript/commonjs/src/useTourPersistence.d.ts +63 -0
  101. package/lib/typescript/commonjs/src/useTourPersistence.d.ts.map +1 -0
  102. package/lib/typescript/commonjs/src/useTourScroll.d.ts +36 -0
  103. package/lib/typescript/commonjs/src/useTourScroll.d.ts.map +1 -0
  104. package/lib/typescript/commonjs/src/utils.d.ts +97 -0
  105. package/lib/typescript/commonjs/src/utils.d.ts.map +1 -0
  106. package/lib/typescript/commonjs/src/validation.d.ts +11 -0
  107. package/lib/typescript/commonjs/src/validation.d.ts.map +1 -0
  108. package/lib/typescript/module/package.json +1 -0
  109. package/lib/typescript/module/src/SpotlightOverlay.d.ts +46 -0
  110. package/lib/typescript/module/src/SpotlightOverlay.d.ts.map +1 -0
  111. package/lib/typescript/module/src/Tooltip.d.ts +9 -0
  112. package/lib/typescript/module/src/Tooltip.d.ts.map +1 -0
  113. package/lib/typescript/module/src/TourGuideContext.d.ts +46 -0
  114. package/lib/typescript/module/src/TourGuideContext.d.ts.map +1 -0
  115. package/lib/typescript/module/src/TourGuideOverlay.d.ts +22 -0
  116. package/lib/typescript/module/src/TourGuideOverlay.d.ts.map +1 -0
  117. package/lib/typescript/module/src/TourTarget.d.ts +36 -0
  118. package/lib/typescript/module/src/TourTarget.d.ts.map +1 -0
  119. package/lib/typescript/module/src/accessibility.d.ts +22 -0
  120. package/lib/typescript/module/src/accessibility.d.ts.map +1 -0
  121. package/lib/typescript/module/src/dev.d.ts +22 -0
  122. package/lib/typescript/module/src/dev.d.ts.map +1 -0
  123. package/lib/typescript/module/src/events.d.ts +57 -0
  124. package/lib/typescript/module/src/events.d.ts.map +1 -0
  125. package/lib/typescript/module/src/index.d.ts +21 -0
  126. package/lib/typescript/module/src/index.d.ts.map +1 -0
  127. package/lib/typescript/module/src/shapes.d.ts +50 -0
  128. package/lib/typescript/module/src/shapes.d.ts.map +1 -0
  129. package/lib/typescript/module/src/storage.d.ts +15 -0
  130. package/lib/typescript/module/src/storage.d.ts.map +1 -0
  131. package/lib/typescript/module/src/themes.d.ts +10 -0
  132. package/lib/typescript/module/src/themes.d.ts.map +1 -0
  133. package/lib/typescript/module/src/types.d.ts +599 -0
  134. package/lib/typescript/module/src/types.d.ts.map +1 -0
  135. package/lib/typescript/module/src/useTourPersistence.d.ts +63 -0
  136. package/lib/typescript/module/src/useTourPersistence.d.ts.map +1 -0
  137. package/lib/typescript/module/src/useTourScroll.d.ts +36 -0
  138. package/lib/typescript/module/src/useTourScroll.d.ts.map +1 -0
  139. package/lib/typescript/module/src/utils.d.ts +97 -0
  140. package/lib/typescript/module/src/utils.d.ts.map +1 -0
  141. package/lib/typescript/module/src/validation.d.ts +11 -0
  142. package/lib/typescript/module/src/validation.d.ts.map +1 -0
  143. package/llms.txt +62 -0
  144. package/package.json +281 -4
  145. package/src/SpotlightOverlay.tsx +742 -0
  146. package/src/Tooltip.tsx +638 -0
  147. package/src/TourGuideContext.tsx +644 -0
  148. package/src/TourGuideOverlay.tsx +1382 -0
  149. package/src/TourTarget.tsx +74 -0
  150. package/src/accessibility.ts +60 -0
  151. package/src/dev.ts +39 -0
  152. package/src/events.ts +76 -0
  153. package/src/index.tsx +64 -0
  154. package/src/shapes.ts +184 -0
  155. package/src/storage.ts +114 -0
  156. package/src/themes.ts +83 -0
  157. package/src/types.ts +648 -0
  158. package/src/useTourPersistence.ts +147 -0
  159. package/src/useTourScroll.ts +60 -0
  160. package/src/utils.ts +401 -0
  161. package/src/validation.ts +248 -0
@@ -0,0 +1,742 @@
1
+ import React, { useRef, useEffect, useMemo, useState } from 'react';
2
+ import { StyleSheet, View, Animated, Pressable, Dimensions } from 'react-native';
3
+ import Svg, { Rect, Path, Defs, Mask } from 'react-native-svg';
4
+
5
+ import type { SpotlightMotion, SpotlightStyles, SpotlightTarget } from './types';
6
+ import { computeShape, roundedRectPath } from './shapes';
7
+ import type { SpotlightBorderRadius, SpotlightPadding } from './shapes';
8
+
9
+ const AnimatedRect = Animated.createAnimatedComponent(Rect);
10
+
11
+ const DEFAULT_SPOTLIGHT_STYLES: SpotlightStyles = {};
12
+
13
+ // Resolve optional dependencies once at module scope
14
+ interface BlurProvider {
15
+ Component: React.ComponentType<Record<string, unknown>>;
16
+ /** community: @react-native-community/blur; expo: expo-blur (works in Expo Go) */
17
+ kind: 'community' | 'expo';
18
+ }
19
+ let _resolvedBlur: BlurProvider | null | undefined;
20
+ let _resolvedLinearGradient: React.ComponentType<Record<string, unknown>> | null | undefined;
21
+ let _resolvedMaskedView: React.ComponentType<Record<string, unknown>> | null | undefined;
22
+
23
+ const getBlurProvider = (): BlurProvider | null => {
24
+ if (_resolvedBlur === undefined) {
25
+ _resolvedBlur = null;
26
+ try {
27
+ const community = require('@react-native-community/blur').BlurView;
28
+ if (community) _resolvedBlur = { Component: community, kind: 'community' };
29
+ } catch {
30
+ // fall through to expo-blur
31
+ }
32
+ if (!_resolvedBlur) {
33
+ try {
34
+ // expo-blur ships inside Expo Go, so blur works there without a dev build.
35
+ const expo = require('expo-blur').BlurView;
36
+ if (expo) _resolvedBlur = { Component: expo, kind: 'expo' };
37
+ } catch {
38
+ _resolvedBlur = null;
39
+ }
40
+ }
41
+ }
42
+ return _resolvedBlur;
43
+ };
44
+
45
+ const getLinearGradient = () => {
46
+ if (_resolvedLinearGradient === undefined) {
47
+ try {
48
+ _resolvedLinearGradient = require('react-native-linear-gradient').default;
49
+ } catch {
50
+ _resolvedLinearGradient = null;
51
+ }
52
+ }
53
+ return _resolvedLinearGradient;
54
+ };
55
+
56
+ const getMaskedView = () => {
57
+ if (_resolvedMaskedView === undefined) {
58
+ try {
59
+ _resolvedMaskedView = require('@react-native-masked-view/masked-view').default;
60
+ } catch {
61
+ _resolvedMaskedView = null;
62
+ }
63
+ }
64
+ return _resolvedMaskedView;
65
+ };
66
+
67
+ let maskIdCounter = 0;
68
+
69
+ export interface SpotlightOverlayProps {
70
+ target: SpotlightTarget | null;
71
+ /** Uniform number or per-side padding around the spotlight */
72
+ padding?: SpotlightPadding;
73
+ borderRadius?: SpotlightBorderRadius;
74
+ styles?: SpotlightStyles;
75
+ screenWidth: number;
76
+ screenHeight: number;
77
+ animationDuration?: number;
78
+ onBackdropPress?: () => void;
79
+ onSpotlightPress?: () => void;
80
+ /**
81
+ * Leave the spotlight hole touch-transparent so the real element underneath
82
+ * receives taps. Only meaningful when the overlay is NOT inside a Modal
83
+ * (inline overlay mode) — a Modal swallows touches regardless.
84
+ */
85
+ interactive?: boolean;
86
+ /** How the spotlight travels between steps (default: 'morph') */
87
+ motion?: SpotlightMotion;
88
+ /**
89
+ * Keep the (expensive) blur subtree mounted even while the current step has
90
+ * blur disabled, cross-fading its opacity instead of mounting/unmounting at
91
+ * step boundaries — that mount is what caused a visible hitch when moving
92
+ * between blurred and unblurred steps. Set by TourGuideOverlay when any step
93
+ * in the tour enables blur.
94
+ */
95
+ mountBlurLayer?: boolean;
96
+ /**
97
+ * Additional cutout subpaths that stay punched out of the backdrop —
98
+ * spotlights kept lit from earlier steps (`step.keepSpotlight`). Purely
99
+ * visual: press bands and the blur mask only consider the current spotlight.
100
+ */
101
+ keptPaths?: string[];
102
+ }
103
+
104
+ /**
105
+ * Component that creates a spotlight effect with a dark overlay.
106
+ * Supports smooth animated transitions between targets.
107
+ * The spotlight automatically matches the target's shape via border radius.
108
+ * Optionally supports blur and gradient effects if dependencies are installed.
109
+ */
110
+ const SpotlightOverlay: React.FC<SpotlightOverlayProps> = ({
111
+ target,
112
+ padding = 0,
113
+ borderRadius: customBorderRadius,
114
+ styles = DEFAULT_SPOTLIGHT_STYLES,
115
+ screenWidth,
116
+ screenHeight,
117
+ animationDuration = 300,
118
+ onBackdropPress,
119
+ onSpotlightPress,
120
+ interactive = false,
121
+ motion = 'morph',
122
+ mountBlurLayer = false,
123
+ keptPaths,
124
+ }) => {
125
+ const {
126
+ overlayOpacity = 0.6,
127
+ overlayColor = 'black',
128
+ blurAmount = 4,
129
+ enableBlur = false,
130
+ enableGradient = false,
131
+ gradientColors = ['rgba(217,217,217,0)', 'rgba(19,32,14,0.64)'],
132
+ enablePulse = false,
133
+ pulseColor = '#FFFFFF',
134
+ pulseWidth = 2,
135
+ pulseDuration = 1500,
136
+ pulseMinOpacity = 0.2,
137
+ pulseMaxOpacity = 0.8,
138
+ maskPath,
139
+ blurTarget,
140
+ } = styles;
141
+
142
+ // Unique mask IDs per instance to avoid SVG conflicts
143
+ const maskIds = useRef({
144
+ inverse: `inverse-mask-${++maskIdCounter}`,
145
+ spotlight: `spotlight-mask-${maskIdCounter}`,
146
+ }).current;
147
+
148
+ // Animated values for smooth spotlight transitions
149
+ const animX = useRef(new Animated.Value(0)).current;
150
+ const animY = useRef(new Animated.Value(0)).current;
151
+ const animWidth = useRef(new Animated.Value(0)).current;
152
+ const animHeight = useRef(new Animated.Value(0)).current;
153
+ const animRx = useRef(new Animated.Value(0)).current;
154
+ const animRy = useRef(new Animated.Value(0)).current;
155
+ const hasAnimated = useRef(false);
156
+ const pulseOpacity = useRef(new Animated.Value(0)).current;
157
+ // Whole-overlay opacity used by the 'fade' motion preset.
158
+ const overlayFade = useRef(new Animated.Value(1)).current;
159
+ // Blur layer cross-fade (used with mountBlurLayer): fading beats remounting.
160
+ const blurFade = useRef(new Animated.Value(enableBlur ? 1 : 0)).current;
161
+ useEffect(() => {
162
+ Animated.timing(blurFade, {
163
+ toValue: enableBlur ? 1 : 0,
164
+ duration: 220,
165
+ useNativeDriver: true,
166
+ }).start();
167
+ }, [enableBlur, blurFade]);
168
+ // Pulse visibility combined with the fade, so the pulse ring dips with the
169
+ // backdrop instead of floating alone during a fade transition.
170
+ const pulseVisible = useRef(Animated.multiply(pulseOpacity, overlayFade)).current;
171
+ // Guards nested animation callbacks against applying a stale shape after a
172
+ // newer step's effect has already run.
173
+ const shapeEpoch = useRef(0);
174
+ // Direct handles to the two SVG paths so per-frame updates go through
175
+ // setNativeProps instead of setState — a React re-render per animation frame
176
+ // is invisible on a flagship and very visible on a low-end Android.
177
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
178
+ const overlayPathRef = useRef<any>(null);
179
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
180
+ const pulsePathRef = useRef<any>(null);
181
+
182
+ // Path data for per-corner border radius shapes
183
+ const [pathD, setPathD] = useState('');
184
+
185
+ // Compute shape result synchronously. A user-supplied maskPath overrides the
186
+ // automatic shape with an arbitrary SVG subpath (rendered as a static path;
187
+ // shape animation is skipped for custom paths).
188
+ const shapeResult = useMemo(() => {
189
+ if (!target) return null;
190
+ const auto = computeShape(target, padding, customBorderRadius);
191
+ if (maskPath) {
192
+ const autoBounds =
193
+ auto.kind === 'rect'
194
+ ? { x: auto.x, y: auto.y, width: auto.width, height: auto.height }
195
+ : auto.bounds;
196
+ try {
197
+ const d = maskPath({
198
+ target,
199
+ bounds: autoBounds,
200
+ screenWidth,
201
+ screenHeight,
202
+ });
203
+ if (typeof d === 'string' && d.length > 0) {
204
+ return { kind: 'path' as const, d, bounds: autoBounds };
205
+ }
206
+ } catch {
207
+ // A throwing maskPath falls back to the automatic shape.
208
+ }
209
+ }
210
+ return auto;
211
+ }, [target, padding, customBorderRadius, maskPath, screenWidth, screenHeight]);
212
+
213
+ const usePathRendering = shapeResult?.kind === 'path';
214
+
215
+ // Bounding box for press overlay positioning
216
+ const bounds = useMemo(() => {
217
+ if (!shapeResult) return null;
218
+ if (shapeResult.kind === 'rect') {
219
+ return {
220
+ x: shapeResult.x,
221
+ y: shapeResult.y,
222
+ width: shapeResult.width,
223
+ height: shapeResult.height,
224
+ };
225
+ }
226
+ return shapeResult.bounds;
227
+ }, [shapeResult]);
228
+
229
+ // Interactive mode: four press bands around the hole catch backdrop touches
230
+ // while the hole itself stays open so taps reach the real element. The
231
+ // rounded corners leave sub-corner-radius slivers touch-transparent —
232
+ // visually part of the backdrop, acceptable.
233
+ const pressBands = useMemo(() => {
234
+ if (!interactive || !bounds) return null;
235
+ return [
236
+ { left: 0, top: 0, right: 0, height: Math.max(0, bounds.y) }, // above
237
+ { left: 0, top: bounds.y + bounds.height, right: 0, bottom: 0 }, // below
238
+ { left: 0, top: bounds.y, width: Math.max(0, bounds.x), height: bounds.height }, // left
239
+ { left: bounds.x + bounds.width, top: bounds.y, right: 0, height: bounds.height }, // right
240
+ ];
241
+ }, [interactive, bounds]);
242
+
243
+ // The dark overlay is drawn as a single even-odd path: a full-screen rectangle
244
+ // with the spotlight punched out as a real hole. This replaces an SVG <Mask>,
245
+ // which on the new architecture (Fabric) leaves the cutout partially filled
246
+ // (a white film over the highlighted element) instead of fully transparent.
247
+ const [overlayPathD, setOverlayPathD] = useState('');
248
+ const animVals = useRef({ x: 0, y: 0, w: 0, h: 0, rx: 0 });
249
+
250
+ // The overlay Modal is statusBarTranslucent / edge-to-edge, so it spans the
251
+ // full PHYSICAL screen — but `screenWidth`/`screenHeight` come from
252
+ // Dimensions.get('window'), which on Android excludes the status/navigation
253
+ // bars. Sizing the dark backdrop to the window left an undimmed strip at the
254
+ // bottom (under the nav bar). Cover the full screen instead; over-drawing past
255
+ // the Modal bounds is harmless (clipped). iOS: window == screen, so no change.
256
+ const physicalScreen = Dimensions.get('screen');
257
+ const coverWidth = Math.max(screenWidth, physicalScreen.width);
258
+ const coverHeight = Math.max(screenHeight, physicalScreen.height);
259
+
260
+ // Kept spotlights from earlier steps are appended as extra holes — the
261
+ // evenodd fill rule punches every subpath out of the full-screen rect.
262
+ const keptSuffix = keptPaths && keptPaths.length > 0 ? ` ${keptPaths.join(' ')}` : '';
263
+
264
+ const buildOverlayPath = (innerSubpath: string) =>
265
+ `M0,0 H${coverWidth} V${coverHeight} H0 Z ${innerSubpath}${keptSuffix}`;
266
+
267
+ // Keep the overlay path in sync with the animated spotlight (rect shapes).
268
+ // Per-corner (path) shapes set the path directly in the animation effect.
269
+ useEffect(() => {
270
+ if (usePathRendering) return undefined;
271
+ // Five listeners (x/y/w/h/rx) fire once each per animation frame; building
272
+ // the path string five times per frame is wasted work. Coalesce to one
273
+ // update per frame via requestAnimationFrame; values are read at flush
274
+ // time, so the last-written values always win.
275
+ let frameScheduled = false;
276
+ const update = () => {
277
+ if (frameScheduled) return;
278
+ if (typeof requestAnimationFrame === 'function') {
279
+ frameScheduled = true;
280
+ requestAnimationFrame(() => {
281
+ frameScheduled = false;
282
+ flushUpdate();
283
+ });
284
+ } else {
285
+ flushUpdate();
286
+ }
287
+ };
288
+ const flushUpdate = () => {
289
+ const v = animVals.current;
290
+ const inner = roundedRectPath(v.x, v.y, v.w, v.h, v.rx);
291
+ // Fast path: write the path data straight to the native SVG nodes. Both
292
+ // the dark hole and the pulse ring are driven from the SAME string here,
293
+ // so they can never drift apart. Falls back to setState when
294
+ // setNativeProps is unavailable (tests, exotic hosts); the final frame is
295
+ // always committed to state when the animation ends.
296
+ const overlayNode = overlayPathRef.current;
297
+ if (overlayNode && typeof overlayNode.setNativeProps === 'function') {
298
+ overlayNode.setNativeProps({ d: buildOverlayPath(inner) });
299
+ const pulseNode = pulsePathRef.current;
300
+ if (pulseNode && typeof pulseNode.setNativeProps === 'function') {
301
+ pulseNode.setNativeProps({ d: inner });
302
+ }
303
+ } else {
304
+ setOverlayPathD(buildOverlayPath(inner));
305
+ setPathD(inner);
306
+ }
307
+ };
308
+ const ix = animX.addListener(({ value }) => {
309
+ animVals.current.x = value;
310
+ update();
311
+ });
312
+ const iy = animY.addListener(({ value }) => {
313
+ animVals.current.y = value;
314
+ update();
315
+ });
316
+ const iw = animWidth.addListener(({ value }) => {
317
+ animVals.current.w = value;
318
+ update();
319
+ });
320
+ const ih = animHeight.addListener(({ value }) => {
321
+ animVals.current.h = value;
322
+ update();
323
+ });
324
+ const ir = animRx.addListener(({ value }) => {
325
+ animVals.current.rx = value;
326
+ update();
327
+ });
328
+ return () => {
329
+ animX.removeListener(ix);
330
+ animY.removeListener(iy);
331
+ animWidth.removeListener(iw);
332
+ animHeight.removeListener(ih);
333
+ animRx.removeListener(ir);
334
+ };
335
+ // eslint-disable-next-line react-hooks/exhaustive-deps
336
+ }, [
337
+ usePathRendering,
338
+ screenWidth,
339
+ screenHeight,
340
+ animX,
341
+ animY,
342
+ animWidth,
343
+ animHeight,
344
+ animRx,
345
+ keptSuffix,
346
+ ]);
347
+
348
+ // Animation effect
349
+ useEffect(() => {
350
+ if (!shapeResult) return;
351
+ const epoch = ++shapeEpoch.current;
352
+
353
+ // Snap every animated value + both state paths to a rect result at once.
354
+ const applyRectImmediate = (r: {
355
+ x: number;
356
+ y: number;
357
+ width: number;
358
+ height: number;
359
+ rx: number;
360
+ ry: number;
361
+ }) => {
362
+ animX.setValue(r.x);
363
+ animY.setValue(r.y);
364
+ animWidth.setValue(r.width);
365
+ animHeight.setValue(r.height);
366
+ animRx.setValue(r.rx);
367
+ animRy.setValue(r.ry);
368
+ animVals.current = { x: r.x, y: r.y, w: r.width, h: r.height, rx: r.rx };
369
+ const inner = roundedRectPath(r.x, r.y, r.width, r.height, r.rx);
370
+ setOverlayPathD(buildOverlayPath(inner));
371
+ // Same source as the dark hole — keeps the pulse border in lockstep.
372
+ setPathD(inner);
373
+ };
374
+
375
+ if (shapeResult.kind === 'path') {
376
+ // Path shapes (per-corner radius / custom maskPath): applied immediately —
377
+ // arbitrary paths can't be tweened reliably, so they jump. Restore the
378
+ // fade opacity in case an interrupted 'fade' left it near 0.
379
+ overlayFade.stopAnimation();
380
+ overlayFade.setValue(1);
381
+ const b = shapeResult.bounds;
382
+ animX.setValue(b.x);
383
+ animY.setValue(b.y);
384
+ animWidth.setValue(b.width);
385
+ animHeight.setValue(b.height);
386
+ setPathD(shapeResult.d);
387
+ setOverlayPathD(buildOverlayPath(shapeResult.d));
388
+ hasAnimated.current = true;
389
+ return;
390
+ }
391
+
392
+ // Rect shapes: first target always snaps into place (nothing to move from).
393
+ if (!hasAnimated.current || motion === 'none') {
394
+ overlayFade.stopAnimation();
395
+ overlayFade.setValue(1);
396
+ applyRectImmediate(shapeResult);
397
+ hasAnimated.current = true;
398
+ return;
399
+ }
400
+
401
+ // 'fade': dip the whole overlay out, jump the hole, fade back in.
402
+ // Halves scale with animationDuration (default 300ms ≈ the old 140/180).
403
+ if (motion === 'fade') {
404
+ Animated.timing(overlayFade, {
405
+ toValue: 0,
406
+ duration: Math.max(60, animationDuration * 0.45),
407
+ useNativeDriver: true,
408
+ }).start(() => {
409
+ // A newer step took over mid-dip: it either runs its own fade from the
410
+ // current opacity, or its branch restores opacity itself — never leave
411
+ // the overlay stuck invisible.
412
+ if (epoch !== shapeEpoch.current) return;
413
+ applyRectImmediate(shapeResult);
414
+ Animated.timing(overlayFade, {
415
+ toValue: 1,
416
+ duration: Math.max(80, animationDuration * 0.55),
417
+ useNativeDriver: true,
418
+ }).start();
419
+ });
420
+ return;
421
+ }
422
+
423
+ // 'morph' / 'bounce' below never touch overlayFade during the move, so make
424
+ // sure an interrupted fade can't leave the backdrop invisible.
425
+ overlayFade.stopAnimation();
426
+ overlayFade.setValue(1);
427
+
428
+ // 'morph' (timing) / 'bounce' (spring): animate position, size and radius.
429
+ const animate =
430
+ motion === 'bounce'
431
+ ? (value: Animated.Value, toValue: number) =>
432
+ Animated.spring(value, {
433
+ toValue,
434
+ friction: 7,
435
+ tension: 90,
436
+ useNativeDriver: false,
437
+ })
438
+ : (value: Animated.Value, toValue: number) =>
439
+ Animated.timing(value, {
440
+ toValue,
441
+ duration: animationDuration,
442
+ useNativeDriver: false,
443
+ });
444
+
445
+ Animated.parallel([
446
+ animate(animX, shapeResult.x),
447
+ animate(animY, shapeResult.y),
448
+ animate(animWidth, shapeResult.width),
449
+ animate(animHeight, shapeResult.height),
450
+ animate(animRx, shapeResult.rx),
451
+ animate(animRy, shapeResult.ry),
452
+ ]).start(({ finished }) => {
453
+ // Commit the final frame to React state so a later unrelated re-render
454
+ // can't paint a stale path from before the setNativeProps fast path.
455
+ if (finished && epoch === shapeEpoch.current) {
456
+ applyRectImmediate(shapeResult);
457
+ }
458
+ });
459
+ // keptSuffix is intentionally NOT a dep: a kept-paths change must repaint
460
+ // the path (separate effect below), not replay the whole step transition —
461
+ // with 'fade' that replay double-blinked the backdrop on every
462
+ // keepSpotlight step change.
463
+ // eslint-disable-next-line react-hooks/exhaustive-deps
464
+ }, [
465
+ shapeResult,
466
+ motion,
467
+ animX,
468
+ animY,
469
+ animWidth,
470
+ animHeight,
471
+ animRx,
472
+ animRy,
473
+ animationDuration,
474
+ overlayFade,
475
+ ]);
476
+
477
+ // Repaint-only path for kept-hole changes: rebuild the overlay path from the
478
+ // CURRENT inner subpath with the new kept suffix, without touching the
479
+ // animated values or the transition.
480
+ useEffect(() => {
481
+ if (pathD) setOverlayPathD(buildOverlayPath(pathD));
482
+ // buildOverlayPath closes over keptSuffix; pathD is the current inner.
483
+ // eslint-disable-next-line react-hooks/exhaustive-deps
484
+ }, [keptSuffix]);
485
+
486
+ // Pulse animation
487
+ useEffect(() => {
488
+ if (!enablePulse || !target) {
489
+ pulseOpacity.setValue(0);
490
+ return;
491
+ }
492
+
493
+ const halfDuration = pulseDuration / 2;
494
+ const loop = Animated.loop(
495
+ Animated.sequence([
496
+ Animated.timing(pulseOpacity, {
497
+ toValue: pulseMaxOpacity,
498
+ duration: halfDuration,
499
+ useNativeDriver: true,
500
+ }),
501
+ Animated.timing(pulseOpacity, {
502
+ toValue: pulseMinOpacity,
503
+ duration: halfDuration,
504
+ useNativeDriver: true,
505
+ }),
506
+ ])
507
+ );
508
+ loop.start();
509
+
510
+ return () => loop.stop();
511
+ }, [enablePulse, target, pulseDuration, pulseMinOpacity, pulseMaxOpacity, pulseOpacity]);
512
+
513
+ // No target (centered / no-spotlight step, or the measurement gap between
514
+ // steps): the screen stays dimmed and the press handler available. Kept
515
+ // spotlights (keepSpotlight) must survive this branch too — a plain solid
516
+ // backdrop made accumulated holes vanish on centered steps and blink off
517
+ // between steps.
518
+ if (!target || !shapeResult || !bounds) {
519
+ if (keptPaths && keptPaths.length > 0) {
520
+ const w = Math.max(screenWidth, Dimensions.get('screen').width);
521
+ const h = Math.max(screenHeight, Dimensions.get('screen').height);
522
+ const d = `M0,0 H${w} V${h} H0 Z ${keptPaths.join(' ')}`;
523
+ return (
524
+ <View style={StyleSheet.absoluteFill} pointerEvents="box-none">
525
+ <Pressable style={StyleSheet.absoluteFill} onPress={onBackdropPress}>
526
+ <Svg height={h} width={w} style={StyleSheet.absoluteFill}>
527
+ <Path d={d} fill={overlayColor} fillOpacity={overlayOpacity} fillRule="evenodd" />
528
+ </Svg>
529
+ </Pressable>
530
+ </View>
531
+ );
532
+ }
533
+ return (
534
+ <Pressable
535
+ style={[
536
+ StyleSheet.absoluteFill,
537
+ { backgroundColor: overlayColor, opacity: overlayOpacity },
538
+ ]}
539
+ onPress={onBackdropPress}
540
+ />
541
+ );
542
+ }
543
+
544
+ // Resolve optional libraries (cached at module scope)
545
+ const wantsBlurLayer = enableBlur || mountBlurLayer;
546
+ const blurProvider = wantsBlurLayer ? getBlurProvider() : null;
547
+ const LinearGradient = enableGradient ? getLinearGradient() : null;
548
+ const MaskedView = wantsBlurLayer && blurProvider ? getMaskedView() : null;
549
+
550
+ // --- Cutout shape rendering helpers ---
551
+ const renderRectCutout = (fill: string, stroke?: string, sw?: number) => (
552
+ <AnimatedRect
553
+ x={animX}
554
+ y={animY}
555
+ width={animWidth}
556
+ height={animHeight}
557
+ rx={animRx}
558
+ ry={animRy}
559
+ fill={fill}
560
+ stroke={stroke}
561
+ strokeWidth={sw}
562
+ />
563
+ );
564
+
565
+ const renderPathCutout = (fill: string, stroke?: string, sw?: number) => (
566
+ <Path d={pathD} fill={fill} stroke={stroke} strokeWidth={sw} />
567
+ );
568
+
569
+ const renderCutout = (fill: string, stroke?: string, sw?: number) =>
570
+ usePathRendering ? renderPathCutout(fill, stroke, sw) : renderRectCutout(fill, stroke, sw);
571
+
572
+ // Optional masked blur/gradient backdrop — shared by both render branches so
573
+ // interactive steps keep the same backdrop styling as everything else.
574
+ const renderBlurLayer = () =>
575
+ wantsBlurLayer && blurProvider && MaskedView ? (
576
+ <Animated.View style={[StyleSheet.absoluteFill, { opacity: blurFade }]} pointerEvents="none">
577
+ <MaskedView
578
+ style={StyleSheet.absoluteFill}
579
+ maskElement={
580
+ <Svg height={coverHeight} width={coverWidth}>
581
+ <Defs>
582
+ <Mask id={maskIds.inverse}>
583
+ <Rect x="0" y="0" width={coverWidth} height={coverHeight} fill="white" />
584
+ {renderCutout('black')}
585
+ </Mask>
586
+ </Defs>
587
+ <Rect
588
+ x="0"
589
+ y="0"
590
+ width={coverWidth}
591
+ height={coverHeight}
592
+ fill="black"
593
+ mask={`url(#${maskIds.inverse})`}
594
+ />
595
+ </Svg>
596
+ }
597
+ >
598
+ {blurProvider.kind === 'community' ? (
599
+ <blurProvider.Component
600
+ style={StyleSheet.absoluteFill}
601
+ blurType="light"
602
+ blurAmount={blurAmount}
603
+ reducedTransparencyFallbackColor="white"
604
+ />
605
+ ) : (
606
+ // expo-blur: real blur on iOS out of the box. On Android real blur
607
+ // additionally needs spotlightStyles.blurTarget (a ref to an
608
+ // expo-blur BlurTargetView wrapping the app content); without it,
609
+ // Android degrades to a soft tint.
610
+ <blurProvider.Component
611
+ style={StyleSheet.absoluteFill}
612
+ intensity={Math.min(100, blurAmount * 2)}
613
+ tint="light"
614
+ {...(blurTarget ? { blurTarget, blurMethod: 'dimezisBlurView' } : {})}
615
+ />
616
+ )}
617
+ {enableGradient && LinearGradient ? (
618
+ <LinearGradient
619
+ colors={gradientColors}
620
+ start={{ x: 0.5, y: 0 }}
621
+ end={{ x: 0.5, y: 1 }}
622
+ style={StyleSheet.absoluteFill}
623
+ />
624
+ ) : null}
625
+ </MaskedView>
626
+ </Animated.View>
627
+ ) : null;
628
+
629
+ if (pressBands) {
630
+ return (
631
+ <View style={StyleSheet.absoluteFill} pointerEvents="box-none">
632
+ {/* The dark backdrop never intercepts touches in interactive mode */}
633
+ <Animated.View
634
+ style={[StyleSheet.absoluteFill, { opacity: overlayFade }]}
635
+ pointerEvents="none"
636
+ >
637
+ {renderBlurLayer()}
638
+ <Svg height={coverHeight} width={coverWidth} style={StyleSheet.absoluteFill}>
639
+ <Path
640
+ ref={overlayPathRef}
641
+ d={motion === 'none' ? buildOverlayPath(shapeResult.kind === 'rect' ? roundedRectPath(shapeResult.x, shapeResult.y, shapeResult.width, shapeResult.height, shapeResult.rx) : shapeResult.d) : overlayPathD}
642
+ fill={overlayColor}
643
+ fillOpacity={overlayOpacity}
644
+ fillRule="evenodd"
645
+ />
646
+ </Svg>
647
+ {enablePulse ? (
648
+ <Animated.View
649
+ style={[StyleSheet.absoluteFill, { opacity: pulseVisible }]}
650
+ pointerEvents="none"
651
+ >
652
+ <Svg height={coverHeight} width={coverWidth}>
653
+ <Path
654
+ ref={pulsePathRef}
655
+ d={pathD}
656
+ fill="none"
657
+ stroke={pulseColor}
658
+ strokeWidth={pulseWidth}
659
+ />
660
+ </Svg>
661
+ </Animated.View>
662
+ ) : null}
663
+ </Animated.View>
664
+ {/* Backdrop touches are caught by the bands; the hole is left open */}
665
+ {pressBands.map((band, i) => (
666
+ <Pressable
667
+ key={`band-${i}`}
668
+ style={[{ position: 'absolute' }, band]}
669
+ onPress={onBackdropPress}
670
+ accessibilityElementsHidden
671
+ importantForAccessibility="no-hide-descendants"
672
+ />
673
+ ))}
674
+ </View>
675
+ );
676
+ }
677
+
678
+ return (
679
+ <View style={StyleSheet.absoluteFill} pointerEvents="box-none">
680
+ {/* Backdrop press layer — behind everything */}
681
+ <Pressable style={StyleSheet.absoluteFill} onPress={onBackdropPress}>
682
+ {/* Optional: Masked blur effect */}
683
+ {renderBlurLayer()}
684
+
685
+ {/* Main dark overlay: full screen with the spotlight punched out as a
686
+ true hole via an even-odd path (Fabric-safe; no <Mask>). Sized to the
687
+ physical screen so it covers the whole Modal (incl. the Android
688
+ nav-bar area), not just the window. */}
689
+ <Animated.View
690
+ style={[StyleSheet.absoluteFill, { opacity: overlayFade }]}
691
+ pointerEvents="none"
692
+ >
693
+ <Svg height={coverHeight} width={coverWidth} style={StyleSheet.absoluteFill}>
694
+ <Path
695
+ ref={overlayPathRef}
696
+ d={motion === 'none' ? buildOverlayPath(shapeResult.kind === 'rect' ? roundedRectPath(shapeResult.x, shapeResult.y, shapeResult.width, shapeResult.height, shapeResult.rx) : shapeResult.d) : overlayPathD}
697
+ fill={overlayColor}
698
+ fillOpacity={overlayOpacity}
699
+ fillRule="evenodd"
700
+ />
701
+ </Svg>
702
+ </Animated.View>
703
+ </Pressable>
704
+
705
+ {/* Pulse border overlay — driven by the SAME `pathD` state as the dark
706
+ cutout (set together in the same render), so the pulse outline and the
707
+ transparent hole always animate as one and never drift apart. */}
708
+ {enablePulse ? (
709
+ <Animated.View
710
+ style={[StyleSheet.absoluteFill, { opacity: pulseVisible }]}
711
+ pointerEvents="none"
712
+ >
713
+ <Svg height={coverHeight} width={coverWidth}>
714
+ <Path
715
+ ref={pulsePathRef}
716
+ d={pathD}
717
+ fill="none"
718
+ stroke={pulseColor}
719
+ strokeWidth={pulseWidth}
720
+ />
721
+ </Svg>
722
+ </Animated.View>
723
+ ) : null}
724
+
725
+ {/* Spotlight press area — on top of backdrop, over the cutout */}
726
+ {onSpotlightPress ? (
727
+ <Pressable
728
+ style={{
729
+ position: 'absolute',
730
+ left: bounds.x,
731
+ top: bounds.y,
732
+ width: bounds.width,
733
+ height: bounds.height,
734
+ }}
735
+ onPress={onSpotlightPress}
736
+ />
737
+ ) : null}
738
+ </View>
739
+ );
740
+ };
741
+
742
+ export default SpotlightOverlay;