react-native-touchable-safe
v1.1.5
Published
Consistent view hierarchy and API for React Native `Touchable*` components.
Downloads
238
Maintainers
Readme
react-native-touchable-safe
A single easy-to-use <Touchable>
component, which harnesses the power of all React Native's Touchable*
components.
- Simple API that bridges the differences between RN's various
Touchable*
types. - A consistent
View
hierarchy, to avoid tricky layout issues when switching betweenTouchable*
types. - Handling the incompatability of ripple customisation on Android API level < 21.
Motivation
As it stands, TouchableOpacity
and TouchableHighlight
wrap their children in a View
, whereas TouchableNativeFeedback
and TouchableWithoutFeedback
do not.
This can lead to headaches and platform-specific bugs when trying to create advanced Flexbox layouts with different touchable styles on Android/iOS.
An example of this is available here: https://snack.expo.io/ry6kXjX8W
This library makes the situation consistent and easy to reason about:
<Touchable>
always introduces another view in the hierarchy, which can have its layout customised withouterStyle
.<Touchable>
always must only have one child, which it applies its effect (e.g. opacity) to natively.
Installation
$ npm install --save react-native-touchable-safe
# Or, with Yarn
$ yarn add react-native-touchable-safe
Getting started
This component provides a simple API, where alternating the component used per platform is as simple as:
return (
<Touchable android="native" ios="opacity">
<MyButton />
</Touchable>
)
In fact, these are the default behaviours, so simply <Touchable>
is enough to achieve this effect.
The android/ios props only need to be used when deviating from the defaults.
Props
If you don't want to use the defaults (TouchableNativeFeedback
on Android and TouchableOpacity
on iOS), you can specify another type.
Use all
to set all platforms to the same effect, or ios
and android
to differentiate it per platform.
all?
:'opacity' | 'highlight' | 'without'
ios?
:'opacity' | 'highlight' | 'without'
- (default:'opacity'
)android?
:'native' | 'opacity' | 'highlight' | 'without'
- (default:'native'
)
Some very common behaviours used by all touchable types:
onPress?
:() => void
outerStyle?
:Object | number
- Style to pass to the outerView
component which wraps every type of touchable component. Typically used to specify things like<Touchable outerStyle={{ flex: 1 }}>
.outerProps?
:Object
- Similar toouterStyle
, but lets you set any props (althoughstyle
is the main use case).disabled?
:boolean
- Remove any touch functionality and feedback.
Seeing as setting a custom native ripple requires calling TouchableNativeFeedback.Ripple
, the following top-level convenience props can be used to quickly customise the ripple:
nativeBorderless?
:boolean
- Forandroid="native"
, should the ripple effect be borderless.nativePressColor?
:string
- (default:'rgba(0, 0, 0, .1)'
) - Forandroid="native"
, what color should the ripple be.
Any props which you only want passed to one type of touchable component can be controlled with the following props.
nativeProps?
:Object
- Any props to pass on to aTouchableNativeFeedback
component.opacityProps?
:Object
- Any props to pass on to aTouchableOpacity
component.highlightProps?
:Object
- Any props to pass on to aTouchableHighlight
component.withoutProps?
:Object
- Any props to pass on to aTouchableWithoutFeedback
component.
And finally, anything else will be passed down to all touchable components.
Examples
Defaults
NativeFeedback on Android, Opacity on iOS
import React from 'react'
import Touchable from 'react-native-touchable-safe'
import MyButton from './MyButton'
export default () => (
<Touchable onPress={() => console.log('Pressed')}>
<MyButton />
</Touchable>
)
Mixed
A row of different styled buttons, which all behave consistently
import React from 'react'
import { StyleSheet } from 'react-native'
import Touchable from 'react-native-touchable-safe'
import MyButton from './MyButton'
export default ({ disabled }) => (
<View style={styles.row}>
{/* Android: native, iOS: highlight */}
<Touchable
ios="highlight"
onPress={() => {
console.log('Pressed A')
}}
outerStyle={styles.touchWrap}
nativeBorderless
nativePressColor="rgba(0, 0, 255, .5)"
>
<MyButton title="A" />
</Touchable>
{/* Both: opacity (50% opacity) */}
<Touchable
all="opacity"
onPress={() => {
console.log('Pressed B')
}}
outerStyle={styles.touchWrap}
opacityProps={{ activeOpacity: 0.5 }}
>
<MyButton title="B" />
</Touchable>
{/* Both: no feedback */}
<Touchable
all="without"
onPress={() => {
console.log('Pressed C')
}}
outerStyle={styles.touchWrap}
>
<MyButton title="C" />
</Touchable>
{/* Both: defaults, disabled based on prop */}
<Touchable
onPress={() => {
console.log('Pressed D')
}}
outerStyle={styles.touchWrap}
disabled={disabled}
>
{/* Visual styling of disabled elements handled manually */}
<MyButton title="D" greyedOut={disabled} />
</Touchable>
</View>
)
const styles = StyleSheet.create({
row: {
flexDirection: 'row',
alignItems: 'top',
height: 200,
},
touchWrap: {
flex: 1,
height: 100,
},
})