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

event-from

v1.0.0

Published

Determine if an event was caused by mouse, touch or key input.

Downloads

3,728

Readme

Event From

npm bundle size (version) npm type definitions

Determine if a browser event was caused by mouse, touch, or key input. Can be used to:

  • Ignore mouse events caused by touch input.
  • Determine if focus was initiated from the keyboard (to know when to add focus styles).
  • Determine if a click event was from mouse, touch, or key input.
  • And anything else where knowing the type of user interaction that generated the event is helpful.
  • If you're using React you may be interested in React Interactive, which uses Event From under the hood.

Live demo app for Event From

Code is in the /demo folder.


npm install --save event-from
import { eventFrom } from 'event-from';

const handleEvent = (event) => {
  // call eventFrom in the event handler and pass in the event
  // eventFrom will return 1 of 3 strings: 'mouse' | 'touch' | 'key'
  const eventFromValue = eventFrom(event);
  // ...your logic using the eventFromValue
};

Ignore mouse events caused by touch input

Note that a touch interaction will fire Touch Events as the interaction is in progress (touch on the screen), and will fire Mouse Events during a long press (extended touch on the screen), or after the touch interaction has finished (after the touch is removed from the screen) to support sites that only listen for Mouse Events.

import { eventFrom } from 'event-from';

const handleMouseEvent = (e) => {
  // early return to ignore mouse events not from mouse input
  if (eventFrom(e) !== 'mouse') return;

  // code for handling mouse events from mouse input
};

element.addEventListener('mouseenter', handleMouseEvent, false);

Determine if a focus event was from key input to add focus styles

import { eventFrom } from 'event-from';

const handleFocusEvent = (e) => {
  if (eventFrom(e) === 'key') {
    // add focus styles when focus is from keyboard input
  }
};

element.addEventListener('focus', handleFocusEvent, false);

Determine if a click event was from mouse, touch, or key input

import { eventFrom } from 'event-from';

const handleClickEvent = (e) => {
  switch (eventFrom(e)) {
    case 'mouse':
      // click event from mouse
      break;
    case 'touch':
      // click event from touch
      break;
    case 'key':
      // click event from key
      break;
  }
};

element.addEventListener('click', handleClickEvent, false);

setEventFrom(value)

value: 'mouse' | 'touch' | 'key'

Temporarily set the return value for eventFrom(e). This is useful when manually generating events, for example calling el.focus() or el.click(), and you want eventFrom(e) to treat that event as occurring from a specific input.

import { eventFrom, setEventFrom } from 'event-from';

const handleFocusEvent = (e) => {
  if (eventFrom(e) === 'key') {
    // add focus styles when focus is from keyboard input
  }
};

const element = document.getElementById('focus-example');

element.addEventListener('focus', handleFocusEvent, false);

// somewhere in your code where you want to call focus on the element
// and have it be treated as an event from 'key' input,
// now the call to eventFrom(e) in handleFocusEvent will return 'key'
setEventFrom('key');
element.focus();

How it works

Event From sets passive capture phase event listeners on the document and window and tracks the recent event history to know what input type is responsible for the event that's passed to eventFrom(event).

The listeners that Event From sets are all low frequency event listeners (enter/leave/down/up/focus/etc). Event From does not set any high frequency listeners such as move or scroll listeners.