-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathFocusLock.vue
executable file
·206 lines (164 loc) · 4.63 KB
/
FocusLock.vue
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
198
199
200
201
202
203
204
205
206
<template>
<div ref="rootEl">
<div v-if="hasLeadingGuards" :tabIndex="disabled ? -1 : 0" aria-hidden="true" data-focus-guard></div>
<div @focusout="onBlur" v-bind="groupAttr" data-lock>
<slot></slot>
</div>
<div v-if="hasTailingGuards" :tabIndex="disabled ? -1 : 0" aria-hidden="true" data-focus-guard></div>
</div>
</template>
<script setup lang="ts">
import {
computed,
getCurrentInstance,
onMounted,
onUnmounted,
ref,
toRefs,
watch,
type ComponentInternalInstance
} from 'vue'
import { focusInside, focusIsHidden, moveFocusInside } from 'focus-lock'
import { constants } from 'focus-lock'
interface VueFocusLockProps {
/**
* controls focus restoration behavior.
* @default false, but is expected to be enabled almost always
**/
returnFocus?: boolean
/**
* disables the lock
* @default false
**/
disabled?: boolean
/**
* removes virtual nodes around the lock
* @default false
**/
noFocusGuards?: boolean | 'tail'
/**
* specifies a group for the lock
* other nodes can be added to the group by adding similar `[constants.FOCUS_GROUP]`('data-focus-lock') attribute
**/
group?: string
}
interface LockState {
instance?: ComponentInternalInstance['proxy']
observed?: HTMLElement | null
disabled: boolean
onActivation: () => void
}
let instances: LockState[] = []
let originalFocusedElement: Element | HTMLElement
const props = defineProps<VueFocusLockProps>()
const { returnFocus, disabled, noFocusGuards, group } = toRefs(props)
const rootEl = ref<HTMLDivElement | null>(null)
const data = ref<LockState>({ disabled: true, onActivation: () => {} })
const groupAttr = computed(() => {
return { [constants.FOCUS_GROUP]: group.value }
})
const hasLeadingGuards = computed(() => {
return noFocusGuards.value !== true
})
const hasTailingGuards = computed(() => {
return hasLeadingGuards.value && noFocusGuards.value !== 'tail'
})
watch(disabled, () => {
data.value.disabled = disabled.value
emitChange()
})
onMounted(() => {
const currentInstance = getCurrentInstance()
if (!currentInstance) {
return
}
data.value = {
instance: currentInstance.proxy,
observed: rootEl.value!.querySelector<HTMLElement>('[data-lock]'),
disabled: disabled.value,
onActivation: () => {
originalFocusedElement = originalFocusedElement || (document && document.activeElement)
}
}
if (!instances.length) {
attachHandler()
}
instances.push(data.value)
emitChange()
})
onUnmounted(() => {
const currentInstance = getCurrentInstance()
if (!currentInstance) {
return
}
instances = instances.filter(({ instance }) => instance !== currentInstance.proxy)
if (!instances.length) {
detachHandler()
}
if (
returnFocus.value &&
originalFocusedElement &&
(originalFocusedElement as HTMLElement).focus
) {
;(originalFocusedElement as HTMLElement).focus()
}
emitChange()
})
function deferAction(action: () => void) {
setTimeout(action, 0)
}
let lastActiveTrap: LockState | null = null
let lastActiveFocus: Element | null = null
let focusWasOutsideWindow = false
const focusOnBody = () => document && document.activeElement === document.body
const isFreeFocus = () => focusOnBody() || focusIsHidden()
const activateTrap = () => {
if (lastActiveTrap) {
const { observed, onActivation } = lastActiveTrap
if (focusWasOutsideWindow || !isFreeFocus() || !lastActiveFocus) {
if (observed && !focusInside(observed)) {
onActivation()
moveFocusInside(observed, lastActiveFocus as Element)
}
focusWasOutsideWindow = false
lastActiveFocus = document && document.activeElement
}
}
}
const reducePropsToState = (propsList: LockState[]): LockState => {
return propsList.filter(({ disabled }) => !disabled).slice(-1)[0]
}
const handleStateChangeOnClient = (trap: LockState) => {
if (lastActiveTrap !== trap) {
lastActiveTrap = null
}
lastActiveTrap = trap
if (trap) {
activateTrap()
deferAction(activateTrap)
}
}
const emitChange = () => {
handleStateChangeOnClient(reducePropsToState(instances))
}
const onTrap = () => {
activateTrap()
}
const onBlur = () => {
deferAction(activateTrap)
}
const onWindowBlur = () => {
focusWasOutsideWindow = true
lastActiveFocus = null
}
const attachHandler = () => {
document.addEventListener('focusin', onTrap, true)
document.addEventListener('focusout', onBlur)
window.addEventListener('blur', onWindowBlur)
}
const detachHandler = () => {
document.removeEventListener('focusin', onTrap, true)
document.removeEventListener('focusout', onBlur)
window.removeEventListener('blur', onWindowBlur)
}
</script>