@dataloop-ai/components 0.20.287 → 0.20.288
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
package/src/assets/globals.scss
CHANGED
|
@@ -521,4 +521,25 @@ html {
|
|
|
521
521
|
font-weight: var(--dl-typography-body-body3-font-weight);
|
|
522
522
|
}
|
|
523
523
|
|
|
524
|
+
// Focus ring for menu items reached through the keyboard (see utils/menu-keyboard-nav.ts)
|
|
525
|
+
.dl-menu .dl-list-item:focus-visible,
|
|
526
|
+
.dl-menu [data-menu-item]:focus-visible,
|
|
527
|
+
.dl-menu .container-wrapper > label.container:focus-visible {
|
|
528
|
+
outline: 2px solid var(--dl-color-secondary);
|
|
529
|
+
outline-offset: -2px;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
.dl-menu .dl-list-item:focus-visible {
|
|
533
|
+
background-color: var(--dl-list-item-hover);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
.dl-menu .dl-switch-checkbox:focus-visible {
|
|
537
|
+
outline: none;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
.dl-menu .dl-switch-checkbox:focus-visible + .dl-switch {
|
|
541
|
+
outline: 2px solid var(--dl-color-secondary);
|
|
542
|
+
outline-offset: 2px;
|
|
543
|
+
}
|
|
544
|
+
|
|
524
545
|
@import 'grid';
|
|
@@ -58,6 +58,7 @@ import {
|
|
|
58
58
|
ClickOutsideEvent
|
|
59
59
|
} from '../../../utils/click-outside'
|
|
60
60
|
import { addFocusFn } from '../../../utils/focus-manager'
|
|
61
|
+
import { attachMenuKeyboardNav } from '../../../utils/menu-keyboard-nav'
|
|
61
62
|
|
|
62
63
|
import {
|
|
63
64
|
validatePosition,
|
|
@@ -105,6 +106,10 @@ export default defineComponent({
|
|
|
105
106
|
autoClose: Boolean,
|
|
106
107
|
|
|
107
108
|
noRefocus: Boolean,
|
|
109
|
+
/**
|
|
110
|
+
* Disables the built-in keyboard navigation between menu items
|
|
111
|
+
*/
|
|
112
|
+
noKeyboardNav: Boolean,
|
|
108
113
|
|
|
109
114
|
fitContainer: Boolean,
|
|
110
115
|
fitContent: Boolean,
|
|
@@ -550,6 +555,30 @@ export default defineComponent({
|
|
|
550
555
|
emit('highlighted-item', value, arrowNavItems.value[value] ?? null)
|
|
551
556
|
})
|
|
552
557
|
|
|
558
|
+
// Menus driven by arrowNavItems keep focus on their input instead
|
|
559
|
+
let detachKeyboardNav: (() => void) | null = null
|
|
560
|
+
const updateKeyboardNav = () => {
|
|
561
|
+
const el = innerRef.value
|
|
562
|
+
if (
|
|
563
|
+
showing.value &&
|
|
564
|
+
el &&
|
|
565
|
+
!props.noKeyboardNav &&
|
|
566
|
+
!arrowNavItems.value?.length
|
|
567
|
+
) {
|
|
568
|
+
if (!detachKeyboardNav) {
|
|
569
|
+
detachKeyboardNav = attachMenuKeyboardNav(el)
|
|
570
|
+
}
|
|
571
|
+
return
|
|
572
|
+
}
|
|
573
|
+
detachKeyboardNav?.()
|
|
574
|
+
detachKeyboardNav = null
|
|
575
|
+
}
|
|
576
|
+
watch([showing, innerRef], updateKeyboardNav, { flush: 'post' })
|
|
577
|
+
onBeforeUnmount(() => {
|
|
578
|
+
detachKeyboardNav?.()
|
|
579
|
+
detachKeyboardNav = null
|
|
580
|
+
})
|
|
581
|
+
|
|
553
582
|
const { zIndex, height, width } = toRefs(props)
|
|
554
583
|
|
|
555
584
|
const computedStyles = computed<any>(() => {
|
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Built-in keyboard support for DlMenu content (and so DlPopup, which renders
|
|
3
|
+
* through DlMenu). DlMenu attaches it to its content element while showing,
|
|
4
|
+
* unless `arrowNavItems` drives the menu or `noKeyboardNav` is set.
|
|
5
|
+
*
|
|
6
|
+
* - Opening a menu from the keyboard moves focus to its first item
|
|
7
|
+
* - ArrowDown / ArrowUp / Home / End move between items
|
|
8
|
+
* - Enter / Space activate the focused item
|
|
9
|
+
* - ArrowRight, Enter or Space on an item with a submenu opens it,
|
|
10
|
+
* ArrowLeft closes it and returns to the parent item
|
|
11
|
+
* - Tab on an item closes the whole menu chain and returns focus to its trigger
|
|
12
|
+
* - Escape is already handled by DlMenu (closes the top-most menu)
|
|
13
|
+
*
|
|
14
|
+
* Keys are only handled while focus is on a menu item (or on the menu itself),
|
|
15
|
+
* so other focusable content in a menu or popup keeps its own key handling.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const ITEM_SELECTOR = [
|
|
19
|
+
'.dl-list-item',
|
|
20
|
+
'[data-menu-item]',
|
|
21
|
+
'.container-wrapper > label.container', // DlRadio
|
|
22
|
+
'input.dl-switch-checkbox' // DlSwitch
|
|
23
|
+
].join(', ')
|
|
24
|
+
const SUBMENU_ICON_SELECTOR = '.icon-dl-right-chevron'
|
|
25
|
+
|
|
26
|
+
interface MenuNavInstance {
|
|
27
|
+
root: HTMLElement
|
|
28
|
+
opener: HTMLElement | null
|
|
29
|
+
parent: MenuNavInstance | null
|
|
30
|
+
onKeydown: (e: KeyboardEvent) => void
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const instances: MenuNavInstance[] = []
|
|
34
|
+
let listenersInstalled = false
|
|
35
|
+
let keyboardMode = false
|
|
36
|
+
// Enter / Space currently held down, used to swallow their keyup (see below)
|
|
37
|
+
let heldActivationKey: string | null = null
|
|
38
|
+
let keyupToSwallow: string | null = null
|
|
39
|
+
let pendingSubmenuFocus = false
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* DlMenu toggles on Enter keyup at its trigger, and native buttons click on
|
|
43
|
+
* Space keyup. When a key press opens a menu or activates an item (which
|
|
44
|
+
* closes the menu and refocuses the trigger), the keyup that follows would
|
|
45
|
+
* immediately toggle the menu again, so it is swallowed at its target.
|
|
46
|
+
*/
|
|
47
|
+
const swallowEvent = (e: Event) => {
|
|
48
|
+
e.preventDefault()
|
|
49
|
+
e.stopImmediatePropagation()
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const onWindowKeydown = (e: KeyboardEvent) => {
|
|
53
|
+
keyboardMode = true
|
|
54
|
+
if (e.key === 'Enter' || e.key === ' ') {
|
|
55
|
+
heldActivationKey = e.key
|
|
56
|
+
}
|
|
57
|
+
if (e.key !== 'ArrowDown' && e.key !== 'ArrowUp') {
|
|
58
|
+
return
|
|
59
|
+
}
|
|
60
|
+
// Menu opened with the mouse: arrows move focus into it, as long as focus
|
|
61
|
+
// is still on its trigger (or nowhere)
|
|
62
|
+
const top = instances[instances.length - 1]
|
|
63
|
+
const active = document.activeElement
|
|
64
|
+
if (
|
|
65
|
+
top &&
|
|
66
|
+
(!active || active === document.body || active === top.opener) &&
|
|
67
|
+
!isTextField(active as HTMLElement) &&
|
|
68
|
+
getItems(top.root).length
|
|
69
|
+
) {
|
|
70
|
+
e.preventDefault()
|
|
71
|
+
focusItem(top, e.key === 'ArrowDown' ? 0 : -1)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const onWindowKeyup = (e: KeyboardEvent) => {
|
|
76
|
+
if (e.key === heldActivationKey) {
|
|
77
|
+
heldActivationKey = null
|
|
78
|
+
}
|
|
79
|
+
if (keyupToSwallow && e.key === keyupToSwallow) {
|
|
80
|
+
keyupToSwallow = null
|
|
81
|
+
e.target?.addEventListener('keyup', swallowEvent, {
|
|
82
|
+
capture: true,
|
|
83
|
+
once: true
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const onWindowPointerdown = () => {
|
|
89
|
+
keyboardMode = false
|
|
90
|
+
pendingSubmenuFocus = false
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const installListeners = () => {
|
|
94
|
+
if (listenersInstalled) {
|
|
95
|
+
return
|
|
96
|
+
}
|
|
97
|
+
listenersInstalled = true
|
|
98
|
+
window.addEventListener('keydown', onWindowKeydown, true)
|
|
99
|
+
window.addEventListener('keyup', onWindowKeyup, true)
|
|
100
|
+
window.addEventListener('pointerdown', onWindowPointerdown, true)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const isTextField = (el: HTMLElement | null) => {
|
|
104
|
+
if (!el) {
|
|
105
|
+
return false
|
|
106
|
+
}
|
|
107
|
+
if (el.isContentEditable || el.tagName === 'TEXTAREA') {
|
|
108
|
+
return true
|
|
109
|
+
}
|
|
110
|
+
return (
|
|
111
|
+
el.tagName === 'INPUT' &&
|
|
112
|
+
!['checkbox', 'radio'].includes((el as HTMLInputElement).type)
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const isListItem = (el: HTMLElement) => {
|
|
117
|
+
return el.classList.contains('dl-list-item')
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const isClickableListItem = (el: HTMLElement) => {
|
|
121
|
+
// DlListItem exposes clickable && !disabled only through this inline var
|
|
122
|
+
return el.style.getPropertyValue('--dl-list-item-cursor') === 'pointer'
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const isEnabled = (el: HTMLElement) => {
|
|
126
|
+
if (isListItem(el)) {
|
|
127
|
+
return isClickableListItem(el)
|
|
128
|
+
}
|
|
129
|
+
if (el.tagName === 'INPUT') {
|
|
130
|
+
return !(el as HTMLInputElement).disabled
|
|
131
|
+
}
|
|
132
|
+
return (
|
|
133
|
+
!el.classList.contains('disabled') &&
|
|
134
|
+
el.getAttribute('aria-disabled') !== 'true'
|
|
135
|
+
)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const hasSubmenu = (el: HTMLElement) => {
|
|
139
|
+
return isListItem(el) && !!el.querySelector(SUBMENU_ICON_SELECTOR)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const getItems = (root: HTMLElement): HTMLElement[] => {
|
|
143
|
+
const candidates = Array.from(
|
|
144
|
+
root.querySelectorAll<HTMLElement>(ITEM_SELECTOR)
|
|
145
|
+
)
|
|
146
|
+
const items = candidates.filter(
|
|
147
|
+
(el) =>
|
|
148
|
+
isEnabled(el) &&
|
|
149
|
+
el.getClientRects().length > 0 &&
|
|
150
|
+
// controls inside a clickable row are reached through the row
|
|
151
|
+
!candidates.some(
|
|
152
|
+
(other) =>
|
|
153
|
+
other !== el &&
|
|
154
|
+
isListItem(other) &&
|
|
155
|
+
isClickableListItem(other) &&
|
|
156
|
+
other.contains(el)
|
|
157
|
+
)
|
|
158
|
+
)
|
|
159
|
+
for (const item of items) {
|
|
160
|
+
if (isListItem(item)) {
|
|
161
|
+
item.setAttribute('role', 'menuitem')
|
|
162
|
+
if (!item.hasAttribute('tabindex')) {
|
|
163
|
+
item.setAttribute('tabindex', '-1')
|
|
164
|
+
}
|
|
165
|
+
if (hasSubmenu(item)) {
|
|
166
|
+
item.setAttribute('aria-haspopup', 'menu')
|
|
167
|
+
}
|
|
168
|
+
} else if (!item.hasAttribute('tabindex')) {
|
|
169
|
+
item.setAttribute('tabindex', '-1')
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return items
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const focusItem = (instance: MenuNavInstance, index: number) => {
|
|
176
|
+
const items = getItems(instance.root)
|
|
177
|
+
if (!items.length) {
|
|
178
|
+
return
|
|
179
|
+
}
|
|
180
|
+
const wrapped = ((index % items.length) + items.length) % items.length
|
|
181
|
+
items[wrapped].focus({ preventScroll: true })
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const childOf = (instance: MenuNavInstance, opener: HTMLElement) => {
|
|
185
|
+
return instances.find((i) => i.parent === instance && i.opener === opener)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Submenus open on hover (mouseenter -> show()), which is idempotent,
|
|
190
|
+
* unlike the anchor click handler that toggles.
|
|
191
|
+
*/
|
|
192
|
+
const openSubmenu = (instance: MenuNavInstance, item: HTMLElement) => {
|
|
193
|
+
const child = childOf(instance, item)
|
|
194
|
+
if (child) {
|
|
195
|
+
focusItem(child, 0)
|
|
196
|
+
return
|
|
197
|
+
}
|
|
198
|
+
pendingSubmenuFocus = true
|
|
199
|
+
const hoverTarget =
|
|
200
|
+
(item.closest('.list-item-wrapper') as HTMLElement) || item
|
|
201
|
+
hoverTarget.dispatchEvent(new MouseEvent('mouseenter'))
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** DlMenu only closes from its own escape-key handler, so reuse it. */
|
|
205
|
+
const dispatchEscape = () => {
|
|
206
|
+
for (const type of ['keydown', 'keyup']) {
|
|
207
|
+
const evt = new KeyboardEvent(type, { key: 'Escape' })
|
|
208
|
+
Object.defineProperty(evt, 'keyCode', { get: () => 27 })
|
|
209
|
+
window.dispatchEvent(evt)
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const nextFrame = () => {
|
|
214
|
+
return new Promise((resolve) => requestAnimationFrame(resolve))
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const closeSubmenu = (instance: MenuNavInstance) => {
|
|
218
|
+
const parentItem = instance.opener
|
|
219
|
+
dispatchEscape()
|
|
220
|
+
requestAnimationFrame(() => {
|
|
221
|
+
if (parentItem?.isConnected) {
|
|
222
|
+
parentItem.focus({ preventScroll: true })
|
|
223
|
+
}
|
|
224
|
+
})
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const closeChain = async (instance: MenuNavInstance) => {
|
|
228
|
+
const chain: MenuNavInstance[] = []
|
|
229
|
+
for (let i = instance; i; i = i.parent) {
|
|
230
|
+
chain.push(i)
|
|
231
|
+
}
|
|
232
|
+
const trigger = chain[chain.length - 1].opener
|
|
233
|
+
// A v-model bound menu hides asynchronously, so close one level per frame
|
|
234
|
+
for (const level of chain) {
|
|
235
|
+
if (!instances.includes(level)) {
|
|
236
|
+
continue
|
|
237
|
+
}
|
|
238
|
+
dispatchEscape()
|
|
239
|
+
await nextFrame()
|
|
240
|
+
}
|
|
241
|
+
if (trigger?.isConnected) {
|
|
242
|
+
trigger.focus({ preventScroll: true })
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const onMenuKeydown = (instance: MenuNavInstance, e: KeyboardEvent) => {
|
|
247
|
+
const target = e.target as HTMLElement
|
|
248
|
+
if (e.defaultPrevented || isTextField(target)) {
|
|
249
|
+
return
|
|
250
|
+
}
|
|
251
|
+
const items = getItems(instance.root)
|
|
252
|
+
const current = items.find((i) => i === target || i.contains(target))
|
|
253
|
+
// Other focusable content (buttons, selects, editors...) keeps its keys
|
|
254
|
+
if (!items.length || (!current && target !== instance.root)) {
|
|
255
|
+
return
|
|
256
|
+
}
|
|
257
|
+
const index = current ? items.indexOf(current) : -1
|
|
258
|
+
|
|
259
|
+
switch (e.key) {
|
|
260
|
+
case 'ArrowDown':
|
|
261
|
+
focusItem(instance, index + 1)
|
|
262
|
+
break
|
|
263
|
+
case 'ArrowUp':
|
|
264
|
+
focusItem(instance, index < 0 ? -1 : index - 1)
|
|
265
|
+
break
|
|
266
|
+
case 'Home':
|
|
267
|
+
focusItem(instance, 0)
|
|
268
|
+
break
|
|
269
|
+
case 'End':
|
|
270
|
+
focusItem(instance, -1)
|
|
271
|
+
break
|
|
272
|
+
case 'ArrowRight':
|
|
273
|
+
if (!current || !hasSubmenu(current)) {
|
|
274
|
+
return
|
|
275
|
+
}
|
|
276
|
+
openSubmenu(instance, current)
|
|
277
|
+
break
|
|
278
|
+
case 'ArrowLeft':
|
|
279
|
+
if (!instance.parent) {
|
|
280
|
+
return
|
|
281
|
+
}
|
|
282
|
+
closeSubmenu(instance)
|
|
283
|
+
break
|
|
284
|
+
case 'Enter':
|
|
285
|
+
case ' ':
|
|
286
|
+
if (!current) {
|
|
287
|
+
return
|
|
288
|
+
}
|
|
289
|
+
// A focused switch toggles natively on Space
|
|
290
|
+
if (current.tagName === 'INPUT' && e.key === ' ') {
|
|
291
|
+
return
|
|
292
|
+
}
|
|
293
|
+
keyupToSwallow = e.key
|
|
294
|
+
if (hasSubmenu(current)) {
|
|
295
|
+
openSubmenu(instance, current)
|
|
296
|
+
} else {
|
|
297
|
+
current.click()
|
|
298
|
+
}
|
|
299
|
+
break
|
|
300
|
+
case 'Tab':
|
|
301
|
+
if (!current) {
|
|
302
|
+
return
|
|
303
|
+
}
|
|
304
|
+
closeChain(instance)
|
|
305
|
+
break
|
|
306
|
+
default:
|
|
307
|
+
return
|
|
308
|
+
}
|
|
309
|
+
e.preventDefault()
|
|
310
|
+
e.stopPropagation()
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Enables keyboard navigation for a menu content element.
|
|
315
|
+
* Call when the menu content is rendered; returns the cleanup function.
|
|
316
|
+
*/
|
|
317
|
+
export const attachMenuKeyboardNav = (root: HTMLElement): (() => void) => {
|
|
318
|
+
installListeners()
|
|
319
|
+
const opener =
|
|
320
|
+
document.activeElement instanceof HTMLElement
|
|
321
|
+
? document.activeElement
|
|
322
|
+
: null
|
|
323
|
+
const parent =
|
|
324
|
+
[...instances]
|
|
325
|
+
.reverse()
|
|
326
|
+
.find((i) => opener && i.root.contains(opener)) || null
|
|
327
|
+
const instance: MenuNavInstance = {
|
|
328
|
+
root,
|
|
329
|
+
opener,
|
|
330
|
+
parent,
|
|
331
|
+
onKeydown: (e) => onMenuKeydown(instance, e)
|
|
332
|
+
}
|
|
333
|
+
instances.push(instance)
|
|
334
|
+
root.addEventListener('keydown', instance.onKeydown)
|
|
335
|
+
if (!root.hasAttribute('role')) {
|
|
336
|
+
root.setAttribute('role', 'menu')
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// Opened by an Enter keydown: its keyup must not toggle it closed
|
|
340
|
+
if (heldActivationKey === 'Enter') {
|
|
341
|
+
keyupToSwallow = 'Enter'
|
|
342
|
+
}
|
|
343
|
+
const focusFirst =
|
|
344
|
+
(parent ? pendingSubmenuFocus : keyboardMode) &&
|
|
345
|
+
!root.querySelector('[autofocus], [data-autofocus]')
|
|
346
|
+
if (parent) {
|
|
347
|
+
pendingSubmenuFocus = false
|
|
348
|
+
}
|
|
349
|
+
if (focusFirst) {
|
|
350
|
+
requestAnimationFrame(() => {
|
|
351
|
+
if (instances.includes(instance)) {
|
|
352
|
+
focusItem(instance, 0)
|
|
353
|
+
}
|
|
354
|
+
})
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
return () => {
|
|
358
|
+
root.removeEventListener('keydown', instance.onKeydown)
|
|
359
|
+
const index = instances.indexOf(instance)
|
|
360
|
+
if (index > -1) {
|
|
361
|
+
instances.splice(index, 1)
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|