@@ -4,14 +4,12 @@ import { installSidebarTree } from "./sidebarTree";
44
55function setup ( ) {
66 const root = document . createElement ( "div" ) ;
7- root . setAttribute ( "role" , "tree" ) ;
87 document . body . append ( root ) ;
98 let dispose : ( ( ) => void ) | undefined ;
109 const row = ( id : string , parent ?: string ) => {
1110 const element = document . createElement ( "button" ) ;
1211 element . dataset . sidebarRow = id ;
1312 if ( parent ) element . dataset . sidebarParent = parent ;
14- element . setAttribute ( "role" , "treeitem" ) ;
1513 element . textContent = id ;
1614 root . append ( element ) ;
1715 return element ;
@@ -36,7 +34,7 @@ function press(target: HTMLElement, key: string, init: KeyboardEventInit = {}) {
3634}
3735
3836describe ( "sidebar keyboard navigation" , ( ) => {
39- it ( "moves focus without activating rows, clamps at the ends, and keeps one Tab stop " , ( ) => {
37+ it ( "moves focus without activating rows, clamps at the ends, and keeps every row in the Tab order " , ( ) => {
4038 using tree = setup ( ) ;
4139 const first = tree . row ( "first" ) ;
4240 const second = tree . row ( "second" ) ;
@@ -54,10 +52,10 @@ describe("sidebar keyboard navigation", () => {
5452 press ( last , "Home" ) ;
5553 expect ( document . activeElement ) . toBe ( first ) ;
5654 expect ( activate ) . not . toHaveBeenCalled ( ) ;
57- expect ( tree . root . querySelectorAll ( '[tabindex="0"]' ) ) . toHaveLength ( 1 ) ;
55+ expect ( [ first . tabIndex , second . tabIndex , last . tabIndex ] ) . toEqual ( [ 0 , 0 , 0 ] ) ;
5856 } ) ;
5957
60- it ( "expands a group before entering its first child and returns to the parent" , async ( ) => {
58+ it ( "expands a group before entering its first child and returns to the parent" , ( ) => {
6159 using tree = setup ( ) ;
6260 const group = tree . row ( "project" ) ;
6361 group . setAttribute ( "aria-expanded" , "false" ) ;
@@ -72,12 +70,6 @@ describe("sidebar keyboard navigation", () => {
7270 press ( group , "ArrowRight" ) ;
7371 expect ( group . getAttribute ( "aria-expanded" ) ) . toBe ( "true" ) ;
7472 expect ( document . activeElement ) . toBe ( group ) ;
75- // The tree picks up the new child from its mutation observer.
76- await vi . waitFor ( ( ) =>
77- expect (
78- tree . root . querySelector ( '[data-sidebar-parent="project"]' ) ?. getAttribute ( "aria-level" ) ,
79- ) . toBe ( "2" ) ,
80- ) ;
8173 press ( group , "ArrowRight" ) ;
8274 const child = document . activeElement as HTMLElement ;
8375 expect ( child . dataset . sidebarRow ) . toBe ( "thread" ) ;
@@ -104,7 +96,7 @@ describe("sidebar keyboard navigation", () => {
10496 expect ( document . activeElement ) . toBe ( selected ) ;
10597 } ) ;
10698
107- it ( "lets Tab reach only the focused row's buttons" , ( ) => {
99+ it ( "leaves every row's buttons in the Tab order " , ( ) => {
108100 using tree = setup ( ) ;
109101 const first = tree . row ( "first" ) ;
110102 const second = tree . row ( "second" ) ;
@@ -119,10 +111,10 @@ describe("sidebar keyboard navigation", () => {
119111 const secondAction = action ( "second" ) ;
120112 tree . install ( ) ;
121113 first . focus ( ) ;
122- expect ( [ firstAction . tabIndex , secondAction . tabIndex ] ) . toEqual ( [ 0 , - 1 ] ) ;
114+ expect ( [ firstAction . tabIndex , secondAction . tabIndex ] ) . toEqual ( [ 0 , 0 ] ) ;
123115 press ( first , "ArrowDown" ) ;
124116 expect ( document . activeElement ) . toBe ( second ) ;
125- expect ( [ firstAction . tabIndex , secondAction . tabIndex ] ) . toEqual ( [ - 1 , 0 ] ) ;
117+ expect ( [ firstAction . tabIndex , secondAction . tabIndex ] ) . toEqual ( [ 0 , 0 ] ) ;
126118 } ) ;
127119
128120 it ( "keeps a rendered row under a collapsed group reachable" , ( ) => {
@@ -152,26 +144,6 @@ describe("sidebar keyboard navigation", () => {
152144 expect ( undo . tabIndex ) . toBe ( 0 ) ;
153145 } ) ;
154146
155- it ( "gives row buttons back their own tab order when the tree reinstalls" , ( ) => {
156- using tree = setup ( ) ;
157- tree . row ( "first" ) ;
158- const second = tree . row ( "second" ) ;
159- const action = ( owner : string ) => {
160- const button = document . createElement ( "button" ) ;
161- button . dataset . sidebarActionFor = owner ;
162- tree . root . append ( button ) ;
163- return button ;
164- } ;
165- const firstAction = action ( "first" ) ;
166- const secondAction = action ( "second" ) ;
167- const dispose = installSidebarTree ( tree . root ) ;
168- second . focus ( ) ;
169- dispose ( ) ;
170- expect ( [ firstAction . tabIndex , secondAction . tabIndex ] ) . toEqual ( [ 0 , 0 ] ) ;
171- tree . install ( ) ;
172- expect ( [ firstAction . tabIndex , secondAction . tabIndex ] ) . toEqual ( [ - 1 , 0 ] ) ;
173- } ) ;
174-
175147 it ( "keeps the scroll position when focus follows a row that moved away" , async ( ) => {
176148 using tree = setup ( ) ;
177149 Object . assign ( tree . root . style , { height : "100px" , overflow : "auto" } ) ;
@@ -191,24 +163,26 @@ describe("sidebar keyboard navigation", () => {
191163 expect ( tree . root . scrollTop ) . toBe ( 0 ) ;
192164 } ) ;
193165
194- it ( "does not pull focus back after a click outside the tree " , async ( ) => {
166+ it ( "does not pull focus back after a click outside the rows " , async ( ) => {
195167 using tree = setup ( ) ;
196168 const first = tree . row ( "first" ) ;
197169 tree . install ( ) ;
198170 const text = document . createElement ( "p" ) ;
199171 text . textContent = "Chat text" ;
200172 document . body . append ( text ) ;
201173 using _text = { [ Symbol . dispose ] : ( ) => text . remove ( ) } ;
202- first . focus ( ) ;
203- await userEvent . click ( text ) ;
204- expect ( document . activeElement ) . toBe ( document . body ) ;
205- // A status change re-renders the tree while the user works elsewhere.
206- tree . row ( "second" ) ;
207- await vi . waitFor ( ( ) =>
208- expect (
209- tree . root . querySelector ( '[data-sidebar-row="second"]' ) ?. getAttribute ( "aria-posinset" ) ,
210- ) . toBe ( "2" ) ,
211- ) ;
212- expect ( document . activeElement ) . toBe ( document . body ) ;
174+ const spacer = document . createElement ( "div" ) ;
175+ spacer . textContent = "Empty list space" ;
176+ tree . root . append ( spacer ) ;
177+ for ( const target of [ text , spacer ] ) {
178+ first . focus ( ) ;
179+ await userEvent . click ( target ) ;
180+ expect ( document . activeElement ) . toBe ( document . body ) ;
181+ // A status change re-renders the list while the user works elsewhere.
182+ tree . row ( `second-${ target . tagName } ` ) ;
183+ // The list's mutation observer callback is queued ahead of this continuation.
184+ await Promise . resolve ( ) ;
185+ expect ( document . activeElement ) . toBe ( document . body ) ;
186+ }
213187 } ) ;
214188} ) ;
0 commit comments