fix(app): layout jumping
This commit is contained in:
@@ -35,6 +35,25 @@ export function List<T>(props: ListProps<T> & { ref?: (ref: ListRef) => void })
|
|||||||
mouseActive: false,
|
mouseActive: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const scrollIntoView = (container: HTMLDivElement, node: HTMLElement, block: "center" | "nearest") => {
|
||||||
|
const containerRect = container.getBoundingClientRect()
|
||||||
|
const nodeRect = node.getBoundingClientRect()
|
||||||
|
const top = nodeRect.top - containerRect.top + container.scrollTop
|
||||||
|
const bottom = top + nodeRect.height
|
||||||
|
const viewTop = container.scrollTop
|
||||||
|
const viewBottom = viewTop + container.clientHeight
|
||||||
|
const target =
|
||||||
|
block === "center"
|
||||||
|
? top - container.clientHeight / 2 + nodeRect.height / 2
|
||||||
|
: top < viewTop
|
||||||
|
? top
|
||||||
|
: bottom > viewBottom
|
||||||
|
? bottom - container.clientHeight
|
||||||
|
: viewTop
|
||||||
|
const max = Math.max(0, container.scrollHeight - container.clientHeight)
|
||||||
|
container.scrollTop = Math.max(0, Math.min(target, max))
|
||||||
|
}
|
||||||
|
|
||||||
const { filter, grouped, flat, active, setActive, onKeyDown, onInput } = useFilteredList<T>(props)
|
const { filter, grouped, flat, active, setActive, onKeyDown, onInput } = useFilteredList<T>(props)
|
||||||
|
|
||||||
const searchProps = () => (typeof props.search === "object" ? props.search : {})
|
const searchProps = () => (typeof props.search === "object" ? props.search : {})
|
||||||
@@ -65,24 +84,31 @@ export function List<T>(props: ListProps<T> & { ref?: (ref: ListRef) => void })
|
|||||||
)
|
)
|
||||||
|
|
||||||
createEffect(() => {
|
createEffect(() => {
|
||||||
if (!scrollRef()) return
|
const scroll = scrollRef()
|
||||||
|
if (!scroll) return
|
||||||
if (!props.current) return
|
if (!props.current) return
|
||||||
const key = props.key(props.current)
|
const key = props.key(props.current)
|
||||||
requestAnimationFrame(() => {
|
requestAnimationFrame(() => {
|
||||||
const element = scrollRef()?.querySelector(`[data-key="${CSS.escape(key)}"]`)
|
const element = scroll.querySelector(`[data-key="${CSS.escape(key)}"]`)
|
||||||
element?.scrollIntoView({ block: "center" })
|
if (!(element instanceof HTMLElement)) return
|
||||||
|
scrollIntoView(scroll, element, "center")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
createEffect(() => {
|
createEffect(() => {
|
||||||
const all = flat()
|
const all = flat()
|
||||||
if (store.mouseActive || all.length === 0) return
|
if (store.mouseActive || all.length === 0) return
|
||||||
|
const scroll = scrollRef()
|
||||||
|
if (!scroll) return
|
||||||
if (active() === props.key(all[0])) {
|
if (active() === props.key(all[0])) {
|
||||||
scrollRef()?.scrollTo(0, 0)
|
scroll.scrollTo(0, 0)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const element = scrollRef()?.querySelector(`[data-key="${CSS.escape(active()!)}"]`)
|
const key = active()
|
||||||
element?.scrollIntoView({ block: "center" })
|
if (!key) return
|
||||||
|
const element = scroll.querySelector(`[data-key="${CSS.escape(key)}"]`)
|
||||||
|
if (!(element instanceof HTMLElement)) return
|
||||||
|
scrollIntoView(scroll, element, "center")
|
||||||
})
|
})
|
||||||
|
|
||||||
createEffect(() => {
|
createEffect(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user