npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-mouse-action

v2.0.0

Published

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

Downloads

6,679

Readme

use-mouse-action Build Status

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

Example

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.