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

ember-keyboard-shortcuts

v1.2.0

Published

ember-cli addon that uses mousetrap to facilitate keyboard shortcuts

Downloads

13,760

Readme

ember-keyboard-shortcuts

Compatibility

  • Ember.js v2.18 or above
  • Ember CLI v2.13 or above

Installation

In your ember-cli project:

ember install ember-keyboard-shortcuts

Usage

We expose two functions to setup and remove keyboard shortcuts. You can use it in routes, components or controllers.

In a route

import Route from '@ember/routing/route';
import {
  bindKeyboardShortcuts,
  unbindKeyboardShortcuts
} from 'ember-keyboard-shortcuts';
export default Route.extend({
  keyboardShortcuts: {
    // trigger 'cancel' action when esc is pressed
    esc: 'cancel',
    'ctrl+c': {
      action: 'myAction', // action to trigger
      global: false, // whether to trigger inside input (default: true)
      preventDefault: true // (default: true)rue
    },

    // trigger function when tab is pressed
    tab() {
      console.log('Tab pressed');
      return false; // preventDefault
    }
  },

  activate() {
    bindKeyboardShortcuts(this);
  },

  deactivate() {
    unbindKeyboardShortcuts(this);
  },

  actions: {
    cancel() {
      this.transitionTo('posts');
    }
  }
});

In a component

import Component from '@ember/component';
import {
  bindKeyboardShortcuts,
  unbindKeyboardShortcuts
} from 'ember-keyboard-shortcuts';

export default Component.extend({
  keyboardShortcuts: {
    f: {
      action: 'myAction', // action to trigger
      global: false, // whether to trigger inside input (default: true)
      preventDefault: true // (default: true)
    }
  },

  didInsertElement() {
    this._super(...arguments);
    bindKeyboardShortcuts(this);
  },

  willDestroyElement() {
    this._super(...arguments);
    unbindKeyboardShortcuts(this);
  },

  actions: {
    myAction() {
      alert('key `f` was pressed from component some-component');
    }
  }
});

Available shortcut options

  • action: action to trigger. Can be a function or a string containing action name.
  • global: indicates whether events should be triggered within input, textarea and select. Default: true.
  • scoped: indicates that the shortcuts should only be registered for the current component/view and its children. Implies global: true. Default: false.
  • preventDefault: prevents the default action and stops the event from bubbling up. Applies only when the action is a string. Default: true.

Testing

You will want to use the triggerKeyEvent helper from ember-test-helpers. We listen for the keydown event by default. You must always scope the trigger in your tests to at least a child element of the scoping option you pass.

// Setting `global: false,`
await triggerKeyEvent(document, 'keydown', <keycode/name>);

// Setting `scoped: true,`
await triggerKeyEvent(featureUnderTest.element, 'keydown', <keycode/name>);

// Passing `targetElement: <element>,`
await triggerKeyEvent(<targetElement>, 'keydown', <keycode/name>);

// Default behavior
await triggerKeyEvent(document.body, 'keydown', <keycode/name>);

Migrating from mixins

Prior versions, you could use this addon with mixins. We have deprecated that behavior in order to calling specific functions to setup shortcuts as well to destroy event listeners.

Here is an example of not using mixins in a route.

import Route from '@ember/routing/route';
import {
  bindKeyboardShortcuts,
  unbindKeyboardShortcuts
} from 'ember-keyboard-shortcuts';

export default Ember.Route.extend({
  // No changes required in this block
  keyboardShortcuts: {
    esc: 'cancel',
    'ctrl+c': {
      action: 'cancel',
      global: false,
      preventDefault: true
    },
    tab() {
      console.log('Tab pressed');
      return false;
    }
  },

  activate() {
    bindKeyboardShortcuts(this);
  },

  deactivate() {
    unbindKeyboardShortcuts(this);
  },

  actions: {
    cancel() {
      this.transitionTo('posts');
    }
  }
});

In summary, if you used to use ember-keyboard-shortcuts in routes, you will add a function call activate and deactivate.

To migrate from a components or a view, you should use didInsertElement and willDestroyElement hooks.

Development

See the Contributing guide for details.

For more information on using ember-cli, visit http://www.ember-cli.com/.