Skip to content

Commit f1ba32e

Browse files
authored
Support pointer position styles
Support pointer position styles
2 parents f4a4b7c + e842fe4 commit f1ba32e

File tree

8 files changed

+61
-20
lines changed

8 files changed

+61
-20
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React, { useMemo } from 'react';
2+
import { StyleSheet, View } from 'react-native';
3+
4+
import { getPointerWrapperStyle } from '../../utils';
5+
import type { PointerWrapperProps } from '../../types';
6+
7+
import Pointer from '../Pointer';
8+
9+
import styles from './styles';
10+
11+
const PointerWrapper: React.FC<PointerWrapperProps> = ({
12+
tooltipLayout,
13+
...pointerProps
14+
}) => {
15+
const pointerStyles = useMemo(
16+
() =>
17+
StyleSheet.flatten([
18+
styles.wrapper,
19+
getPointerWrapperStyle(pointerProps, tooltipLayout),
20+
]),
21+
[pointerProps, tooltipLayout]
22+
);
23+
24+
return (
25+
<View style={pointerStyles}>
26+
<Pointer {...pointerProps} />
27+
</View>
28+
);
29+
};
30+
31+
export default PointerWrapper;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './PointerWrapper';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { StyleSheet } from 'react-native';
2+
3+
const pointerWrapperStyles = StyleSheet.create({
4+
wrapper: {
5+
position: 'absolute',
6+
},
7+
});
8+
9+
export default pointerWrapperStyles;

src/components/TooltipWrapper/TooltipWrapper.tsx

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import React, { useMemo } from 'react';
2-
import { StyleSheet, View, ViewStyle } from 'react-native';
32
import Animated, { useAnimatedStyle } from 'react-native-reanimated';
43

54
import { useLayout } from '../../hooks';
5+
import { getPositionStyles } from '../../utils';
66
import type { TooltipWrapperProps } from '../../types';
7-
import { getPointerWrapperStyle, getPositionStyles } from '../../utils';
87

9-
import Pointer from '../Pointer';
8+
import PointerWrapper from '../PointerWrapper';
109

1110
import styles from './styles';
1211

@@ -37,22 +36,17 @@ const TooltipWrapperComponent: React.FC<TooltipWrapperProps> = (props) => {
3736
});
3837
}, [layout, position, props]);
3938

40-
const pointerStyles = useMemo(() => {
41-
return StyleSheet.flatten<ViewStyle>([
42-
styles.pointer,
43-
getPointerWrapperStyle({ position, ...pointerProps }, layout),
44-
]);
45-
}, [position, pointerProps, layout]);
46-
4739
return (
4840
<Animated.View
4941
onLayout={onLayout}
5042
style={[styles.tooltipWrapper, positionStyles, stylez]}
5143
>
5244
{children}
53-
<View style={pointerStyles}>
54-
<Pointer {...pointerProps} position={position} />
55-
</View>
45+
<PointerWrapper
46+
{...pointerProps}
47+
tooltipLayout={layout}
48+
position={position}
49+
/>
5650
</Animated.View>
5751
);
5852
};

src/components/TooltipWrapper/styles.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ const tooltipWrapperStyles = StyleSheet.create({
55
position: 'absolute',
66
opacity: 0,
77
},
8-
pointer: {
9-
position: 'absolute',
10-
},
118
});
129

1310
export default tooltipWrapperStyles;

src/types/ComponentsProps.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ export interface OverlayProps
2020
overlayStyle?: ViewStyle;
2121
}
2222

23+
export interface PointerWrapperProps extends PointerProps {
24+
tooltipLayout?: LayoutRectangle;
25+
}
26+
2327
export interface TooltipWrapperProps
2428
extends RequiredChildrenProp,
2529
AnimatedPresenceProp,
26-
PointerProps {
30+
Omit<PointerWrapperProps, 'tooltipLayout'> {
2731
offset?: number;
2832
childrenLayout?: LayoutRectangle;
2933
}

src/utils/pointer.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ export function getPointerStyles(props: PointerProps) {
6969

7070
export function getPointerWrapperStyle(
7171
props: PointerProps,
72-
layout: LayoutRectangle
72+
layout: LayoutRectangle = { x: 0, y: 0, width: 0, height: 0 }
7373
): ViewStyle {
7474
const { position = 'top', pointerStyle: pointerStyles } = props;
75-
const { width, height } = getSafeStyle(pointerStyles);
75+
const { width, height, ...restStyle } = getSafeStyle(pointerStyles);
7676

7777
const size = isVerticalPosition(position) ? height : width;
7878

@@ -84,28 +84,32 @@ export function getPointerWrapperStyle(
8484
return {
8585
top: -size,
8686
left: sideWhenVertical,
87+
...restStyle,
8788
};
8889
}
8990
case 'left': {
9091
return {
9192
right: -size,
9293
top: sideWhenHorizontal,
94+
...restStyle,
9395
};
9496
}
9597
case 'top': {
9698
return {
9799
bottom: -size,
98100
left: sideWhenVertical,
101+
...restStyle,
99102
};
100103
}
101104
case 'right': {
102105
return {
103106
left: -size,
104107
top: sideWhenHorizontal,
108+
...restStyle,
105109
};
106110
}
107111
default: {
108-
return {};
112+
return { ...restStyle };
109113
}
110114
}
111115
}

src/utils/validation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export function getSafeStyle(style?: ViewStyle) {
1818
const safeHeight = (isValidNumber(height) && height) || 0;
1919

2020
return {
21+
...style,
2122
width: safeWidth,
2223
height: safeHeight,
2324
};

0 commit comments

Comments
 (0)