From bd0d7e61e93c96ae10d946ca68b3a02bdadd163c Mon Sep 17 00:00:00 2001 From: Christel Westerberg Date: Wed, 28 Aug 2024 15:34:06 +0200 Subject: [PATCH] fix: handle target=_blank in link --- components/TempDesignSystem/Link/index.tsx | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/components/TempDesignSystem/Link/index.tsx b/components/TempDesignSystem/Link/index.tsx index 8ef6452da..6856f87aa 100644 --- a/components/TempDesignSystem/Link/index.tsx +++ b/components/TempDesignSystem/Link/index.tsx @@ -1,7 +1,7 @@ "use client" import NextLink from "next/link" import { usePathname, useRouter } from "next/navigation" -import { startTransition, useCallback, useEffect } from "react" +import { startTransition, useCallback } from "react" import useRouterTransitionStore from "@/stores/router-transition" @@ -60,15 +60,21 @@ export default function Link({ prefetch={prefetch} className={classNames} onClick={(e) => { + if (onClick) { + onClick(e) + } + if (trackingId) { + trackClickById() + } + if (props.target === "_blank") { + // If link should open in new tab, we don't want to + // track navigation nor start a router transition. + return + } + e.preventDefault() trackPageViewStart() startTransition(() => { startRouterTransition() - if (trackingId) { - trackClickById() - } - if (onClick) { - onClick(e) - } router.push(href) }) }}