Files
Jessica Lawshe 66671f9bf2 Merge pull request #34936 from overleaf/jel-scroll-to-notification
[web] Scroll to new notification added above domains table

GitOrigin-RevId: f62851398de3a54ef07c0e125c65a29dd84af6f8
2026-09-17 11:33:41 +00:00

29 lines
633 B
TypeScript

import { useEffect, useRef } from 'react'
import { elementIsInView } from '@/shared/utils/element-in-view'
type UseScrollIntoViewProps = {
id: string | null
}
function useScrollIntoView({ id }: UseScrollIntoViewProps) {
const hasMountedRef = useRef(false)
useEffect(() => {
if (!hasMountedRef.current) {
hasMountedRef.current = true
return
}
if (!id) {
return
}
const targetElement = document.getElementById(id)
if (targetElement && !elementIsInView(targetElement)) {
targetElement.scrollIntoView({ behavior: 'smooth' })
}
}, [id])
}
export default useScrollIntoView