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

@evanminto/keyboard-observer

v1.1.1

Published

JavaScript utility for observing keyboard events fired while a specific element is focused

Downloads

1

Readme

KeyboardObserver

JavaScript utility for observing keyboard events fired while a specific element is focused, with an API inspired by native observers like MutationObserver and ResizeObserver.

Installation

npm install @evanminto/keyboard-observer --save-dev

Usage

// Assuming you're using a module bundler that can resolve Node modules.
import KeyboardObserver from '@evanminto/keyboard-observer';

function keyboardCallback(records) {
  records.forEach(record => {
    // Do something based on the record's event or target properties.
  });
}

// Create the observer (not observing anything yet).
const observer = new KeyboardObserver(keyboardCallback);

const target1 = document.querySelector('#my_element');
const target2 = document.querySelector('#my_element2');

// Start observing a target.
observer.observe(target1);

// You can observe multiple targets at once.
observer.observe(target2);

// Stop observing a single target.
observer.unobserve(target2);

// Stop observing all targets.
observer.disconnect(target2);

Use Cases

Building complex, accessible UI components requires keyboard controls that are only active when the element has focus. KeyboardObserver keeps track of this focus for you, so that your code can respond to only the keyboard events that are dispatched while the element is actually active.

Full Documentation

KeyboardObserver

Reports keyboard events dispatched while a user is focused on an observed Element or SVGElement.

Methods

KeyboardObserver(callback)

Constructor. Creates and returns new ResizeObserver object.

Parameters

callback: The method called whenever a keyboard event occurs. The method is called with an array of KeyboardRecord objects.

disconnect()
observe(target, options)
Parameters

target: A reference to an Element or SVGElement to be observed. options: Object with options for customizing the behavior. options.penetrateShadowRoots: If false, target matching will only work on light DOM elements, not shadow DOM elements. (Default: true)

unobserve(target)
Parameters

target: A reference to an Element or SVGElement to be unobserved.

KeyboardRecord

The object passed to the callback function used in the KeyboardObserver constructor.

Properties

event: (KeyboardEvent)

target: (Element|SVGElement)