@@ -237,9 +237,11 @@ describe("session menu", () => {
237237
238238 it ( "opens an accessible icon picker with keyboard grid navigation" , async ( ) => {
239239 const menu = await mountMenu ( ) ;
240+ const dropdown = menu . querySelector ( "wa-dropdown" ) ;
240241
241242 await openIconPicker ( menu ) ;
242243
244+ expect ( menu . querySelector ( "wa-dropdown" ) ) . toBe ( dropdown ) ;
243245 expect ( menu . querySelector ( ".session-menu__icon-picker" ) ?. getAttribute ( "role" ) ) . toBe ( "dialog" ) ;
244246 const choices = Array . from (
245247 menu . querySelectorAll < HTMLButtonElement > ( ".session-menu__icon-choice" ) ,
@@ -249,6 +251,47 @@ describe("session menu", () => {
249251 expect ( document . activeElement ) . toBe ( choices [ 1 ] ) ;
250252 } ) ;
251253
254+ it ( "keeps Tab ownership inside the nested icon-picker dialog" , async ( ) => {
255+ const trigger = document . createElement ( "button" ) ;
256+ document . body . append ( trigger ) ;
257+ containers . push ( trigger ) ;
258+ const menu = await mountMenu ( { trigger } ) ;
259+
260+ await openIconPicker ( menu ) ;
261+
262+ const choice = menu . querySelector < HTMLButtonElement > ( ".session-menu__icon-choice" ) ;
263+ expect ( document . activeElement ) . toBe ( choice ) ;
264+ choice ?. dispatchEvent (
265+ new KeyboardEvent ( "keydown" , { key : "Tab" , bubbles : true , cancelable : true } ) ,
266+ ) ;
267+
268+ expect ( document . activeElement ) . toBe ( choice ) ;
269+ } ) ;
270+
271+ it ( "preserves an intentionally focused icon-picker field after the dropdown opens" , async ( ) => {
272+ const menu = await mountMenu ( ) ;
273+ await openIconPicker ( menu ) ;
274+ const input = menu . querySelector < HTMLInputElement > ( ".session-menu__emoji-field input" ) ;
275+ input ?. focus ( ) ;
276+
277+ menu . querySelector ( "wa-dropdown" ) ?. dispatchEvent ( new CustomEvent ( "wa-after-show" ) ) ;
278+
279+ expect ( document . activeElement ) . toBe ( input ) ;
280+ } ) ;
281+
282+ it ( "keeps one dropdown and restores its originating item after leaving the icon picker" , async ( ) => {
283+ const menu = await mountMenu ( ) ;
284+ const dropdown = menu . querySelector ( "wa-dropdown" ) ;
285+ await openIconPicker ( menu ) ;
286+
287+ menu . querySelector < HTMLButtonElement > ( ".session-menu__icon-picker-back" ) ?. click ( ) ;
288+ await menu . updateComplete ;
289+ await Promise . resolve ( ) ;
290+
291+ expect ( menu . querySelector ( "wa-dropdown" ) ) . toBe ( dropdown ) ;
292+ expect ( document . activeElement ) . toBe ( menuItem ( menu , "Change icon" ) ) ;
293+ } ) ;
294+
252295 it ( "opens group actions and dispatches group, removal, and creation choices" , async ( ) => {
253296 const onAction = vi . fn < ( action : SessionMenuAction ) => void > ( ) ;
254297 const menu = await mountMenu ( {
@@ -439,6 +482,40 @@ describe("session menu", () => {
439482 expect ( onAction ) . not . toHaveBeenCalled ( ) ;
440483 } ) ;
441484
485+ it ( "returns focus to its durable trigger before a Tab leaves the menu" , async ( ) => {
486+ const trigger = document . createElement ( "button" ) ;
487+ document . body . append ( trigger ) ;
488+ containers . push ( trigger ) ;
489+ const menu = await mountMenu ( { trigger } ) ;
490+ const item = menuItem ( menu , "Open chat" ) ;
491+ item . focus ( ) ;
492+
493+ const keydown = new KeyboardEvent ( "keydown" , {
494+ key : "Tab" ,
495+ bubbles : true ,
496+ cancelable : true ,
497+ } ) ;
498+ item . dispatchEvent ( keydown ) ;
499+
500+ expect ( document . activeElement ) . toBe ( trigger ) ;
501+ expect ( keydown . defaultPrevented ) . toBe ( false ) ;
502+ } ) ;
503+
504+ it ( "does not reclaim focus when Tab originates outside the open menu" , async ( ) => {
505+ const trigger = document . createElement ( "button" ) ;
506+ const outside = document . createElement ( "button" ) ;
507+ document . body . append ( trigger , outside ) ;
508+ containers . push ( trigger , outside ) ;
509+ await mountMenu ( { trigger } ) ;
510+ outside . focus ( ) ;
511+
512+ outside . dispatchEvent (
513+ new KeyboardEvent ( "keydown" , { key : "Tab" , bubbles : true , cancelable : true } ) ,
514+ ) ;
515+
516+ expect ( document . activeElement ) . toBe ( outside ) ;
517+ } ) ;
518+
442519 it ( "closes on Escape without leaking the key past the menu" , async ( ) => {
443520 const trigger = document . createElement ( "button" ) ;
444521 document . body . append ( trigger ) ;
0 commit comments