npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-touchable-safe

v1.1.5

Published

Consistent view hierarchy and API for React Native `Touchable*` components.

Downloads

372

Readme

react-native-touchable-safe

npm version Circle CI Status license npm downloads

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 between Touchable* 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 with outerStyle.
  • <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 outer View component which wraps every type of touchable component. Typically used to specify things like <Touchable outerStyle={{ flex: 1 }}>.
  • outerProps?: Object - Similar to outerStyle, but lets you set any props (although style 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 - For android="native", should the ripple effect be borderless.
  • nativePressColor?: string - (default: 'rgba(0, 0, 0, .1)') - For android="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 a TouchableNativeFeedback component.
  • opacityProps?: Object - Any props to pass on to a TouchableOpacity component.
  • highlightProps?: Object - Any props to pass on to a TouchableHighlight component.
  • withoutProps?: Object - Any props to pass on to a TouchableWithoutFeedback 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,
  },
})