diff --git a/src/index.js b/src/index.js index 3834c0d..579233a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,18 +1,25 @@ -import React from 'react'; +import React from 'react' +import Mousetrap from 'mousetrap' +import 'mousetrap/plugins/global-bind/mousetrap-global-bind' + export function mouseTrap(Base) { return class extends React.Component { constructor(props) { super(props); this.__mousetrapBindings = []; - this.Mousetrap = require('mousetrap'); } bindShortcut(key, callback) { - this.Mousetrap.bind(key, callback); + Mousetrap.bind(key, callback); this.__mousetrapBindings.push(key); } + bindGlobalShortcut(key, callback) { + Mousetrap.bindGlobal(key, callback) + this.__mousetrapBindings.push(key) + } + unbindShortcut(key) { var index = this.__mousetrapBindings.indexOf(key); @@ -20,7 +27,7 @@ export function mouseTrap(Base) { this.__mousetrapBindings.splice(index, 1); } - this.Mousetrap.unbind(key); + Mousetrap.unbind(key); } unbindAllShortcuts() { @@ -29,7 +36,7 @@ export function mouseTrap(Base) { } this.__mousetrapBindings.forEach(binding => { - this.Mousetrap.unbind(binding); + Mousetrap.unbind(binding); }); this.__mousetrapBindings = []; } @@ -43,6 +50,7 @@ export function mouseTrap(Base) { ); @@ -50,4 +58,4 @@ export function mouseTrap(Base) { }; } -export default mouseTrap; +export default mouseTrap; \ No newline at end of file