@dataloop-ai/components 0.20.288 → 0.20.290
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/.nvmrc +1 -1
- package/package.json +2 -2
- package/src/assets/globals.scss +0 -21
- package/src/components/basic/DlPopup/DlPopup.vue +0 -1
- package/src/components/compound/DlCharts/charts/DlColumnChart/DlColumnChart.vue +1 -1
- package/src/components/compound/DlCharts/charts/DlDoughnutChart/DlDoughnutChart.vue +1 -1
- package/src/components/compound/DlCharts/charts/DlLineChart/DlLineChart.vue +5 -17
- package/src/components/compound/DlCharts/charts/DlScatterChart/DlScatterChart.vue +5 -17
- package/src/components/compound/DlDateTime/DlDatePicker/DlDatePicker.vue +6 -2
- package/src/components/essential/DlMenu/DlMenu.vue +0 -29
- package/src/utils/menu-keyboard-nav.ts +0 -364
package/.nvmrc
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
v24.22.1
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dataloop-ai/components",
|
|
3
|
-
"version": "0.20.
|
|
3
|
+
"version": "0.20.290",
|
|
4
4
|
"exports": {
|
|
5
5
|
".": "./index.ts",
|
|
6
6
|
"./models": "./models.ts",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"check-only": "node misc/check-only.js"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@dataloop-ai/icons": "^3.1.
|
|
26
|
+
"@dataloop-ai/icons": "^3.1.122",
|
|
27
27
|
"@monaco-editor/loader": "^1.4.0",
|
|
28
28
|
"@types/flat": "^5.0.2",
|
|
29
29
|
"@types/lodash": "^4.14.184",
|
package/src/assets/globals.scss
CHANGED
|
@@ -521,25 +521,4 @@ 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
|
-
|
|
545
524
|
@import 'grid';
|
|
@@ -175,7 +175,7 @@ export default defineComponent({
|
|
|
175
175
|
...CommonProps,
|
|
176
176
|
...ColumnChartProps
|
|
177
177
|
} as { [key: string]: any },
|
|
178
|
-
setup(props) {
|
|
178
|
+
setup(props: Record<string, any>) {
|
|
179
179
|
const { variables } = useThemeVariables()
|
|
180
180
|
|
|
181
181
|
const chartWidth = ref('100%')
|
|
@@ -99,7 +99,7 @@ import DlEmptyState from '../../../../basic/DlEmptyState/DlEmptyState.vue'
|
|
|
99
99
|
import { DlEmptyStateProps } from '../../../../basic/DlEmptyState/types'
|
|
100
100
|
|
|
101
101
|
type SetupParams = {
|
|
102
|
-
attrs: { onItemClick?: (index: number) => void }
|
|
102
|
+
attrs: { onItemClick?: (index: number) => void; [key: string]: unknown }
|
|
103
103
|
listeners?: { itemClick?: (index: number) => void }
|
|
104
104
|
}
|
|
105
105
|
|
|
@@ -1,20 +1,8 @@
|
|
|
1
1
|
<template>
|
|
2
|
-
<div
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
<dl-empty-state
|
|
7
|
-
v-if="isEmpty"
|
|
8
|
-
v-bind="emptyStateProps"
|
|
9
|
-
>
|
|
10
|
-
<template
|
|
11
|
-
v-for="(_, slot) in $slots"
|
|
12
|
-
#[slot]="props"
|
|
13
|
-
>
|
|
14
|
-
<slot
|
|
15
|
-
:name="slot"
|
|
16
|
-
v-bind="props"
|
|
17
|
-
/>
|
|
2
|
+
<div :style="cssVars" :class="chartWrapperClasses">
|
|
3
|
+
<dl-empty-state v-if="isEmpty" v-bind="emptyStateProps">
|
|
4
|
+
<template v-for="(_, slot) in $slots" #[slot]="props">
|
|
5
|
+
<slot :name="slot" v-bind="props" />
|
|
18
6
|
</template>
|
|
19
7
|
</dl-empty-state>
|
|
20
8
|
<DlLine
|
|
@@ -186,7 +174,7 @@ export default defineComponent({
|
|
|
186
174
|
...CommonProps,
|
|
187
175
|
...ColumnChartProps
|
|
188
176
|
} as { [key: string]: any },
|
|
189
|
-
setup(props) {
|
|
177
|
+
setup(props: Record<string, any>) {
|
|
190
178
|
const { variables } = useThemeVariables()
|
|
191
179
|
const {
|
|
192
180
|
data,
|
|
@@ -1,20 +1,8 @@
|
|
|
1
1
|
<template>
|
|
2
|
-
<div
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
<dl-empty-state
|
|
7
|
-
v-if="isEmpty"
|
|
8
|
-
v-bind="emptyStateProps"
|
|
9
|
-
>
|
|
10
|
-
<template
|
|
11
|
-
v-for="(_, slot) in $slots"
|
|
12
|
-
#[slot]="props"
|
|
13
|
-
>
|
|
14
|
-
<slot
|
|
15
|
-
:name="slot"
|
|
16
|
-
v-bind="props"
|
|
17
|
-
/>
|
|
2
|
+
<div :style="cssVars" :class="chartWrapperClasses">
|
|
3
|
+
<dl-empty-state v-if="isEmpty" v-bind="emptyStateProps">
|
|
4
|
+
<template v-for="(_, slot) in $slots" #[slot]="props">
|
|
5
|
+
<slot :name="slot" v-bind="props" />
|
|
18
6
|
</template>
|
|
19
7
|
</dl-empty-state>
|
|
20
8
|
<DLScatter
|
|
@@ -178,7 +166,7 @@ export default defineComponent({
|
|
|
178
166
|
...CommonProps,
|
|
179
167
|
...ColumnChartProps
|
|
180
168
|
} as { [key: string]: any },
|
|
181
|
-
setup(props) {
|
|
169
|
+
setup(props: Record<string, any>) {
|
|
182
170
|
const { variables } = useThemeVariables()
|
|
183
171
|
const {
|
|
184
172
|
data,
|
|
@@ -189,8 +189,12 @@ export default defineComponent({
|
|
|
189
189
|
this.calendarTo.activeDate = cDateTo
|
|
190
190
|
|
|
191
191
|
if (this.activeDateFrom && this.activeDateTo) {
|
|
192
|
-
this.calendarFrom.activeDate = new CalendarDate(
|
|
193
|
-
|
|
192
|
+
this.calendarFrom.activeDate = new CalendarDate(
|
|
193
|
+
this.activeDateFrom as CalendarDate
|
|
194
|
+
)
|
|
195
|
+
this.calendarTo.activeDate = new CalendarDate(
|
|
196
|
+
this.activeDateTo as CalendarDate
|
|
197
|
+
)
|
|
194
198
|
}
|
|
195
199
|
},
|
|
196
200
|
beforeUnmount() {
|
|
@@ -58,7 +58,6 @@ 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'
|
|
62
61
|
|
|
63
62
|
import {
|
|
64
63
|
validatePosition,
|
|
@@ -106,10 +105,6 @@ export default defineComponent({
|
|
|
106
105
|
autoClose: Boolean,
|
|
107
106
|
|
|
108
107
|
noRefocus: Boolean,
|
|
109
|
-
/**
|
|
110
|
-
* Disables the built-in keyboard navigation between menu items
|
|
111
|
-
*/
|
|
112
|
-
noKeyboardNav: Boolean,
|
|
113
108
|
|
|
114
109
|
fitContainer: Boolean,
|
|
115
110
|
fitContent: Boolean,
|
|
@@ -555,30 +550,6 @@ export default defineComponent({
|
|
|
555
550
|
emit('highlighted-item', value, arrowNavItems.value[value] ?? null)
|
|
556
551
|
})
|
|
557
552
|
|
|
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
|
-
|
|
582
553
|
const { zIndex, height, width } = toRefs(props)
|
|
583
554
|
|
|
584
555
|
const computedStyles = computed<any>(() => {
|
|
@@ -1,364 +0,0 @@
|
|
|
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
|
-
}
|