|
1
|
import React from "react" |
|
2
|
import NextLink from "next/link" |
|
3
|
import type { LinkProps as NextLinkProps } from "next/link" |
|
4
|
import clsx from "clsx" |
|
5
|
import { TriangleRightMini } from "@medusajs/icons" |
|
6
|
|
|
7
|
export type LinkProps = Partial<NextLinkProps> & |
|
8
|
React.AllHTMLAttributes<HTMLAnchorElement> & { |
|
9
|
href?: string |
|
10
|
children?: React.ReactNode |
|
11
|
className?: string |
|
12
|
withIcon?: boolean |
|
13
|
variant?: "default" | "content" |
|
14
|
} |
|
15
|
|
|
16
|
const HASH_REGEX = /#.*$/ |
|
17
|
|
|
18
|
export const Link = ({ |
|
19
|
href, |
|
20
|
children, |
|
21
|
className, |
|
22
|
withIcon = false, |
|
23
|
variant = "default", |
|
24
|
...rest |
|
25
|
}: LinkProps) => { |
|
26
|
const hash = href?.match(HASH_REGEX)?.[0] || "" |
|
27
|
const hrefWithoutHash = href?.replace(HASH_REGEX, "") || "" |
|
28
|
if (hrefWithoutHash.endsWith("page.mdx")) { |
|
29
|
href = hrefWithoutHash.replace("/page.mdx", "") + hash |
|
30
|
} |
|
31
|
return ( |
|
32
|
<NextLink |
|
33
|
href={href || ""} |
|
34
|
{...rest} |
|
35
|
className={clsx( |
|
36
|
variant === "default" && |
|
37
|
"text-medusa-fg-interactive hover:text-medusa-fg-interactive-hover", |
|
38
|
variant === "content" && [ |
|
39
|
"underline decoration-medusa-fg-muted hover:decoration-medusa-fg-interactive", |
|
40
|
"decoration-[1.5px] font-medium transition-[text-decoration-color]", |
|
41
|
], |
|
42
|
withIcon && "flex gap-0.25 items-center group", |
|
43
|
className |
|
44
|
)} |
|
45
|
> |
|
46
|
{children} |
|
47
|
{withIcon && ( |
|
48
|
<TriangleRightMini className="group-hover:translate-x-docs_0.125 transition-transform" /> |
|
49
|
)} |
|
50
|
</NextLink> |
|
51
|
) |
|
52
|
} |