Skip to content

Commit 213e984

Browse files
committed
refactor: separate child wrapper from tooltip component
1 parent cec2483 commit 213e984

File tree

6 files changed

+70
-25
lines changed

6 files changed

+70
-25
lines changed

src/Tooltip.tsx

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
import React, { cloneElement, Fragment } from 'react';
2+
import { View } from 'react-native';
23
import { Portal } from '@gorhom/portal';
34
import { AnimatePresence } from 'framer-motion';
4-
import { StyleSheet, View } from 'react-native';
55

66
import { useLayout } from './hooks';
77

8-
import { Wrapper } from './components';
98
import type { TooltipProps } from './types';
9+
import { Wrapper, ChildWrapper } from './components';
1010

1111
const Tooltip: React.FC<TooltipProps> = (props) => {
12-
const { children, isVisible, renderContent, childrenStyle, ...restProps } =
13-
props;
12+
const {
13+
children,
14+
isVisible,
15+
renderContent,
16+
withOverlay,
17+
childrenStyle,
18+
...restProps
19+
} = props;
1420
const { onLayout, ...layout } = useLayout();
1521

1622
return (
@@ -22,19 +28,17 @@ const Tooltip: React.FC<TooltipProps> = (props) => {
2228
<AnimatePresence>
2329
{isVisible && (
2430
<Fragment>
25-
<Wrapper {...restProps} childrenLayout={layout}>
31+
<Wrapper
32+
{...restProps}
33+
withOverlay={withOverlay}
34+
childrenLayout={layout}
35+
>
2636
{renderContent()}
2737
</Wrapper>
28-
{restProps?.withOverlay && (
29-
<View
30-
style={[
31-
styles.absolute,
32-
childrenStyle,
33-
{ top: layout.y, left: layout.x },
34-
]}
35-
>
36-
{cloneElement(children as React.ReactElement)}
37-
</View>
38+
{withOverlay && (
39+
<ChildWrapper childrenStyle={childrenStyle} layout={layout}>
40+
{children}
41+
</ChildWrapper>
3842
)}
3943
</Fragment>
4044
)}
@@ -45,9 +49,3 @@ const Tooltip: React.FC<TooltipProps> = (props) => {
4549
};
4650

4751
export default Tooltip;
48-
49-
const styles = StyleSheet.create({
50-
absolute: {
51-
position: 'absolute',
52-
},
53-
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React, { cloneElement, ReactElement, useMemo } from 'react';
2+
import { StyleSheet, View } from 'react-native';
3+
4+
import type { ChildWrapperProps } from '../../types';
5+
6+
import styles from './styles';
7+
8+
const ChildWrapper: React.FC<ChildWrapperProps> = ({
9+
childrenStyle,
10+
layout,
11+
children,
12+
}) => {
13+
const stylez = useMemo(
14+
() =>
15+
StyleSheet.flatten([
16+
styles.wrapper,
17+
childrenStyle,
18+
{ top: layout?.y, left: layout?.x },
19+
]),
20+
[childrenStyle, layout?.x, layout?.y]
21+
);
22+
23+
return <View style={stylez}>{cloneElement(children as ReactElement)}</View>;
24+
};
25+
26+
export default ChildWrapper;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './ChildWrapper';
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 childWrapperStyles = StyleSheet.create({
4+
wrapper: {
5+
position: 'absolute',
6+
},
7+
});
8+
9+
export default childWrapperStyles;

src/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { default as Overlay } from './Overlay';
22
export { default as Wrapper } from './Wrapper';
33
export { default as TooltipWrapper } from './TooltipWrapper';
4+
export { default as ChildWrapper } from './ChildWrapper';

src/types/ComponentsProps.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import type { ReactNode } from 'react';
2-
import type { LayoutRectangle, ViewStyle, Animated } from 'react-native';
2+
import type {
3+
LayoutRectangle,
4+
ViewStyle,
5+
Animated,
6+
StyleProp,
7+
} from 'react-native';
38
import type { WithTimingConfig } from 'react-native-reanimated';
49

510
import type {
@@ -17,7 +22,7 @@ export interface OverlayProps
1722
extends RequiredChildrenProp,
1823
AnimatedPresenceProp {
1924
onDismiss?: () => void;
20-
overlayStyle?: ViewStyle;
25+
overlayStyle?: StyleProp<ViewStyle>;
2126
}
2227

2328
export interface PointerWrapperProps extends PointerProps {
@@ -40,10 +45,15 @@ export interface WrapperProps
4045
timingConfig?: WithTimingConfig | Animated.TimingAnimationConfig;
4146
}
4247

48+
export interface ChildWrapperProps extends RequiredChildrenProp {
49+
layout?: LayoutRectangle;
50+
childrenStyle?: StyleProp<ViewStyle>;
51+
}
52+
4353
export interface TooltipProps
44-
extends Omit<WrapperProps, 'children' | 'childrenLayout'> {
54+
extends Omit<WrapperProps, 'children' | 'childrenLayout'>,
55+
Pick<ChildWrapperProps, 'childrenStyle'> {
4556
children: ReactNode;
4657
isVisible: boolean;
4758
renderContent: () => ReactNode;
48-
childrenStyle?: ViewStyle;
4959
}

0 commit comments

Comments
 (0)