@momo-kits/animated-tooltip 0.168.1-beta.28 → 0.168.1-beta.29

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/helpers.ts ADDED
@@ -0,0 +1,141 @@
1
+ import { ViewStyle } from 'react-native';
2
+ import { Colors, Spacing } from '@momo-kits/foundation';
3
+ import styles from './styles';
4
+ import { TooltipPlacement, TooltipProps } from './types';
5
+
6
+ type Align = NonNullable<TooltipProps['align']>;
7
+
8
+ export const getPlacementStyle = ({
9
+ anchorPosition,
10
+ tooltipSize,
11
+ placement,
12
+ align,
13
+ hostX,
14
+ hostY,
15
+ offset,
16
+ }: {
17
+ anchorPosition: { x: number; y: number; width: number; height: number };
18
+ tooltipSize: { width: number; height: number };
19
+ placement: TooltipPlacement;
20
+ align: Align;
21
+ hostX: number;
22
+ hostY: number;
23
+ offset: number;
24
+ }): {
25
+ top?: number;
26
+ left?: number;
27
+ right?: number;
28
+ bottom?: number;
29
+ } => {
30
+ const { x, y, width, height } = anchorPosition;
31
+ const { width: tooltipWidth, height: tooltipHeight } = tooltipSize;
32
+
33
+ let position: { top?: number; left?: number; right?: number } = {};
34
+
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;
40
+ } 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
+ }
50
+ }
51
+
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;
57
+ } 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
+ }
67
+ }
68
+
69
+ return position;
70
+ };
71
+
72
+ export const getTooltipArrowStyle = ({
73
+ arrowSize,
74
+ align,
75
+ placement,
76
+ tooltipSize,
77
+ }: {
78
+ arrowSize: number;
79
+ align: Align;
80
+ placement: TooltipPlacement;
81
+ tooltipSize: { width: number; height: number };
82
+ }): object[] => {
83
+ const size = arrowSize;
84
+ const common = [
85
+ styles.arrow,
86
+ {
87
+ width: size * 2,
88
+ height: size * 2,
89
+ backgroundColor: Colors.blue_03,
90
+ transform: [{ rotate: '45deg' }],
91
+ borderRadius: size / 2,
92
+ },
93
+ ];
94
+ const inset = size + Spacing.M;
95
+ const centerTop = (tooltipSize.height || 0) / 2 - size;
96
+ let horizontalAlign: ViewStyle = { alignSelf: 'center' };
97
+ let verticalAlign: ViewStyle = { top: centerTop };
98
+ if (align === 'start') {
99
+ horizontalAlign = { left: inset };
100
+ verticalAlign = { top: inset };
101
+ } else if (align === 'end') {
102
+ horizontalAlign = { right: inset };
103
+ verticalAlign = { bottom: inset };
104
+ }
105
+
106
+ switch (placement) {
107
+ case 'top':
108
+ return [
109
+ ...common,
110
+ {
111
+ bottom: -(size - 1),
112
+ ...horizontalAlign,
113
+ },
114
+ ];
115
+ case 'bottom':
116
+ return [
117
+ ...common,
118
+ {
119
+ top: -(size - 1),
120
+ ...horizontalAlign,
121
+ },
122
+ ];
123
+ case 'left':
124
+ return [
125
+ ...common,
126
+ {
127
+ right: -(size - 1),
128
+ ...verticalAlign,
129
+ },
130
+ ];
131
+ case 'right':
132
+ default:
133
+ return [
134
+ ...common,
135
+ {
136
+ left: -(size - 1),
137
+ ...verticalAlign,
138
+ },
139
+ ];
140
+ }
141
+ };
package/index.tsx CHANGED
@@ -11,24 +11,21 @@ import React, {
11
11
  import {
12
12
  Animated,
13
13
  Easing,
14
- TouchableOpacity,
15
14
  TouchableWithoutFeedback,
16
15
  useWindowDimensions,
17
16
  View,
18
- ViewStyle,
19
17
  } from 'react-native';
20
18
  import {
21
- Colors,
22
- Icon,
23
19
  MiniAppContext,
24
20
  ScreenContext,
25
21
  Spacing,
26
- Text,
27
22
  useTooltipPortal,
28
23
  } from '@momo-kits/foundation';
29
24
  import styles from './styles';
30
25
  import { TooltipPlacement, TooltipProps, TooltipRef } from './types';
31
- import { IconButton, PrimaryButton, SecondaryButton } from './TooltipButtons';
26
+ import { usePreloadMeasure } from './usePreloadMeasure';
27
+ import { getPlacementStyle, getTooltipArrowStyle } from './helpers';
28
+ import { renderTooltipContent } from './renderButtons';
32
29
 
33
30
  const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
34
31
  {
@@ -128,26 +125,12 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
128
125
  animate(visibleRef.current);
129
126
  }, [tooltipSize, animate]);
130
127
 
131
- // Preload anchor measurement on mount for faster initial display
132
- useEffect(() => {
133
- // Measure immediately on mount to have position ready
134
- const timer = setTimeout(() => {
135
- // Measure host position first
136
- if (portal.hostRef?.current) {
137
- portal.hostRef.current.measureInWindow((hostX, hostY) => {
138
- setHostPosition({ x: hostX, y: hostY });
139
- });
140
- }
141
-
142
- anchorRef.current?.measureInWindow((x, y, width, height) => {
143
- if (x !== 0 || y !== 0 || width !== 0 || height !== 0) {
144
- setAnchorPosition({ x, y, width, height });
145
- }
146
- });
147
- }, 0);
148
-
149
- return () => clearTimeout(timer);
150
- }, [portal.hostRef]);
128
+ usePreloadMeasure({
129
+ hostRef: portal.hostRef,
130
+ anchorRef,
131
+ setHostPosition,
132
+ setAnchorPosition,
133
+ });
151
134
 
152
135
  useEffect(() => {
153
136
  if (!visibleRef.current) {
@@ -264,244 +247,31 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
264
247
  });
265
248
  const translate = isVertical ? { translateY: slide } : { translateX: slide };
266
249
 
267
- const placementStyle = useMemo((): {
268
- top?: number;
269
- left?: number;
270
- right?: number;
271
- bottom?: number;
272
- } => {
273
- const { x, y, width, height } = anchorPosition;
274
- const { width: tooltipWidth, height: tooltipHeight } = tooltipSize;
275
-
276
- let position: { top?: number; left?: number; right?: number } = {};
277
-
278
- // Vertical positioning (relative to portal host)
279
- if (placement === 'top') {
280
- position.top = y - tooltipHeight - TOOLTIP_OFFSET - hostPosition.y;
281
- } else if (placement === 'bottom') {
282
- position.top = y + height + TOOLTIP_OFFSET - hostPosition.y;
283
- } else {
284
- // For left/right placement, calculate vertical center alignment
285
- if (align === 'start') {
286
- position.top = y - hostPosition.y;
287
- } else if (align === 'end') {
288
- position.top = y + height - tooltipHeight - hostPosition.y;
289
- } else {
290
- // center
291
- position.top = y + height / 2 - tooltipHeight / 2 - hostPosition.y;
292
- }
293
- }
294
-
295
- // Horizontal positioning (relative to portal host)
296
- if (placement === 'left') {
297
- position.left = x - tooltipWidth - TOOLTIP_OFFSET - hostPosition.x;
298
- } else if (placement === 'right') {
299
- position.left = x + width + TOOLTIP_OFFSET - hostPosition.x;
300
- } else {
301
- // For top/bottom placement, calculate horizontal alignment
302
- if (align === 'start') {
303
- position.left = x - hostPosition.x;
304
- } else if (align === 'end') {
305
- position.left = x + width - tooltipWidth - hostPosition.x;
306
- } else {
307
- // center
308
- position.left = x + width / 2 - tooltipWidth / 2 - hostPosition.x;
309
- }
310
- }
311
-
312
- return position;
313
- }, [anchorPosition, tooltipSize, placement, TOOLTIP_OFFSET, hostPosition.y, hostPosition.x, align]);
314
-
315
- const renderButtons = () => {
316
- if (!buttons.length) {
317
- return null;
318
- }
319
-
320
- if (buttons.length === 1) {
321
- const btn = buttons[0];
322
- const onPress = btn.onPress ?? (() => { });
323
- return (
324
- <View style={styles.buttonsRow}>
325
- <View
326
- style={[
327
- styles.button,
328
- btn.icon ? styles.iconButton : styles.textButton,
329
- ]}
330
- >
331
- {btn.icon ? (
332
- <IconButton icon={btn.icon} onPress={onPress} />
333
- ) : (
334
- <PrimaryButton title={btn.title || ''} onPress={onPress} />
335
- )}
336
- </View>
337
- </View>
338
- );
339
- }
340
-
341
- if (buttons.length === 2) {
342
- const [first, second] = buttons;
343
- const firstPress = first.onPress ?? (() => { });
344
- const secondPress = second.onPress ?? (() => { });
345
- const bothIcon = !!first.icon && !!second.icon;
346
-
347
- return (
348
- <View style={styles.buttonsRow}>
349
- {bothIcon ? (
350
- <View style={styles.buttonRowContainer}>
351
- <IconButton
352
- icon={second.icon as string}
353
- onPress={secondPress}
354
- style={styles.buttonSpacing}
355
- />
356
- <IconButton icon={first.icon as string} onPress={firstPress} />
357
- </View>
358
- ) : (
359
- <View style={styles.buttonRowContainer}>
360
- <SecondaryButton
361
- title={second.title || ''}
362
- onPress={secondPress}
363
- style={styles.buttonSpacing}
364
- />
365
- <PrimaryButton title={first.title || ''} onPress={firstPress} />
366
- </View>
367
- )}
368
- </View>
369
- );
370
- }
371
-
372
- return (
373
- <View style={styles.buttonsRow}>
374
- {buttons.map((btn, idx) => {
375
- const isIcon = !!btn.icon;
376
- const key = btn.title || btn.icon || `${idx}`;
377
- const onPress = btn.onPress ?? (() => { });
378
-
379
- return (
380
- <View
381
- key={key}
382
- style={[
383
- styles.button,
384
- isIcon ? styles.iconButton : styles.textButton,
385
- ]}
386
- >
387
- {isIcon ? (
388
- <IconButton icon={btn.icon as string} onPress={onPress} />
389
- ) : (
390
- <PrimaryButton title={btn.title || ''} onPress={onPress} />
391
- )}
392
- </View>
393
- );
394
- })}
395
- </View>
396
- );
397
- };
398
-
399
- const renderContent = () => {
400
- return (
401
- <View style={styles.content}>
402
- <View style={styles.contentHeader}>
403
- <View
404
- style={{
405
- minWidth: 100,
406
- maxWidth: SCREEN_WIDTH - Spacing.M * 2 - 48,
407
- }}
408
- >
409
- {title ? (
410
- <Text
411
- typography={'header_s_semibold'}
412
- color={Colors.black_01}
413
- style={styles.title}
414
- >
415
- {title}
416
- </Text>
417
- ) : null}
418
- {description ? (
419
- <Text
420
- typography={'description_default_regular'}
421
- color={Colors.black_01}
422
- style={styles.description}
423
- >
424
- {description}
425
- </Text>
426
- ) : null}
427
- </View>
428
- <TouchableOpacity
429
- onPress={onPressClose}
430
- hitSlop={{ top: 8, bottom: 8, left: 8, right: 8 }}
431
- style={styles.closeIconSpacing}
432
- >
433
- <Icon
434
- source={'navigation_close'}
435
- size={20}
436
- color={Colors.black_01}
437
- />
438
- </TouchableOpacity>
439
- </View>
440
- {renderButtons()}
441
- </View>
442
- );
443
- };
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
+ );
444
263
 
445
- const getArrowStyle: () => object[] = () => {
446
- const size = arrowSize;
447
- const common = [
448
- styles.arrow,
449
- {
450
- width: size * 2,
451
- height: size * 2,
452
- backgroundColor: Colors.black_17,
453
- transform: [{ rotate: '45deg' }],
454
- borderRadius: size / 2,
455
- },
456
- ];
457
- const inset = size + Spacing.M;
458
- const centerTop = (tooltipSize.height || 0) / 2 - size;
459
- let horizontalAlign: ViewStyle = { alignSelf: 'center' };
460
- let verticalAlign: ViewStyle = { top: centerTop };
461
- if (align === 'start') {
462
- horizontalAlign = { left: inset };
463
- verticalAlign = { top: inset };
464
- } else if (align === 'end') {
465
- horizontalAlign = { right: inset };
466
- verticalAlign = { bottom: inset };
467
- }
264
+ const renderContent = () =>
265
+ renderTooltipContent({
266
+ title,
267
+ description,
268
+ buttons,
269
+ screenWidth: SCREEN_WIDTH,
270
+ onPressClose,
271
+ });
468
272
 
469
- switch (placement) {
470
- case 'top':
471
- return [
472
- ...common,
473
- {
474
- bottom: -(size - 1),
475
- ...horizontalAlign,
476
- },
477
- ];
478
- case 'bottom':
479
- return [
480
- ...common,
481
- {
482
- top: -(size - 1),
483
- ...horizontalAlign,
484
- },
485
- ];
486
- case 'left':
487
- return [
488
- ...common,
489
- {
490
- right: -(size - 1),
491
- ...verticalAlign,
492
- },
493
- ];
494
- case 'right':
495
- default:
496
- return [
497
- ...common,
498
- {
499
- left: -(size - 1),
500
- ...verticalAlign,
501
- },
502
- ];
503
- }
504
- };
273
+ const getArrowStyle = () =>
274
+ getTooltipArrowStyle({ arrowSize, align, placement, tooltipSize });
505
275
 
506
276
  useEffect(() => {
507
277
  let isActive = true; // Track if effect is still active
@@ -524,15 +294,7 @@ const Tooltip = forwardRef<TooltipRef, TooltipProps>(function Tooltip(
524
294
  const content = (
525
295
  <>
526
296
  <TouchableWithoutFeedback onPress={handleHide}>
527
- <View
528
- style={{
529
- position: 'absolute',
530
- top: -9999,
531
- left: -9999,
532
- right: -9999,
533
- bottom: -9999,
534
- }}
535
- />
297
+ <View style={styles.backdrop} />
536
298
  </TouchableWithoutFeedback>
537
299
 
538
300
  <Animated.View
package/package.json CHANGED
@@ -1,9 +1,8 @@
1
1
  {
2
2
  "name": "@momo-kits/animated-tooltip",
3
- "version": "0.168.1-beta.28",
3
+ "version": "0.168.1-beta.29",
4
4
  "private": false,
5
5
  "main": "index.tsx",
6
- "dependencies": {},
7
6
  "peerDependencies": {
8
7
  "@momo-kits/foundation": "latest",
9
8
  "react": "*",
@@ -13,4 +12,4 @@
13
12
  "publishConfig": {
14
13
  "registry": "https://registry.npmjs.org/"
15
14
  }
16
- }
15
+ }
@@ -0,0 +1,150 @@
1
+ import React from 'react';
2
+ import { TouchableOpacity, View } from 'react-native';
3
+ import { Colors, Icon, Spacing, Text } from '@momo-kits/foundation';
4
+ import styles from './styles';
5
+ import { TooltipButton } from './types';
6
+ import { IconButton, PrimaryButton, SecondaryButton } from './TooltipButtons';
7
+
8
+ export const renderTooltipButtons = (buttons: TooltipButton[]) => {
9
+ if (!buttons.length) {
10
+ return null;
11
+ }
12
+
13
+ if (buttons.length === 1) {
14
+ const btn = buttons[0];
15
+ const onPress = btn.onPress ?? (() => { });
16
+ return (
17
+ <View style={styles.buttonsRow}>
18
+ <View
19
+ style={[
20
+ styles.button,
21
+ btn.icon ? styles.iconButton : styles.textButton,
22
+ ]}
23
+ >
24
+ {btn.icon ? (
25
+ <IconButton icon={btn.icon} onPress={onPress} />
26
+ ) : (
27
+ <PrimaryButton title={btn.title || ''} onPress={onPress} />
28
+ )}
29
+ </View>
30
+ </View>
31
+ );
32
+ }
33
+
34
+ if (buttons.length === 2) {
35
+ const [first, second] = buttons;
36
+ const firstPress = first.onPress ?? (() => { });
37
+ const secondPress = second.onPress ?? (() => { });
38
+ const bothIcon = !!first.icon && !!second.icon;
39
+
40
+ return (
41
+ <View style={styles.buttonsRow}>
42
+ {bothIcon ? (
43
+ <View style={styles.buttonRowContainer}>
44
+ <IconButton
45
+ icon={second.icon as string}
46
+ onPress={secondPress}
47
+ style={styles.buttonSpacing}
48
+ />
49
+ <IconButton icon={first.icon as string} onPress={firstPress} />
50
+ </View>
51
+ ) : (
52
+ <View style={styles.buttonRowContainer}>
53
+ <SecondaryButton
54
+ title={second.title || ''}
55
+ onPress={secondPress}
56
+ style={styles.buttonSpacing}
57
+ />
58
+ <PrimaryButton title={first.title || ''} onPress={firstPress} />
59
+ </View>
60
+ )}
61
+ </View>
62
+ );
63
+ }
64
+
65
+ return (
66
+ <View style={styles.buttonsRow}>
67
+ {buttons.map((btn, idx) => {
68
+ const isIcon = !!btn.icon;
69
+ const key = btn.title || btn.icon || `${idx}`;
70
+ const onPress = btn.onPress ?? (() => { });
71
+
72
+ return (
73
+ <View
74
+ key={key}
75
+ style={[
76
+ styles.button,
77
+ isIcon ? styles.iconButton : styles.textButton,
78
+ ]}
79
+ >
80
+ {isIcon ? (
81
+ <IconButton icon={btn.icon as string} onPress={onPress} />
82
+ ) : (
83
+ <PrimaryButton title={btn.title || ''} onPress={onPress} />
84
+ )}
85
+ </View>
86
+ );
87
+ })}
88
+ </View>
89
+ );
90
+ };
91
+
92
+ export const renderTooltipContent = ({
93
+ title,
94
+ description,
95
+ buttons,
96
+ screenWidth,
97
+ onPressClose,
98
+ }: {
99
+ title?: string;
100
+ description?: string;
101
+ buttons: TooltipButton[];
102
+ screenWidth: number;
103
+ onPressClose?: () => void;
104
+ }) => {
105
+ return (
106
+ <View style={styles.content}>
107
+ <View style={styles.contentHeader}>
108
+ <View
109
+ style={[
110
+ styles.contentTextContainer,
111
+ {
112
+ maxWidth: screenWidth - Spacing.M * 2 - 48,
113
+ },
114
+ ]}
115
+ >
116
+ {title ? (
117
+ <Text
118
+ typography={'header_s_semibold'}
119
+ color={Colors.black_01}
120
+ style={styles.title}
121
+ >
122
+ {title}
123
+ </Text>
124
+ ) : null}
125
+ {description ? (
126
+ <Text
127
+ typography={'description_default_regular'}
128
+ color={Colors.black_01}
129
+ style={styles.description}
130
+ >
131
+ {description}
132
+ </Text>
133
+ ) : null}
134
+ </View>
135
+ <TouchableOpacity
136
+ onPress={onPressClose}
137
+ hitSlop={{ top: 8, bottom: 8, left: 8, right: 8 }}
138
+ style={styles.closeIconSpacing}
139
+ >
140
+ <Icon
141
+ source={'navigation_close'}
142
+ size={20}
143
+ color={Colors.black_01}
144
+ />
145
+ </TouchableOpacity>
146
+ </View>
147
+ {renderTooltipButtons(buttons)}
148
+ </View>
149
+ );
150
+ };
package/styles.ts CHANGED
@@ -8,7 +8,7 @@ export default StyleSheet.create({
8
8
  tooltip: {
9
9
  position: 'absolute',
10
10
  padding: Spacing.M,
11
- backgroundColor: Colors.black_17,
11
+ backgroundColor: Colors.blue_03,
12
12
  borderRadius: Radius.S,
13
13
  },
14
14
  text: {
@@ -67,4 +67,14 @@ export default StyleSheet.create({
67
67
  anchorView: {
68
68
  alignSelf: 'flex-start',
69
69
  },
70
+ contentTextContainer: {
71
+ minWidth: 100,
72
+ },
73
+ backdrop: {
74
+ position: 'absolute',
75
+ top: -9999,
76
+ left: -9999,
77
+ right: -9999,
78
+ bottom: -9999,
79
+ },
70
80
  });
@@ -0,0 +1,37 @@
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
+ export const usePreloadMeasure = ({
7
+ hostRef,
8
+ anchorRef,
9
+ setHostPosition,
10
+ setAnchorPosition,
11
+ }: {
12
+ hostRef?: RefObject<View | null> | null;
13
+ anchorRef: RefObject<View | null>;
14
+ setHostPosition: (position: { x: number; y: number }) => void;
15
+ setAnchorPosition: (position: Position) => void;
16
+ }) => {
17
+ // Preload anchor measurement on mount for faster initial display
18
+ useEffect(() => {
19
+ // Measure immediately on mount to have position ready
20
+ const timer = setTimeout(() => {
21
+ // Measure host position first
22
+ if (hostRef?.current) {
23
+ hostRef.current.measureInWindow((hostX, hostY) => {
24
+ setHostPosition({ x: hostX, y: hostY });
25
+ });
26
+ }
27
+
28
+ anchorRef.current?.measureInWindow((x, y, width, height) => {
29
+ if (x !== 0 || y !== 0 || width !== 0 || height !== 0) {
30
+ setAnchorPosition({ x, y, width, height });
31
+ }
32
+ });
33
+ }, 0);
34
+
35
+ return () => clearTimeout(timer);
36
+ }, [hostRef, anchorRef, setHostPosition, setAnchorPosition]);
37
+ };