diff --git a/.gitignore b/.gitignore index a5f83ea..17cf261 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ .DS_Store *.log node_modules -lib +dist es diff --git a/package.json b/package.json index 11c5dae..caf9d83 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,14 @@ { "name": "react-keyboardist", "version": "0.2.0", - "description": "A simple keyboard global manager for react. Powered by keyboardist.", - "main": "lib/index.js", + "description": + "A simple keyboard global manager for react. Powered by keyboardist.", + "main": "dist/index.js", "scripts": { "flow": "flow", "favicon": "cpx \"src/docs/favicon.*\" \"docs\"", "dev": "webpack-dev-server --mode development", - "build:lib": "babel src/lib -d lib", + "build:lib": "babel src/lib -d dist", "build:docs": "webpack --mode production", "build": "npm run build:lib && npm run build:docs && npm run favicon" }, diff --git a/src/lib/global.js b/src/lib/global.js new file mode 100644 index 0000000..a263565 --- /dev/null +++ b/src/lib/global.js @@ -0,0 +1,55 @@ +// @flow +import * as React from 'react'; +import createListener from 'keyboardist'; + +const listeners = { + keydown: createListener('keydown'), + keyup: createListener('keyup'), +}; + +type Props = { + eventName: 'keydown' | 'keyup', + bindings: { [string]: any }, + monitor?: any, +}; + +class Keyboard extends React.PureComponent { + subs = []; + + keyboardListener: any; + + static defaultProps = { + eventName: 'keydown', + bindings: {}, + monitor: null, + }; + + constructor(props: Props) { + super(props); + this.keyboardListener = listeners[props.eventName]; + } + + componentDidMount() { + const { bindings, monitor } = this.props; + + Object.keys(bindings).forEach(eventName => { + const callback = bindings[eventName]; + const subscription = this.keyboardListener.subscribe(eventName, callback); + this.subs.push(subscription); + }); + + if (monitor) { + this.keyboardListener.setMonitor(monitor); + } + } + + componentWillUnmount() { + this.subs.forEach(subscription => subscription.unsubscribe()); + } + + render() { + return null; + } +} + +export default Keyboard; diff --git a/src/lib/input.js b/src/lib/input.js new file mode 100644 index 0000000..9bc1045 --- /dev/null +++ b/src/lib/input.js @@ -0,0 +1,46 @@ +import * as React from 'react'; +import createListener from 'keyboardist'; + +class Input extends React.PureComponent { + subs = []; + + static defaultProps = { + component: 'input', + eventName: 'keydown', + bindings: {}, + monitor: null, + }; + + componentDidMount() { + const { bindings, monitor, eventName } = this.props; + + this.keyboardListener = createListener(eventName, this.element); + + Object.keys(bindings).forEach(eventName => { + const callback = bindings[eventName]; + const subscription = this.keyboardListener.subscribe(eventName, callback); + this.subs.push(subscription); + }); + + if (monitor) { + this.keyboardListener.setMonitor(monitor); + } + } + + componentWillUnmount() { + this.subs.forEach(subscription => subscription.unsubscribe()); + } + + render() { + const { + component: Component, + eventName, + bindings, + monitor, + ...props + } = this.props; + return (this.element = el)} {...props} />; + } +} + +export default Input;