Background
In PR #1 (V1 portfolio), it was suggested to improve the Label component by wrapping it in React.forwardRef to maintain composability and setting a displayName for better debugging.
Requested Changes
- Wrap the Label component in React.forwardRef to forward refs to the underlying Radix UI primitive
- Add a displayName for better debugging
Why
- Improves component composability
- Ensures refs can be properly forwarded to the underlying DOM element
- Follows React best practices for reusable components
- Better debugging experience with named components
Original Discussion
#1 (comment)
Implementation Example
import * as React from "react"
import * as LabelPrimitive from "@radix-ui/react-label"
import { cn } from "@/lib/utils"
const Label = React.forwardRef<
React.ElementRef<typeof LabelPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root>
>(({ className, ...props }, ref) => (
<LabelPrimitive.Root
ref={ref}
data-slot="label"
className={cn(
"flex items-center gap-2 text-sm leading-none font-medium select-none group-data-[disabled=true]:pointer-events-none group-data-[disabled=true]:opacity-50 peer-disabled:cursor-not-allowed peer-disabled:opacity-50",
className
)}
{...props}
/>
))
Label.displayName = "Label"
export { Label }
Background
In PR #1 (V1 portfolio), it was suggested to improve the Label component by wrapping it in React.forwardRef to maintain composability and setting a displayName for better debugging.
Requested Changes
Why
Original Discussion
#1 (comment)
Implementation Example