(el?: HTMLElement | null)
| 1 | export 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 | } |
no outgoing calls
no test coverage detected