Skip to content

Add a ShortcutsProvider #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 12 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,39 +106,28 @@ file.
import keymap from './keymap'
```

#### 3. Rise of the ShortcutsManager
#### 3. Wrap your application in \<ShortcutsProvider\>

Define your keymap in whichever supported format but in the end it must be an
object. `ShortcutsManager` can’t parse JSON and will certainly not be happy
object. `ShortcutsProvider` can’t parse JSON and will certainly not be happy
about the situation.

Wrap your application (or the part that you want to use keyboard shortcuts with) in a `<ShortcutsProvider>` tag. This will provide the necessary attributes in `context` for `<Shortcuts>` (below) to function.

```javascript
import keymap from './keymap'
import { ShortcutManager } from 'react-shortcuts'

const shortcutManager = new ShortcutManager(keymap)
import { ShortcutsProvider } from 'react-shortcuts'

// Or like this
// ...

const shortcutManager = new ShortcutManager()
shortcutManager.setKeymap(keymap)
```
<ShortcutsProvider keymap={ keymap } >
<MyApplication >

#### 4. Include `shortcutManager` into getChildContext of some parent component. So that `<shortcuts>` can receive it.

```javascript
class App extends React.Component {
getChildContext() {
return { shortcuts: shortcutManager }
}
}

App.childContextTypes = {
shortcuts: PropTypes.object.isRequired
}
</MyApplication>
</ShortcutsProvider>
```

#### 5. Require the <shortcuts> component
#### 4. Require the <shortcuts> component

You need to require the component in the file you want to use shortcuts in.
For example `<TodoItem>`.
Expand Down Expand Up @@ -202,7 +191,7 @@ class TodoItem extends React.Component {
- `isolate`: bool
- Use this when a child component has React's key handler (onKeyUp, onKeyPress, onKeyDown). Otherwise, React Shortcuts stops propagation of that event due to nature of event delegation that React uses internally.
- `alwaysFireHandler`: bool
- Use this when you want events keep firing on the focused input elements.
- Use this when you want events keep firing on the focused input elements.


## Thanks, Atom
Expand Down
35 changes: 35 additions & 0 deletions src/component/ShortcutsProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';

import shortcutManager from '../shortcut-manager';

class ShortcutsProvider extends Component {

getChildContext() {
const { keymap } = this.props;

return {
shortcuts: new shortcutManager(keymap),
};
}

render() {
return this.props.children;
}

}

ShortcutsProvider.propTypes = {
children: PropTypes.node,
keymap: PropTypes.object.isRequired,
};

ShortcutsProvider.defaultProps = {
children: null,
};

ShortcutsProvider.childContextTypes = {
shortcuts: PropTypes.object.isRequired,
};

export default ShortcutsProvider;
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
ShortcutManager: require('./shortcut-manager'),
Shortcuts: require('./component/'),
ShortcutsProvider: require('./component/ShortcutsProvider'),
}