1
- import React from 'react' ;
1
+ import React , { useRef } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
-
4
3
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' ;
5
4
import { faUserCircle } from '@fortawesome/free-solid-svg-icons' ;
6
5
import { getConfig } from '@edx/frontend-platform' ;
@@ -12,6 +11,30 @@ import LearningUserMenuSlot from '../plugin-slots/LearningUserMenuSlot';
12
11
import messages from './messages' ;
13
12
14
13
const 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
+
15
38
const dropdownItems = [
16
39
{
17
40
message : intl . formatMessage ( messages . dashboard ) ,
@@ -43,8 +66,13 @@ const AuthenticatedUserDropdown = ({ intl, username }) => {
43
66
{ username }
44
67
</ span >
45
68
</ 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
+ />
48
76
</ Dropdown . Menu >
49
77
</ Dropdown >
50
78
) ;
0 commit comments