@@ -52,6 +52,14 @@ const AGENT_VALUE_PREFIX = "agent:";
5252const COMMAND_VALUE_PREFIX = "command:" ;
5353const LINK_VALUE_PREFIX = "link:" ;
5454
55+ function sidebarMenuItems ( dropdown : Element | null ) {
56+ return [
57+ ...( dropdown ?. querySelectorAll < HTMLElement & { active : boolean } > (
58+ ":scope > wa-dropdown-item:not([disabled]), :scope > .sidebar-agent-menu__agent-grid > wa-dropdown-item:not([disabled])" ,
59+ ) ?? [ ] ) ,
60+ ] ;
61+ }
62+
5563// Nested overlays bubble lifecycle events through the dropdown. Only the
5664// owner's completed hide may remove its menu or consume its Escape state.
5765function closeMenuAfterOwnDropdownHide ( event : Event , onClose : ( restoreFocus ?: boolean ) => void ) {
@@ -66,11 +74,7 @@ function moveSidebarMenuFocus(event: KeyboardEvent): boolean {
6674 return false ;
6775 }
6876 const dropdown = ( event . currentTarget as HTMLElement ) . closest ( "wa-dropdown" ) ;
69- const items = [
70- ...( dropdown ?. querySelectorAll < HTMLElement & { active : boolean } > (
71- ":scope > wa-dropdown-item:not([disabled]), :scope > .sidebar-agent-menu__agent-grid > wa-dropdown-item:not([disabled])" ,
72- ) ?? [ ] ) ,
73- ] ;
77+ const items = sidebarMenuItems ( dropdown ) ;
7478 const footer = dropdown ?. querySelector < HTMLElement > ( ".sidebar-identity-menu__footer" ) ;
7579 const controls = [
7680 ...items ,
@@ -98,6 +102,18 @@ function moveSidebarMenuFocus(event: KeyboardEvent): boolean {
98102 return true ;
99103}
100104
105+ function focusActiveAgentMenuItem ( dropdown : HTMLElement ) {
106+ const items = sidebarMenuItems ( dropdown ) ;
107+ const target =
108+ items . find ( ( item ) => item . classList . contains ( "sidebar-agent-menu__agent-switch--active" ) ) ??
109+ items . find ( ( item ) => item . classList . contains ( "sidebar-agent-menu__agent-switch" ) ) ;
110+ if ( ! target ) {
111+ return ;
112+ }
113+ items . forEach ( ( item ) => ( item . active = item === target ) ) ;
114+ target . focus ( { preventScroll : true } ) ;
115+ }
116+
101117type AgentMenuAgent = {
102118 id : string ;
103119 name ?: string ;
@@ -333,10 +349,28 @@ export function renderSidebarAgentMenu(params: SidebarAgentMenuParams) {
333349 ( event . currentTarget as HTMLElement )
334350 . querySelector < HTMLInputElement > ( ".sidebar-agent-menu__filter input" )
335351 ?. focus ( ) ;
352+ } else {
353+ focusActiveAgentMenuItem ( event . currentTarget as HTMLElement ) ;
354+ }
355+ } }
356+ @keydown=${ ( event : KeyboardEvent ) => {
357+ if ( moveSidebarMenuFocus ( event ) ) {
358+ return ;
359+ }
360+ const item =
361+ event . target instanceof HTMLElement
362+ ? event . target . closest < HTMLElement > (
363+ ".sidebar-agent-menu__agent-grid > wa-dropdown-item:not([disabled])" ,
364+ )
365+ : null ;
366+ if ( ( event . key === "Enter" || event . key === " " ) && item ) {
367+ event . preventDefault ( ) ;
368+ event . stopPropagation ( ) ;
369+ item . click ( ) ;
370+ return ;
336371 }
372+ trackDropdownKeyboardDismissal ( event , params . onTabAway ) ;
337373 } }
338- @keydown=${ ( event : KeyboardEvent ) =>
339- trackDropdownKeyboardDismissal ( event , params . onTabAway ) }
340374 @wa-after-hide=${ ( event : Event ) => closeMenuAfterOwnDropdownHide ( event , params . onClose ) }
341375 >
342376 < button
0 commit comments