Skip to content

Commit

Permalink
Expose focus-trap configuration
Browse files Browse the repository at this point in the history
This is an extension to #178 released with 0.2.1 and enables more fine grained control over the focus-trap, which might be necessary to work around issues with clickOutsideDeactivates vs allowOutsideClick described in [the projects README](https://github.com/focus-trap/focus-trap#createfocustrapelement-createoptions)
  • Loading branch information
pichfl committed Jan 12, 2021
1 parent 8862e8c commit 5d4b127
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
8 changes: 4 additions & 4 deletions addon/components/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ export default Component.extend({
didInsertElement() {
this._super(...arguments);

let { clickOutsideDeactivates } = this.modals;

this.focusTrap = createFocusTrap(this.element, {
clickOutsideDeactivates,
let { focusTrapOptions: options } = this.modals;

options = Object.assign({}, options, {
onDeactivate: () => {
this.modal.close();
},
});

this.focusTrap = createFocusTrap(this.element, options);

this.focusTrap.activate();
},

Expand Down
6 changes: 5 additions & 1 deletion addon/services/modals.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default Service.extend({
count: alias('_stack.length'),
top: alias('_stack.lastObject'),

clickOutsideDeactivates: true,
focusTrapOptions: null,

backdropDuration: 600,
backdropTransition: fade,
Expand All @@ -37,6 +37,10 @@ export default Service.extend({
init() {
this._super(...arguments);
this._stack = A([]);

this.focusTrapOptions = this.focusTrapOptions || {
clickOutsideDeactivates: true,
};
},

willDestroy() {
Expand Down
4 changes: 3 additions & 1 deletion tests/application/basics-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ module('Application | basics', function (hooks) {
});

test('clicking the backdrop does not close the modal if `clickOutsideDeactivates` is `false`', async function (assert) {
this.owner.lookup('service:modals').clickOutsideDeactivates = false;
this.owner.lookup('service:modals').focusTrapOptions = {
clickOutsideDeactivates: false,
};

await visit('/');
assert.dom('.epm-backdrop').doesNotExist();
Expand Down

0 comments on commit 5d4b127

Please sign in to comment.