Skip to content

Commit f04ecf0

Browse files
committed
feat(datepicker): Tab through focus-trapped navElements
1 parent 5bbf131 commit f04ecf0

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/components/Datepicker.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<template>
2-
<div class="vdp-datepicker" :class="[wrapperClass, { rtl: isRtl }]">
2+
<div
3+
class="vdp-datepicker"
4+
:class="[wrapperClass, { rtl: isRtl }]"
5+
@keydown.tab="tabThroughNavigation($event)"
6+
>
37
<DateInput
48
:id="id"
59
ref="dateInput"

src/mixins/navMixin.vue

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export default {
44
return {
55
navElements: [],
66
tabbableCell: null,
7+
navElementsFocusedIndex: 0,
78
}
89
},
910
methods: {
@@ -111,6 +112,47 @@ export default {
111112
pickerCells.querySelector('button.today:not(.muted):enabled') ||
112113
pickerCells.querySelector('button.cell:not(.muted):enabled')
113114
},
115+
/**
116+
* Tab backwards through the focus-trapped elements
117+
*/
118+
tabBackwards() {
119+
this.navElementsFocusedIndex -= 1
120+
121+
if (this.navElementsFocusedIndex < 0) {
122+
this.navElementsFocusedIndex = this.navElements.length - 1
123+
}
124+
125+
this.navElements[this.navElementsFocusedIndex].focus()
126+
},
127+
/**
128+
* Tab forwards through the focus-trapped elements
129+
*/
130+
tabForwards() {
131+
this.navElementsFocusedIndex += 1
132+
133+
if (this.navElementsFocusedIndex >= this.navElements.length) {
134+
this.navElementsFocusedIndex = 0
135+
}
136+
137+
this.navElements[this.navElementsFocusedIndex].focus()
138+
},
139+
/**
140+
* Tab through the focus-trapped elements
141+
* @param event
142+
*/
143+
tabThroughNavigation(event) {
144+
// Allow normal tabbing when closed
145+
if (!this.isOpen) {
146+
return
147+
}
148+
event.preventDefault()
149+
150+
if (event.shiftKey) {
151+
this.tabBackwards()
152+
} else {
153+
this.tabForwards()
154+
}
155+
},
114156
},
115157
}
116158
</script>

0 commit comments

Comments
 (0)