Skip to content

Commit 0a2d701

Browse files
committed
refactor(link box): create custom linkbox and link-overlay components
1 parent 40cdb74 commit 0a2d701

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

src/components/Contributors.tsx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import shuffle from "lodash/shuffle"
33

44
import { Flex } from "@/components/ui/flex"
55
import InlineLink from "@/components/ui/Link"
6+
import { LinkBox, LinkOverlay } from "@/components/ui/link-box"
67

78
import data from "!!raw-loader!@/../.all-contributorsrc"
89

@@ -32,8 +33,9 @@ const Contributors = () => {
3233

3334
<Flex className="flex-wrap">
3435
{contributorsList.map((contributor) => (
35-
<Flex
36-
className="relative z-10 m-2 max-w-[132px] transform flex-col shadow-table transition-transform duration-100 hover:scale-[1.02] hover:rounded hover:bg-background-table-hover hover:no-underline hover:shadow-table-box-hover focus:scale-[1.02] focus:rounded focus:no-underline focus:shadow-table-box-hover"
36+
<LinkBox
37+
as="div"
38+
className="m-2 max-w-[132px] transform shadow-table transition-transform duration-100 hover:scale-[1.02] hover:rounded hover:bg-background-table-hover hover:no-underline hover:shadow-table-box-hover focus:scale-[1.02] focus:rounded focus:no-underline focus:shadow-table-box-hover"
3739
key={contributor.login}
3840
>
3941
<img
@@ -43,16 +45,18 @@ const Contributors = () => {
4345
/>
4446
<div className="p-4">
4547
<h3 className="mb-4 mt-2 text-md">
46-
<InlineLink
47-
className="static flex-grow text-body no-underline before:absolute before:left-0 before:top-0 before:z-0 before:block before:h-full before:w-full before:cursor-pointer before:content-[''] hover:no-underline"
48-
href={contributor.profile}
49-
hideArrow
50-
>
51-
{contributor.name}
52-
</InlineLink>
48+
<LinkOverlay asChild>
49+
<InlineLink
50+
className="text-body no-underline hover:no-underline"
51+
href={contributor.profile}
52+
hideArrow
53+
>
54+
{contributor.name}
55+
</InlineLink>
56+
</LinkOverlay>
5357
</h3>
5458
</div>
55-
</Flex>
59+
</LinkBox>
5660
))}
5761
</Flex>
5862
</>

src/components/ui/link-box.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {
2+
type BaseHTMLAttributes,
3+
type ElementRef,
4+
type ElementType,
5+
forwardRef,
6+
} from "react"
7+
import { Slot } from "@radix-ui/react-slot"
8+
9+
import { cn } from "@/lib/utils/cn"
10+
11+
type LinkBoxElement = ElementRef<"div">
12+
13+
type LinkBoxProps = BaseHTMLAttributes<HTMLDivElement> & { as?: ElementType }
14+
15+
const LinkBox = forwardRef<LinkBoxElement, LinkBoxProps>(
16+
({ as: Comp = "div", className, ...props }, ref) => {
17+
return (
18+
<Comp ref={ref} className={cn("relative z-10", className)} {...props} />
19+
)
20+
}
21+
)
22+
23+
LinkBox.displayName = "LinkBox"
24+
25+
type LinkOverlayElement = ElementRef<"a">
26+
27+
type LinkOverlayProps = BaseHTMLAttributes<HTMLAnchorElement> & {
28+
asChild?: boolean
29+
}
30+
31+
const LinkOverlay = forwardRef<LinkOverlayElement, LinkOverlayProps>(
32+
({ asChild, className, ...props }, ref) => {
33+
const Comp = asChild ? Slot : "a"
34+
35+
return (
36+
<Comp
37+
ref={ref}
38+
className={cn(
39+
"before:absolute before:left-0 before:top-0 before:z-0 before:block before:h-full before:w-full before:cursor-pointer before:content-['']",
40+
className
41+
)}
42+
{...props}
43+
/>
44+
)
45+
}
46+
)
47+
48+
LinkOverlay.displayName = "LinkOverlay"
49+
50+
export { LinkBox, LinkOverlay }

0 commit comments

Comments
 (0)