@@ -28,7 +28,7 @@ export function render<Elements extends Element | Element[] = HTMLElement>(
28
28
if ( typeof focus === 'string' ) {
29
29
; ( assertSingleNodeFromXPath ( focus , div ) as HTMLElement ) . focus ( )
30
30
} else if ( focus !== false ) {
31
- const element : HTMLElement | null = getFocusableElement ( div )
31
+ const element : HTMLElement | null = findFocusable ( div )
32
32
element ?. focus ( )
33
33
}
34
34
@@ -103,23 +103,14 @@ export function setup<Elements extends Element | Element[] = HTMLElement>(
103
103
...render < Elements > ( ui , { eventHandlers, focus, selection} ) ,
104
104
}
105
105
}
106
- export function getFocusableElement (
107
- parent : Element | ShadowRoot ,
108
- ) : HTMLElement | null {
109
- const possibleFocusableElement : HTMLElement | null =
110
- parent . querySelector ( FOCUSABLE_SELECTOR )
111
- if ( possibleFocusableElement ) {
112
- return possibleFocusableElement
113
- }
114
-
115
- const children = Array . from ( parent . children )
116
- for ( const child of children ) {
117
- if ( 'shadowRoot' in child && child . shadowRoot ) {
118
- const possibleFocusableChildElement = getFocusableElement (
119
- child . shadowRoot ,
120
- )
121
- if ( possibleFocusableChildElement ) {
122
- return possibleFocusableChildElement
106
+ function findFocusable ( container : Element | ShadowRoot ) : HTMLElement | null {
107
+ for ( const el of Array . from ( container . querySelectorAll ( '*' ) ) ) {
108
+ if ( el . matches ( FOCUSABLE_SELECTOR ) ) {
109
+ return el as HTMLElement
110
+ } else if ( el . shadowRoot ) {
111
+ const f = findFocusable ( el . shadowRoot )
112
+ if ( f ) {
113
+ return f
123
114
}
124
115
}
125
116
}
0 commit comments