feat: whyrating - initial project from turbostarter boilerplate
This commit is contained in:
52
apps/web/src/modules/common/turbo-link.tsx
Normal file
52
apps/web/src/modules/common/turbo-link.tsx
Normal file
@@ -0,0 +1,52 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
import type { ComponentProps } from "react";
|
||||
|
||||
type TurboLinkProps = ComponentProps<typeof Link>;
|
||||
|
||||
export const TurboLink = ({
|
||||
onMouseEnter,
|
||||
onPointerEnter,
|
||||
onTouchStart,
|
||||
onFocus,
|
||||
children,
|
||||
...props
|
||||
}: TurboLinkProps) => {
|
||||
const router = useRouter();
|
||||
const strHref =
|
||||
typeof props.href === "string" ? props.href : props.href.href;
|
||||
|
||||
const conditionalPrefetch = () => {
|
||||
if (strHref) {
|
||||
void router.prefetch(strHref);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Link
|
||||
{...props}
|
||||
prefetch={false}
|
||||
onMouseEnter={(e) => {
|
||||
conditionalPrefetch();
|
||||
onMouseEnter?.(e);
|
||||
}}
|
||||
onPointerEnter={(e) => {
|
||||
conditionalPrefetch();
|
||||
onPointerEnter?.(e);
|
||||
}}
|
||||
onTouchStart={(e) => {
|
||||
conditionalPrefetch();
|
||||
onTouchStart?.(e);
|
||||
}}
|
||||
onFocus={(e) => {
|
||||
conditionalPrefetch();
|
||||
onFocus?.(e);
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</Link>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user