Tip
If you're looking for a more extensive and future-proof way to handle touch-based input, check out the Pressable API.
Usage
TouchableHighlight is a wrapper for handling pressEvents of a View. While pressing down, the opacity of the wrapped view
is decreased, allowing the underlay color to show through.
function MyComponent(props: MyComponentProps) { return ( <View {...props} style={{ flex: 1, backgroundColor: '$semantic.color.surface.container.secondary', }} > <Text>My Component</Text> </View> );}
<TouchableHighlight activeOpacity="$core.opacity.lg" underlayColor="$semantic.color.surface.interactive.standards.active.secondary" onPress={() => alert('Pressed!')}> <MyComponent /></TouchableHighlight>;Considerations
- Visual Artifacts: The underlay comes from wrapping the child in a
Viewcomponent. This can sometimes cause unwanted visual artifacts and affect layout if not used correctly. For example, if thebackgroundColorof the wrappedViewis not explicitly set to an opaque color. - Children:
TouchableHighlightcan only have a single child. If you wish to have several children, wrap them in aView.
Accessibility Considerations
- Feedback for Users: Ensure that the visual feedback (
underlayColorcolor) is noticeable for users with visual impairments. - Screen Reader Support: Label the component appropriately for screen readers so users understand the interaction.
- Keyboard Focus: Ensure the component can be focused and activated using a keyboard for accessibility.
TouchableHighlight Classes
| Class Name | Description |
|---|---|
| abyss-touchable-highlight-root | TouchableHighlight root element. |
TouchableHighlight Props
Extends React Native - TouchableHighlightopens in a new window props.
| Name | Type | Default | Description |
|---|---|---|---|
activeOpacity | Opacity | undefined | Determines what the opacity of the wrapped view should be when touch is active | |
disableDefaultProviderProps | boolean | undefined | false | If set to true, the component will not use the DefaultPropsProvider values. If you aren’t using the DefaultPropsProvider, this prop can be ignored. |
hitSlop | Insets | Inset | null | undefined | This defines how far your touch can start away from the button. This is added to pressRetentionOffset when moving off of the button. NOTE The touch area never extends past the parent view bounds and the Z-index of sibling views always takes precedence if a touch hits two overlapping views. | |
pressRetentionOffset | Insets | Inset | null | undefined | When the scroll view is disabled, this defines how far your touch may move off of the button, before deactivating the button. Once deactivated, try moving it back and you'll see that the button is once again reactivated! Move it back and forth several times while the scroll view is disabled. Ensure you pass in a constant to reduce memory allocations. | |
rootTestID | string | undefined | Adds the passed in test ID to the root element of the component. | |
style | Style<"TouchableHighlight"> | Style properties with Abyss token mapping | |
styles | Styles<TouchableHighlightClasses> | undefined | Add custom styles to component by class name with Abyss token mapping | |
testID | string | undefined | Using this prop will append -abyss and the name of the element class at the end of the testID passed in. If you want to only use string passed in on the root element, use the rootTestID prop instead. Used to locate this view in end-to-end tests. | |
underlayColor | Color | undefined | The color of the underlay that will show through when the touch is active |