-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathSpotlightTour.provider.tsx
More file actions
311 lines (283 loc) · 8.07 KB
/
SpotlightTour.provider.tsx
File metadata and controls
311 lines (283 loc) · 8.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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
243
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
299
300
301
302
303
304
305
306
307
308
309
310
311
import {
forwardRef,
useCallback,
useImperativeHandle,
useMemo,
useRef,
useState,
} from "react";
import { type ChildFn, isChildFunction } from "../helpers/common";
import {
type BackdropPressBehavior,
type Motion,
type OSConfig,
type Shape,
type ShapeOptions,
type SpotlightTour,
SpotlightTourContext,
type SpotlightTourCtx,
type TooltipProps,
type TourState,
type TourStatus,
type TourStep,
ZERO_SPOT,
} from "./SpotlightTour.context";
import { TourOverlay, type TourOverlayRef } from "./components/tour-overlay/TourOverlay.component";
import type { ColorValue, LayoutRectangle } from "react-native";
export interface SpotlightTourProviderProps extends TooltipProps {
/**
* The children to render in the provider. It accepts either a React
* component, or a function that returns a React component. When the child is
* a function, the `SpotlightTour` context can be accessed from the first
* argument.
*/
children: ChildFn<SpotlightTour> | React.ReactNode;
/**
* Additional y position around the spotlight target point to account for
* UI elements like status bars or navigation bars, especially when using
* translucent or edge-to-edge modes. This creates extra space between the
* target element and the spotlight mask.
*
* @default 0
*/
maskOffset?: number;
/**
* Sets the default transition motion for all steps. You can override this
* value on each step too.
*
* @default bounce
*/
motion?: Motion;
/**
* Define if the animations in the tour should use the native driver or not.
* A boolean can be used to apply the same value to both Android and iOS, or
* an object with `android` and `ios` keys can be used to define a value for
* each OS.
*
* @default false
*/
nativeDriver?: boolean | OSConfig<boolean>;
/**
* Sets the default behavior of pressing the tour's backdrop. You can use
* either one of the following values:
* - A callback function with the {@link SpotlightTour} options object in the
* first argument. This allows more granular control over the tour.
* - The `continue` literal string, which is a shortcut to move to the next
* step, and stop the tour on the last step.
* - the `stop` literal string, which is a shortcut to stop the tour.
*
* **NOTE:** You can also override this behavior on each step configuration.
*/
onBackdropPress?: BackdropPressBehavior;
/**
* Handler which gets executed when {@link SpotlightTour.pause|pause} is
* invoked. It receives the {@link TourState} so
* you can access the step index where the tour paused.
*/
onPause?: (values: TourState) => void;
/**
* Handler which gets executed when {@link SpotlightTour.resume|resume} is
* invoked. It receives the {@link ResumeParams} so
* you can access the step index where the tour resumed.
*/
onResume?: (values: TourState) => void;
/**
* Handler which gets executed when {@link SpotlightTour.stop|stop} is
* invoked. It receives the {@link TourState} so
* you can access the `current` step index where the tour stopped
* and a bool value `isLast` indicating if the step where the tour stopped is
* the last one.
*/
onStop?: (values: TourState) => void;
/**
* The color of the overlay of the tour.
*
* @default black
*/
overlayColor?: ColorValue;
/**
* The opacity applied to the overlay of the tour (between 0 to 1).
*
* @default 0.45
*/
overlayOpacity?: number;
/**
* Configures the default spotlight shape for all steps. You can override
* this value on each step too.
*
* @default circle
*/
shape?: Shape | ShapeOptions;
/**
* An array of `TourStep` objects that define each step of the tour.
*/
steps: TourStep[];
/**
* Android only: Enable translucent status and navigation bars to allow the
* tour overlay to extend behind system bars. Uses react-native-edge-to-edge
* internally to handle the translucent bars setup.
*
* @platform android
* @default false
*/
translucent?: boolean;
}
/**
* React provider component to get access to the SpotlightTour context.
*/
export const SpotlightTourProvider = forwardRef<SpotlightTour, SpotlightTourProviderProps>((props, ref) => {
const {
arrow,
children,
flip,
maskOffset = 0,
motion = "bounce",
nativeDriver = true,
offset,
onBackdropPress,
onPause,
onResume,
onStop,
overlayColor = "black",
overlayOpacity = 0.45,
placement,
shape = "circle",
shift,
steps,
translucent,
} = props;
const [current, setCurrent] = useState<number>();
const [spot, setSpot] = useState(ZERO_SPOT);
const [pausedAt, setPausedAt] = useState<number>();
const overlay = useRef<TourOverlayRef>({
hideTooltip: () => Promise.resolve({ finished: false }),
});
const status = useMemo((): TourStatus => {
if (current === undefined) {
return pausedAt !== undefined
? "paused"
: "idle";
}
return "running";
}, [current, pausedAt]);
const currentStep = useMemo((): TourStep => {
const step = current !== undefined
? steps[current]
: undefined;
return step ?? { render: () => <></> };
}, [steps, current]);
const renderStep = useCallback((index: number): void => {
const step = steps[index];
if (step !== undefined) {
Promise.all([
overlay.current.hideTooltip(),
Promise.resolve().then(step.before),
])
.then(() => setCurrent(index));
}
}, [steps]);
const changeSpot = useCallback((newSpot: LayoutRectangle): void => {
setSpot(newSpot);
}, []);
const start = useCallback((): void => {
renderStep(0);
}, [renderStep]);
const stop = useCallback((): void => {
setCurrent(prev => {
if (prev !== undefined) {
onStop?.({
index: prev,
isLast: prev === steps.length - 1,
});
}
return undefined;
});
setSpot(ZERO_SPOT);
}, [onStop]);
const next = useCallback((): void => {
if (current !== undefined) {
current === steps.length - 1
? stop()
: renderStep(current + 1);
}
}, [stop, renderStep, current, steps.length]);
const previous = useCallback((): void => {
if (current !== undefined && current > 0) {
renderStep(current - 1);
}
}, [renderStep, current]);
const goTo = useCallback((index: number): void => {
renderStep(index);
}, [renderStep]);
const pause = useCallback((): void => {
setCurrent(prev => {
if (prev !== undefined) {
setPausedAt(prev);
onPause?.({
index: prev,
isLast: prev === steps.length - 1,
});
}
return undefined;
});
}, [onPause, steps.length]);
const resume = useCallback((): void => {
const index = pausedAt ?? 0;
const isLast = index === steps.length - 1;
goTo(index);
setPausedAt(undefined);
onResume?.({ index, isLast });
}, [goTo, onResume, pausedAt, steps.length]);
const tour = useMemo((): SpotlightTourCtx => ({
changeSpot,
current,
goTo,
next,
pause,
previous,
resume,
spot,
start,
status,
steps,
stop,
}), [changeSpot, current, goTo, next, previous, spot, start, steps, stop, pause]);
useImperativeHandle(ref, () => ({
current,
goTo,
next,
pause,
previous,
resume,
start,
status,
stop,
}));
return (
<SpotlightTourContext.Provider value={tour}>
{isChildFunction(children)
? <SpotlightTourContext.Consumer>{children}</SpotlightTourContext.Consumer>
: <>{children}</>
}
<TourOverlay
backdropOpacity={overlayOpacity}
color={overlayColor}
current={current}
maskOffset={maskOffset}
motion={motion}
nativeDriver={nativeDriver}
onBackdropPress={onBackdropPress}
ref={overlay}
shape={shape}
spot={spot}
tourStep={currentStep}
arrow={arrow}
flip={flip}
offset={offset}
placement={placement}
shift={shift}
translucent={translucent}
/>
</SpotlightTourContext.Provider>
);
});