Giter VIP home page Giter VIP logo

use-mouse-action's Introduction

use-mouse-action Build Status

React Hooks to listen to both mouse down or up and click events with a once called function.

This hook can be used to create fast usable drop-down buttons.

Installation

npm install use-mouse-action

Usage

import useMouseAction from 'use-mouse-action';

const Component = () => {
  const props = useMouseAction({
    onAction: () => console.log('You clicked or mouse downed me!'),
    down: true
  });

  return (
    <button type="button" {...props}>
      Click me fast!
    </button>
  );
};

Hooks

This library provides three React Hooks:

  • useMouseAction
  • useMouseDown
  • useMouseUp

They all want the same parameter: either the callback function or an options object with the callback function in the onAction parameter.

They all return an object of event listeners to add on the element.

import { useMouseDown } from 'use-mouse-action';

/** ... */

const props = useMouseDown(() => toggle);
return (<button type="button" {...props}>Click me</button>);

The useMouseDown and useMouseUp are both a shortcut to respectively set the down and up option to true.

Options

  • onAction (required): The function called on custom click triggered.
  • down (default: false): If the element should listen to mousedown event.
  • up (default: false): If the element should listen to mouseup event.
  • touch (default: false): If the element should listen to touch equivalent events.
  • timeout (default: 10): Short timeout in milliseconds to prevents multiple events.

You can provide functions that should listen to each event with theses options:

  • onClick
  • onMouseDown
  • onMouseUp
  • onTouchStart
  • onTouchEnd

Why

Sometimes, we would like to listen to mousedown or mouseup events on buttons, but these events are difficult to call by disabled peoples or by people that navigates with their keyboard.

We would like to listen both to mousedown/mouseup and click event, but we now must to make sure that our action is triggered only once.

Moreover, we would add on top of that the listening of touch events.

That's why I created this React Hooks library.

Build

npm run build

Test

npm test

License

This project is licensed under the MIT license.

use-mouse-action's People

Contributors

dimitrinicolas avatar gittmaan 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.