@@ -166,7 +166,6 @@ export class Menu extends Component<MenuProps, {}, MenuEvents> {
166166
167167 protected override onAttach ( el : HTMLElement ) {
168168 el . style . setProperty ( 'display' , 'contents' ) ;
169- this . _focus . _attachMenu ( el ) ;
170169 }
171170
172171 protected override onConnect ( el : HTMLElement ) {
@@ -294,7 +293,10 @@ export class Menu extends Component<MenuProps, {}, MenuEvents> {
294293 this . _isTriggerDisabled . set ( disabled ) ;
295294 }
296295
296+ private _wasKeyboardExpand = false ;
297297 private _onExpandedChange ( isExpanded : boolean , event ?: Event ) {
298+ this . _wasKeyboardExpand = isKeyboardEvent ( event ) ;
299+
298300 event ?. stopPropagation ( ) ;
299301
300302 if ( this . _expanded ( ) === isExpanded ) return ;
@@ -325,12 +327,9 @@ export class Menu extends Component<MenuProps, {}, MenuEvents> {
325327 this . _toggleMediaControls ( event ) ;
326328 tick ( ) ;
327329
328- if ( isKeyboardEvent ( event ) ) {
329- if ( isExpanded ) {
330- content ?. focus ( ) ;
331- } else {
332- trigger ?. focus ( ) ;
333- }
330+ if ( this . _wasKeyboardExpand ) {
331+ if ( isExpanded ) content ?. focus ( ) ;
332+ else trigger ?. focus ( ) ;
334333
335334 for ( const el of [ this . el , content ] ) {
336335 el && el . setAttribute ( 'data-keyboard' , '' ) ;
@@ -352,28 +351,26 @@ export class Menu extends Component<MenuProps, {}, MenuEvents> {
352351 this . _menuObserver ?. _onOpen ?.( event ) ;
353352 } else {
354353 if ( this . isSubmenu ) {
355- // A little delay so submenu closing doesn't jump menu size when closing.
356- setTimeout ( ( ) => {
357- for ( const el of this . _submenus ) el . close ( event ) ;
358- } , 300 ) ;
354+ for ( const el of this . _submenus ) el . close ( event ) ;
359355 } else {
360356 this . _media . activeMenu = null ;
361357 }
362358
363359 this . _menuObserver ?. _onClose ?.( event ) ;
364360 }
365361
366- if ( isExpanded && ! isKeyboardEvent ( event ) ) {
367- requestAnimationFrame ( ( ) => {
368- this . _focus . _update ( ) ;
369- // Timeout to allow size to be updated via transition.
370- setTimeout ( ( ) => {
371- this . _focus . _scroll ( ) ;
372- } , 100 ) ;
373- } ) ;
362+ if ( isExpanded ) {
363+ requestAnimationFrame ( this . _updateFocus . bind ( this ) ) ;
374364 }
375365 }
376366
367+ private _updateFocus ( ) {
368+ if ( this . _isTransitionActive || this . _isSubmenuOpen ) return ;
369+ this . _focus . _update ( ) ;
370+ if ( this . _wasKeyboardExpand ) this . _focus . _focusActive ( ) ;
371+ this . _focus . _scroll ( ) ;
372+ }
373+
377374 private _isExpanded ( ) {
378375 return ! this . _isDisabled ( ) && this . _expanded ( ) ;
379376 }
@@ -404,9 +401,7 @@ export class Menu extends Component<MenuProps, {}, MenuEvents> {
404401 return ;
405402 }
406403
407- // A little delay so submenu closing doesn't jump menu size when closing.
408- if ( this . isSubmenu ) return setTimeout ( this . close . bind ( this , event ) , 800 ) ;
409- else this . close ( event ) ;
404+ this . close ( event ) ;
410405 }
411406
412407 private _getCloseTarget ( ) {
@@ -453,8 +448,11 @@ export class Menu extends Component<MenuProps, {}, MenuEvents> {
453448 this . _submenus . delete ( menu ) ;
454449 }
455450
451+ private _isSubmenuOpen = false ;
456452 private _onSubmenuOpenBind = this . _onSubmenuOpen . bind ( this ) ;
457453 private _onSubmenuOpen ( event : MenuOpenEvent ) {
454+ this . _isSubmenuOpen = true ;
455+
458456 const content = this . _content ( ) ;
459457
460458 if ( this . isSubmenu ) {
@@ -483,6 +481,8 @@ export class Menu extends Component<MenuProps, {}, MenuEvents> {
483481
484482 private _onSubmenuCloseBind = this . _onSubmenuClose . bind ( this ) ;
485483 private _onSubmenuClose ( event : MenuCloseEvent ) {
484+ this . _isSubmenuOpen = false ;
485+
486486 const content = this . _content ( ) ;
487487
488488 if ( this . isSubmenu ) {
@@ -533,11 +533,15 @@ export class Menu extends Component<MenuProps, {}, MenuEvents> {
533533 setStyle ( content , '--menu-height' , height + 'px' ) ;
534534 } ) ;
535535
536+ protected _isTransitionActive = false ;
536537 protected _onResizeTransition ( event : TransitionEvent ) {
537538 const content = this . _content ( ) ;
538539 if ( content && event . propertyName === 'height' ) {
539- const hasStarted = event . type === 'transitionstart' ;
540- setAttribute ( content , 'data-resizing' , hasStarted ) ;
540+ this . _isTransitionActive = event . type === 'transitionstart' ;
541+
542+ setAttribute ( content , 'data-resizing' , this . _isTransitionActive ) ;
543+
544+ if ( this . _expanded ( ) ) this . _updateFocus ( ) ;
541545 }
542546 }
543547
0 commit comments