Coder Social home page Coder Social logo

dishui / use-double-click Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tim-soft/use-double-click

0.0 0.0 0.0 482 KB

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

Home Page: https://timellenberger.com/libraries/use-double-click

License: MIT License

JavaScript 100.00%

use-double-click's Introduction

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

Prop Description
onSingleClick A callback function for single click events
onDoubleClick A callback function for double click events
ref Dom node to watch for double clicks
latency The amount of time (in milliseconds) to wait before differentiating a single from a double click

License

MIT © Tim Ellenberger

use-double-click's People

Contributors

muhammetsait avatar tim-soft avatar

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.