mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-01-02 02:48:02 -05:00
12 lines
487 B
TypeScript
12 lines
487 B
TypeScript
export const scrollIntoViewCentered = (element: HTMLElement) => {
|
|
// Because elem.scrollIntoView({ block: 'center' }) doesn't work on safari (mobile/desktop).
|
|
const elementRect = element.getBoundingClientRect()
|
|
const absoluteElementTop = elementRect.top + window.pageYOffset
|
|
const halfWindowHeight = window.innerHeight / 2
|
|
const middle =
|
|
absoluteElementTop -
|
|
halfWindowHeight +
|
|
Math.min(elementRect.height / 2, halfWindowHeight - 58)
|
|
window.scrollTo(0, middle)
|
|
}
|