feat: prevent scroll offset on page navigation (#77)

This commit is contained in:
Valerio Ageno
2024-11-04 19:29:37 +01:00
committed by GitHub
parent edfbd03f13
commit 258b4af0b1
2 changed files with 23 additions and 4 deletions
+9 -2
View File
@@ -5,13 +5,20 @@ import useRoute from '../hooks/useRoute'
import { useInView } from 'react-intersection-observer'
interface TuonoLinkProps {
/**
* If "true" the route gets loaded when the link enters the viewport. Default "true"
*/
preload?: boolean
/**
* If "false" the scroll offset will be kept across page navigation. Default "true"
*/
scroll?: boolean
}
export default function Link(
componentProps: AnchorHTMLAttributes<HTMLAnchorElement> & TuonoLinkProps,
): JSX.Element {
const { preload = true, ...props } = componentProps
const { preload = true, scroll = true, ...props } = componentProps
const router = useRouter()
const route = useRoute(props.href)
const { ref } = useInView({
@@ -30,7 +37,7 @@ export default function Link(
return
}
router.push(props.href || '')
router.push(props.href || '', { scroll })
}
return (