Skip to content

Commit

Permalink
feat: make modal accessible
Browse files Browse the repository at this point in the history
  • Loading branch information
ell-ska committed Feb 7, 2025
1 parent 94fc1f9 commit ca961f0
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions resources/views/components/modal/base.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,39 @@
<div
x-data="{
show: @js($show),
focusables() {
const selector = 'a, button, input:not([type=\'hidden\']), textarea, select, details, [tabindex]:not([tabindex=\'-1\'])'
return [...$el.querySelectorAll(selector)].filter(element => !element.hasAttribute('disabled'))
},
firstFocusable() {
return this.focusables()[0]
},
lastFocusable() {
return this.focusables().slice(-1)[0]
},
nextFocusable() {
const focusables = this.focusables()
const nextFocusableIndex = (focusables.indexOf(document.activeElement) + 1) % (focusables.length + 1)
return focusables[nextFocusableIndex] || this.firstFocusable()
},
prevFocusable() {
const focusables = this.focusables()
const prevFocusableIndex = Math.max(0, focusables.indexOf(document.activeElement)) -1
return focusables[prevFocusableIndex] || this.lastFocusable()
},
}"
x-show="show"
x-init="
$watch('show', (show) => {
if (show) {
document.body.classList.add('overflow-hidden')
} else {
document.body.classList.remove('overflow-hidden')
}
})
"
x-on:open-modal.window="
if ($event.detail === '{{ $name }}') {
show = true
Expand All @@ -18,15 +49,9 @@
show = false
}
"
x-init="
$watch('show', (show) => {
if (show) {
document.body.classList.add('overflow-hidden')
} else {
document.body.classList.remove('overflow-hidden')
}
})
"
x-on:keydown.escape.window="show = false"
x-on:keydown.tab.prevent="$event.shiftKey || nextFocusable().focus()"
x-on:keydown.shift.tab.prevent="prevFocusable().focus()"
x-cloak
@class([
'flex' => $show,
Expand Down

0 comments on commit ca961f0

Please sign in to comment.