Last active
August 29, 2015 14:22
-
-
Save alvinsj/c6451a18817f76dac2e2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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('br-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