udc

use-double-click

🐁 A simple React hook for differentiating single and double clicks on the same component.

Showing:

Popularity

Downloads/wk

1.3K

GitHub Stars

30

Maintenance

Last Commit

2mos ago

Contributors

1

Package

Dependencies

0

Size (min+gzip)

0.4KB

License

MIT

Type Definitions

Tree-Shakeable

Yes?

Categories

Readme

use-double-click

npm NPM npm bundle size PRs Welcome Codecov Travis (.org)

use-double-click is a simple React hook for differentiating single and double clicks on the same component.

Documentation https://timellenberger.com/libraries/use-double-click

Check out the demo on Codesandbox

What's wrong with onDoubleClick()?

When you double click on an element, onClick() fires twice alongside your single onDoubleClick() callback. This effect isn't desirable when a single click and a double click have different functions!

useDoubleClick() waits within a latency window after a click for a secondary click, and only after this period either the onSingleClick or onDoubleClick() callback will fire a single time.

Install

yarn add use-double-click

Usage

import { useRef } from 'react';
import useDoubleClick from 'use-double-click';

const Button = () => {
  const buttonRef = useRef();
  
  useDoubleClick({
    onSingleClick: e => {
      console.log(e, 'single click');
    },
    onDoubleClick: e => {
      console.log(e, 'double click');
    },
    ref: buttonRef,
    latency: 250
  });
  
  return <button ref={buttonRef}>Click Me</button>
}

Props

PropDescription
onSingleClickA callback function for single click events
onDoubleClickA callback function for double click events
refDom node to watch for double clicks
latencyThe amount of time (in milliseconds) to wait before differentiating a single from a double click

License

MIT Β© Tim Ellenberger

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100