1- import React from 'react' ;
1+ import React , { useRef } from 'react' ;
22import PropTypes from 'prop-types' ;
3-
43import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' ;
54import { faUserCircle } from '@fortawesome/free-solid-svg-icons' ;
65import { getConfig } from '@edx/frontend-platform' ;
@@ -12,6 +11,30 @@ import LearningUserMenuSlot from '../plugin-slots/LearningUserMenuSlot';
1211import messages from './messages' ;
1312
1413const AuthenticatedUserDropdown = ( { intl, username } ) => {
14+ const firstMenuItemRef = useRef ( null ) ;
15+ const lastMenuItemRef = useRef ( null ) ;
16+
17+ const handleKeyDown = ( event ) => {
18+ if ( event . key === 'Tab' ) {
19+ event . preventDefault ( ) ;
20+
21+ const isShiftTab = event . shiftKey ;
22+ const currentElement = document . activeElement ;
23+ const focusElement = isShiftTab
24+ ? currentElement . previousElementSibling
25+ : currentElement . nextElementSibling ;
26+
27+ // If the element has reached the start or end of the list, loop the focus
28+ if ( isShiftTab && currentElement === firstMenuItemRef . current ) {
29+ lastMenuItemRef . current . focus ( ) ;
30+ } else if ( ! isShiftTab && currentElement === lastMenuItemRef . current ) {
31+ firstMenuItemRef . current . focus ( ) ;
32+ } else if ( focusElement && focusElement . tagName === 'A' ) {
33+ focusElement . focus ( ) ;
34+ }
35+ }
36+ } ;
37+
1538 const dropdownItems = [
1639 {
1740 message : intl . formatMessage ( messages . dashboard ) ,
@@ -43,8 +66,13 @@ const AuthenticatedUserDropdown = ({ intl, username }) => {
4366 { username }
4467 </ span >
4568 </ Dropdown . Toggle >
46- < Dropdown . Menu className = "dropdown-menu-right" >
47- < LearningUserMenuSlot items = { dropdownItems } />
69+ < Dropdown . Menu className = "dropdown-menu-right" role = "menu" >
70+ < LearningUserMenuSlot
71+ items = { dropdownItems }
72+ firstMenuItemRef = { firstMenuItemRef }
73+ lastMenuItemRef = { lastMenuItemRef }
74+ handleKeyDown = { handleKeyDown }
75+ />
4876 </ Dropdown . Menu >
4977 </ Dropdown >
5078 ) ;
0 commit comments