Git Product home page Git Product logo

touchable-hook's Introduction

touchable-hook

npm package Follow on Twitter

touchable-hook provides a react hook that emulates native touch behaviour for building performant, customizable interactive widgets like buttons, list items, etc. It's a limited implementation of the touchable interface found in react-native-web. It provides the basic touchable behaviour for Sancho-UI and is built using react-gesture-responder.

Why?

When building mobile web apps it's challenging to get interactive elements to feel just right. Using this hook makes it easier:

  • hover state is provided only when using a mouse.
  • active state is available after a configurable delay. This is useful for avoiding highlighting list elements when scrolling, but providing immediate visual feedback on elements like buttons.
  • mouse and touch support.
  • keyboard support which emulates both button and anchor behaviour.
  • long press support

Install

Install both touchable-hook and react-gesture-responder using yarn or npm.

yarn add touchable-hook react-gesture-responder
import { useTouchable } from "touchable-hook";

function TouchableHighlight({ onPress, onLongPress }) {
  const { bind, active, hover } = useTouchable({
    onPress,
    onLongPress,
    behavior: "button" // or 'link'
  });

  return (
    <div role="button" tabIndex={0} {...bind}>
      This is a touchable highlight
    </div>
  );
}

touchable-hook's People

Contributors

bmcmahen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

touchable-hook's Issues

using a reducer sometimes results in status mismatches

I think a reducer probably runs in a async lifecycle which means that we can't necessarily assume that event order will be reflected in touchable state.

This means that if a mouseDown and mouseUp happens really fast, there's a chance (?) the mouseup event triggers before reducer returns the correct transition for mouseDown.

Solution:

Create our own reducer outside of the render lifecycle that performs synchronously.

memory leak?

React sometimes gives me weird warning when unmounting components using touchable hook. Unsure where it's coming from

  • possibly touchable delay being fired after unmount? Unlikely, since I am cleaning that up.
  • scroll event listener being fired?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.