2026-05-17 21:37:42 -05:00
|
|
|
import { useEffect } from 'react'
|
|
|
|
|
import { useLocation } from 'react-router-dom'
|
|
|
|
|
|
|
|
|
|
export default function ScrollToTop() {
|
2026-05-27 22:33:54 -05:00
|
|
|
const { pathname, hash } = useLocation()
|
2026-05-27 23:40:09 -05:00
|
|
|
|
|
|
|
|
// Cross-page navigation: scroll to hash or top on route change
|
2026-05-17 21:37:42 -05:00
|
|
|
useEffect(() => {
|
2026-05-27 22:33:54 -05:00
|
|
|
if (hash) {
|
|
|
|
|
const el = document.querySelector(hash)
|
|
|
|
|
if (el) {
|
|
|
|
|
el.scrollIntoView({ behavior: 'smooth' })
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-17 21:37:42 -05:00
|
|
|
window.scrollTo(0, 0)
|
2026-05-27 22:33:54 -05:00
|
|
|
}, [pathname, hash])
|
2026-05-27 23:40:09 -05:00
|
|
|
|
|
|
|
|
// Same-page: React Router won't re-navigate if URL is already identical,
|
|
|
|
|
// so intercept clicks on any link pointing to #contact-form directly.
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const handleClick = (e) => {
|
|
|
|
|
const anchor = e.target.closest('a')
|
|
|
|
|
if (!anchor) return
|
|
|
|
|
const href = anchor.getAttribute('href') || ''
|
|
|
|
|
if (!href.includes('#contact-form')) return
|
|
|
|
|
const el = document.querySelector('#contact-form')
|
|
|
|
|
if (!el) return
|
|
|
|
|
e.preventDefault()
|
|
|
|
|
el.scrollIntoView({ behavior: 'smooth' })
|
|
|
|
|
window.history.pushState(null, '', '#contact-form')
|
|
|
|
|
}
|
|
|
|
|
document.addEventListener('click', handleClick)
|
|
|
|
|
return () => document.removeEventListener('click', handleClick)
|
|
|
|
|
}, [])
|
|
|
|
|
|
2026-05-17 21:37:42 -05:00
|
|
|
return null
|
|
|
|
|
}
|