@@ -33,30 +33,34 @@ export default class PgMenu extends HTMLElement {
3333 } ) ;
3434 $item . addEventListener ( 'up' , ( e : any ) => {
3535 const { index } = e . detail ;
36- let first = this . items [ 0 ] . disabled ? 1 : 0 ;
37- if ( index === first ) {
38- this . focus ( this . items . length - 1 ) ;
39- } else {
40- this . focus ( index - 1 ) ;
41- }
36+ this . #focus( index - 1 , - 1 , index ) ;
4237 } ) ;
4338 $item . addEventListener ( 'down' , ( e : any ) => {
4439 const { index } = e . detail ;
45- if ( index === this . items . length - 1 ) {
46- let first = this . items [ 0 ] . disabled ? 1 : 0 ;
47- this . focus ( first ) ;
48- } else {
49- this . focus ( index + 1 ) ;
50- }
40+ this . #focus( index + 1 , 1 , index ) ;
5141 } ) ;
5242 }
5343 } ) ;
5444 }
5545
56- focus ( index ) {
46+ #focus( index , fallback , initIndex ) {
47+ if ( index === initIndex ) {
48+ return ;
49+ }
5750 if ( index === - 1 ) {
58- index = this . items [ 0 ] . disabled ? 1 : 0 ;
51+ return this . #focus( this . items . length - 1 , fallback , initIndex ) ;
52+ }
53+ if ( index >= this . items . length ) {
54+ return this . #focus( 0 , fallback , initIndex ) ;
55+ }
56+ const item = this . $items . children [ index ] as any ;
57+ if ( item . focusable === false || this . items [ index ] . disabled ) {
58+ return this . #focus( index + fallback , fallback , initIndex ) ;
5959 }
60+ item ?. focus ( ) ;
61+ }
62+
63+ focus ( index ) {
6064 const item = this . $items . children [ index ] as HTMLElement ;
6165 item ?. focus ( ) ;
6266 }
0 commit comments