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

elm-keyboard-shortcut

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

elm-keyboard-shortcut

companion package for elm-keyboard-shortcut

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
439
decreased by-9.11%
Maintainers
1
Weekly downloads
 
Created
Source

elm-keyboard-shortcut

Why?

Listening to the keyboard without dealing with where the focus is or anything like this is a bit messy. One way to do this to use Browser.Events.keyPressed and the like. However, this means you have to keep track of subscription and to correctly interpret each keystroke in your app based on your model, etc.

An alternative would be to listen to keyboard events directly within your views. You already have access to the model or the relevant part of the models in there and if your event-listener is not on display then it should not capture anything. The main issue with that approach is the focus: only focusable element can listen to keyboard event and only when focused will those event listeners be triggered. Hence, you now need to keep that in your mind when developing.

With this package, you get the best of both world but you need to use a custom element for it. The custom element will let you define which shortcut you'd like to listen to and to listen for them directly on the document.body so you can be sure you'll get the events you're expecting.

As with anything, there are drawbacks. If you have nested shortcut listener, e.g. you have a modal inside another modal and you'd like both to be cleared by pressing ESC - this is a bad design but anyhow -, then you may end up with one press on ESC to be caught by both shortcut listener. The best way to handle this is probably to remove the ESC shortcut from the listener of the first modal when the second one is open.

How?

For an example of how to use this shortcut package, please have a look at the example section.

Installation

To use this in your project:

  • Start by installing the elm package: elm install leojpod/elm-keyboard-shortcut

  • Then install the companion JS package: npm install elm-keyboard-shortcut or yarn elm-keyboard-shortcut

  • Finaly import the package in your project and that should do it: import 'elm-keyboard-shortcut'

FAQs

Package last updated on 28 Oct 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