@@ -11,16 +11,12 @@ import { MenuOpenContext, MenubarContext } from './contexts';
1111import usePrevious from '../../common/usePrevious' ;
1212
1313/**
14- * @component
15- * @param {object } props
14+ * Menubar manages a collection of menu items and their submenus. It provides keyboard navigation,
15+ * focus and state management, and other accessibility features for the menu items and submenus.
16+ *
1617 * @param {React.ReactNode } props.children - Menu items that will be rendered in the menubar
1718 * @param {string } [props.className='nav__menubar'] - CSS class name to apply to the menubar
1819 * @returns {JSX.Element }
19- */
20-
21- /**
22- * Menubar manages a collection of menu items and their submenus. It provides keyboard navigation,
23- * focus and state management, and other accessibility features for the menu items and submenus.
2420 *
2521 * @example
2622 * <Menubar>
@@ -31,20 +27,16 @@ import usePrevious from '../../common/usePrevious';
3127 */
3228
3329function Menubar ( { children, className } ) {
34- // core state for menu management
3530 const [ menuOpen , setMenuOpen ] = useState ( 'none' ) ;
3631 const [ activeIndex , setActiveIndex ] = useState ( 0 ) ;
3732 const prevIndex = usePrevious ( activeIndex ) ;
3833 const [ hasFocus , setHasFocus ] = useState ( false ) ;
3934
40- // refs for menu items and their ids
4135 const menuItems = useRef ( new Set ( ) ) . current ;
4236 const menuItemToId = useRef ( new Map ( ) ) . current ;
4337
44- // ref for hiding submenus
4538 const timerRef = useRef ( null ) ;
4639
47- // get the id of a menu item by its index
4840 const getMenuId = useCallback (
4941 ( index ) => {
5042 const items = Array . from ( menuItems ) ;
@@ -54,9 +46,6 @@ function Menubar({ children, className }) {
5446 [ menuItems , menuItemToId , activeIndex ]
5547 ) ;
5648
57- /**
58- * navigation functions
59- */
6049 const prev = useCallback ( ( ) => {
6150 const newIndex = ( activeIndex - 1 + menuItems . size ) % menuItems . size ;
6251 setActiveIndex ( newIndex ) ;
@@ -85,8 +74,6 @@ function Menubar({ children, className }) {
8574 setActiveIndex ( menuItems . size - 1 ) ;
8675 } , [ ] ) ;
8776
88- // closes the menu and returns focus to the active menu item
89- // is called on Escape key press
9077 const close = useCallback ( ( ) => {
9178 if ( menuOpen === 'none' ) return ;
9279
@@ -96,27 +83,17 @@ function Menubar({ children, className }) {
9683 activeNode . focus ( ) ;
9784 } , [ activeIndex , menuItems , menuOpen ] ) ;
9885
99- // toggle the open state of a submenu
10086 const toggleMenuOpen = useCallback ( ( id ) => {
10187 setMenuOpen ( ( prevState ) => ( prevState === id ? 'none' : id ) ) ;
10288 } ) ;
10389
104- /**
105- * Register top level menu items. Stores both the DOM node and the id of the submenu.
106- * Access to the DOM node is needed for focus management and tabindex control,
107- * while the id is needed to toggle the submenu open and closed.
108- *
109- * @param {React.RefObject } ref - a ref to the DOM node of the menu item
110- * @param {string } submenuId - the id of the submenu that the menu item opens
111- *
112- */
11390 const registerTopLevelItem = useCallback (
11491 ( ref , submenuId ) => {
11592 const menuItemNode = ref . current ;
11693
11794 if ( menuItemNode ) {
11895 menuItems . add ( menuItemNode ) ;
119- menuItemToId . set ( menuItemNode , submenuId ) ; // store the id of the submenu
96+ menuItemToId . set ( menuItemNode , submenuId ) ;
12097 }
12198
12299 return ( ) => {
@@ -127,9 +104,6 @@ function Menubar({ children, className }) {
127104 [ menuItems , menuItemToId ]
128105 ) ;
129106
130- /**
131- * focus and blur management
132- */
133107 const clearHideTimeout = useCallback ( ( ) => {
134108 if ( timerRef . current ) {
135109 clearTimeout ( timerRef . current ) ;
@@ -164,7 +138,6 @@ function Menubar({ children, className }) {
164138 [ nodeRef ]
165139 ) ;
166140
167- // keyboard navigation
168141 const keyHandlers = {
169142 ArrowLeft : ( e ) => {
170143 e . preventDefault ( ) ;
@@ -183,7 +156,6 @@ function Menubar({ children, className }) {
183156 } ,
184157 Tab : ( e ) => {
185158 e . stopPropagation ( ) ;
186- // close
187159 } ,
188160 Home : ( e ) => {
189161 e . preventDefault ( ) ;
@@ -195,17 +167,15 @@ function Menubar({ children, className }) {
195167 e . stopPropagation ( ) ;
196168 last ( ) ;
197169 }
198- // to do : support direct access keys
170+ // TO DO : support direct access keys
199171 } ;
200172
201- // focus the active menu item and set its tabindex
202173 useEffect ( ( ) => {
203174 if ( activeIndex !== prevIndex ) {
204175 const items = Array . from ( menuItems ) ;
205176 const activeNode = items [ activeIndex ] ;
206177 const prevNode = items [ prevIndex ] ;
207178
208- // roving tabindex
209179 prevNode ?. setAttribute ( 'tabindex' , '-1' ) ;
210180 activeNode ?. setAttribute ( 'tabindex' , '0' ) ;
211181
@@ -219,7 +189,6 @@ function Menubar({ children, className }) {
219189 clearHideTimeout ( ) ;
220190 } , [ clearHideTimeout ] ) ;
221191
222- // context value for dropdowns and menu items
223192 const contextValue = useMemo (
224193 ( ) => ( {
225194 createMenuHandlers : ( menu ) => ( {
0 commit comments