Skip to content

Instantly share code, notes, and snippets.

@tnrich
Created July 8, 2015 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tnrich/1ceea5cdc705c6e40d31 to your computer and use it in GitHub Desktop.
Save tnrich/1ceea5cdc705c6e40d31 to your computer and use it in GitHub Desktop.
/** @jsx React.DOM */
// NOTE: This file is formatted for React.js + Browserify
// You might need to make some changes to use it without Browserify
var MousetrapMixin,
Mousetrap = require('mousetrap');
MousetrapMixin = {
/**
* Array for keeping track of shortcuts bindings
*/
mousetrapBindings: [],
/**
* Bind a function to a keyboard shortcut
*
* @param key
* @param callback
*/
bindShortcut: function (key, callback) {
Mousetrap.bind(key, callback);
this.mousetrapBindings.push(key);
},
/**
* Unbind a keyboard shortcut
*
* @param key
*/
unbindShortcut: function (key) {
var index = this.mousetrapBindings.indexOf(key);
if (index > -1) {
this.mousetrapBindings.splice(index, 1);
}
Mousetrap.unbind(binding);
},
/**
* Remove any Mousetrap bindings
*/
unbindAllShortcuts: function () {
if (this.mousetrapBindings.length < 1) {
return;
}
this.mousetrapBindings.forEach(function (binding) {
Mousetrap.unbind(binding);
});
},
/**
* Handle component unmount
*/
componentWillUnmount: function () {
// Remove any Mousetrap bindings before unmounting
this.unbindAllShortcuts();
}
};
module.exports = MousetrapMixin;
@cassidoo
Copy link

How would you suggest using something like this with ES6, since mixins aren't supported?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment