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

stack-shortcuts

v0.2.0

Published

Easy shortcuts api for working with modern web apps

Downloads

2

Readme

Stack shortcuts

Build Status Coverage Status npm version gzip bundle size MIT Licence

Quick start

npm

npm i stack-shortcuts --save

browser

<script src="https://raw.githubusercontent.com/BusinessDuck/stack-shortcuts/master/lib/shortcuts.umd.js"></script>

Sandbox

Edit React stack-shortcuts example

The problem

Initial data:

Imagine, you have an application with small picture, you can move it along x and y axis by the keyboard arrows. Mouse click on the picture, should open a modal dialogue with full sized picture. And now for example you need to use arrowUp and arrowDown buttons for navigate to related images (prev, next)

Lets see the task problems:

Problem 1: To handle keypress event you should use inconvenient and not clear contract

element.addEventListener('keydown', handler);

function handler(event) {
    event.key; // does not supported to any browsers
    event.keyIdentifier ; // deprecated
    event.keyCode; // supports all browsers but inconvenient

    event.keyCode === 27 // ESC (should google it every time)
}

Problem 2: When you have a different controllers for picture and modal dialogue you should resolve hotkey press in two different controllers, they are shouldn't to know about each other. The best solution will be create third control (or use parent control) for manage hotkeys depend from child controllers state. Its a big problem for big apps.

Problem 3: If you want yo handle key pressing in different controllers you will be depends from DOM positions of elements. Because events is bubbling. That mean yuo may solve this problem by useCapture it's works, but not clean. That will produce refactoring and debugging processes as well.

Greet the solution!

Diagram of the concept

Abstract layer is a controller. By using stack-shortcuts you can create a layers with handlers and condition statement and handle hotkeys pressing or pass to next layer if condition is false

// controller 1
...
this.shortuts = shortcuts({
    'CMD+F': (event, next) => {
        // event - native event
        // next - call pass event to next in stack layer handler
        // like a express controller

        if (event.target === myDOMElement) {
            alert('its works');
        } else {
            next();
        }
    }
});

// controller 2
this.shortuts = shortcuts({
    'CMD+F': (event, next) => {
        // event - native event
        // next - call pass event to next in stack layer handler
        // like a express controller

        if (event.target === mySecondDOMElement) {
            alert('its works');
        } else {
            next();
        }
    }
});

Getting Started

npm i stack-shortcuts --save

Features

  1. DOM element position independent

  2. Human shortcuts names

    shortcuts({
        'ALT+SHIFT+T': () => null, //handle ALT+SHIFT+T
        'ALL': () => null, // handle aall kedon
    });
  3. Platform auto mapping CMD to CTRL

    shortcuts({
        'CMD+F': (event, next) => null, //handle CTRL+F on windows
    });
  4. Destructor

    const hotkeys = shortcuts({
        'CMD+F': () => null
    });
    
    hotkeys.destroy(); // remove shortcuts layer
  5. Next callback

    shortcuts({
        'CMD+F': (event, next) => {
            next(); // pass to next in stack layer
        },
    });
  6. Easy to debugging call stack in stacktrace

    You will see the all chain of stack layers calls in debugger stacktrace

  7. Dynamically add and remove shortcuts

    const layer = shortcuts({ // init config
        'CMD+F': (event, next) => {
            next(); // pass to next in stack layer
        },
    });
    
    layer.add('CMD+V', (event, next) => null);
    layer.remove('CMD+V', (event, next) => null);
    
  8. Order free, both is the same

    const layer = shortcuts();
    
    layer.add('SHIFT+CMD+V', (event, next) => null);
    layer.remove('SHIFT+CMD+V');
    
    layer.add('CMD+SHIFT+V', (event, next) => null);
    layer.remove('CMD+SHIFT+V');
  9. No dependencies and small size (less than 3 kb)

See example in examples

Works fine with react and other frameworks out of the box.