@momo-kits/animated-tooltip 0.169.4 → 0.169.6

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.
@@ -11,6 +11,7 @@ import {
11
11
  } from '@momo-kits/foundation';
12
12
  import styles from './styles';
13
13
 
14
+ /** Main tooltip action: a medium foundation Button of type secondary. */
14
15
  const PrimaryButton: FC<ButtonProps> = ({ title, onPress }) => {
15
16
  return (
16
17
  <Button
@@ -23,6 +24,7 @@ const PrimaryButton: FC<ButtonProps> = ({ title, onPress }) => {
23
24
  );
24
25
  };
25
26
 
27
+ /** Text-only action shown left of the primary in a two-button row. */
26
28
  const SecondaryButton: FC<ButtonProps> = ({ title, onPress, style }) => {
27
29
  return (
28
30
  <TouchableOpacity
@@ -36,6 +38,7 @@ const SecondaryButton: FC<ButtonProps> = ({ title, onPress, style }) => {
36
38
  );
37
39
  };
38
40
 
41
+ /** 36pt (scaled) icon-only action. */
39
42
  const IconButton: FC<IconButtonProps> = ({ icon, onPress, style }) => {
40
43
  return (
41
44
  <TouchableOpacity
package/helpers.ts CHANGED
@@ -5,6 +5,10 @@ import { TooltipPlacement, TooltipProps } from './types';
5
5
 
6
6
  type Align = NonNullable<TooltipProps['align']>;
7
7
 
8
+ /**
9
+ * Top/left of the bubble inside the portal host: the anchor's window frame
10
+ * minus the host origin, pushed out by offset and aligned start/center/end.
11
+ */
8
12
  export const getPlacementStyle = ({
9
13
  anchorPosition,
10
14
  tooltipSize,
@@ -28,47 +32,45 @@ export const getPlacementStyle = ({
28
32
  bottom?: number;
29
33
  } => {
30
34
  const { x, y, width, height } = anchorPosition;
31
- const { width: tooltipWidth, height: tooltipHeight } = tooltipSize;
35
+ const { width: tooltipWidth, height: tooltipHeight } = tooltipSize;
32
36
 
33
- let position: { top?: number; left?: number; right?: number } = {};
37
+ let position: { top?: number; left?: number; right?: number } = {};
34
38
 
35
- // Vertical positioning (relative to portal host)
36
- if (placement === 'top') {
37
- position.top = y - tooltipHeight - offset - hostY;
38
- } else if (placement === 'bottom') {
39
- position.top = y + height + offset - hostY;
39
+ if (placement === 'top') {
40
+ position.top = y - tooltipHeight - offset - hostY;
41
+ } else if (placement === 'bottom') {
42
+ position.top = y + height + offset - hostY;
43
+ } else {
44
+ if (align === 'start') {
45
+ position.top = y - hostY;
46
+ } else if (align === 'end') {
47
+ position.top = y + height - tooltipHeight - hostY;
40
48
  } else {
41
- // For left/right placement, calculate vertical center alignment
42
- if (align === 'start') {
43
- position.top = y - hostY;
44
- } else if (align === 'end') {
45
- position.top = y + height - tooltipHeight - hostY;
46
- } else {
47
- // center
48
- position.top = y + height / 2 - tooltipHeight / 2 - hostY;
49
- }
49
+ position.top = y + height / 2 - tooltipHeight / 2 - hostY;
50
50
  }
51
+ }
51
52
 
52
- // Horizontal positioning (relative to portal host)
53
- if (placement === 'left') {
54
- position.left = x - tooltipWidth - offset - hostX;
55
- } else if (placement === 'right') {
56
- position.left = x + width + offset - hostX;
53
+ if (placement === 'left') {
54
+ position.left = x - tooltipWidth - offset - hostX;
55
+ } else if (placement === 'right') {
56
+ position.left = x + width + offset - hostX;
57
+ } else {
58
+ if (align === 'start') {
59
+ position.left = x - hostX;
60
+ } else if (align === 'end') {
61
+ position.left = x + width - tooltipWidth - hostX;
57
62
  } else {
58
- // For top/bottom placement, calculate horizontal alignment
59
- if (align === 'start') {
60
- position.left = x - hostX;
61
- } else if (align === 'end') {
62
- position.left = x + width - tooltipWidth - hostX;
63
- } else {
64
- // center
65
- position.left = x + width / 2 - tooltipWidth / 2 - hostX;
66
- }
63
+ position.left = x + width / 2 - tooltipWidth / 2 - hostX;
67
64
  }
65
+ }
68
66
 
69
- return position;
67
+ return position;
70
68
  };
71
69
 
70
+ /**
71
+ * Rotated-square arrow on the bubble edge that faces the anchor, aligned the
72
+ * same way as the bubble.
73
+ */
72
74
  export const getTooltipArrowStyle = ({
73
75
  arrowSize,
74
76
  align,
package/index.tsx CHANGED
@@ -1,5 +1,6 @@
1
1
  import React, {
2
- forwardRef, useCallback,
2
+ forwardRef,
3
+ useCallback,
3
4
  useContext,
4
5
  useEffect,
5
6
  useImperativeHandle,
@@ -27,6 +28,21 @@ import { usePreloadMeasure } from './usePreloadMeasure';
27
28
  import { getPlacementStyle, getTooltipArrowStyle } from './helpers';
28
29
  import { renderTooltipContent } from './renderButtons';
29
30
 
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
+
35
+ /** Whether two measured frames have the same origin and size. */
36
+ const isSameFrame = (a: Frame, b: Frame) => {
37
+ const isSameOrigin = a.x === b.x && a.y === b.y;
38
+ const isSameSize = a.width === b.width && a.height === b.height;
39
+ return isSameOrigin && isSameSize;
40
+ };
41
+
42
+ /**
43
+ * Wraps children as the anchor and shows a bubble through the foundation
44
+ * tooltip portal. Opened and closed only through the ref (show/hide).
45
+ */
30
46
  const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
31
47
  {
32
48
  children,
@@ -50,16 +66,13 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
50
66
  const arrowSize = 6;
51
67
  const [tooltipSize, setTooltipSize] = useState({ width: 0, height: 0 });
52
68
 
53
- // Portal integration
54
69
  const portal = useTooltipPortal();
55
70
  const portalId = useRef(
56
71
  `tooltip-${Math.random().toString(36).substr(2, 9)}`,
57
72
  ).current;
58
73
 
59
- // Internal ref for anchor - no ref required from consumer
60
74
  const anchorRef = useRef<View>(null);
61
75
 
62
- // Anchor position from measureInWindow
63
76
  const [anchorPosition, setAnchorPosition] = useState({
64
77
  x: 0,
65
78
  y: 0,
@@ -67,7 +80,7 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
67
80
  height: 0,
68
81
  });
69
82
 
70
- // Portal host position for relative calculation
83
+ // Portal content is positioned relative to the host, not the window.
71
84
  const [hostPosition, setHostPosition] = useState({
72
85
  x: 0,
73
86
  y: 0,
@@ -82,7 +95,7 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
82
95
  return `${app.appId}/${app.code}/${screen.screenName}/${componentName}`;
83
96
  }, [accessibilityLabel, app, screen, componentName]);
84
97
 
85
- // Ref-based visibility for imperative control (no state re-renders)
98
+ // Visibility lives in a ref read by the effects; forceUpdate re-renders.
86
99
  const visibleRef = useRef(false);
87
100
  const [, forceUpdate] = useReducer((x: number) => x + 1, 0);
88
101
  const animatedValue = useRef(new Animated.Value(0)).current;
@@ -98,28 +111,33 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
98
111
  }
99
112
  };
100
113
 
101
- const setVisibility = useCallback((nextVisible: boolean) => {
102
- if (visibleRef.current !== nextVisible) {
103
- visibleRef.current = nextVisible;
104
- forceUpdate(); // Trigger re-render to update portal
105
- onVisibleChange?.(nextVisible);
106
- }
107
- },[onVisibleChange]);
108
-
109
- const animate = useCallback((nextVisible: boolean) => {
110
- Animated.timing(animatedValue, {
111
- toValue: nextVisible ? 1 : 0,
112
- duration: ANIMATION_DURATION,
113
- useNativeDriver: true,
114
- easing: nextVisible ? Easing.out(Easing.quad) : Easing.in(Easing.quad),
115
- }).start();
116
- }, [animatedValue]);
114
+ const setVisibility = useCallback(
115
+ (nextVisible: boolean) => {
116
+ if (visibleRef.current !== nextVisible) {
117
+ visibleRef.current = nextVisible;
118
+ forceUpdate();
119
+ onVisibleChange?.(nextVisible);
120
+ }
121
+ },
122
+ [onVisibleChange],
123
+ );
124
+
125
+ const animate = useCallback(
126
+ (nextVisible: boolean) => {
127
+ Animated.timing(animatedValue, {
128
+ toValue: nextVisible ? 1 : 0,
129
+ duration: ANIMATION_DURATION,
130
+ useNativeDriver: true,
131
+ easing: nextVisible ? Easing.out(Easing.quad) : Easing.in(Easing.quad),
132
+ }).start();
133
+ },
134
+ [animatedValue],
135
+ );
117
136
 
118
137
  useEffect(() => {
119
- // Only animate if we have tooltip size (to avoid position jump on first show)
138
+ // Wait for onLayout on first show so the bubble does not jump into place.
120
139
  const hasSize = tooltipSize.width > 0 || tooltipSize.height > 0;
121
140
  if (visibleRef.current && !hasSize) {
122
- // Don't animate yet, wait for onLayout
123
141
  return;
124
142
  }
125
143
  animate(visibleRef.current);
@@ -142,16 +160,14 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
142
160
  let rafId: number | null = null;
143
161
  let lastPosition = { x: 0, y: 0, width: 0, height: 0 };
144
162
  let lastHostPosition = { x: 0, y: 0 };
145
- let isMounted = true; // Track mounted state
146
- let isActive = true; // Track active state for early cancellation
163
+ let isMounted = true;
164
+ let isActive = true;
147
165
 
148
166
  const measureAnchor = () => {
149
- // Check flags before any work
150
167
  if (!isMounted || !isActive || !anchorRef.current) {
151
168
  return;
152
169
  }
153
170
 
154
- // Measure portal host position first
155
171
  if (portal.hostRef?.current) {
156
172
  portal.hostRef.current.measureInWindow((hostX, hostY) => {
157
173
  if (!isMounted || !isActive) return;
@@ -164,31 +180,22 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
164
180
  }
165
181
 
166
182
  anchorRef.current.measureInWindow((x, y, width, height) => {
167
- // Double-check flags in callback
168
183
  if (!isMounted || !isActive) return;
169
184
 
170
- // Only update if position changed (to avoid unnecessary re-renders)
171
- if (
172
- x !== lastPosition.x ||
173
- y !== lastPosition.y ||
174
- width !== lastPosition.width ||
175
- height !== lastPosition.height
176
- ) {
185
+ if (!isSameFrame({ x, y, width, height }, lastPosition)) {
177
186
  lastPosition = { x, y, width, height };
178
187
  setAnchorPosition({ x, y, width, height });
179
188
  }
180
189
  });
181
190
  };
182
191
 
183
- // Defer measurement to next frame (single measurement, not polling)
192
+ // Measure once on the next frame, after layout settles (no polling).
184
193
  rafId = requestAnimationFrame(measureAnchor);
185
194
 
186
195
  return () => {
187
- // Set flags immediately to stop all pending work
188
196
  isMounted = false;
189
197
  isActive = false;
190
198
 
191
- // Cancel any pending animation frame
192
199
  if (rafId !== null) {
193
200
  cancelAnimationFrame(rafId);
194
201
  rafId = null;
@@ -206,14 +213,13 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
206
213
  const handleShow = useCallback(() => {
207
214
  clearTimers();
208
215
  showTimer.current = setTimeout(() => {
209
- // Measure host position first
210
216
  if (portal.hostRef?.current) {
211
217
  portal.hostRef.current.measureInWindow((hostX, hostY) => {
212
218
  setHostPosition({ x: hostX, y: hostY });
213
219
  });
214
220
  }
215
221
 
216
- // Measure anchor position before showing tooltip
222
+ // Show only once the anchor is measured so the bubble opens in place.
217
223
  anchorRef.current?.measureInWindow((x, y, width, height) => {
218
224
  setAnchorPosition({ x, y, width, height });
219
225
  setVisibility(true);
@@ -229,15 +235,10 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
229
235
  );
230
236
  }, [setVisibility]);
231
237
 
232
-
233
- useImperativeHandle(
234
- ref,
235
- () => ({
236
- show: handleShow,
237
- hide: handleHide,
238
- }),
239
- [handleHide, handleShow],
240
- );
238
+ useImperativeHandle(ref, () => ({ show: handleShow, hide: handleHide }), [
239
+ handleHide,
240
+ handleShow,
241
+ ]);
241
242
 
242
243
  const isVertical = placement === 'top' || placement === 'bottom';
243
244
  const slideFrom = placement === 'top' || placement === 'left' ? 4 : -4;
@@ -247,46 +248,49 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
247
248
  });
248
249
  const translate = isVertical ? { translateY: slide } : { translateX: slide };
249
250
 
250
- const placementStyle = useMemo(
251
- () =>
252
- getPlacementStyle({
253
- anchorPosition,
254
- tooltipSize,
255
- placement,
256
- align,
257
- hostX: hostPosition.x,
258
- hostY: hostPosition.y,
259
- offset: TOOLTIP_OFFSET,
260
- }),
261
- [anchorPosition, tooltipSize, placement, TOOLTIP_OFFSET, hostPosition.y, hostPosition.x, align]
262
- );
263
-
264
- const renderContent = () =>
265
- renderTooltipContent({
251
+ const placementStyle = useMemo(() => {
252
+ return getPlacementStyle({
253
+ anchorPosition,
254
+ tooltipSize,
255
+ placement,
256
+ align,
257
+ hostX: hostPosition.x,
258
+ hostY: hostPosition.y,
259
+ offset: TOOLTIP_OFFSET,
260
+ });
261
+ }, [
262
+ anchorPosition,
263
+ tooltipSize,
264
+ placement,
265
+ TOOLTIP_OFFSET,
266
+ hostPosition.y,
267
+ hostPosition.x,
268
+ align,
269
+ ]);
270
+
271
+ const renderContent = () => {
272
+ return renderTooltipContent({
266
273
  title,
267
274
  description,
268
275
  buttons,
269
276
  screenWidth: SCREEN_WIDTH,
270
277
  onPressClose,
271
278
  });
279
+ };
272
280
 
273
- const getArrowStyle = () =>
274
- getTooltipArrowStyle({ arrowSize, align, placement, tooltipSize });
281
+ const getArrowStyle = () => {
282
+ return getTooltipArrowStyle({ arrowSize, align, placement, tooltipSize });
283
+ };
275
284
 
276
285
  useEffect(() => {
277
- let isActive = true; // Track if effect is still active
286
+ let isActive = true;
278
287
 
279
288
  if (!visibleRef.current) {
280
289
  portal.unregister(portalId, true); // Immediate cleanup when hiding
281
290
  return;
282
291
  }
283
292
 
284
- const hasMeasurement =
285
- anchorPosition.x !== 0 ||
286
- anchorPosition.y !== 0 ||
287
- anchorPosition.width !== 0 ||
288
- anchorPosition.height !== 0;
289
-
293
+ const hasMeasurement = !isSameFrame(anchorPosition, EMPTY_FRAME);
290
294
  if (!hasMeasurement) {
291
295
  return;
292
296
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo-kits/animated-tooltip",
3
- "version": "0.169.4",
3
+ "version": "0.169.6",
4
4
  "private": false,
5
5
  "main": "index.tsx",
6
6
  "dependencies": {},
package/renderButtons.tsx CHANGED
@@ -5,6 +5,10 @@ import styles from './styles';
5
5
  import { TooltipButton } from './types';
6
6
  import { IconButton, PrimaryButton, SecondaryButton } from './TooltipButtons';
7
7
 
8
+ /**
9
+ * Right-aligned action row. With two buttons the first is the primary, on the
10
+ * right; buttons with `icon` render as icon buttons (a pair only if both do).
11
+ */
8
12
  export const renderTooltipButtons = (buttons: TooltipButton[]) => {
9
13
  if (!buttons.length) {
10
14
  return null;
@@ -12,7 +16,7 @@ export const renderTooltipButtons = (buttons: TooltipButton[]) => {
12
16
 
13
17
  if (buttons.length === 1) {
14
18
  const btn = buttons[0];
15
- const onPress = btn.onPress ?? (() => { });
19
+ const onPress = btn.onPress ?? (() => {});
16
20
  return (
17
21
  <View style={styles.buttonsRow}>
18
22
  <View
@@ -33,8 +37,8 @@ export const renderTooltipButtons = (buttons: TooltipButton[]) => {
33
37
 
34
38
  if (buttons.length === 2) {
35
39
  const [first, second] = buttons;
36
- const firstPress = first.onPress ?? (() => { });
37
- const secondPress = second.onPress ?? (() => { });
40
+ const firstPress = first.onPress ?? (() => {});
41
+ const secondPress = second.onPress ?? (() => {});
38
42
  const bothIcon = !!first.icon && !!second.icon;
39
43
 
40
44
  return (
@@ -67,7 +71,7 @@ export const renderTooltipButtons = (buttons: TooltipButton[]) => {
67
71
  {buttons.map((btn, idx) => {
68
72
  const isIcon = !!btn.icon;
69
73
  const key = btn.title || btn.icon || `${idx}`;
70
- const onPress = btn.onPress ?? (() => { });
74
+ const onPress = btn.onPress ?? (() => {});
71
75
 
72
76
  return (
73
77
  <View
@@ -89,6 +93,7 @@ export const renderTooltipButtons = (buttons: TooltipButton[]) => {
89
93
  );
90
94
  };
91
95
 
96
+ /** Bubble body: title, description, close icon and the action row. */
92
97
  export const renderTooltipContent = ({
93
98
  title,
94
99
  description,
@@ -137,11 +142,7 @@ export const renderTooltipContent = ({
137
142
  hitSlop={{ top: 8, bottom: 8, left: 8, right: 8 }}
138
143
  style={styles.closeIconSpacing}
139
144
  >
140
- <Icon
141
- source={'navigation_close'}
142
- size={20}
143
- color={Colors.black_01}
144
- />
145
+ <Icon source={'navigation_close'} size={20} color={Colors.black_01} />
145
146
  </TouchableOpacity>
146
147
  </View>
147
148
  {renderTooltipButtons(buttons)}
package/styles.ts CHANGED
@@ -39,7 +39,6 @@ export default StyleSheet.create({
39
39
  },
40
40
  textButton: {},
41
41
  iconButton: {},
42
- // New styles from inline
43
42
  buttonRowContainer: {
44
43
  flexDirection: 'row',
45
44
  alignItems: 'center',
@@ -53,7 +52,6 @@ export default StyleSheet.create({
53
52
  closeIconSpacing: {
54
53
  marginLeft: Spacing.S,
55
54
  },
56
- // TooltipButtons styles
57
55
  secondaryButtonContainer: {
58
56
  paddingHorizontal: Spacing.M,
59
57
  paddingVertical: Spacing.S,
@@ -3,6 +3,7 @@ import { View } from 'react-native';
3
3
 
4
4
  type Position = { x: number; y: number; width: number; height: number };
5
5
 
6
+ /** Measures host and anchor once after mount so the first show() opens fast. */
6
7
  export const usePreloadMeasure = ({
7
8
  hostRef,
8
9
  anchorRef,
@@ -14,11 +15,8 @@ export const usePreloadMeasure = ({
14
15
  setHostPosition: (position: { x: number; y: number }) => void;
15
16
  setAnchorPosition: (position: Position) => void;
16
17
  }) => {
17
- // Preload anchor measurement on mount for faster initial display
18
18
  useEffect(() => {
19
- // Measure immediately on mount to have position ready
20
19
  const timer = setTimeout(() => {
21
- // Measure host position first
22
20
  if (hostRef?.current) {
23
21
  hostRef.current.measureInWindow((hostX, hostY) => {
24
22
  setHostPosition({ x: hostX, y: hostY });