Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

use-double-tap

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-double-tap

React hook for handling double tap on mobile devices

  • 1.3.6-alpha.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.3K
increased by4.23%
Maintainers
1
Weekly downloads
 
Created
Source

:point_up_2: React Double Tap Hook :point_up_2:

React hook for handling double tap on mobile devices

codecov npm type definitions npm bundle size npm GitHub

Install

npm install --save use-double-tap

or

yarn add use-double-tap

Basic Usage

import React from 'react';

import { useDoubleTap } from 'use-double-tap';

const Example = () => {
    const bind = useDoubleTap((event) => {
      // Your action here
      console.log('Double tapped');
    });

    return <button {...bind}>Tap me</button>;
}

export default Example;

Live demo

Advanced usage

Custom capture threshold

You can also manually specify time threshold for capturing double tap event (default: 300ms).

useDoubleTap(() => {
  // Your action here
}, 500);

In the example above, second tap must occur within 500ms period to register double tap.

Additional options

Supplied as third argument.

useDoubleTap(() => {
  // Actions on double tap
}, 300, {
  // Options here
})

List of possible options:

OptionTypeDescription
onSingleTapFunctionCallback for handling case when double tap is not triggered,
because of capture timeout.

For example if threshold parameter is 300ms and second tap occurs after 400ms
then onSingleTap is triggered after capture interval (300ms) expires.

Disable event binding

If you pass falsy value as callback (like null) double tap will not bind to the component.

useDoubleTap(null);

This allows you to dynamically control if event should be bound. For example:

const bind = useDoubleTap(isMobile ? () => {
  console.log('Double tapped');
} : null);

:warning: Warning

This hook internally use onClick event to detect double tap, so be careful not to override your existing event listener.

This is where disabling listener binding may come handy - you can use double tap detection only when necessary.

Why onClick?

Because it leverages built in event listener which can also detect mobile tap event.

This way we can get rid of complicated edge cases when combining onTouchStart onTouchEnd onTouchCancel onTouchMove events.

Also this approach greatly reduce package size as well as increase speed and flexibility.

License

MIT © minwork

Keywords

FAQs

Package last updated on 15 Aug 2023

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc