1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
import { createFocusTrap } from 'focus-trap'
import { focusable, tabbable, isFocusable } from 'tabbable'
export default function (Alpine) {
let lastFocused
let currentFocused
window.addEventListener('focusin', () => {
lastFocused = currentFocused
currentFocused = document.activeElement
})
Alpine.magic('focus', el => {
let within = el
return {
__noscroll: false,
__wrapAround: false,
within(el) { within = el; return this },
withoutScrolling() { this.__noscroll = true; return this },
noscroll() { this.__noscroll = true; return this },
withWrapAround() { this.__wrapAround = true; return this },
wrap() { return this.withWrapAround() },
focusable(el) {
return isFocusable(el)
},
previouslyFocused() {
return lastFocused
},
lastFocused() {
return lastFocused
},
focused() {
return currentFocused
},
focusables() {
if (Array.isArray(within)) return within
return focusable(within, { displayCheck: 'none' })
},
all() { return this.focusables() },
isFirst(el) {
let els = this.all()
return els[0] && els[0].isSameNode(el)
},
isLast(el) {
let els = this.all()
return els.length && els.slice(-1)[0].isSameNode(el)
},
getFirst() { return this.all()[0] },
getLast() { return this.all().slice(-1)[0] },
getNext() {
let list = this.all()
let current = document.activeElement
// Can't find currently focusable element in list.
if (list.indexOf(current) === -1) return
// This is the last element in the list and we want to wrap around.
if (this.__wrapAround && list.indexOf(current) === list.length - 1) {
return list[0]
}
return list[list.indexOf(current) + 1]
},
getPrevious() {
let list = this.all()
let current = document.activeElement
// Can't find currently focusable element in list.
if (list.indexOf(current) === -1) return
// This is the first element in the list and we want to wrap around.
if (this.__wrapAround && list.indexOf(current) === 0) {
return list.slice(-1)[0]
}
return list[list.indexOf(current) - 1]
},
first() { this.focus(this.getFirst()) },
last() { this.focus(this.getLast()) },
next() { this.focus(this.getNext()) },
previous() { this.focus(this.getPrevious()) },
prev() { return this.previous() },
focus(el) {
if (! el) return
setTimeout(() => {
if (! el.hasAttribute('tabindex')) el.setAttribute('tabindex', '0')
el.focus({ preventScroll: this._noscroll })
})
}
}
})
Alpine.directive('trap', Alpine.skipDuringClone(
(el, { expression, modifiers }, { effect, evaluateLater, cleanup }) => {
let evaluator = evaluateLater(expression)
let oldValue = false
let trap = createFocusTrap(el, {
escapeDeactivates: false,
allowOutsideClick: true,
fallbackFocus: () => el,
})
let undoInert = () => {}
let undoDisableScrolling = () => {}
const releaseFocus = () => {
undoInert()
undoInert = () => {}
undoDisableScrolling()
undoDisableScrolling = () => {}
trap.deactivate({
returnFocus: !modifiers.includes('noreturn')
})
}
effect(() => evaluator(value => {
if (oldValue === value) return
// Start trapping.
if (value && ! oldValue) {
setTimeout(() => {
if (modifiers.includes('inert')) undoInert = setInert(el)
if (modifiers.includes('noscroll')) undoDisableScrolling = disableScrolling()
trap.activate()
});
}
// Stop trapping.
if (! value && oldValue) {
releaseFocus()
}
oldValue = !! value
}))
cleanup(releaseFocus)
},
// When cloning, we only want to add aria-hidden attributes to the
// DOM and not try to actually trap, as trapping can mess with the
// live DOM and isn't just isolated to the cloned DOM.
(el, { expression, modifiers }, { evaluate }) => {
if (modifiers.includes('inert') && evaluate(expression)) setInert(el)
},
))
}
function setInert(el) {
let undos = []
crawlSiblingsUp(el, (sibling) => {
let cache = sibling.hasAttribute('aria-hidden')
sibling.setAttribute('aria-hidden', 'true')
undos.push(() => cache || sibling.removeAttribute('aria-hidden'))
})
return () => {
while(undos.length) undos.pop()()
}
}
function crawlSiblingsUp(el, callback) {
if (el.isSameNode(document.body) || ! el.parentNode) return
Array.from(el.parentNode.children).forEach(sibling => {
if (! sibling.isSameNode(el)) callback(sibling)
crawlSiblingsUp(el.parentNode, callback)
})
}
function disableScrolling() {
let overflow = document.documentElement.style.overflow
let paddingRight = document.documentElement.style.paddingRight
let scrollbarWidth = window.innerWidth - document.documentElement.clientWidth
document.documentElement.style.overflow = 'hidden'
document.documentElement.style.paddingRight = `${scrollbarWidth}px`
return () => {
document.documentElement.style.overflow = overflow
document.documentElement.style.paddingRight = paddingRight
}
}
|