Skip to content

Commit a939bda

Browse files
committed
Merge branch 'v2.x' of https://github.com/nascent-africa/jetstrap into v2.x
2 parents f4f62cf + e83ed7e commit a939bda

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

resources/views/components/modal.blade.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,26 @@
2727
<div
2828
x-data="{
2929
show: @entangle($attributes->wire('model')).defer,
30+
focusables() {
31+
// All focusable element types...
32+
let selector = 'a, button, input, textarea, select, details, [tabindex]:not([tabindex=\'-1\'])'
33+
return [...$el.querySelectorAll(selector)]
34+
// All non-disabled elements...
35+
.filter(el => ! el.hasAttribute('disabled'))
36+
},
37+
firstFocusable() { return this.focusables()[0] },
38+
lastFocusable() { return this.focusables().slice(-1)[0] },
39+
nextFocusable() { return this.focusables()[this.nextFocusableIndex()] || this.firstFocusable() },
40+
prevFocusable() { return this.focusables()[this.prevFocusableIndex()] || this.lastFocusable() },
41+
nextFocusableIndex() { return (this.focusables().indexOf(document.activeElement) + 1) % (this.focusables().length + 1) },
42+
prevFocusableIndex() { return Math.max(0, this.focusables().indexOf(document.activeElement)) -1 },
3043
}"
3144
x-init="() => {
3245
let modal = $('#{{ $id }}');
3346
$watch('show', value => {
3447
if (value) {
3548
modal.modal('show')
49+
setTimeout(() => (focusables()[0]).focus(), 250);
3650
} else {
3751
modal.modal('hide')
3852
}
@@ -41,10 +55,18 @@
4155
modal.on('hide.bs.modal', function () {
4256
show = false
4357
})
58+
59+
modal.click(function(e) {
60+
if (e.target == this) {
61+
show = false;
62+
}
63+
});
4464
}"
65+
x-on:keydown.escape.window="show = false"
66+
x-on:keydown.tab.prevent="$event.shiftKey || nextFocusable().focus()"
67+
x-on:keydown.shift.tab.prevent="prevFocusable().focus()"
4568
wire:ignore.self
4669
class="modal fade"
47-
tabindex="-1"
4870
id="{{ $id }}"
4971
aria-labelledby="{{ $id }}"
5072
aria-hidden="true"

0 commit comments

Comments
 (0)