@@ -257,8 +257,6 @@ export class Virtual {
257257
258258 this . #treeCache =
259259 this . #container && tree ? flattenTree ( this . #container, tree , null ) : [ ] ;
260-
261- this . #attachFocusListeners( ) ;
262260 }
263261
264262 return this . #treeCache;
@@ -290,28 +288,9 @@ export class Virtual {
290288 }
291289
292290 #invalidateTreeCache( ) {
293- this . #detachFocusListeners( ) ;
294291 this . #treeCache = null ;
295292 }
296293
297- #attachFocusListeners( ) {
298- this . #getAccessibilityTree( ) . forEach ( ( treeNode ) => {
299- treeNode . node . addEventListener (
300- "focus" ,
301- this . #handleFocusChange. bind ( this )
302- ) ;
303- } ) ;
304- }
305-
306- #detachFocusListeners( ) {
307- this . #getAccessibilityTree( ) . forEach ( ( treeNode ) => {
308- treeNode . node . removeEventListener (
309- "focus" ,
310- this . #handleFocusChange. bind ( this )
311- ) ;
312- } ) ;
313- }
314-
315294 async #handleFocusChange( { target } : Event ) {
316295 await tick ( ) ;
317296
@@ -322,10 +301,13 @@ export class Virtual {
322301 return ;
323302 }
324303
325- // We've covered the tree having no length so there must be at least one
326- // index or we default back to the beginning of the tree.
327- const newActiveNode =
328- tree . find ( ( { node } ) => node === target ) ?? tree . at ( 0 ) ! ;
304+ // We've covered the tree having no length so there should be at least one
305+ // matching node, but if not we will not update the state
306+ const newActiveNode = tree . find ( ( { node } ) => node === target ) ;
307+
308+ if ( ! newActiveNode ) {
309+ return ;
310+ }
329311
330312 this . #updateState( newActiveNode , true ) ;
331313 }
@@ -619,6 +601,8 @@ export class Virtual {
619601 return ;
620602 }
621603
604+ this . #container. addEventListener ( "focusin" , this . #handleFocusChange. bind ( this ) ) ;
605+
622606 this . #updateState( tree [ 0 ] ) ;
623607
624608 return ;
0 commit comments