@@ -323,6 +323,13 @@ const wrapperStyleOverride = computed(() => {
323323});
324324const el = ref (null );
325325function 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}
328335onClickOutside (el, onClickOut);
@@ -372,8 +379,14 @@ function onUp() {
372379 }
373380}
374381function 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}
378391const button = ref (null );
379392const 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