-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCoachmarkOverlay.tsx
More file actions
219 lines (193 loc) · 6.01 KB
/
CoachmarkOverlay.tsx
File metadata and controls
219 lines (193 loc) · 6.01 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
import React, { useEffect, useState, useMemo, useCallback, memo } from 'react';
import {
Dimensions,
Modal,
Pressable,
StyleSheet,
type LayoutChangeEvent,
Platform,
} from 'react-native';
import Animated, {
useSharedValue,
withTiming,
useAnimatedStyle,
} from 'react-native-reanimated';
import { useCoachmarkContext } from '../core/CoachmarkContext';
import type { TooltipRenderProps, TooltipRenderer } from '../core/types';
import { useOrientationChange } from '../hooks/useOrientationChange';
import { useTooltipPosition } from '../hooks/useTooltipPosition';
import { useTourMeasurement } from '../hooks/useTourMeasurement';
import { isReduceMotionEnabled } from '../utils/accessibility';
import { CoachmarkErrorBoundary } from './CoachmarkErrorBoundary';
import { AnimatedMask } from './Mask';
import { Tooltip } from './Tooltip';
/**
* Memoized wrapper component to safely render custom tooltips with hooks
* This ensures hooks are always called in the same order by always mounting the same component
*/
const CustomTooltipWrapper = memo<{
renderer: TooltipRenderer | undefined;
props: TooltipRenderProps;
fallback: React.ReactElement;
}>(({ renderer, props, fallback }) => {
if (renderer) {
return <>{renderer(props)}</>;
}
return fallback;
});
CustomTooltipWrapper.displayName = 'CustomTooltipWrapper';
/**
* Main overlay component for displaying coachmarks
* Optimized with React.memo and custom hooks to reduce re-renders
*/
export const CoachmarkOverlay: React.FC = () => {
const { state, getAnchor, setMeasured, next, back, stop, theme } =
useCoachmarkContext();
const [tooltipSize, setTooltipSize] = useState<{
width: number;
height: number;
} | null>(null);
const [reduceMotion, setReduceMotion] = useState(false);
const opacity = useSharedValue(0);
const holeX = useSharedValue(0);
const holeY = useSharedValue(0);
const holeWidth = useSharedValue(1);
const holeHeight = useSharedValue(1);
const { width: W, height: H } =
Platform.OS === 'android'
? Dimensions.get('screen') // includes status bar area
: Dimensions.get('window');
const activeStep = state.activeTour?.steps[state.index];
useEffect(() => {
isReduceMotionEnabled().then(setReduceMotion);
}, []);
useEffect(() => {
const duration = reduceMotion ? 0 : theme.motion.durationMs;
opacity.value = withTiming(state.isActive ? 1 : 0, {
duration,
});
}, [opacity, state.isActive, theme.motion.durationMs, reduceMotion]);
const { targetRect, holeShape, holeRadius, remeasure } = useTourMeasurement({
activeStep,
index: state.index,
tourKey: state.activeTour?.key,
getAnchor,
setMeasured,
next,
reduceMotion,
durationMs: theme.motion.durationMs,
holeX,
holeY,
holeWidth,
holeHeight,
state,
});
const tooltipPos = useTooltipPosition({
targetRect,
tooltipSize,
activeStep,
});
const handleOrientationChange = useCallback(() => {
remeasure();
}, [remeasure]);
useOrientationChange(state.isActive, handleOrientationChange);
const aStyle = useAnimatedStyle(() => ({ opacity: opacity.value }));
const handleTooltipLayout = useCallback((e: LayoutChangeEvent) => {
const { width, height } = e.nativeEvent.layout;
setTooltipSize({ width, height });
}, []);
const handleSkip = useCallback(() => stop('skipped'), [stop]);
const tooltipRenderProps: TooltipRenderProps | null = useMemo(() => {
if (!activeStep || !state.activeTour) return null;
return {
theme,
title: activeStep.title,
description: activeStep.description,
index: state.index,
count: state.activeTour.steps.length,
isFirst: state.index === 0,
isLast: state.index === state.activeTour.steps.length - 1,
onNext: next,
onBack: back,
onSkip: handleSkip,
currentStep: activeStep,
};
}, [
theme,
activeStep,
state.index,
state.activeTour,
next,
back,
handleSkip,
]);
if (!state.isActive || !activeStep || !tooltipRenderProps) return null;
const customRenderer =
activeStep.renderTooltip || state.activeTour?.renderTooltip;
return (
<Modal
transparent
visible
animationType="none"
statusBarTranslucent={Platform.OS === 'android'}
>
<Animated.View style={[StyleSheet.absoluteFill, aStyle]}>
<AnimatedMask
width={W}
height={H}
holes={[
{
x: holeX,
y: holeY,
width: holeWidth,
height: holeHeight,
shape: holeShape,
radius: holeRadius,
},
]}
backdropColor={theme.backdropColor}
backdropOpacity={theme.backdropOpacity}
/>
<Pressable style={StyleSheet.absoluteFill} onPress={next} />
<Animated.View
style={[
styles.tooltipContainer,
{ left: tooltipPos.x, top: tooltipPos.y },
]}
onLayout={handleTooltipLayout}
>
<CoachmarkErrorBoundary
onError={(error) => {
console.error('[Coachmark] Custom tooltip error:', error);
}}
onReset={next}
>
<CustomTooltipWrapper
renderer={customRenderer}
props={tooltipRenderProps}
fallback={
<Tooltip
theme={theme}
title={activeStep.title}
description={activeStep.description}
pos={{ x: 0, y: 0 }}
index={state.index}
count={state.activeTour?.steps.length ?? 0}
onNext={next}
onBack={state.index > 0 ? back : undefined}
onSkip={handleSkip}
onLayout={(size) => setTooltipSize(size)}
/>
}
/>
</CoachmarkErrorBoundary>
</Animated.View>
</Animated.View>
</Modal>
);
};
const styles = StyleSheet.create({
tooltipContainer: {
position: 'absolute',
},
});