@@ -6,7 +6,7 @@ import type { MenuMode } from '../interface';
66import { getMenuId } from '../context/IdContext' ;
77
88// destruct to reduce minify size
9- const { LEFT , RIGHT , UP , DOWN , ENTER , ESC } = KeyCode ;
9+ const { LEFT , RIGHT , UP , DOWN , ENTER , ESC , HOME , END } = KeyCode ;
1010
1111const ArrowKeys = [ UP , DOWN , LEFT , RIGHT ] ;
1212
@@ -215,7 +215,7 @@ export default function useAccessibility<T extends HTMLElement>(
215215 return e => {
216216 const { which } = e ;
217217
218- if ( [ ...ArrowKeys , ENTER , ESC ] . includes ( which ) ) {
218+ if ( [ ...ArrowKeys , ENTER , ESC , HOME , END ] . includes ( which ) ) {
219219 // Convert key to elements
220220 let elements : Set < HTMLElement > ;
221221 let key2element : Map < string , HTMLElement > ;
@@ -259,12 +259,12 @@ export default function useAccessibility<T extends HTMLElement>(
259259 ) ;
260260
261261 // Some mode do not have fully arrow operation like inline
262- if ( ! offsetObj ) {
262+ if ( ! offsetObj && which !== HOME && which !== END ) {
263263 return ;
264264 }
265265
266266 // Arrow prevent default to avoid page scroll
267- if ( ArrowKeys . includes ( which ) ) {
267+ if ( ArrowKeys . includes ( which ) || [ HOME , END ] . includes ( which ) ) {
268268 e . preventDefault ( ) ;
269269 }
270270
@@ -295,7 +295,11 @@ export default function useAccessibility<T extends HTMLElement>(
295295 }
296296 } ;
297297
298- if ( offsetObj . sibling || ! focusMenuElement ) {
298+ if (
299+ [ HOME , END ] . includes ( which ) ||
300+ offsetObj . sibling ||
301+ ! focusMenuElement
302+ ) {
299303 // ========================== Sibling ==========================
300304 // Find walkable focus menu element container
301305 let parentQueryContainer : HTMLElement ;
@@ -306,13 +310,23 @@ export default function useAccessibility<T extends HTMLElement>(
306310 }
307311
308312 // Get next focus element
309- const targetElement = getNextFocusElement (
313+ let targetElement ;
314+ const focusableElements = getFocusableElements (
310315 parentQueryContainer ,
311316 elements ,
312- focusMenuElement ,
313- offsetObj . offset ,
314317 ) ;
315-
318+ if ( which === HOME ) {
319+ targetElement = focusableElements [ 0 ] ;
320+ } else if ( which === END ) {
321+ targetElement = focusableElements [ focusableElements . length - 1 ] ;
322+ } else {
323+ targetElement = getNextFocusElement (
324+ parentQueryContainer ,
325+ elements ,
326+ focusMenuElement ,
327+ offsetObj . offset ,
328+ ) ;
329+ }
316330 // Focus menu item
317331 tryFocus ( targetElement ) ;
318332
0 commit comments