Skip to content

Instantly share code, notes, and snippets.

@Cretezy
Last active May 15, 2018 02:41
Show Gist options
  • Save Cretezy/5dfa7435a93005c3227837c1438ba556 to your computer and use it in GitHub Desktop.
Save Cretezy/5dfa7435a93005c3227837c1438ba556 to your computer and use it in GitHub Desktop.

Better react-hotkeys HOC

Usage:

withHotKeys({ new: "alt+n" }, { new: props => event => console.log(props, event) })(...)

Useful to use with recompose.

import React from "react";
import { HotKeys } from "react-hotkeys";
export default (keyMap, handlers) => Component => props => {
const bindedHandlers = Object.entries(handlers).reduce(
(bindedHandlers, [name, fn]) => ({
...bindedHandlers,
[name]: event => fn(props)(event)
}),
{}
);
return (
<HotKeys
component="document-fragment"
keyMap={keyMap}
handlers={bindedHandlers}
>
<Component {...props} />
</HotKeys>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment