@@ -62,6 +62,9 @@ watch(menuRef, () => {
6262 if (! menuRef .value ) return
6363 // Set up focus for the first menu item
6464 const firstMenuItem = menuRef .value .querySelector (' [role="menuitem"]' ) as HTMLButtonElement
65+ if (! firstMenuItem ) {
66+ throw new Error (' Cannot find a menuitem to focus' )
67+ }
6568 firstMenuItem .tabIndex = 0
6669 firstMenuItem .focus ()
6770})
@@ -92,7 +95,10 @@ function onMenuKeyDown(event: KeyboardEvent) {
9295 const menuItems: HTMLElement [] = Array .from (menu .querySelectorAll (' [role="menuitem"]' ))
9396 // Find the current item
9497 let currentIndex = menuItems .findIndex (menuItem => menuItem .tabIndex !== - 1 )
95- let currentMenuItem = menuItems .at (currentIndex )!
98+ const currentMenuItem = menuItems .at (currentIndex )
99+ if (! currentMenuItem ) {
100+ throw new Error (` Missing menuitem at index ${currentIndex } ` )
101+ }
96102
97103 switch (event .key ) {
98104 case menuItemNavKeys .prev :
@@ -112,13 +118,16 @@ function onMenuKeyDown(event: KeyboardEvent) {
112118 return
113119 }
114120
121+ const menuItemToFocus = menuItems .at (currentIndex )
122+ if (! menuItemToFocus ) {
123+ throw new RangeError (` currentIndex (${currentIndex }) outside of range of menu items ` )
124+ }
125+
115126 event .preventDefault ()
116127
117128 currentMenuItem .tabIndex = - 1
118- // Update and focus the new current item
119- currentMenuItem = menuItems .at (currentIndex )!
120- currentMenuItem .tabIndex = 0
121- currentMenuItem .focus ()
129+ menuItemToFocus .tabIndex = 0
130+ menuItemToFocus .focus ()
122131}
123132
124133function mod(n : number , m : number ): number {
0 commit comments