Created
July 8, 2015 19:15
-
-
Save tnrich/1ceea5cdc705c6e40d31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How would you suggest using something like this with ES6, since mixins aren't supported?