Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new prop to make the tooltip wrapper scrollable for small screen devices. #180

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/tooltip.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ declare module 'react-native-walkthrough-tooltip' {
childrenWrapperStyle?: StyleProp<ViewStyle>;

// Styles the view element that wraps the original children
parentWrapperStyle?: StyleProp<ViewStyle>
parentWrapperStyle?: StyleProp<ViewStyle>;
}

export interface TooltipProps extends Partial<TooltipStyleProps> {
Expand Down Expand Up @@ -146,6 +146,9 @@ declare module 'react-native-walkthrough-tooltip' {

// Support for nested elements within the Tooltip component.
children?: React.ReactNode;

// Make the tooltip wrapper scrollable.
scrollable?: boolean;
}

/**
Expand Down
53 changes: 25 additions & 28 deletions src/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Modal,
TouchableWithoutFeedback,
View,
ScrollView,
} from 'react-native';
import rfcIsEqual from 'react-fast-compare';
import {
Expand All @@ -32,10 +33,10 @@ const DEFAULT_DISPLAY_INSETS = {
right: 24,
};

const computeDisplayInsets = insetsFromProps =>
const computeDisplayInsets = (insetsFromProps) =>
Object.assign({}, DEFAULT_DISPLAY_INSETS, insetsFromProps);

const invertPlacement = placement => {
const invertPlacement = (placement) => {
switch (placement) {
case 'top':
return 'bottom';
Expand Down Expand Up @@ -77,6 +78,7 @@ class Tooltip extends Component {
topAdjustment: 0,
horizontalAdjustment: 0,
accessible: true,
scrollable: false,
};

static propTypes = {
Expand Down Expand Up @@ -109,6 +111,7 @@ class Tooltip extends Component {
topAdjustment: PropTypes.number,
horizontalAdjustment: PropTypes.number,
accessible: PropTypes.bool,
scrollable: PropTypes.bool,
};

constructor(props) {
Expand Down Expand Up @@ -212,7 +215,7 @@ class Tooltip extends Component {
return null;
}

updateWindowDims = dims => {
updateWindowDims = (dims) => {
this.setState(
{
windowDims: dims.window,
Expand Down Expand Up @@ -241,15 +244,15 @@ class Tooltip extends Component {
);
};

measureContent = e => {
measureContent = (e) => {
const { width, height } = e.nativeEvent.layout;
const contentSize = new Size(width, height);
this.setState({ contentSize }, () => {
this.computeGeometry();
});
};

onChildMeasurementComplete = rect => {
onChildMeasurementComplete = (rect) => {
this.setState(
{
childRect: rect,
Expand All @@ -276,7 +279,7 @@ class Tooltip extends Component {
(x, y, width, height, pageX, pageY) => {
const childRect = new Rect(pageX, pageY, width, height);
if (
Object.values(childRect).every(value => value !== undefined)
Object.values(childRect).every((value) => value !== undefined)
) {
this.onChildMeasurementComplete(childRect);
} else {
Expand Down Expand Up @@ -304,13 +307,8 @@ class Tooltip extends Component {

computeGeometry = () => {
const { arrowSize, childContentSpacing } = this.props;
const {
childRect,
contentSize,
displayInsets,
placement,
windowDims,
} = this.state;
const { childRect, contentSize, displayInsets, placement, windowDims } =
this.state;

const options = {
displayInsets,
Expand Down Expand Up @@ -411,12 +409,15 @@ class Tooltip extends Component {
});

const hasChildren = React.Children.count(this.props.children) > 0;

const onPressBackground = () => {
if (this.props.closeOnBackgroundInteraction) {
this.props.onClose();
}
};
const screenHeight = Dimensions.get('window').height;

if (this.props.scrollable) {
generatedStyles.containerStyle = {
...generatedStyles.containerStyle,
height: screenHeight * 1.3,
backgroundColor: 'rgba(0,0,0, 0.5)',
};
}

const onPressContent = () => {
if (this.props.closeOnContentInteraction) {
Expand All @@ -426,10 +427,10 @@ class Tooltip extends Component {

return (
<TouchableWithoutFeedback
onPress={onPressBackground}
onPress={this.props.onClose}
accessible={this.props.accessible}
>
<View style={generatedStyles.containerStyle}>
<ScrollView contentContainerStyle={generatedStyles.containerStyle}>
<View style={[generatedStyles.backgroundStyle]}>
<View style={generatedStyles.tooltipStyle}>
{hasChildren ? <View style={generatedStyles.arrowStyle} /> : null}
Expand All @@ -449,18 +450,14 @@ class Tooltip extends Component {
{hasChildren && this.props.showChildInTooltip
? this.renderChildInTooltip()
: null}
</View>
</ScrollView>
</TouchableWithoutFeedback>
);
};

render() {
const {
children,
isVisible,
useReactNativeModal,
modalComponent,
} = this.props;
const { children, isVisible, useReactNativeModal, modalComponent } =
this.props;

const hasChildren = React.Children.count(children) > 0;
const showTooltip = isVisible && !this.state.waitingForInteractions;
Expand Down