Skip to content

Commit a9cdc13

Browse files
authored
Merge pull request #1790 from Marghen/fix-cv-dropdown-accessibility-problems
feat: fix CvDropdown accessibility problems with focus and blur
2 parents 04e1fc8 + 50b2cdb commit a9cdc13

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/components/CvDropdown/CvDropdown.vue

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,13 @@ const wrapperStyleOverride = computed(() => {
323323
});
324324
const el = ref(null);
325325
function onClickOut() {
326+
// Move focus to button if focus is inside dropdown list
327+
const dropListEl = dropList.value;
328+
if (dropListEl && dropListEl.contains(document.activeElement)) {
329+
if (button.value && typeof button.value.focus === 'function') {
330+
button.value.focus();
331+
}
332+
}
326333
open.value = false;
327334
}
328335
onClickOutside(el, onClickOut);
@@ -372,8 +379,14 @@ function onUp() {
372379
}
373380
}
374381
function onEsc() {
382+
// Move focus to button if focus is inside dropdown list
383+
const dropListEl = dropList.value;
384+
if (dropListEl && dropListEl.contains(document.activeElement)) {
385+
if (button.value && typeof button.value.focus === 'function') {
386+
button.value.focus();
387+
}
388+
}
375389
open.value = false;
376-
doFocus();
377390
}
378391
const button = ref(null);
379392
const listBox = ref(null);
@@ -384,8 +397,10 @@ function onTab(ev) {
384397
open.value = false;
385398
} else if (ev.target === null || listBox.value?.contains(ev.target)) {
386399
// list has focus, close and return focus to dropdown
400+
if (button.value && typeof button.value.focus === 'function') {
401+
button.value.focus();
402+
}
387403
open.value = false;
388-
doFocus();
389404
ev.preventDefault();
390405
}
391406
}
@@ -394,9 +409,6 @@ function onClick(ev) {
394409
if (props.disabled) {
395410
ev.preventDefault();
396411
} else {
397-
open.value = !open.value;
398-
doFocus(); // open or close (Some browsers do not focus on button when clicked)
399-
400412
let target = ev.target;
401413
while (
402414
!target.classList.contains(`${carbonPrefix}--dropdown-link`) &&
@@ -408,6 +420,14 @@ function onClick(ev) {
408420
if (target.classList.contains(`${carbonPrefix}--dropdown-link`)) {
409421
const targetItemEl = target.parentNode;
410422
dataValue.value = targetItemEl.getAttribute('data-value');
423+
// Move focus to button before closing
424+
if (button.value && typeof button.value.focus === 'function') {
425+
button.value.focus();
426+
}
427+
open.value = false;
428+
} else {
429+
open.value = !open.value;
430+
doFocus(); // open or close (Some browsers do not focus on button when clicked)
411431
}
412432
}
413433
}

0 commit comments

Comments
 (0)