mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-14 22:23:42 +01:00
[web] Scroll to new notification added above domains table GitOrigin-RevId: f62851398de3a54ef07c0e125c65a29dd84af6f8
29 lines
633 B
TypeScript
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
|