/[sudobot]/branches/7.x/docs/components/Navigation/Link.tsx
ViewVC logotype

Contents of /branches/7.x/docs/components/Navigation/Link.tsx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (show annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 2066 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 "use client";
2
3 import { useRouterContext } from "@/contexts/RouterContext";
4 import NextLink from "next/link";
5 import { usePathname } from "next/navigation";
6 import { ComponentProps, FC, KeyboardEvent, MouseEvent } from "react";
7
8 type LinkProps = Omit<ComponentProps<typeof NextLink>, "as"> & {
9 nextLink?: boolean;
10 as?: keyof JSX.IntrinsicElements | FC;
11 };
12
13 export default function Link({
14 nextLink = true,
15 as = NextLink as unknown as keyof JSX.IntrinsicElements,
16 onClick,
17 onKeyUp,
18 href,
19 children,
20 ...props
21 }: LinkProps) {
22 const { setIsChanging } = useRouterContext();
23 const pathname = usePathname();
24 const Component = ((nextLink ? NextLink : as) ?? "a") as "a";
25 const propsToPass = {
26 ...props,
27 href,
28 } as ComponentProps<typeof NextLink>;
29
30 if (nextLink) {
31 propsToPass.as = as as unknown as keyof JSX.IntrinsicElements;
32 }
33
34 const stringHref = href.toString();
35
36 const onNavigate = () => {
37 const pos = stringHref.indexOf("?");
38
39 if (
40 !stringHref.startsWith("https://") &&
41 !stringHref.startsWith("http://") &&
42 pathname !==
43 stringHref
44 .substring(0, pos === -1 ? undefined : pos)
45 .replace(/\/+/g, "/")
46 .replace(/\/$/g, "")
47 ) {
48 console.log(
49 pathname,
50 stringHref.substring(0, pos === -1 ? undefined : pos),
51 );
52 setIsChanging(true);
53 }
54 };
55
56 return (
57 <Component
58 href={href as string}
59 {...props}
60 onClick={event => {
61 event.stopPropagation();
62 onClick?.(event as MouseEvent<any, any>);
63 onNavigate();
64 }}
65 onKeyUp={event => {
66 onKeyUp?.(event as KeyboardEvent<any>);
67
68 if (event.key === "Enter") {
69 onNavigate();
70 }
71 }}
72 >
73 {children}
74 </Component>
75 );
76 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26