Skip to content
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

Clarify key-responder is about keyboard shortcuts #10

Merged
merged 2 commits into from
Apr 8, 2015
Merged
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
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,33 @@ The addon packages default styles for modal structure and appearance. To use the
@import "ember-modal-dialog/ember-modal-appearance";
```

## Wiring Up a Key Responder
## Keyboard shortcuts

Here is an example of how to extend the `modal-dialog` component within an Ember app that is using [ember-key-responder](https://github.com/yapplabs/ember-key-responder).
A quick-and-dirty way to implement keyboard shortcuts (e.g. to dismiss your modals with `escape`) is to subclass the dialog and attach keyboard events:

**app/components/modal-dialog.js**
```js
// app/components/modal-dialog/component.js
import ModalDialog from 'ember-modal-dialog/components/modal-dialog';

export default ModalDialog.extend({
setup: function() {
Ember.$('body').on('keyup', (e) => {
if (e.keyCode == 27) {
this.sendAction('close');
}
});
}.on('didInsertElement'),

teardown: function() {
Ember.$('body').off('keyup');
}.on('willDestroyElement'),
});
```

This can work, but some apps require a more sophisticated approach. One approach, inspired by Cocoa, takes advantage of the [ember-key-responder](https://github.com/yapplabs/ember-key-responder) library. Here's an example:

```javascript
// app/components/modal-dialog.js
import Component from 'ember-modal-dialog/components/modal-dialog';

export default Component.extend({
Expand All @@ -162,6 +182,8 @@ export default Component.extend({
});
```

View [the library](https://github.com/yapplabs/ember-key-responder) for more information.

## Dependencies

* Requires Ember CLI >= 0.2.0
Expand Down