Skip to content

Commit 56f491a

Browse files
refactor(LinkWithArrow): render <a> when href exists and <button> otherwise
1 parent 81bc2a6 commit 56f491a

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

apps/site/components/Downloads/DownloadReleasesTable/DetailsButton.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use client';
22

3-
import { ArrowUpRightIcon } from '@heroicons/react/24/solid';
43
import { useTranslations } from 'next-intl';
54
import type { FC } from 'react';
65
import { use } from 'react';
76

7+
import LinkWithArrow from '#site/components/LinkWithArrow';
88
import { ReleaseModalContext } from '#site/providers/releaseModalProvider';
99
import type { NodeRelease } from '#site/types';
1010

@@ -18,15 +18,13 @@ const DetailsButton: FC<DetailsButtonProps> = ({ versionData }) => {
1818
const { openModal } = use(ReleaseModalContext);
1919

2020
return (
21-
<button
22-
type="button"
23-
className="cursor-pointer text-green-600 hover:text-green-900 dark:text-green-400 dark:hover:text-green-200"
21+
<LinkWithArrow
2422
onClick={() => openModal(versionData)}
2523
aria-label={t('details')}
24+
className="cursor-pointer"
2625
>
2726
{t('details')}
28-
<ArrowUpRightIcon className="ml-1 inline w-3 fill-neutral-600 dark:fill-white" />
29-
</button>
27+
</LinkWithArrow>
3028
);
3129
};
3230

apps/site/components/Downloads/Release/ChangelogLink.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import type { FC, PropsWithChildren } from 'react';
44
import { useContext } from 'react';
55

6-
import Link from '#site/components/Link';
76
import LinkWithArrow from '#site/components/LinkWithArrow';
87
import { BASE_CHANGELOG_URL } from '#site/next.constants.mjs';
98
import { ReleaseContext } from '#site/providers/releaseProvider';
@@ -12,8 +11,8 @@ const ChangelogLink: FC<PropsWithChildren> = ({ children }) => {
1211
const { release } = useContext(ReleaseContext);
1312

1413
return (
15-
<LinkWithArrow asChild>
16-
<Link href={`${BASE_CHANGELOG_URL}${release.version}`}>{children}</Link>
14+
<LinkWithArrow href={`${BASE_CHANGELOG_URL}${release.version}`}>
15+
{children}
1716
</LinkWithArrow>
1817
);
1918
};

apps/site/components/LinkWithArrow.tsx

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
import { ArrowUpRightIcon } from '@heroicons/react/24/solid';
2-
import type { SlotProps } from '@radix-ui/react-slot';
3-
import { Slot } from '@radix-ui/react-slot';
4-
import type { ComponentProps, FC, PropsWithChildren } from 'react';
2+
import type {
3+
AnchorHTMLAttributes,
4+
ButtonHTMLAttributes,
5+
FC,
6+
PropsWithChildren,
7+
} from 'react';
58

69
import Link from '#site/components/Link';
710

811
type LinkWithArrowProps =
9-
| ({ asChild?: false } & ComponentProps<typeof Link>)
10-
| ({ asChild: true } & SlotProps);
12+
| ({ href: string } & AnchorHTMLAttributes<HTMLAnchorElement>)
13+
| (Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'type'> & { href?: never });
1114

12-
const LinkWithArrow: FC<PropsWithChildren<LinkWithArrowProps>> = ({
13-
children,
14-
asChild = false,
15-
...props
16-
}) => {
17-
const Comp = asChild ? Slot : Link;
15+
const LinkWithArrow: FC<PropsWithChildren<LinkWithArrowProps>> = props => {
16+
const { children, className, ...rest } = props;
17+
18+
if ('href' in props && props.href) {
19+
return (
20+
<Link
21+
{...(rest as AnchorHTMLAttributes<HTMLAnchorElement>)}
22+
className={className}
23+
>
24+
<span>
25+
{children}
26+
<ArrowUpRightIcon className="ml-1 inline w-3 fill-neutral-600 dark:fill-white" />
27+
</span>
28+
</Link>
29+
);
30+
}
1831

1932
return (
20-
<Comp {...props}>
33+
<button
34+
type="button"
35+
{...(rest as ButtonHTMLAttributes<HTMLButtonElement>)}
36+
className={`${className || ''} text-green-600 hover:text-green-900 dark:text-green-400 dark:hover:text-green-200`}
37+
>
2138
<span>
2239
{children}
2340
<ArrowUpRightIcon className="ml-1 inline w-3 fill-neutral-600 dark:fill-white" />
2441
</span>
25-
</Comp>
42+
</button>
2643
);
2744
};
2845

0 commit comments

Comments
 (0)