Skip to content

Refactor Label component to use React.forwardRef #8

Description

@coderabbitai

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

  1. Wrap the Label component in React.forwardRef to forward refs to the underlying Radix UI primitive
  2. 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 }

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions