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

mouse-presence

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mouse-presence

Centralizes handling of mouse appearance/disappearance events in the browser

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.5K
decreased by-4.84%
Maintainers
1
Weekly downloads
 
Created
Source

Mouse Presence

Centralizes handling of mouse appearance/disappearance events in the browser.

Installation

npm: npm install --save mouse-presence

Yarn: yarn add mouse-presence

API Reference

MousePresence.onDisappear(cb)

cb: A callback function that takes no arguments and returns nothing.

returns: A deregisterer for cb.

Registers a callback to be called whenever a mouse cursor disappearance is dispatched.

MousePresence.onAppear(cb)

cb: A callback function that takes no arguments and returns nothing.

returns: A deregisterer for cb.

Registers a callback to be called whenever the mouse is moved after a disappearance was dispatched. (Moving the mouse after the cursor disappeared will cause it to reappear.)

MousePresence.unregisterCallback(cb)

cb: The callback reference to be unregistered.

returns: Nothing.

Unregisters a previously-registered callback from either onDisappear or onAppear (or both). If no such callback is found, nothing happens.

MousePresence.dispatchDisappear()

returns: Nothing.

Dispatches a mouse disappearance event, which will call all registered onDisappear callbacks (will only do this once - subsequent calls will have no effect until the next onAppear event occurs). It will also register a one-time mousemove listener on the document, which when triggered, will call all registered onAppear callbacks.

MousePresence.isPresent()

returns: Boolean indicating whether the mouse cursor is present.

Determine if the mouse cursor is present or not (according to the last reported appearance/disappearance event).

MousePresence.dispatchDisappearOnKeydown(el)

el: The HTMLElement for which the keydown listener should be added to.

returns: A deregisterer for the keydown listener that will be added.

Adds a keydown listener to the provided HTMLElement, which will trigger a dispatchDisappear call when triggered (since typing will cause the mouse cursor to disappear). This is a convenience function for any text-based input elements, to be used instead of explicitly attaching a listener and calling dispatchDisappear yourself.

Examples

Component A:

import MousePresence from 'mouse-presence';

// Gets called on every onDisappear event
MousePresence.onDisappear(() => {
    // Take actions...
});

// Gets called on every onAppear event
MousePresence.onAppear(() => {
    // Take actions...
});

Component B:

import MousePresence from 'mouse-presence';

// Only gets called for one onDisappear event, then unregisters itself
let disappearCb = MousePresence.onDisappear(() => {
    MousePresence.unregisterCallback(disappearCb);
});

Component C:

import MousePresence from 'mouse-presence';

// Mouse cursor disappears when typing
this.element.addEventListener('keydown', () => {
    MousePresence.dispatchDisappear();
    // Will immediately dispatch onDisappear
    // Will automatically attach a mousemove listener, upon which onAppear will be dispatched
    // Will not redispatch onDisappear until the onAppear event occurs
});

FAQs

Package last updated on 21 Dec 2018

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