@momo-kits/animated-tooltip 0.169.15 → 0.169.16-beta.14
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.
- package/index.tsx +208 -226
- package/package.json +1 -1
- package/renderButtons.tsx +29 -112
- package/styles.ts +6 -38
- package/types.ts +9 -3
- package/utils.ts +179 -51
- package/TooltipButtons.tsx +0 -57
- package/usePreloadMeasure.ts +0 -35
package/index.tsx
CHANGED
|
@@ -5,56 +5,90 @@ import React, {
|
|
|
5
5
|
useEffect,
|
|
6
6
|
useImperativeHandle,
|
|
7
7
|
useMemo,
|
|
8
|
-
useReducer,
|
|
9
8
|
useRef,
|
|
10
9
|
useState,
|
|
11
10
|
} from 'react';
|
|
11
|
+
import { Animated, Dimensions, Easing, Modal, View } from 'react-native';
|
|
12
12
|
import {
|
|
13
|
-
|
|
14
|
-
Easing,
|
|
15
|
-
TouchableWithoutFeedback,
|
|
16
|
-
useWindowDimensions,
|
|
17
|
-
View,
|
|
18
|
-
} from 'react-native';
|
|
19
|
-
import {
|
|
13
|
+
ApplicationContext,
|
|
20
14
|
MiniAppContext,
|
|
21
15
|
ScreenContext,
|
|
22
|
-
Spacing,
|
|
23
|
-
useTooltipPortal,
|
|
24
16
|
} from '@momo-kits/foundation';
|
|
25
17
|
import styles from './styles';
|
|
26
18
|
import { TooltipPlacement, TooltipProps, TooltipRef } from './types';
|
|
27
|
-
import {
|
|
28
|
-
|
|
19
|
+
import {
|
|
20
|
+
getFlippedPosition,
|
|
21
|
+
getPlacementStyle,
|
|
22
|
+
getTooltipArrowStyle,
|
|
23
|
+
TOOLTIP_OFFSET,
|
|
24
|
+
} from './utils';
|
|
29
25
|
import { renderTooltipContent } from './renderButtons';
|
|
30
26
|
|
|
31
|
-
type Frame = { x: number; y: number; width: number; height: number };
|
|
32
|
-
|
|
33
|
-
const EMPTY_FRAME: Frame = { x: 0, y: 0, width: 0, height: 0 };
|
|
34
27
|
const ANIMATION_DURATION = 180;
|
|
35
|
-
const TOOLTIP_OFFSET = Spacing.S;
|
|
36
|
-
const ARROW_SIZE = 6;
|
|
37
28
|
const SLIDE_DISTANCE = 4;
|
|
29
|
+
// MoMo's RN fork alerts in dev on a raw Modal unless it is flagged as a kit
|
|
30
|
+
// Modal; spread so the untyped prop passes type checks.
|
|
31
|
+
const MODAL_KIT_PROPS = { isModalKit: true };
|
|
32
|
+
|
|
33
|
+
// Caps the settle wait at about one second of frames.
|
|
34
|
+
const MAX_SETTLE_FRAMES = 60;
|
|
35
|
+
|
|
36
|
+
type Frame = { x: number; y: number; width: number; height: number };
|
|
37
|
+
|
|
38
|
+
// Module-level so every Tooltip instance can close the one currently open.
|
|
39
|
+
let closeActiveTooltip: (() => void) | null = null;
|
|
38
40
|
|
|
39
|
-
/** Whether
|
|
40
|
-
const
|
|
41
|
-
const
|
|
42
|
-
const
|
|
43
|
-
return
|
|
41
|
+
/** Whether the frame overlaps the window, not sitting on a screen still off it. */
|
|
42
|
+
const isOnScreen = ({ x, y, width, height }: Frame) => {
|
|
43
|
+
const window = Dimensions.get('window');
|
|
44
|
+
const isInX = x + width > 0 && x < window.width;
|
|
45
|
+
return isInX && y + height > 0 && y < window.height;
|
|
44
46
|
};
|
|
45
47
|
|
|
46
48
|
/**
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
+
* Measures the view each frame until two on-screen reads match, so a tooltip
|
|
50
|
+
* shown while its screen still slides in opens at the settled spot.
|
|
51
|
+
*/
|
|
52
|
+
const measureSettled = (
|
|
53
|
+
getView: () => View | null,
|
|
54
|
+
isStale: () => boolean,
|
|
55
|
+
onSettled: (frame: Frame) => void,
|
|
56
|
+
previous?: Frame,
|
|
57
|
+
framesLeft = MAX_SETTLE_FRAMES,
|
|
58
|
+
) => {
|
|
59
|
+
const view = getView();
|
|
60
|
+
if (!view || isStale()) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
view.measureInWindow((x, y, width, height) => {
|
|
64
|
+
if (isStale()) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const frame = { x, y, width, height };
|
|
68
|
+
const isSame = previous?.x === x && previous?.y === y;
|
|
69
|
+
const isSettled = isSame && isOnScreen(frame);
|
|
70
|
+
if (isSettled || framesLeft <= 0) {
|
|
71
|
+
onSettled(frame);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
requestAnimationFrame(() => {
|
|
75
|
+
measureSettled(getView, isStale, onSettled, frame, framesLeft - 1);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Wraps children as the anchor and shows a bubble in its own Modal so it
|
|
82
|
+
* draws above headers and sheets. Opened and closed through the ref.
|
|
49
83
|
*/
|
|
50
84
|
const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
|
|
51
85
|
{
|
|
52
86
|
children,
|
|
53
87
|
title,
|
|
54
88
|
description,
|
|
55
|
-
buttons = [],
|
|
56
89
|
placement = 'top',
|
|
57
90
|
align = 'center',
|
|
91
|
+
shouldFlip = false,
|
|
58
92
|
accessibilityLabel,
|
|
59
93
|
onVisibleChange,
|
|
60
94
|
onPressClose,
|
|
@@ -62,17 +96,13 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
|
|
|
62
96
|
},
|
|
63
97
|
ref,
|
|
64
98
|
) {
|
|
99
|
+
const { theme } = useContext(ApplicationContext);
|
|
65
100
|
const app = useContext<any>(MiniAppContext);
|
|
66
101
|
const screen = useContext<any>(ScreenContext);
|
|
67
|
-
const screenWidth = useWindowDimensions().width;
|
|
68
102
|
const [tooltipSize, setTooltipSize] = useState({ width: 0, height: 0 });
|
|
69
103
|
|
|
70
|
-
const portal = useTooltipPortal();
|
|
71
|
-
const portalId = useRef(
|
|
72
|
-
`tooltip-${Math.random().toString(36).substr(2, 9)}`,
|
|
73
|
-
).current;
|
|
74
|
-
|
|
75
104
|
const anchorRef = useRef<View>(null);
|
|
105
|
+
const hostRef = useRef<View>(null);
|
|
76
106
|
|
|
77
107
|
const [anchorPosition, setAnchorPosition] = useState({
|
|
78
108
|
x: 0,
|
|
@@ -81,11 +111,11 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
|
|
|
81
111
|
height: 0,
|
|
82
112
|
});
|
|
83
113
|
|
|
84
|
-
//
|
|
85
|
-
const [hostPosition, setHostPosition] = useState
|
|
86
|
-
x:
|
|
87
|
-
y:
|
|
88
|
-
});
|
|
114
|
+
// Window origin of the Modal host; null until measured after it lays out.
|
|
115
|
+
const [hostPosition, setHostPosition] = useState<{
|
|
116
|
+
x: number;
|
|
117
|
+
y: number;
|
|
118
|
+
} | null>(null);
|
|
89
119
|
|
|
90
120
|
const componentName = 'Tooltip';
|
|
91
121
|
const componentId = useMemo(() => {
|
|
@@ -96,32 +126,30 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
|
|
|
96
126
|
return `${app.appId}/${app.code}/${screen.screenName}/${componentName}`;
|
|
97
127
|
}, [accessibilityLabel, app, screen, componentName]);
|
|
98
128
|
|
|
99
|
-
|
|
129
|
+
const [isVisible, setIsVisible] = useState(false);
|
|
100
130
|
const isVisibleRef = useRef(false);
|
|
101
|
-
const
|
|
131
|
+
const isMountedRef = useRef(false);
|
|
132
|
+
// Bumped on every hide so a pending anchor measure cannot reopen it.
|
|
133
|
+
const showToken = useRef(0);
|
|
134
|
+
const onVisibleChangeRef = useRef(onVisibleChange);
|
|
135
|
+
onVisibleChangeRef.current = onVisibleChange;
|
|
102
136
|
const animatedValue = useRef(new Animated.Value(0)).current;
|
|
103
|
-
const showTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
104
137
|
const hideTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
105
138
|
|
|
106
|
-
const
|
|
107
|
-
if (showTimer.current) {
|
|
108
|
-
clearTimeout(showTimer.current);
|
|
109
|
-
}
|
|
139
|
+
const clearHideTimer = useCallback(() => {
|
|
110
140
|
if (hideTimer.current) {
|
|
111
141
|
clearTimeout(hideTimer.current);
|
|
142
|
+
hideTimer.current = null;
|
|
112
143
|
}
|
|
113
|
-
};
|
|
144
|
+
}, []);
|
|
114
145
|
|
|
115
|
-
const setVisibility = useCallback(
|
|
116
|
-
(nextVisible
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
},
|
|
123
|
-
[onVisibleChange],
|
|
124
|
-
);
|
|
146
|
+
const setVisibility = useCallback((nextVisible: boolean) => {
|
|
147
|
+
if (isVisibleRef.current !== nextVisible) {
|
|
148
|
+
isVisibleRef.current = nextVisible;
|
|
149
|
+
setIsVisible(nextVisible);
|
|
150
|
+
onVisibleChangeRef.current?.(nextVisible);
|
|
151
|
+
}
|
|
152
|
+
}, []);
|
|
125
153
|
|
|
126
154
|
const animate = useCallback(
|
|
127
155
|
(nextVisible: boolean) => {
|
|
@@ -135,114 +163,104 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
|
|
|
135
163
|
[animatedValue],
|
|
136
164
|
);
|
|
137
165
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
166
|
+
/** Hides at once without the fade-out delay; also used to close a sibling. */
|
|
167
|
+
const hideNow = useCallback(() => {
|
|
168
|
+
showToken.current += 1;
|
|
169
|
+
clearHideTimer();
|
|
170
|
+
animatedValue.stopAnimation();
|
|
171
|
+
animatedValue.setValue(0);
|
|
172
|
+
setHostPosition(null);
|
|
173
|
+
setVisibility(false);
|
|
174
|
+
if (closeActiveTooltip === hideNow) {
|
|
175
|
+
closeActiveTooltip = null;
|
|
143
176
|
}
|
|
144
|
-
|
|
145
|
-
}, [tooltipSize, animate]);
|
|
146
|
-
|
|
147
|
-
usePreloadMeasure({
|
|
148
|
-
hostRef: portal.hostRef,
|
|
149
|
-
anchorRef,
|
|
150
|
-
setHostPosition,
|
|
151
|
-
setAnchorPosition,
|
|
152
|
-
});
|
|
177
|
+
}, [animatedValue, clearHideTimer, setVisibility]);
|
|
153
178
|
|
|
179
|
+
// Stay invisible until both bubble size and host origin are known.
|
|
180
|
+
const hasSize = tooltipSize.width > 0 || tooltipSize.height > 0;
|
|
181
|
+
const isReady = isVisible && hasSize && hostPosition !== null;
|
|
154
182
|
useEffect(() => {
|
|
155
|
-
if (
|
|
156
|
-
|
|
157
|
-
setHostPosition({ x: 0, y: 0 });
|
|
158
|
-
return;
|
|
183
|
+
if (isReady) {
|
|
184
|
+
animate(true);
|
|
159
185
|
}
|
|
160
|
-
|
|
161
|
-
let rafId: number | null = null;
|
|
162
|
-
let lastPosition = { x: 0, y: 0, width: 0, height: 0 };
|
|
163
|
-
let lastHostPosition = { x: 0, y: 0 };
|
|
164
|
-
let isMounted = true;
|
|
165
|
-
let isActive = true;
|
|
166
|
-
|
|
167
|
-
const measureAnchor = () => {
|
|
168
|
-
if (!isMounted || !isActive || !anchorRef.current) {
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
if (portal.hostRef?.current) {
|
|
173
|
-
portal.hostRef.current.measureInWindow((hostX, hostY) => {
|
|
174
|
-
if (!isMounted || !isActive) return;
|
|
175
|
-
|
|
176
|
-
if (hostX !== lastHostPosition.x || hostY !== lastHostPosition.y) {
|
|
177
|
-
lastHostPosition = { x: hostX, y: hostY };
|
|
178
|
-
setHostPosition({ x: hostX, y: hostY });
|
|
179
|
-
}
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
anchorRef.current.measureInWindow((x, y, width, height) => {
|
|
184
|
-
if (!isMounted || !isActive) return;
|
|
185
|
-
|
|
186
|
-
if (!isSameFrame({ x, y, width, height }, lastPosition)) {
|
|
187
|
-
lastPosition = { x, y, width, height };
|
|
188
|
-
setAnchorPosition({ x, y, width, height });
|
|
189
|
-
}
|
|
190
|
-
});
|
|
191
|
-
};
|
|
192
|
-
|
|
193
|
-
// Measure once on the next frame, after layout settles (no polling).
|
|
194
|
-
rafId = requestAnimationFrame(measureAnchor);
|
|
195
|
-
|
|
196
|
-
return () => {
|
|
197
|
-
isMounted = false;
|
|
198
|
-
isActive = false;
|
|
199
|
-
|
|
200
|
-
if (rafId !== null) {
|
|
201
|
-
cancelAnimationFrame(rafId);
|
|
202
|
-
rafId = null;
|
|
203
|
-
}
|
|
204
|
-
};
|
|
205
|
-
}, [portal.hostRef]);
|
|
186
|
+
}, [isReady, animate]);
|
|
206
187
|
|
|
207
188
|
useEffect(() => {
|
|
189
|
+
isMountedRef.current = true;
|
|
208
190
|
return () => {
|
|
209
|
-
|
|
191
|
+
isMountedRef.current = false;
|
|
192
|
+
clearHideTimer();
|
|
210
193
|
animatedValue.stopAnimation();
|
|
194
|
+
if (closeActiveTooltip === hideNow) {
|
|
195
|
+
closeActiveTooltip = null;
|
|
196
|
+
}
|
|
211
197
|
};
|
|
212
|
-
}, [animatedValue]);
|
|
198
|
+
}, [animatedValue, clearHideTimer, hideNow]);
|
|
213
199
|
|
|
214
200
|
const handleShow = useCallback(() => {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
201
|
+
clearHideTimer();
|
|
202
|
+
if (closeActiveTooltip && closeActiveTooltip !== hideNow) {
|
|
203
|
+
closeActiveTooltip();
|
|
204
|
+
}
|
|
205
|
+
closeActiveTooltip = hideNow;
|
|
206
|
+
showToken.current += 1;
|
|
207
|
+
const token = showToken.current;
|
|
208
|
+
|
|
209
|
+
const isStale = () => !isMountedRef.current || token !== showToken.current;
|
|
210
|
+
|
|
211
|
+
// Show only once the anchor settles so the bubble opens in place.
|
|
212
|
+
measureSettled(
|
|
213
|
+
() => anchorRef.current,
|
|
214
|
+
isStale,
|
|
215
|
+
frame => {
|
|
216
|
+
setAnchorPosition(frame);
|
|
217
|
+
// Reopened during its own fade-out: isReady stays true, so fade back in.
|
|
218
|
+
if (isVisibleRef.current) {
|
|
219
|
+
animate(true);
|
|
220
|
+
}
|
|
226
221
|
setVisibility(true);
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
}, [
|
|
222
|
+
},
|
|
223
|
+
);
|
|
224
|
+
}, [animate, clearHideTimer, hideNow, setVisibility]);
|
|
230
225
|
|
|
231
226
|
const handleHide = useCallback(() => {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
227
|
+
clearHideTimer();
|
|
228
|
+
showToken.current += 1;
|
|
229
|
+
animate(false);
|
|
230
|
+
hideTimer.current = setTimeout(hideNow, ANIMATION_DURATION);
|
|
231
|
+
}, [animate, clearHideTimer, hideNow]);
|
|
232
|
+
|
|
233
|
+
const handlePressClose = useCallback(() => {
|
|
234
|
+
handleHide();
|
|
235
|
+
onPressClose?.();
|
|
236
|
+
}, [handleHide, onPressClose]);
|
|
237
|
+
|
|
238
|
+
const handleHostLayout = useCallback(() => {
|
|
239
|
+
hostRef.current?.measureInWindow((x, y) => {
|
|
240
|
+
setHostPosition({ x, y });
|
|
241
|
+
});
|
|
242
|
+
}, []);
|
|
238
243
|
|
|
239
244
|
useImperativeHandle(ref, () => ({ show: handleShow, hide: handleHide }), [
|
|
240
245
|
handleHide,
|
|
241
246
|
handleShow,
|
|
242
247
|
]);
|
|
243
248
|
|
|
244
|
-
const
|
|
245
|
-
|
|
249
|
+
const shown = useMemo(() => {
|
|
250
|
+
if (!shouldFlip || !hasSize) {
|
|
251
|
+
return { placement, align };
|
|
252
|
+
}
|
|
253
|
+
return getFlippedPosition({
|
|
254
|
+
anchorPosition,
|
|
255
|
+
tooltipSize,
|
|
256
|
+
placement,
|
|
257
|
+
align,
|
|
258
|
+
});
|
|
259
|
+
}, [shouldFlip, hasSize, anchorPosition, tooltipSize, placement, align]);
|
|
260
|
+
|
|
261
|
+
const isVertical = shown.placement === 'top' || shown.placement === 'bottom';
|
|
262
|
+
const isBeforeAnchor =
|
|
263
|
+
shown.placement === 'top' || shown.placement === 'left';
|
|
246
264
|
const slideFrom = isBeforeAnchor ? SLIDE_DISTANCE : -SLIDE_DISTANCE;
|
|
247
265
|
const slide = animatedValue.interpolate({
|
|
248
266
|
inputRange: [0, 1],
|
|
@@ -250,103 +268,67 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
|
|
|
250
268
|
});
|
|
251
269
|
const translate = isVertical ? { translateY: slide } : { translateX: slide };
|
|
252
270
|
|
|
271
|
+
const hostX = hostPosition?.x ?? 0;
|
|
272
|
+
const hostY = hostPosition?.y ?? 0;
|
|
253
273
|
const placementStyle = useMemo(() => {
|
|
254
274
|
return getPlacementStyle({
|
|
255
275
|
anchorPosition,
|
|
256
276
|
tooltipSize,
|
|
257
|
-
placement,
|
|
258
|
-
align,
|
|
259
|
-
hostX
|
|
260
|
-
hostY
|
|
277
|
+
placement: shown.placement,
|
|
278
|
+
align: shown.align,
|
|
279
|
+
hostX,
|
|
280
|
+
hostY,
|
|
261
281
|
offset: TOOLTIP_OFFSET,
|
|
262
282
|
});
|
|
263
|
-
}, [
|
|
264
|
-
anchorPosition,
|
|
265
|
-
tooltipSize,
|
|
266
|
-
placement,
|
|
267
|
-
hostPosition.y,
|
|
268
|
-
hostPosition.x,
|
|
269
|
-
align,
|
|
270
|
-
]);
|
|
271
|
-
|
|
272
|
-
const renderContent = () => {
|
|
273
|
-
return renderTooltipContent({
|
|
274
|
-
title,
|
|
275
|
-
description,
|
|
276
|
-
buttons,
|
|
277
|
-
screenWidth,
|
|
278
|
-
onPressClose,
|
|
279
|
-
});
|
|
280
|
-
};
|
|
281
|
-
|
|
282
|
-
const getArrowStyle = () => {
|
|
283
|
-
return getTooltipArrowStyle({
|
|
284
|
-
arrowSize: ARROW_SIZE,
|
|
285
|
-
align,
|
|
286
|
-
placement,
|
|
287
|
-
tooltipSize,
|
|
288
|
-
});
|
|
289
|
-
};
|
|
290
|
-
|
|
291
|
-
useEffect(() => {
|
|
292
|
-
let isActive = true;
|
|
293
|
-
|
|
294
|
-
if (!isVisibleRef.current) {
|
|
295
|
-
portal.unregister(portalId, true); // Immediate cleanup when hiding
|
|
296
|
-
return;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
const hasMeasurement = !isSameFrame(anchorPosition, EMPTY_FRAME);
|
|
300
|
-
if (!hasMeasurement) {
|
|
301
|
-
return;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
const content = (
|
|
305
|
-
<>
|
|
306
|
-
<TouchableWithoutFeedback onPress={handleHide}>
|
|
307
|
-
<View style={styles.backdrop} />
|
|
308
|
-
</TouchableWithoutFeedback>
|
|
309
|
-
|
|
310
|
-
<Animated.View
|
|
311
|
-
pointerEvents="auto"
|
|
312
|
-
style={[
|
|
313
|
-
{
|
|
314
|
-
maxWidth: screenWidth - Spacing.M * 2,
|
|
315
|
-
},
|
|
316
|
-
containerStyle,
|
|
317
|
-
styles.tooltip,
|
|
318
|
-
placementStyle,
|
|
319
|
-
{
|
|
320
|
-
opacity: animatedValue,
|
|
321
|
-
transform: [translate],
|
|
322
|
-
},
|
|
323
|
-
]}
|
|
324
|
-
onLayout={event => {
|
|
325
|
-
if (isActive) {
|
|
326
|
-
setTooltipSize(event.nativeEvent.layout);
|
|
327
|
-
}
|
|
328
|
-
}}
|
|
329
|
-
>
|
|
330
|
-
{renderContent()}
|
|
331
|
-
<View style={getArrowStyle()} />
|
|
332
|
-
</Animated.View>
|
|
333
|
-
</>
|
|
334
|
-
);
|
|
283
|
+
}, [anchorPosition, tooltipSize, shown, hostY, hostX]);
|
|
335
284
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
}, [isVisibleRef.current, anchorPosition, placement, align, tooltipSize]);
|
|
285
|
+
const bubbleColor = theme.colors.interactive.primary;
|
|
286
|
+
const arrowStyle = getTooltipArrowStyle({
|
|
287
|
+
align: shown.align,
|
|
288
|
+
placement: shown.placement,
|
|
289
|
+
tooltipSize,
|
|
290
|
+
color: bubbleColor,
|
|
291
|
+
});
|
|
344
292
|
|
|
345
293
|
return (
|
|
346
294
|
<View style={styles.container} accessibilityLabel={componentId}>
|
|
347
295
|
<View ref={anchorRef} style={styles.anchorView}>
|
|
348
296
|
{children}
|
|
349
297
|
</View>
|
|
298
|
+
<Modal
|
|
299
|
+
visible={isVisible}
|
|
300
|
+
transparent
|
|
301
|
+
animationType="none"
|
|
302
|
+
statusBarTranslucent
|
|
303
|
+
navigationBarTranslucent
|
|
304
|
+
onRequestClose={handleHide}
|
|
305
|
+
{...MODAL_KIT_PROPS}
|
|
306
|
+
>
|
|
307
|
+
<View ref={hostRef} style={styles.host} onLayout={handleHostLayout}>
|
|
308
|
+
<View style={styles.backdrop} onTouchStart={handleHide} />
|
|
309
|
+
<Animated.View
|
|
310
|
+
pointerEvents="auto"
|
|
311
|
+
style={[
|
|
312
|
+
containerStyle,
|
|
313
|
+
styles.tooltip,
|
|
314
|
+
placementStyle,
|
|
315
|
+
{
|
|
316
|
+
backgroundColor: bubbleColor,
|
|
317
|
+
opacity: animatedValue,
|
|
318
|
+
transform: [translate],
|
|
319
|
+
},
|
|
320
|
+
]}
|
|
321
|
+
onLayout={event => setTooltipSize(event.nativeEvent.layout)}
|
|
322
|
+
>
|
|
323
|
+
{renderTooltipContent({
|
|
324
|
+
title,
|
|
325
|
+
description,
|
|
326
|
+
onPressClose: handlePressClose,
|
|
327
|
+
})}
|
|
328
|
+
<View style={arrowStyle} />
|
|
329
|
+
</Animated.View>
|
|
330
|
+
</View>
|
|
331
|
+
</Modal>
|
|
350
332
|
</View>
|
|
351
333
|
);
|
|
352
334
|
});
|
package/package.json
CHANGED
package/renderButtons.tsx
CHANGED
|
@@ -1,131 +1,40 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { TouchableOpacity, View } from 'react-native';
|
|
3
|
-
import { Colors, Icon,
|
|
3
|
+
import { Colors, Icon, Text } from '@momo-kits/foundation';
|
|
4
4
|
import styles from './styles';
|
|
5
|
-
import { TooltipButton } from './types';
|
|
6
|
-
import { IconButton, PrimaryButton, SecondaryButton } from './TooltipButtons';
|
|
7
5
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
* right; buttons with `icon` render as icon buttons (a pair only if both do).
|
|
11
|
-
*/
|
|
12
|
-
export const renderTooltipButtons = (buttons: TooltipButton[]) => {
|
|
13
|
-
if (!buttons.length) {
|
|
14
|
-
return null;
|
|
15
|
-
}
|
|
6
|
+
const CLOSE_ICON_SIZE = 20;
|
|
7
|
+
const CLOSE_HIT_SLOP = 12;
|
|
16
8
|
|
|
17
|
-
|
|
18
|
-
const btn = buttons[0];
|
|
19
|
-
const onPress = btn.onPress ?? (() => {});
|
|
20
|
-
return (
|
|
21
|
-
<View style={styles.buttonsRow}>
|
|
22
|
-
<View
|
|
23
|
-
style={[
|
|
24
|
-
styles.button,
|
|
25
|
-
btn.icon ? styles.iconButton : styles.textButton,
|
|
26
|
-
]}
|
|
27
|
-
>
|
|
28
|
-
{btn.icon ? (
|
|
29
|
-
<IconButton icon={btn.icon} onPress={onPress} />
|
|
30
|
-
) : (
|
|
31
|
-
<PrimaryButton title={btn.title || ''} onPress={onPress} />
|
|
32
|
-
)}
|
|
33
|
-
</View>
|
|
34
|
-
</View>
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (buttons.length === 2) {
|
|
39
|
-
const [first, second] = buttons;
|
|
40
|
-
const firstPress = first.onPress ?? (() => {});
|
|
41
|
-
const secondPress = second.onPress ?? (() => {});
|
|
42
|
-
const bothIcon = !!first.icon && !!second.icon;
|
|
43
|
-
|
|
44
|
-
return (
|
|
45
|
-
<View style={styles.buttonsRow}>
|
|
46
|
-
{bothIcon ? (
|
|
47
|
-
<View style={styles.buttonRowContainer}>
|
|
48
|
-
<IconButton
|
|
49
|
-
icon={second.icon as string}
|
|
50
|
-
onPress={secondPress}
|
|
51
|
-
style={styles.buttonSpacing}
|
|
52
|
-
/>
|
|
53
|
-
<IconButton icon={first.icon as string} onPress={firstPress} />
|
|
54
|
-
</View>
|
|
55
|
-
) : (
|
|
56
|
-
<View style={styles.buttonRowContainer}>
|
|
57
|
-
<SecondaryButton
|
|
58
|
-
title={second.title || ''}
|
|
59
|
-
onPress={secondPress}
|
|
60
|
-
style={styles.buttonSpacing}
|
|
61
|
-
/>
|
|
62
|
-
<PrimaryButton title={first.title || ''} onPress={firstPress} />
|
|
63
|
-
</View>
|
|
64
|
-
)}
|
|
65
|
-
</View>
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return (
|
|
70
|
-
<View style={styles.buttonsRow}>
|
|
71
|
-
{buttons.map((btn, idx) => {
|
|
72
|
-
const isIcon = !!btn.icon;
|
|
73
|
-
const key = btn.title || btn.icon || `${idx}`;
|
|
74
|
-
const onPress = btn.onPress ?? (() => {});
|
|
75
|
-
|
|
76
|
-
return (
|
|
77
|
-
<View
|
|
78
|
-
key={key}
|
|
79
|
-
style={[
|
|
80
|
-
styles.button,
|
|
81
|
-
isIcon ? styles.iconButton : styles.textButton,
|
|
82
|
-
]}
|
|
83
|
-
>
|
|
84
|
-
{isIcon ? (
|
|
85
|
-
<IconButton icon={btn.icon as string} onPress={onPress} />
|
|
86
|
-
) : (
|
|
87
|
-
<PrimaryButton title={btn.title || ''} onPress={onPress} />
|
|
88
|
-
)}
|
|
89
|
-
</View>
|
|
90
|
-
);
|
|
91
|
-
})}
|
|
92
|
-
</View>
|
|
93
|
-
);
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
/** Bubble body: required title, optional description, close icon and actions. */
|
|
9
|
+
/** Bubble body: optional title, optional description and the close icon. */
|
|
97
10
|
export const renderTooltipContent = ({
|
|
98
11
|
title,
|
|
99
12
|
description,
|
|
100
|
-
buttons,
|
|
101
|
-
screenWidth,
|
|
102
13
|
onPressClose,
|
|
103
14
|
}: {
|
|
104
|
-
title
|
|
15
|
+
title?: string;
|
|
105
16
|
description?: string;
|
|
106
|
-
buttons: TooltipButton[];
|
|
107
|
-
screenWidth: number;
|
|
108
17
|
onPressClose?: () => void;
|
|
109
18
|
}) => {
|
|
110
19
|
return (
|
|
111
20
|
<View style={styles.content}>
|
|
112
21
|
<View style={styles.contentHeader}>
|
|
113
|
-
<View
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
</Text>
|
|
22
|
+
<View style={styles.contentTextContainer}>
|
|
23
|
+
{title ? (
|
|
24
|
+
<Text
|
|
25
|
+
typography={'header_s_semibold'}
|
|
26
|
+
color={Colors.black_01}
|
|
27
|
+
numberOfLines={1}
|
|
28
|
+
>
|
|
29
|
+
{title}
|
|
30
|
+
</Text>
|
|
31
|
+
) : null}
|
|
124
32
|
{description ? (
|
|
125
33
|
<Text
|
|
126
|
-
typography={'
|
|
34
|
+
typography={'body_default_regular'}
|
|
127
35
|
color={Colors.black_01}
|
|
128
|
-
|
|
36
|
+
numberOfLines={2}
|
|
37
|
+
style={title ? styles.description : undefined}
|
|
129
38
|
>
|
|
130
39
|
{description}
|
|
131
40
|
</Text>
|
|
@@ -133,13 +42,21 @@ export const renderTooltipContent = ({
|
|
|
133
42
|
</View>
|
|
134
43
|
<TouchableOpacity
|
|
135
44
|
onPress={onPressClose}
|
|
136
|
-
hitSlop={{
|
|
45
|
+
hitSlop={{
|
|
46
|
+
top: CLOSE_HIT_SLOP,
|
|
47
|
+
bottom: CLOSE_HIT_SLOP,
|
|
48
|
+
left: CLOSE_HIT_SLOP,
|
|
49
|
+
right: CLOSE_HIT_SLOP,
|
|
50
|
+
}}
|
|
137
51
|
style={styles.closeIconSpacing}
|
|
138
52
|
>
|
|
139
|
-
<Icon
|
|
53
|
+
<Icon
|
|
54
|
+
source={'navigation_close'}
|
|
55
|
+
size={CLOSE_ICON_SIZE}
|
|
56
|
+
color={Colors.black_01}
|
|
57
|
+
/>
|
|
140
58
|
</TouchableOpacity>
|
|
141
59
|
</View>
|
|
142
|
-
{renderTooltipButtons(buttons)}
|
|
143
60
|
</View>
|
|
144
61
|
);
|
|
145
62
|
};
|
package/styles.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { StyleSheet } from 'react-native';
|
|
2
2
|
import { Colors, Radius, Spacing } from '@momo-kits/foundation';
|
|
3
3
|
|
|
4
|
+
const TOOLTIP_WIDTH = 220;
|
|
5
|
+
|
|
4
6
|
export default StyleSheet.create({
|
|
5
7
|
container: {
|
|
6
8
|
position: 'relative',
|
|
7
9
|
},
|
|
8
10
|
tooltip: {
|
|
9
11
|
position: 'absolute',
|
|
12
|
+
width: TOOLTIP_WIDTH,
|
|
10
13
|
padding: Spacing.M,
|
|
11
|
-
backgroundColor: Colors.blue_03,
|
|
12
14
|
borderRadius: Radius.S,
|
|
13
15
|
},
|
|
14
16
|
text: {
|
|
@@ -25,52 +27,18 @@ export default StyleSheet.create({
|
|
|
25
27
|
description: {
|
|
26
28
|
marginTop: Spacing.XS,
|
|
27
29
|
},
|
|
28
|
-
buttonsRow: {
|
|
29
|
-
marginTop: Spacing.M,
|
|
30
|
-
flexDirection: 'row',
|
|
31
|
-
flexWrap: 'wrap',
|
|
32
|
-
justifyContent: 'flex-end',
|
|
33
|
-
},
|
|
34
|
-
button: {
|
|
35
|
-
marginRight: Spacing.XXS,
|
|
36
|
-
marginBottom: Spacing.XXS,
|
|
37
|
-
},
|
|
38
|
-
textButton: {},
|
|
39
|
-
iconButton: {},
|
|
40
|
-
buttonRowContainer: {
|
|
41
|
-
flexDirection: 'row',
|
|
42
|
-
alignItems: 'center',
|
|
43
|
-
},
|
|
44
|
-
buttonSpacing: {
|
|
45
|
-
marginRight: Spacing.S,
|
|
46
|
-
},
|
|
47
30
|
contentHeader: {
|
|
48
31
|
flexDirection: 'row',
|
|
49
32
|
},
|
|
50
33
|
closeIconSpacing: {
|
|
51
34
|
marginLeft: Spacing.S,
|
|
52
35
|
},
|
|
53
|
-
secondaryButtonContainer: {
|
|
54
|
-
paddingHorizontal: Spacing.M,
|
|
55
|
-
paddingVertical: Spacing.S,
|
|
56
|
-
},
|
|
57
|
-
iconButtonContainer: {
|
|
58
|
-
borderRadius: Radius.XL,
|
|
59
|
-
backgroundColor: Colors.black_01,
|
|
60
|
-
alignItems: 'center',
|
|
61
|
-
justifyContent: 'center',
|
|
62
|
-
},
|
|
63
36
|
anchorView: {
|
|
64
37
|
alignSelf: 'flex-start',
|
|
65
38
|
},
|
|
66
39
|
contentTextContainer: {
|
|
67
|
-
|
|
68
|
-
},
|
|
69
|
-
backdrop: {
|
|
70
|
-
position: 'absolute',
|
|
71
|
-
top: -9999,
|
|
72
|
-
left: -9999,
|
|
73
|
-
right: -9999,
|
|
74
|
-
bottom: -9999,
|
|
40
|
+
flex: 1,
|
|
75
41
|
},
|
|
42
|
+
host: StyleSheet.absoluteFillObject,
|
|
43
|
+
backdrop: StyleSheet.absoluteFillObject,
|
|
76
44
|
});
|
package/types.ts
CHANGED
|
@@ -22,13 +22,13 @@ export type TooltipProps = {
|
|
|
22
22
|
/**
|
|
23
23
|
* Title displayed at the top of the tooltip.
|
|
24
24
|
*/
|
|
25
|
-
title
|
|
25
|
+
title?: string;
|
|
26
26
|
/**
|
|
27
27
|
* Description text shown under the title.
|
|
28
28
|
*/
|
|
29
29
|
description?: string;
|
|
30
30
|
/**
|
|
31
|
-
*
|
|
31
|
+
* @deprecated Ignored: the tooltip no longer renders action buttons.
|
|
32
32
|
*/
|
|
33
33
|
buttons?: TooltipButton[];
|
|
34
34
|
/**
|
|
@@ -41,6 +41,12 @@ export type TooltipProps = {
|
|
|
41
41
|
* @default 'center'
|
|
42
42
|
*/
|
|
43
43
|
align?: 'start' | 'center' | 'end';
|
|
44
|
+
/**
|
|
45
|
+
* Flips to the opposite side or align when the bubble would leave the window
|
|
46
|
+
* and the alternative fits. Off by default, so the tooltip stays where set.
|
|
47
|
+
* @default false
|
|
48
|
+
*/
|
|
49
|
+
shouldFlip?: boolean;
|
|
44
50
|
/**
|
|
45
51
|
* Accessibility label for the component.
|
|
46
52
|
*/
|
|
@@ -50,7 +56,7 @@ export type TooltipProps = {
|
|
|
50
56
|
*/
|
|
51
57
|
onVisibleChange?: (visible: boolean) => void;
|
|
52
58
|
/**
|
|
53
|
-
*
|
|
59
|
+
* Called after the close button (X) hides the tooltip.
|
|
54
60
|
*/
|
|
55
61
|
onPressClose?: () => void;
|
|
56
62
|
|
package/utils.ts
CHANGED
|
@@ -1,9 +1,128 @@
|
|
|
1
|
-
import { ViewStyle } from 'react-native';
|
|
2
|
-
import {
|
|
1
|
+
import { Dimensions, ViewStyle } from 'react-native';
|
|
2
|
+
import { Spacing } from '@momo-kits/foundation';
|
|
3
3
|
import styles from './styles';
|
|
4
4
|
import { TooltipPlacement, TooltipProps } from './types';
|
|
5
5
|
|
|
6
6
|
type Align = NonNullable<TooltipProps['align']>;
|
|
7
|
+
type Frame = { x: number; y: number; width: number; height: number };
|
|
8
|
+
type Size = { width: number; height: number };
|
|
9
|
+
|
|
10
|
+
const ARROW_WIDTH = 12;
|
|
11
|
+
const ARROW_HEIGHT = 6;
|
|
12
|
+
const ARROW_GAP = Spacing.XS;
|
|
13
|
+
// Distance from the bubble's start edge to the arrow tip for start/end align.
|
|
14
|
+
const ARROW_EDGE_INSET = Spacing.M + ARROW_WIDTH / 2;
|
|
15
|
+
// Bubble edge to anchor edge: arrow depth plus the gap to the anchor.
|
|
16
|
+
const TOOLTIP_OFFSET = ARROW_HEIGHT + ARROW_GAP;
|
|
17
|
+
// Sides to try in order: the requested one, its opposite, then the other axis.
|
|
18
|
+
const PLACEMENT_FALLBACKS: Record<TooltipPlacement, TooltipPlacement[]> = {
|
|
19
|
+
top: ['top', 'bottom', 'right', 'left'],
|
|
20
|
+
bottom: ['bottom', 'top', 'right', 'left'],
|
|
21
|
+
left: ['left', 'right', 'top', 'bottom'],
|
|
22
|
+
right: ['right', 'left', 'top', 'bottom'],
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Start of the bubble on the cross axis so the arrow tip lands on the anchor
|
|
27
|
+
* center: tip 18 from the start edge, centered, or 18 from the end edge.
|
|
28
|
+
*/
|
|
29
|
+
const getCrossAxisStart = (
|
|
30
|
+
anchorCenter: number,
|
|
31
|
+
tooltipLength: number,
|
|
32
|
+
align: Align,
|
|
33
|
+
) => {
|
|
34
|
+
if (align === 'start') {
|
|
35
|
+
return anchorCenter - ARROW_EDGE_INSET;
|
|
36
|
+
}
|
|
37
|
+
if (align === 'end') {
|
|
38
|
+
return anchorCenter - (tooltipLength - ARROW_EDGE_INSET);
|
|
39
|
+
}
|
|
40
|
+
return anchorCenter - tooltipLength / 2;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/** Whether the bubble fits between the anchor and the window edge on that side. */
|
|
44
|
+
const hasMainAxisRoom = (
|
|
45
|
+
anchor: Frame,
|
|
46
|
+
size: Size,
|
|
47
|
+
placement: TooltipPlacement,
|
|
48
|
+
) => {
|
|
49
|
+
const window = Dimensions.get('window');
|
|
50
|
+
if (placement === 'top') {
|
|
51
|
+
return anchor.y - size.height - TOOLTIP_OFFSET >= 0;
|
|
52
|
+
}
|
|
53
|
+
if (placement === 'left') {
|
|
54
|
+
return anchor.x - size.width - TOOLTIP_OFFSET >= 0;
|
|
55
|
+
}
|
|
56
|
+
if (placement === 'bottom') {
|
|
57
|
+
const bubbleEnd = anchor.y + anchor.height + TOOLTIP_OFFSET + size.height;
|
|
58
|
+
return bubbleEnd <= window.height;
|
|
59
|
+
}
|
|
60
|
+
const bubbleEnd = anchor.x + anchor.width + TOOLTIP_OFFSET + size.width;
|
|
61
|
+
return bubbleEnd <= window.width;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/** Whether the bubble stays inside the window along the anchor's edge. */
|
|
65
|
+
const hasCrossAxisRoom = (
|
|
66
|
+
anchor: Frame,
|
|
67
|
+
size: Size,
|
|
68
|
+
placement: TooltipPlacement,
|
|
69
|
+
align: Align,
|
|
70
|
+
) => {
|
|
71
|
+
const window = Dimensions.get('window');
|
|
72
|
+
const isVertical = placement === 'top' || placement === 'bottom';
|
|
73
|
+
const center = isVertical
|
|
74
|
+
? anchor.x + anchor.width / 2
|
|
75
|
+
: anchor.y + anchor.height / 2;
|
|
76
|
+
const length = isVertical ? size.width : size.height;
|
|
77
|
+
const limit = isVertical ? window.width : window.height;
|
|
78
|
+
const start = getCrossAxisStart(center, length, align);
|
|
79
|
+
return start >= 0 && start + length <= limit;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/** Aligns to try in order: the requested one first, then its mirror, then the rest. */
|
|
83
|
+
const getAlignCandidates = (align: Align): Align[] => {
|
|
84
|
+
if (align === 'start') {
|
|
85
|
+
return ['start', 'end', 'center'];
|
|
86
|
+
}
|
|
87
|
+
if (align === 'end') {
|
|
88
|
+
return ['end', 'start', 'center'];
|
|
89
|
+
}
|
|
90
|
+
return ['center', 'start', 'end'];
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Placement and align that keep the bubble inside the window: the first side
|
|
95
|
+
* with room for both, or the requested values when nothing fits.
|
|
96
|
+
*/
|
|
97
|
+
export const getFlippedPosition = ({
|
|
98
|
+
anchorPosition,
|
|
99
|
+
tooltipSize,
|
|
100
|
+
placement,
|
|
101
|
+
align,
|
|
102
|
+
}: {
|
|
103
|
+
anchorPosition: Frame;
|
|
104
|
+
tooltipSize: Size;
|
|
105
|
+
placement: TooltipPlacement;
|
|
106
|
+
align: Align;
|
|
107
|
+
}): { placement: TooltipPlacement; align: Align } => {
|
|
108
|
+
for (const candidate of PLACEMENT_FALLBACKS[placement]) {
|
|
109
|
+
if (!hasMainAxisRoom(anchorPosition, tooltipSize, candidate)) {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
const fittingAlign = getAlignCandidates(align).find(alignCandidate => {
|
|
113
|
+
return hasCrossAxisRoom(
|
|
114
|
+
anchorPosition,
|
|
115
|
+
tooltipSize,
|
|
116
|
+
candidate,
|
|
117
|
+
alignCandidate,
|
|
118
|
+
);
|
|
119
|
+
});
|
|
120
|
+
if (fittingAlign) {
|
|
121
|
+
return { placement: candidate, align: fittingAlign };
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return { placement, align };
|
|
125
|
+
};
|
|
7
126
|
|
|
8
127
|
/**
|
|
9
128
|
* Top/left of the bubble inside the portal host: the anchor's window frame
|
|
@@ -34,20 +153,16 @@ export const getPlacementStyle = ({
|
|
|
34
153
|
const { x, y, width, height } = anchorPosition;
|
|
35
154
|
const { width: tooltipWidth, height: tooltipHeight } = tooltipSize;
|
|
36
155
|
|
|
37
|
-
|
|
156
|
+
const position: { top?: number; left?: number; right?: number } = {};
|
|
38
157
|
|
|
39
158
|
if (placement === 'top') {
|
|
40
159
|
position.top = y - tooltipHeight - offset - hostY;
|
|
41
160
|
} else if (placement === 'bottom') {
|
|
42
161
|
position.top = y + height + offset - hostY;
|
|
43
162
|
} else {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
position.top = y + height - tooltipHeight - hostY;
|
|
48
|
-
} else {
|
|
49
|
-
position.top = y + height / 2 - tooltipHeight / 2 - hostY;
|
|
50
|
-
}
|
|
163
|
+
const anchorCenterY = y + height / 2;
|
|
164
|
+
position.top =
|
|
165
|
+
getCrossAxisStart(anchorCenterY, tooltipHeight, align) - hostY;
|
|
51
166
|
}
|
|
52
167
|
|
|
53
168
|
if (placement === 'left') {
|
|
@@ -55,89 +170,102 @@ export const getPlacementStyle = ({
|
|
|
55
170
|
} else if (placement === 'right') {
|
|
56
171
|
position.left = x + width + offset - hostX;
|
|
57
172
|
} else {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
position.left = x + width - tooltipWidth - hostX;
|
|
62
|
-
} else {
|
|
63
|
-
position.left = x + width / 2 - tooltipWidth / 2 - hostX;
|
|
64
|
-
}
|
|
173
|
+
const anchorCenterX = x + width / 2;
|
|
174
|
+
position.left =
|
|
175
|
+
getCrossAxisStart(anchorCenterX, tooltipWidth, align) - hostX;
|
|
65
176
|
}
|
|
66
177
|
|
|
67
178
|
return position;
|
|
68
179
|
};
|
|
69
180
|
|
|
70
181
|
/**
|
|
71
|
-
*
|
|
72
|
-
*
|
|
182
|
+
* Sharp 12x6 triangle on the bubble edge facing the anchor, built with the
|
|
183
|
+
* border trick; its tip sits ARROW_EDGE_INSET from the start edge for start/end.
|
|
73
184
|
*/
|
|
74
185
|
export const getTooltipArrowStyle = ({
|
|
75
|
-
arrowSize,
|
|
76
186
|
align,
|
|
77
187
|
placement,
|
|
78
188
|
tooltipSize,
|
|
189
|
+
color,
|
|
79
190
|
}: {
|
|
80
|
-
arrowSize: number;
|
|
81
191
|
align: Align;
|
|
82
192
|
placement: TooltipPlacement;
|
|
83
193
|
tooltipSize: { width: number; height: number };
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
width: size * 2,
|
|
90
|
-
height: size * 2,
|
|
91
|
-
backgroundColor: Colors.blue_03,
|
|
92
|
-
transform: [{ rotate: '45deg' }],
|
|
93
|
-
borderRadius: size / 2,
|
|
94
|
-
},
|
|
95
|
-
];
|
|
96
|
-
const inset = size + Spacing.M;
|
|
97
|
-
const centerTop = (tooltipSize.height || 0) / 2 - size;
|
|
194
|
+
color: string;
|
|
195
|
+
}): ViewStyle[] => {
|
|
196
|
+
const half = ARROW_WIDTH / 2;
|
|
197
|
+
const edgeStart = ARROW_EDGE_INSET - half;
|
|
198
|
+
const centerTop = (tooltipSize.height || 0) / 2 - half;
|
|
98
199
|
let horizontalAlign: ViewStyle = { alignSelf: 'center' };
|
|
99
200
|
let verticalAlign: ViewStyle = { top: centerTop };
|
|
100
201
|
if (align === 'start') {
|
|
101
|
-
horizontalAlign = { left:
|
|
102
|
-
verticalAlign = { top:
|
|
202
|
+
horizontalAlign = { left: edgeStart };
|
|
203
|
+
verticalAlign = { top: edgeStart };
|
|
103
204
|
} else if (align === 'end') {
|
|
104
|
-
horizontalAlign = { right:
|
|
105
|
-
verticalAlign = { bottom:
|
|
205
|
+
horizontalAlign = { right: edgeStart };
|
|
206
|
+
verticalAlign = { bottom: edgeStart };
|
|
106
207
|
}
|
|
107
208
|
|
|
209
|
+
const sides: ViewStyle = {
|
|
210
|
+
borderLeftWidth: half,
|
|
211
|
+
borderRightWidth: half,
|
|
212
|
+
borderLeftColor: 'transparent',
|
|
213
|
+
borderRightColor: 'transparent',
|
|
214
|
+
};
|
|
215
|
+
const ends: ViewStyle = {
|
|
216
|
+
borderTopWidth: half,
|
|
217
|
+
borderBottomWidth: half,
|
|
218
|
+
borderTopColor: 'transparent',
|
|
219
|
+
borderBottomColor: 'transparent',
|
|
220
|
+
};
|
|
221
|
+
|
|
108
222
|
switch (placement) {
|
|
109
223
|
case 'top':
|
|
110
224
|
return [
|
|
111
|
-
|
|
225
|
+
styles.arrow,
|
|
226
|
+
sides,
|
|
112
227
|
{
|
|
113
|
-
|
|
114
|
-
|
|
228
|
+
borderTopWidth: ARROW_HEIGHT,
|
|
229
|
+
borderTopColor: color,
|
|
230
|
+
bottom: -ARROW_HEIGHT,
|
|
115
231
|
},
|
|
232
|
+
horizontalAlign,
|
|
116
233
|
];
|
|
117
234
|
case 'bottom':
|
|
118
235
|
return [
|
|
119
|
-
|
|
236
|
+
styles.arrow,
|
|
237
|
+
sides,
|
|
120
238
|
{
|
|
121
|
-
|
|
122
|
-
|
|
239
|
+
borderBottomWidth: ARROW_HEIGHT,
|
|
240
|
+
borderBottomColor: color,
|
|
241
|
+
top: -ARROW_HEIGHT,
|
|
123
242
|
},
|
|
243
|
+
horizontalAlign,
|
|
124
244
|
];
|
|
125
245
|
case 'left':
|
|
126
246
|
return [
|
|
127
|
-
|
|
247
|
+
styles.arrow,
|
|
248
|
+
ends,
|
|
128
249
|
{
|
|
129
|
-
|
|
130
|
-
|
|
250
|
+
borderLeftWidth: ARROW_HEIGHT,
|
|
251
|
+
borderLeftColor: color,
|
|
252
|
+
right: -ARROW_HEIGHT,
|
|
131
253
|
},
|
|
254
|
+
verticalAlign,
|
|
132
255
|
];
|
|
133
256
|
case 'right':
|
|
134
257
|
default:
|
|
135
258
|
return [
|
|
136
|
-
|
|
259
|
+
styles.arrow,
|
|
260
|
+
ends,
|
|
137
261
|
{
|
|
138
|
-
|
|
139
|
-
|
|
262
|
+
borderRightWidth: ARROW_HEIGHT,
|
|
263
|
+
borderRightColor: color,
|
|
264
|
+
left: -ARROW_HEIGHT,
|
|
140
265
|
},
|
|
266
|
+
verticalAlign,
|
|
141
267
|
];
|
|
142
268
|
}
|
|
143
269
|
};
|
|
270
|
+
|
|
271
|
+
export { TOOLTIP_OFFSET };
|
package/TooltipButtons.tsx
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import React, { FC } from 'react';
|
|
2
|
-
import { TouchableOpacity } from 'react-native';
|
|
3
|
-
import {
|
|
4
|
-
Button,
|
|
5
|
-
ButtonProps,
|
|
6
|
-
Colors,
|
|
7
|
-
Icon,
|
|
8
|
-
IconButtonProps,
|
|
9
|
-
Text,
|
|
10
|
-
useScaleSize,
|
|
11
|
-
} from '@momo-kits/foundation';
|
|
12
|
-
import styles from './styles';
|
|
13
|
-
|
|
14
|
-
/** Main tooltip action: a medium foundation Button of type secondary. */
|
|
15
|
-
const PrimaryButton: FC<ButtonProps> = ({ title, onPress }) => {
|
|
16
|
-
return (
|
|
17
|
-
<Button
|
|
18
|
-
full={false}
|
|
19
|
-
title={title}
|
|
20
|
-
onPress={onPress}
|
|
21
|
-
type={'secondary'}
|
|
22
|
-
size={'medium'}
|
|
23
|
-
/>
|
|
24
|
-
);
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
/** Text-only action shown left of the primary in a two-button row. */
|
|
28
|
-
const SecondaryButton: FC<ButtonProps> = ({ title, onPress, style }) => {
|
|
29
|
-
return (
|
|
30
|
-
<TouchableOpacity
|
|
31
|
-
style={[styles.secondaryButtonContainer, style]}
|
|
32
|
-
onPress={onPress}
|
|
33
|
-
>
|
|
34
|
-
<Text color={Colors.black_01} typography={'action_s_bold'}>
|
|
35
|
-
{title}
|
|
36
|
-
</Text>
|
|
37
|
-
</TouchableOpacity>
|
|
38
|
-
);
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
/** 36pt (scaled) icon-only action. */
|
|
42
|
-
const IconButton: FC<IconButtonProps> = ({ icon, onPress, style }) => {
|
|
43
|
-
return (
|
|
44
|
-
<TouchableOpacity
|
|
45
|
-
onPress={onPress}
|
|
46
|
-
style={[
|
|
47
|
-
{ width: useScaleSize(36), height: useScaleSize(36) },
|
|
48
|
-
styles.iconButtonContainer,
|
|
49
|
-
style,
|
|
50
|
-
]}
|
|
51
|
-
>
|
|
52
|
-
<Icon source={icon} />
|
|
53
|
-
</TouchableOpacity>
|
|
54
|
-
);
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
export { PrimaryButton, SecondaryButton, IconButton };
|
package/usePreloadMeasure.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { RefObject, useEffect } from 'react';
|
|
2
|
-
import { View } from 'react-native';
|
|
3
|
-
|
|
4
|
-
type Position = { x: number; y: number; width: number; height: number };
|
|
5
|
-
|
|
6
|
-
/** Measures host and anchor once after mount so the first show() opens fast. */
|
|
7
|
-
export const usePreloadMeasure = ({
|
|
8
|
-
hostRef,
|
|
9
|
-
anchorRef,
|
|
10
|
-
setHostPosition,
|
|
11
|
-
setAnchorPosition,
|
|
12
|
-
}: {
|
|
13
|
-
hostRef?: RefObject<View | null> | null;
|
|
14
|
-
anchorRef: RefObject<View | null>;
|
|
15
|
-
setHostPosition: (position: { x: number; y: number }) => void;
|
|
16
|
-
setAnchorPosition: (position: Position) => void;
|
|
17
|
-
}) => {
|
|
18
|
-
useEffect(() => {
|
|
19
|
-
const timer = setTimeout(() => {
|
|
20
|
-
if (hostRef?.current) {
|
|
21
|
-
hostRef.current.measureInWindow((hostX, hostY) => {
|
|
22
|
-
setHostPosition({ x: hostX, y: hostY });
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
anchorRef.current?.measureInWindow((x, y, width, height) => {
|
|
27
|
-
if (x !== 0 || y !== 0 || width !== 0 || height !== 0) {
|
|
28
|
-
setAnchorPosition({ x, y, width, height });
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
}, 0);
|
|
32
|
-
|
|
33
|
-
return () => clearTimeout(timer);
|
|
34
|
-
}, [hostRef, anchorRef, setHostPosition, setAnchorPosition]);
|
|
35
|
-
};
|