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

feat: touch to display tooltip #4479

Open
wants to merge 5 commits into
base: main
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,6 @@ CHANGELOG.md
lib/

.expo

# yarn
.yarn
31 changes: 31 additions & 0 deletions example/src/Examples/TooltipExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
FAB,
IconButton,
List,
TextInput,
ToggleButton,
Tooltip,
Card,
Expand Down Expand Up @@ -148,6 +149,30 @@ const TooltipExample = ({ navigation }: Props) => {
</Card>
</Tooltip>
</List.Section>
<List.Section title="Info Tooltip">
<TextInput
placeholder="IBAN"
mode="outlined"
style={styles.textInput}
right={
<TextInput.Icon
icon={() => (
<Tooltip
title="International Bank Account Number"
touchToDisplay={true}
>
<IconButton
icon={'information'}
size={24}
onPress={() => {}}
/>
</Tooltip>
)}
/>
}
/>
</List.Section>
<View style={styles.listBottomMargin} />
</ScreenWrapper>
<View style={styles.fabContainer}>
<Tooltip title="Press Me">
Expand Down Expand Up @@ -181,11 +206,17 @@ const styles = StyleSheet.create({
cardContainer: {
margin: 16,
},
textInput: {
margin: 16,
},
toggleButtonRow: {
paddingHorizontal: 16,
},
iconButtonContainer: {
flexDirection: 'row',
flexWrap: 'wrap',
},
listBottomMargin: {
marginBottom: 128,
},
});
16 changes: 13 additions & 3 deletions src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as React from 'react';
import {
Dimensions,
View,
LayoutChangeEvent,
StyleSheet,
Platform,
Pressable,
StyleSheet,
View,
ViewStyle,
} from 'react-native';

Expand Down Expand Up @@ -42,6 +42,10 @@ export type Props = {
* @optional
*/
theme?: ThemeProp;
/**
* Whether the tooltip should be displayed on touch instead long press on mobile.
*/
touchToDisplay?: boolean;
};

/**
Expand All @@ -67,6 +71,7 @@ const Tooltip = ({
children,
enterTouchDelay = 500,
leaveTouchDelay = 1500,
touchToDisplay = false,
title,
theme: themeOverrides,
titleMaxFontSizeMultiplier,
Expand Down Expand Up @@ -155,13 +160,18 @@ const Tooltip = ({

const mobilePressProps = {
onPress: React.useCallback(() => {
if (touchToDisplay) {
touched.current = true;
setVisible(true);
return null;
}
if (touched.current) {
return null;
} else {
if (children.props.disabled) return null;
return children.props.onPress?.();
}
}, [children.props]),
}, [children.props, touchToDisplay]),
onLongPress: () => handleTouchStart(),
onPressOut: () => handleTouchEnd(),
delayLongPress: enterTouchDelay,
Expand Down