From 5d4b127de5b7bda080595e180ade4c8d92958488 Mon Sep 17 00:00:00 2001 From: Florian Pichler Date: Tue, 12 Jan 2021 12:25:35 +0100 Subject: [PATCH] Expose focus-trap configuration 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) --- addon/components/modal.js | 8 ++++---- addon/services/modals.js | 6 +++++- tests/application/basics-test.js | 4 +++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/addon/components/modal.js b/addon/components/modal.js index ecb93b82..4a0390cf 100644 --- a/addon/components/modal.js +++ b/addon/components/modal.js @@ -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(); }, diff --git a/addon/services/modals.js b/addon/services/modals.js index aa3acf4b..85af43fe 100644 --- a/addon/services/modals.js +++ b/addon/services/modals.js @@ -12,7 +12,7 @@ export default Service.extend({ count: alias('_stack.length'), top: alias('_stack.lastObject'), - clickOutsideDeactivates: true, + focusTrapOptions: null, backdropDuration: 600, backdropTransition: fade, @@ -37,6 +37,10 @@ export default Service.extend({ init() { this._super(...arguments); this._stack = A([]); + + this.focusTrapOptions = this.focusTrapOptions || { + clickOutsideDeactivates: true, + }; }, willDestroy() { diff --git a/tests/application/basics-test.js b/tests/application/basics-test.js index 15694b47..11792550 100644 --- a/tests/application/basics-test.js +++ b/tests/application/basics-test.js @@ -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();