Skip to content

Commit 6d656e4

Browse files
committed
fix(app): querySelector errors, more defensive scroll-to-item
1 parent ae8cff2 commit 6d656e4

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

packages/ui/src/components/list.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ import { Icon, type IconProps } from "./icon"
66
import { IconButton } from "./icon-button"
77
import { TextField } from "./text-field"
88

9+
function findByKey(container: HTMLElement, key: string) {
10+
const nodes = container.querySelectorAll<HTMLElement>('[data-slot="list-item"][data-key]')
11+
for (const node of nodes) {
12+
if (node.getAttribute("data-key") === key) return node
13+
}
14+
}
15+
916
export interface ListSearchProps {
1017
placeholder?: string
1118
autofocus?: boolean
@@ -97,8 +104,8 @@ export function List<T>(props: ListProps<T> & { ref?: (ref: ListRef) => void })
97104
if (!props.current) return
98105
const key = props.key(props.current)
99106
requestAnimationFrame(() => {
100-
const element = scroll.querySelector(`[data-key="${CSS.escape(key)}"]`)
101-
if (!(element instanceof HTMLElement)) return
107+
const element = findByKey(scroll, key)
108+
if (!element) return
102109
scrollIntoView(scroll, element, "center")
103110
})
104111
})
@@ -114,8 +121,8 @@ export function List<T>(props: ListProps<T> & { ref?: (ref: ListRef) => void })
114121
}
115122
const key = active()
116123
if (!key) return
117-
const element = scroll.querySelector(`[data-key="${CSS.escape(key)}"]`)
118-
if (!(element instanceof HTMLElement)) return
124+
const element = findByKey(scroll, key)
125+
if (!element) return
119126
scrollIntoView(scroll, element, "center")
120127
})
121128

0 commit comments

Comments
 (0)