-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try: Add component for handling keyboard events (#1944)
* Try: Add component for handling keyboard events * Warn about consistent callback reference for shortcuts * Remove editable detection from keyboard handling Already default behavior for Mousetrap https://craig.is/killing/mice#api.bind.text-fields
- Loading branch information
Showing
8 changed files
with
98 additions
and
56 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
Keyboard Shortcuts | ||
================== | ||
|
||
`<KeyboardShortcuts />` is a component which renders no children of its own, but instead handles keyboard sequences during the lifetime of the rendering element. | ||
|
||
It uses the [Mousetrap](https://craig.is/killing/mice) library to implement keyboard sequence bindings. | ||
|
||
## Example | ||
|
||
Render `<KeyboardShortcuts />` with a `shortcuts` prop object: | ||
|
||
```jsx | ||
class SelectAllDetection extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
this.setAllSelected = this.setAllSelected.bind( this ); | ||
|
||
this.state = { isAllSelected: false }; | ||
} | ||
|
||
setAllSelected() { | ||
this.setState( { isAllSelected: true } ); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<KeyboardShorcuts shortcuts={ { | ||
'mod+a': this.setAllSelected, | ||
} } /> | ||
Combination pressed? { isAllSelected ? 'Yes' : 'No' } | ||
</div> | ||
); | ||
} | ||
} | ||
``` | ||
__Note:__ The value of each shortcut should be a consistent function reference, not an anonymous function. Otherwise, the callback will not be correctly unbound when the component unmounts. | ||
__Note:__ The callback will not be invoked if the key combination occurs in an editable field. | ||
## Props | ||
The component accepts the following props: | ||
### shortcuts | ||
An object of shortcut bindings, where each key is a keyboard combination, the value of which is the callback to be invoked when the key combination is pressed. | ||
- Type: `Object` | ||
- Required: No | ||
## References | ||
- [Mousetrap documentation](https://craig.is/killing/mice) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import Mousetrap from 'mousetrap'; | ||
import { forEach } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from 'element'; | ||
|
||
class KeyboardShortcuts extends Component { | ||
componentWillMount() { | ||
this.toggleBindings( true ); | ||
} | ||
|
||
componentWillUnmount() { | ||
this.toggleBindings( false ); | ||
} | ||
|
||
toggleBindings( isActive ) { | ||
forEach( this.props.shortcuts, ( callback, key ) => { | ||
Mousetrap[ isActive ? 'bind' : 'unbind' ]( key, callback ); | ||
} ); | ||
} | ||
|
||
render() { | ||
return null; | ||
} | ||
} | ||
|
||
export default KeyboardShortcuts; |
This file contains 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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