import React, { useEffect } from 'react'; import NextRouterLink from 'next/link'; import { useRouter } from 'next/router'; import { Link, LinkProps } from '@digigov/ui/navigation/Link'; const NextLink: React.ForwardRefRenderFunction = ( props, ref ) => { const { href, as, ...other } = props; const isExternalLink = /^(?:[a-z]+:)?\/\/|\.pdf|\.docx?|\.xlsx?|^mailto:/i.test(href || '') || other.target === '_blank'; const router = useRouter(); useEffect(() => { const path = router.asPath; if (path.includes('#')) { setTimeout(() => { const id = path.replace('#', ''); const el = document.getElementById(id); if (el) { window.scroll({ top: scrollY + el.getBoundingClientRect().top, behavior: 'smooth', }); } }, 600); } }, [router.asPath]); if (isExternalLink) { return ( {props.children} ); } return ( {props.children} ); }; export default React.forwardRef(NextLink);