Skip to content

Commit 90cbc7b

Browse files
authored
Merge pull request #14937 from TylerAPfledderer/refactor/linkbox-asChild-strat
refactor(link-box): use `asChild` strategy for `LinkBox` component
2 parents 2c892c7 + fa3576f commit 90cbc7b

File tree

3 files changed

+20
-28
lines changed

3 files changed

+20
-28
lines changed

src/components/Contributors.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ const Contributors = () => {
3434
<Flex className="flex-wrap">
3535
{contributorsList.map((contributor) => (
3636
<LinkBox
37-
as="div"
3837
className="m-2 max-w-[132px] transform shadow transition-transform duration-100 hover:scale-[1.02] hover:rounded hover:bg-background-highlight focus:scale-[1.02] focus:rounded"
3938
key={contributor.login}
4039
>

src/components/ui/avatar.tsx

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -151,25 +151,22 @@ const Avatar = React.forwardRef<
151151

152152
const _ref = ref as React.ForwardedRef<HTMLDivElement>
153153
return (
154-
<LinkBox
155-
// !! Inconsistent strategy, using `as` prop instead of `asChild` bool
156-
as={Center}
157-
ref={_ref}
158-
className={cn(_direction, "gap-x-1 gap-y-0")}
159-
>
160-
<LinkOverlay
161-
asChild
162-
className={cn(
163-
"peer z-overlay inline-flex items-center gap-1 p-1",
164-
size !== "md" ? "text-xs" : "text-sm"
165-
)}
166-
>
167-
<BaseLink {...commonLinkProps}>{label}</BaseLink>
168-
</LinkOverlay>
169-
<AvatarBase size={size}>
170-
<AvatarImage src={src} />
171-
<AvatarFallback>{fallbackInitials}</AvatarFallback>
172-
</AvatarBase>
154+
<LinkBox ref={_ref} className={cn(_direction, "gap-x-1 gap-y-0")} asChild>
155+
<Center>
156+
<LinkOverlay
157+
asChild
158+
className={cn(
159+
"peer z-overlay inline-flex items-center gap-1 p-1",
160+
size !== "md" ? "text-xs" : "text-sm"
161+
)}
162+
>
163+
<BaseLink {...commonLinkProps}>{label}</BaseLink>
164+
</LinkOverlay>
165+
<AvatarBase size={size}>
166+
<AvatarImage src={src} />
167+
<AvatarFallback>{fallbackInitials}</AvatarFallback>
168+
</AvatarBase>
169+
</Center>
173170
</LinkBox>
174171
)
175172
}

src/components/ui/link-box.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
import {
2-
type BaseHTMLAttributes,
3-
type ElementRef,
4-
type ElementType,
5-
forwardRef,
6-
} from "react"
1+
import { type BaseHTMLAttributes, type ElementRef, forwardRef } from "react"
72
import { Slot } from "@radix-ui/react-slot"
83

94
import { cn } from "@/lib/utils/cn"
105

116
type LinkBoxElement = ElementRef<"div">
127

13-
type LinkBoxProps = BaseHTMLAttributes<HTMLDivElement> & { as?: ElementType }
8+
type LinkBoxProps = BaseHTMLAttributes<HTMLDivElement> & { asChild?: boolean }
149

1510
const LinkBox = forwardRef<LinkBoxElement, LinkBoxProps>(
16-
({ as: Comp = "div", className, ...props }, ref) => {
11+
({ asChild, className, ...props }, ref) => {
12+
const Comp = asChild ? Slot : "div"
1713
return (
1814
<Comp ref={ref} className={cn("relative z-10", className)} {...props} />
1915
)

0 commit comments

Comments
 (0)