Socket
Socket
Sign inDemoInstall

react-touch-mouse-handler

Package Overview
Dependencies
8
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-touch-mouse-handler

Higher Order Components for touch and mouse events control.


Version published
Weekly downloads
6
increased by100%
Maintainers
1
Install size
12.8 kB
Created
Weekly downloads
 

Readme

Source

react-touch-mouse-handler

Higher Order Components for touch and mouse events control.

NPM JavaScript Style Guide

This component is useful for better touchscreen and mouse events recognition. Especially, if you need to handle different actions for hybrid devices.

For example, you have a navigation bar with dropdowns. You need to navigate to some url when clicking on a link, open dropdown menu on hover, as well as open dropdown when tapping the link by finger. Commonly, tapping on hybrid devices opens dropdown for a moment and then navigates to the url defined in the link. Because click event follows the mouseenter and touchstart events. Check all mouse and touch events sequence in my codepen test ground.

Install

npm install --save react-touch-mouse-handler

Usage

To make everything working you need to:

  1. Create a method which receives event type argument (myAction in example).
  2. Wrap your component in a function inside TouchMouseHandler component. This child function accepts events argument which should be passed your component.
  3. Pass these events farther to the DOM element.
  4. Write different actions for different event types.
import React, { Component } from 'react'

import TouchMouseHandler from 'react-touch-mouse-handler';

export default class App extends Component {
  myAction = (eventType) => {
    if (eventType === 'touch') {
      // do something if it was touch event (tap)
    }
    if (eventType === 'mouse') {
      // do something if it was mouse event (mouseenter)
    }
  }
  render() {
    return (
      <TouchMouseHandler handleAction={this.myAction}>
        {(events) => (
          <Button events={events}>Click me</Button>
        )}
      </TouchMouseHandler>
    );
  }
}

const Button = ({events}) => (
  <button {...events}>Click me</button>
);

If you have ideas for improvements, please open an issue.

License

MIT © seleckis

FAQs

Last updated on 29 Jul 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc