MCPcopy Create free account
hub / github.com/Power-Playground/app / scrollIntoViewIfNeeded

Function scrollIntoViewIfNeeded

core/src/utils/scrollIntoViewIfNeeded.ts:1–26  ·  view source on GitHub ↗
(el?: HTMLElement | null)

Source from the content-addressed store, hash-verified

1export function scrollIntoViewIfNeeded(el?: HTMLElement | null) {
2 if (!el) return
3
4 const parent = el.parentElement!
5
6 if (el.offsetTop < parent.scrollTop) {
7 parent.scrollTo({
8 top: el.offsetTop - parent.offsetTop,
9 behavior: 'smooth'
10 })
11 }
12
13 const { top, bottom } = el.getBoundingClientRect()
14 const { top: parentTop, bottom: parentBottom } = parent.getBoundingClientRect()
15 if (top < parentTop) {
16 parent.scrollTo({
17 top: el.offsetTop - parent.offsetTop,
18 behavior: 'smooth'
19 })
20 } else if (bottom > parentBottom) {
21 parent.scrollTo({
22 top: el.offsetTop - parent.offsetTop - parent.clientHeight + el.clientHeight,
23 behavior: 'smooth'
24 })
25 }
26}

Callers 3

QuickAccessFunction · 0.90
MenuFunction · 0.90
HistoryDialog.tsxFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected