@momo-kits/animated-tooltip 0.169.8 → 0.169.10

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 CHANGED
@@ -25,12 +25,16 @@ import {
25
25
  import styles from './styles';
26
26
  import { TooltipPlacement, TooltipProps, TooltipRef } from './types';
27
27
  import { usePreloadMeasure } from './usePreloadMeasure';
28
- import { getPlacementStyle, getTooltipArrowStyle } from './helpers';
28
+ import { getPlacementStyle, getTooltipArrowStyle } from './utils';
29
29
  import { renderTooltipContent } from './renderButtons';
30
30
 
31
31
  type Frame = { x: number; y: number; width: number; height: number };
32
32
 
33
33
  const EMPTY_FRAME: Frame = { x: 0, y: 0, width: 0, height: 0 };
34
+ const ANIMATION_DURATION = 180;
35
+ const TOOLTIP_OFFSET = Spacing.S;
36
+ const ARROW_SIZE = 6;
37
+ const SLIDE_DISTANCE = 4;
34
38
 
35
39
  /** Whether two measured frames have the same origin and size. */
36
40
  const isSameFrame = (a: Frame, b: Frame) => {
@@ -58,12 +62,9 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
58
62
  },
59
63
  ref,
60
64
  ) {
61
- const ANIMATION_DURATION = 180;
62
- const TOOLTIP_OFFSET = Spacing.S;
63
65
  const app = useContext<any>(MiniAppContext);
64
66
  const screen = useContext<any>(ScreenContext);
65
- const SCREEN_WIDTH = useWindowDimensions().width;
66
- const arrowSize = 6;
67
+ const screenWidth = useWindowDimensions().width;
67
68
  const [tooltipSize, setTooltipSize] = useState({ width: 0, height: 0 });
68
69
 
69
70
  const portal = useTooltipPortal();
@@ -96,7 +97,7 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
96
97
  }, [accessibilityLabel, app, screen, componentName]);
97
98
 
98
99
  // Visibility lives in a ref read by the effects; forceUpdate re-renders.
99
- const visibleRef = useRef(false);
100
+ const isVisibleRef = useRef(false);
100
101
  const [, forceUpdate] = useReducer((x: number) => x + 1, 0);
101
102
  const animatedValue = useRef(new Animated.Value(0)).current;
102
103
  const showTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
@@ -113,8 +114,8 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
113
114
 
114
115
  const setVisibility = useCallback(
115
116
  (nextVisible: boolean) => {
116
- if (visibleRef.current !== nextVisible) {
117
- visibleRef.current = nextVisible;
117
+ if (isVisibleRef.current !== nextVisible) {
118
+ isVisibleRef.current = nextVisible;
118
119
  forceUpdate();
119
120
  onVisibleChange?.(nextVisible);
120
121
  }
@@ -137,10 +138,10 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
137
138
  useEffect(() => {
138
139
  // Wait for onLayout on first show so the bubble does not jump into place.
139
140
  const hasSize = tooltipSize.width > 0 || tooltipSize.height > 0;
140
- if (visibleRef.current && !hasSize) {
141
+ if (isVisibleRef.current && !hasSize) {
141
142
  return;
142
143
  }
143
- animate(visibleRef.current);
144
+ animate(isVisibleRef.current);
144
145
  }, [tooltipSize, animate]);
145
146
 
146
147
  usePreloadMeasure({
@@ -151,7 +152,7 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
151
152
  });
152
153
 
153
154
  useEffect(() => {
154
- if (!visibleRef.current) {
155
+ if (!isVisibleRef.current) {
155
156
  setAnchorPosition({ x: 0, y: 0, width: 0, height: 0 });
156
157
  setHostPosition({ x: 0, y: 0 });
157
158
  return;
@@ -241,7 +242,8 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
241
242
  ]);
242
243
 
243
244
  const isVertical = placement === 'top' || placement === 'bottom';
244
- const slideFrom = placement === 'top' || placement === 'left' ? 4 : -4;
245
+ const isBeforeAnchor = placement === 'top' || placement === 'left';
246
+ const slideFrom = isBeforeAnchor ? SLIDE_DISTANCE : -SLIDE_DISTANCE;
245
247
  const slide = animatedValue.interpolate({
246
248
  inputRange: [0, 1],
247
249
  outputRange: [slideFrom, 0],
@@ -262,7 +264,6 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
262
264
  anchorPosition,
263
265
  tooltipSize,
264
266
  placement,
265
- TOOLTIP_OFFSET,
266
267
  hostPosition.y,
267
268
  hostPosition.x,
268
269
  align,
@@ -273,19 +274,24 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
273
274
  title,
274
275
  description,
275
276
  buttons,
276
- screenWidth: SCREEN_WIDTH,
277
+ screenWidth,
277
278
  onPressClose,
278
279
  });
279
280
  };
280
281
 
281
282
  const getArrowStyle = () => {
282
- return getTooltipArrowStyle({ arrowSize, align, placement, tooltipSize });
283
+ return getTooltipArrowStyle({
284
+ arrowSize: ARROW_SIZE,
285
+ align,
286
+ placement,
287
+ tooltipSize,
288
+ });
283
289
  };
284
290
 
285
291
  useEffect(() => {
286
292
  let isActive = true;
287
293
 
288
- if (!visibleRef.current) {
294
+ if (!isVisibleRef.current) {
289
295
  portal.unregister(portalId, true); // Immediate cleanup when hiding
290
296
  return;
291
297
  }
@@ -305,7 +311,7 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
305
311
  pointerEvents="auto"
306
312
  style={[
307
313
  {
308
- maxWidth: SCREEN_WIDTH - Spacing.M * 2,
314
+ maxWidth: screenWidth - Spacing.M * 2,
309
315
  },
310
316
  containerStyle,
311
317
  styles.tooltip,
@@ -333,8 +339,8 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
333
339
  isActive = false;
334
340
  portal.unregister(portalId, true);
335
341
  };
336
- // eslint-disable-next-line react-hooks/exhaustive-deps
337
- }, [visibleRef.current, anchorPosition, placement, align, tooltipSize]);
342
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- re-register only when visibility or geometry changes
343
+ }, [isVisibleRef.current, anchorPosition, placement, align, tooltipSize]);
338
344
 
339
345
  return (
340
346
  <View style={styles.container} accessibilityLabel={componentId}>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo-kits/animated-tooltip",
3
- "version": "0.169.8",
3
+ "version": "0.169.10",
4
4
  "private": false,
5
5
  "main": "index.tsx",
6
6
  "dependencies": {},
File without changes