Pressable
Pressable is a Core Component wrapper that can detect various stages of press interactions on any of its defined children.
<Pressable onPress={onPressFunction}>
<Text>I'm pressable!</Text>
</Pressable>
How it worksβ
On an element wrapped by Pressable:
onPressInis called when a press is activated.onPressOutis called when the press gesture is deactivated.
After pressing onPressIn, one of two things will happen:
- The person will remove their finger, triggering
onPressOutfollowed byonPress. - If the person leaves their finger longer than 500 milliseconds before removing it,
onLongPressis triggered. (onPressOutwill still fire when they remove their finger.)
Fingers are not the most precise instruments, and it is common for users to accidentally activate the wrong element or miss the activation area. To help, Pressable has an optional HitRect you can use to define how far a touch can register away from the wrapped element. Presses can start anywhere within a HitRect.
PressRect allows presses to move beyond the element and its HitRect while maintaining activation and being eligible for a "press"βthink of sliding your finger slowly away from a button you're pressing down on.
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.
You can set HitRect with hitSlop and set PressRect with pressRetentionOffset.
Pressable uses React Native's Pressability API. For more information around the state machine flow of Pressability and how it works, check out the implementation for Pressability.
Exampleβ
Propsβ
android_disableSound Androidβ
If true, doesn't play Android system sound on press.
| Type | Default |
|---|---|
| boolean | false |
android_ripple Androidβ
Enables the Android ripple effect and configures its properties.
| Type |
|---|
| RippleConfig |
childrenβ
Either children or a function that receives a boolean reflecting whether the component is currently pressed.
| Type |
|---|
| React Node |
unstable_pressDelayβ
Duration (in milliseconds) to wait after press down before calling onPressIn.
| Type |
|---|
| number |
delayLongPressβ
Duration (in milliseconds) from onPressIn before onLongPress is called.
| Type | Default |
|---|---|
| number | 500 |
disabledβ
Whether the press behavior is disabled.
| Type | Default |
|---|---|
| boolean | false |
hitSlopβ
Sets additional distance outside of element in which a press can be detected.
| Type |
|---|
| Rect or number |
onHoverInβ
Called when the hover is activated to provide visual feedback.
| Type |
|---|
({ nativeEvent: MouseEvent }) => void |
onHoverOutβ
Called when the hover is deactivated to undo visual feedback.
| Type |
|---|
({ nativeEvent: MouseEvent }) => void |
onLongPressβ
Called if the time after onPressIn lasts longer than 500 milliseconds. This time period can be customized with delayLongPress.
| Type |
|---|
({nativeEvent: PressEvent}) => void |
onPressβ
Called after onPressOut.
| Type |
|---|
({nativeEvent: PressEvent}) => void |
onPressInβ
Called immediately when a touch is engaged, before onPressOut and onPress.
| Type |
|---|
({nativeEvent: PressEvent}) => void |
onPressMoveβ
Called when the press location moves.
| Type |
|---|
({nativeEvent: PressEvent}) => void |
onPressOutβ
Called when a touch is released.
| Type |
|---|
({nativeEvent: PressEvent}) => void |
pressRetentionOffsetβ
Additional distance outside of this view in which a touch is considered a press before onPressOut is triggered.
| Type | Default |
|---|---|
| Rect or number | {bottom: 30, left: 20, right: 20, top: 20} |
styleβ
Either view styles or a function that receives a boolean reflecting whether the component is currently pressed and returns view styles.
| Type |
|---|
View Style or ({ pressed: boolean }) => View Style |
testOnly_pressedβ
Used only for documentation or testing (e.g. snapshot testing).
| Type | Default |
|---|---|
| boolean | false |
Type Definitionsβ
RippleConfigβ
Ripple effect configuration for the android_ripple property.
| Type |
|---|
| object |
Properties:
| Name | Type | Required | Description |
|---|---|---|---|
| color | color | No | Defines the color of the ripple effect. |
| borderless | boolean | No | Defines if ripple effect should not include border. |
| radius | number | No | Defines the radius of the ripple effect. |
| foreground | boolean | No | Set to true to add the ripple effect to the foreground of the view, instead of the background. This is useful if one of your child views has a background of its own, or you're e.g. displaying images, and you don't want the ripple to be covered by them. |