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

react-typescript-hook-mouse

v1.0.26

Published

React hook for mouse position, movement and buttion states, now with Typescript support

Downloads

18

Readme

react-typescript-hook-mouse :mouse:

https://nodei.co/npm/react-typescript-hook-mouse.png?downloads=true&downloadRank=true&stars=true

A React hook to access data from mouse events. Now with typescript types!

License: LGPL v3 npm version Build Status Maintainability Test Coverage Known Vulnerabilities dependencies devDependencies code style HitCount

Installation

Using npm:

npm install --save react-typescript-hook-mouse

Using yarn:

yarn add react-typescript-hook-mouse

Usage

import React from 'react';
import useMouse from 'react-typescript-hook-mouse';

export default () => {
  const mouse = useMouse();

  if (!mouse) {
    return <span>Awaiting first mouse event...</span>;
  }

  return (
    <ul>
      <li>
        <span>Mouse position in viewport: </span>
        <span>{`(${String(mouse.position.client.x)}, ${String(mouse.position.client.y)})`}</span>
      </li>
      <li>
        <span>Mouse position on page: </span>
        <span>{`(${String(mouse.position.page.x)}, ${String(mouse.position.page.y)})`}</span>
      </li>
      <li>
        <span>Mouse position on screen: </span>
        <span>{`(${String(mouse.position.screen.x)}, ${String(mouse.position.screen.y)})`}</span>
      </li>
      <li>
        <span>Mouse movement: </span>
        <span>{`(${String(mouse.movement.x)}, ${String(mouse.movement.y)})`}</span>
      </li>
      <li>
        <span>Left button was pressed: </span>
        <span>{String(mouse.buttons.left)}</span>
      </li>
      <li>
        <span>Right button was pressed: </span>
        <span>{String(mouse.buttons.right)}</span>
      </li>
      <li>
        <span>Middle button was pressed: </span>
        <span>{String(mouse.buttons.middle)}</span>
      </li>
      <li>
        <span>Alt key was pressed: </span>
        <span>{String(mouse.keyboard.alt)}</span>
      </li>
      <li>
        <span>Ctrl key was pressed: </span>
        <span>{String(mouse.keyboard.ctrl)}</span>
      </li>
      <li>
        <span>Meta key was pressed: </span>
        <span>{String(mouse.keyboard.meta)}</span>
      </li>
      <li>
        <span>Shift key was pressed: </span>
        <span>{String(mouse.keyboard.shift)}</span>
      </li>
      <li>
        <span>Wheel delta X: </span>
        <span>{String(mouse.wheel.deltaX)}</span>
      </li>
      <li>
        <span>Wheel moved left: </span>
        <span>{String(mouse.wheel.left)}</span>
      </li>
      <li>
        <span>Wheel moved right: </span>
        <span>{String(mouse.wheel.right)}</span>
      </li>
      <li>
        <span>Wheel delta Y: </span>
        <span>{String(mouse.wheel.deltaY)}</span>
      </li>
      <li>
        <span>Wheel moved up: </span>
        <span>{String(mouse.wheel.up)}</span>
      </li>
      <li>
        <span>Wheel moved down: </span>
        <span>{String(mouse.wheel.down)}</span>
      </li>
      <li>
        <span>Wheel delta Z: </span>
        <span>{String(mouse.wheel.deltaZ)}</span>
      </li>
      <li>
        <span>Wheel moved out: </span>
        <span>{String(mouse.wheel.out)}</span>
      </li>
      <li>
        <span>Wheel moved in: </span>
        <span>{String(mouse.wheel.in)}</span>
      </li>
    </ul>
  );
};

Configuration of watched events

You can specify which events you want to watch. By default, the hook watches all the events it knows about (mousedown, mouseup, mousemove, and wheel), but this behavior can be changed by passing a configuration object:

  // Use defaults.
  const mouseAllEvents = useMouse();
  
  // Exactly the same as above.
  const mouseAllEventsExplicit = useMouse({
    mousedown: true,
    mouseup: true,
    mousemove: true,
    wheel: true,
  });

  // Only watch for click events, don't watch movements or wheel events.
  const mouseButtonEvents = useMouse({
    mousedown: true,
    mouseup: true,
    mousemove: false,
    wheel: false,
  });

  // Exactly the same as the above.
  // Event names not given are assumed to be false.
  const mouseButtonEventsImplicit = useMouse({
    mousedown: true,
    mouseup: true,
  });

  // Dynamically register the movement listener,
  // based on the input boolean value.
  // Does not watch the wheel event.
  const mouseEventsDynamic = useMouse({
    mousedown: true,
    mouseup: true,
    mousemove: someVariableMaybeFromAnotherHook,
  });

The hook is smart enough to dynamically change the registrations to only watch for the events you want, so you can update the values in the configuration object at runtime and it will react to alter the event listeners.

Caveats

Data in mouse.keyboard is always read from a MouseEvent and therefore it will only get updated on mouse events, not when the keys are actually pressed on the keyboard.

Contributions

Based from the react-hook-mouse package by Bence A. Toth [email protected].

Contributions are welcome. File bug reports, create pull requests, feel free to reach out on the project github page, or via email.

Licence

LGPL-3.0