Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

event-from

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

event-from

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

  • 0.2.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
501
decreased by-29.73%
Maintainers
1
Weekly downloads
 
Created
Source

Event From

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).
  • Determining 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.
  • You may also be interested in Detect It, which detects if a device is mouseOnly, touchOnly, or hybrid, and if the primary input is mouse or touch.

npm bundle size (version) npm type definitions


Demo website (code in the demo repo)


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

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

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 for accessibility

import { eventFrom } from 'event-from';

const handleFocusEvent = (e) => {
  if (eventFrom(e) === 'key') {
    // set 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);

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.

FAQs

Package last updated on 17 Dec 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc