-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathTourOverlay.component.tsx
More file actions
310 lines (276 loc) · 8.66 KB
/
TourOverlay.component.tsx
File metadata and controls
310 lines (276 loc) · 8.66 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
import {
type FlipOptions,
type Middleware,
type ShiftOptions,
type UseFloatingOptions,
arrow,
flip,
offset,
shift,
useFloating,
} from "@floating-ui/react-native";
import {
type ComponentType,
type RefObject,
forwardRef,
useCallback,
useContext,
useEffect,
useImperativeHandle,
useMemo,
useRef,
} from "react";
import {
Animated,
type ColorValue,
type LayoutRectangle,
Modal,
Platform,
View,
} from "react-native";
import { Defs, Mask, Rect, Svg } from "react-native-svg";
import { vh, vw } from "../../../helpers/responsive";
import {
type BackdropPressBehavior,
type Motion,
type OSConfig,
type Shape,
type ShapeOptions,
SpotlightTourContext,
type TooltipProps,
type TourStep,
} from "../../SpotlightTour.context";
import { Css, DEFAULT_ARROW, arrowCss } from "./TourOverlay.styles";
import { CircleShape } from "./shapes/CircleShape.component";
import { RectShape } from "./shapes/RectShape.component";
import type { Optional, ToOptional } from "../../../helpers/common";
import type { ShapeProps } from "../../../helpers/shape";
export interface TourOverlayRef {
hideTooltip: () => Promise<Animated.EndResult>;
}
interface TourOverlayProps extends ToOptional<TooltipProps> {
backdropOpacity: number;
color: ColorValue;
current: Optional<number>;
maskOffset?: number;
motion: Motion;
nativeDriver: boolean | OSConfig<boolean>;
onBackdropPress: Optional<BackdropPressBehavior>;
shape: Shape | ShapeOptions;
spot: LayoutRectangle;
tourStep: TourStep;
translucent?: boolean;
}
export const TourOverlay = forwardRef<TourOverlayRef, TourOverlayProps>((props, ref) => {
const {
backdropOpacity,
color,
current,
maskOffset = 0,
motion,
nativeDriver,
onBackdropPress,
shape,
spot: defaultSpot,
tourStep,
translucent,
...tooltipProps
} = props;
const { goTo, next, pause, previous, resume, start, steps, stop } = useContext(SpotlightTourContext);
const arrowRef = useRef<View>(null);
const spot = useMemo((): LayoutRectangle => ({
...defaultSpot,
...(translucent && { y: defaultSpot.y + maskOffset }),
}), [defaultSpot, translucent, maskOffset]);
const floating = useMemo((): TooltipProps => ({
arrow: tourStep.arrow ?? tooltipProps.arrow,
flip: tourStep.flip ?? tooltipProps.flip,
offset: tourStep.offset ?? tooltipProps.offset,
placement: tourStep.placement ?? tooltipProps.placement,
shift: tourStep.shift ?? tooltipProps.shift,
}), [tooltipProps, tourStep.arrow, tourStep.flip, tourStep.offset, tourStep.placement, tourStep.shift]);
const floatingOptions = useMemo(() => {
return makeFloatingOptions(arrowRef, floating);
}, [floating]);
const { floatingStyles, middlewareData, placement, refs } = useFloating(floatingOptions);
const tooltipOpacity = useRef(new Animated.Value(0));
const stepMotion = useMemo((): Motion => {
return tourStep.motion ?? motion;
}, [tourStep, motion]);
const shapeOptions = useMemo((): Required<ShapeOptions> => {
const options = tourStep.shape ?? shape;
const padding = 16;
return typeof options !== "string"
? { padding, type: "circle", ...options }
: { padding, type: options };
}, [tourStep, shape]);
const useNativeDriver = useMemo(() => {
const driverConfig: OSConfig<boolean> = typeof nativeDriver === "boolean"
? { android: nativeDriver, ios: nativeDriver, web: nativeDriver }
: nativeDriver;
return Platform.select({
android: driverConfig.android,
default: false,
ios: driverConfig.ios,
web: false,
});
}, [nativeDriver]);
const ShapeMask = useMemo(<P extends ShapeProps>(): ComponentType<P> => {
switch (shapeOptions.type) {
case "circle": return CircleShape;
case "rectangle": return RectShape;
}
}, [shapeOptions]);
const handleBackdropPress = useCallback((): void => {
const handler = tourStep.onBackdropPress ?? onBackdropPress;
if (handler !== undefined && current !== undefined) {
switch (handler) {
case "continue":
return next();
case "stop":
return stop();
default:
return handler({ current, goTo, next, pause, previous, resume, start, status: "running", stop });
}
}
}, [tourStep, onBackdropPress, current, goTo, next, previous, start, stop, pause, resume]);
useEffect(() => {
const { height, width } = spot;
if ([height, width].every(value => value > 0)) {
Animated.timing(tooltipOpacity.current, {
delay: 400,
duration: 400,
toValue: 1,
useNativeDriver,
})
.start();
}
}, [spot, useNativeDriver]);
useImperativeHandle<TourOverlayRef, TourOverlayRef>(ref, () => ({
hideTooltip: () => {
return new Promise(resolve => {
if (current !== undefined) {
Animated.timing(tooltipOpacity.current, {
duration: 400,
toValue: 0,
useNativeDriver,
})
.start(resolve);
} else {
resolve({ finished: true });
}
});
},
}), [current, useNativeDriver]);
return (
<Modal
animationType="fade"
presentationStyle="overFullScreen"
supportedOrientations={["portrait", "landscape", "landscape-left", "landscape-right", "portrait-upside-down"]}
transparent={true}
visible={current !== undefined}
>
<View testID="Overlay View" style={Css.overlayView}>
<Svg
testID="Spot Svg"
height={vh(100)}
width={vw(100)}
viewBox={`0 0 ${vw(100)} ${vh(100)}`}
onPress={handleBackdropPress}
shouldRasterizeIOS={true}
renderToHardwareTextureAndroid={true}
>
<Defs>
<Mask id="mask" x={0} y={0} height="100%" width="100%">
<Rect height={vh(100)} width={vw(100)} fill="#fff" />
<ShapeMask
spot={spot}
setReference={refs.setReference}
motion={stepMotion}
padding={shapeOptions.padding}
useNativeDriver={useNativeDriver}
/>
</Mask>
</Defs>
<Rect
height={vh(100)}
width={vw(100)}
fill={color}
mask="url(#mask)"
opacity={backdropOpacity}
/>
</Svg>
{current !== undefined && (
<Animated.View
ref={refs.setFloating}
testID="Tooltip View"
style={{ ...floatingStyles, opacity: tooltipOpacity.current }}
>
<tourStep.render
current={current}
isFirst={current === 0}
isLast={current === steps.length - 1}
next={next}
previous={previous}
stop={stop}
pause={pause}
resume={resume}
goTo={goTo}
/>
{floating.arrow !== false && (
<View
style={[
Css.tooltipArrow,
arrowCss({
arrow: typeof floating.arrow !== "boolean"
? floating.arrow
: undefined,
data: middlewareData.arrow,
placement,
}),
]}
ref={arrowRef}
/>
)}
</Animated.View>
)}
</View>
</Modal>
);
});
function makeFloatingOptions(arrowRef: RefObject<null | View>, props: Optional<TooltipProps>): UseFloatingOptions {
const arrowOption = typeof props?.arrow === "boolean"
? DEFAULT_ARROW
: props?.arrow;
const { size } = typeof arrowOption === "number"
? { ...DEFAULT_ARROW, size: arrowOption }
: { ...DEFAULT_ARROW, ...arrowOption };
const baseOffset = props?.offset || 4;
const offsetValue = props?.arrow !== false
? (Math.sqrt(2 * size ** 2) / 2) + baseOffset
: baseOffset;
const arrowMw = props?.arrow !== false
? arrow({ element: arrowRef })
: undefined;
const flipMw = flipMiddleware(props?.flip);
const offsetMw = props?.offset !== 0
? offset(offsetValue)
: undefined;
const shiftMw = shiftMiddleware(props?.shift);
return {
middleware: [flipMw, offsetMw, shiftMw, arrowMw].filter(Boolean),
placement: props?.placement,
};
}
function flipMiddleware(flipProps: Optional<boolean | FlipOptions>): Optional<Middleware> {
if (flipProps !== false) {
return flip(flipProps === true ? undefined : flipProps);
}
return undefined;
}
function shiftMiddleware(shiftProps: Optional<boolean | ShiftOptions>): Optional<Middleware> {
if (shiftProps !== false) {
return shift(shiftProps === true ? undefined : shiftProps);
}
return undefined;
}