Skip to content

Commit e25dde6

Browse files
authored
refactor: make modal focus trap switchable (#31)
1 parent d31abb9 commit e25dde6

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

resources/assets/js/modal.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ const Modal = {
1414
defaultSettings: {
1515
reserveScrollBarGap: true,
1616
reserveNavScrollBarGap: true,
17+
disableFocusTrap: false,
1718
},
1819

19-
onModalOpened(scrollable, settings = Modal.defaultSettings) {
20+
onModalOpened(scrollable, settings = {}) {
21+
settings = Object.assign({}, this.defaultSettings, settings);
22+
2023
if (settings.reserveScrollBarGap) {
2124
this.reserveModalScrollBarGap(scrollable);
2225
}
@@ -29,10 +32,16 @@ const Modal = {
2932
reserveScrollBarGap: !!settings.reserveScrollBarGap,
3033
});
3134

32-
this.trapFocus(scrollable);
35+
if (settings.disableFocusTrap) {
36+
scrollable.focus();
37+
} else {
38+
this.trapFocus(scrollable);
39+
}
3340
},
3441

35-
onModalClosed(scrollable, settings = Modal.defaultSettings) {
42+
onModalClosed(scrollable, settings = {}) {
43+
settings = Object.assign({}, this.defaultSettings, settings);
44+
3645
if (settings.reserveScrollBarGap) {
3746
this.restoreModalScrollBarGap(scrollable);
3847
}
@@ -69,11 +78,7 @@ const Modal = {
6978
this.trappedElement = null;
7079
},
7180

72-
alpine(
73-
extraData = {},
74-
modalName = "",
75-
eventSettings = Modal.defaultSettings
76-
) {
81+
alpine(extraData = {}, modalName = "", eventSettings = {}) {
7782
return {
7883
name: modalName,
7984
shown: false,
@@ -146,7 +151,7 @@ const Modal = {
146151
};
147152
},
148153

149-
livewire(extraData = {}, eventSettings = Modal.defaultSettings) {
154+
livewire(extraData = {}, eventSettings = {}) {
150155
return {
151156
init() {
152157
const scrollable = this.getScrollable();

usage/ui.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,13 @@ To use the Livewire modals, use the `ARKEcosystem\UserInterface\Http\Livewire\Co
269269

270270
**Important**: If you need to use a different variable to close the modal, or you can't make use of the trait for a reason, make sure to emit the `modalClosed` event as that is required for proper handling of the modals on the frontend! If you fail to emit this event, the browser window will not be scrollable after the modal disappears.
271271

272+
You can disable the focus trap by passing a parameter in the second argument:
273+
```php
274+
<div x-data="Modal.livewire({{ $extraData }}, [\"disableFocusTrap\" => true])">
275+
<!--...-->
276+
</div>
277+
```
278+
272279
#### Alpine modals
273280

274281
**Important**: for the modals to work properly, they expect a `nav` element inside a `header` element to be used for the header component. If you use the navbar from the UI lib (see `navbar.blade.php`) these elements are already used, but for custom navbars you may need to make adjustments.
@@ -347,6 +354,19 @@ import Modal from "@ui/js/modal";
347354
window.Modal = Modal;
348355
```
349356

357+
You can disable the focus trap by passing a parameter in the third argument:
358+
```js
359+
import Modal from "@ui/js/modal";
360+
361+
window.Modal = Modal;
362+
363+
Modal.alpine(
364+
{}, // extra data
365+
'', // modal name
366+
{ disableFocusTrap: true } // <-- disable focus trap
367+
)
368+
```
369+
350370
### Tooltips
351371

352372
1. Install `tippy.js`

0 commit comments

Comments
 (0)