prevent default on link click

This commit is contained in:
Nate Kelley 2025-04-01 14:06:47 -06:00
parent 81a59370d4
commit 2627cedf03
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
2 changed files with 28 additions and 4 deletions

View File

@ -256,10 +256,22 @@ const DropdownMenuLink: React.FC<{
/> />
); );
if (!link) return <div className={className}>{content}</div>; if (!link)
return (
<div className={className} onClick={(e) => e.stopPropagation()}>
{content}
</div>
);
return ( return (
<Link className={className} href={link} target={isExternal ? '_blank' : '_self'}> <Link
className={className}
href={link}
target={isExternal ? '_blank' : '_self'}
onClick={(e) => {
e.stopPropagation();
e.preventDefault();
}}>
{content} {content}
</Link> </Link>
); );

View File

@ -44,14 +44,26 @@ export const PreventNavigation: React.FC<PreventNavigationProps> = React.memo(
* @param e The triggered event. * @param e The triggered event.
*/ */
const handleClick = useMemoizedFn((event: MouseEvent) => { const handleClick = useMemoizedFn((event: MouseEvent) => {
const target = event.target as HTMLAnchorElement; let target = event.target as HTMLElement;
let href: string | null = null;
// Traverse up the DOM tree looking for an anchor tag with href
while (target && !href) {
if (target instanceof HTMLAnchorElement && target.href) {
href = target.href;
break;
}
target = target.parentElement as HTMLElement;
}
if (isDirty) { if (isDirty) {
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
console.log(href);
confirmationFn.current = () => { confirmationFn.current = () => {
router.push(target.href); if (href) router.push(href);
}; };
setLeavingPage(true); setLeavingPage(true);