Bloom Logo

Label

Renders an accessible label associated with form controls using Radix UI primitives.

importimport { Label } from "@/components/ui/label/label";
$npx @bloomui-react/cli add label
label.tsx
"use client";

import * as LabelPrimitive from "@radix-ui/react-label";
import { cva, type VariantProps } from "class-variance-authority";
import * as React from "react";
import { cn } from "@/lib/utils";

const labelVariants = cva(
  "font-medium text-zinc-900 dark:text-zinc-100 select-none leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70 transition-colors",
  {
    variants: {
      size: {
        sm: "text-xs",
        md: "text-sm",
        lg: "text-base",
      },
    },
    defaultVariants: {
      size: "sm",
    },
  },
);

export interface LabelProps
  extends React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root>,
    VariantProps<typeof labelVariants> {
  isRequired?: boolean;
}

const Label = React.forwardRef<
  React.ComponentRef<typeof LabelPrimitive.Root>,
  LabelProps
>(({ className, isRequired, size, children, ...props }, ref) => (
  <LabelPrimitive.Root
    ref={ref}
    className={cn(labelVariants({ size }), className)}
    {...props}
  >
    {children}
    {isRequired && <span className="text-rose-500 ml-1 font-bold">*</span>}
  </LabelPrimitive.Root>
));
Label.displayName = LabelPrimitive.Root.displayName;

export { Label };

Default

Standard form label component associated with an input.

Sizes

Set label font size using the size prop: sm, md, or lg.

size: sm | md | lg

Required Indicator

Add a red asterisk indicator to required field labels using the isRequired prop.

isRequired: boolean

Props — Label

Supported properties for the Label component.

PropTypeDefaultDescription
isRequiredbooleanfalseRenders a red asterisk required indicator.
size'sm' | 'md' | 'lg''sm'Font size variant for label text.
htmlForstringID of the associated form control input.