Skip to content

Label

An accessible form label component with support for required fields and additional context. Built on Radix UI Label primitive.

Installation

Add Label to your project with the CK-UI CLI. It copies the source into your project, so the code is yours to change.

Terminal
npx ck-ui-cli@latest add label
Adds
Label.tsxto your CK-UI folder (src/components/ck-ui by default)
Installs
@radix-ui/react-labelif your project does not have them yet

First time with CK-UI in this project? Run npx ck-ui-cli@latest init once before adding components. The installation guide explains what it sets up.

Manual installationInstall the packages and copy the files yourself.

Install the following dependencies

Run these commands in your terminal to install the required packages

Terminal
npm install @radix-ui/react-label

Create or update your Label component

Copy this code to create your Label component

/components/ui/Label.tsx
"use client";

import * as React from "react";
import * as LabelPrimitive from "@radix-ui/react-label";
import { cn } from "@/lib/utils";

export interface LabelRootProps extends React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root> {
    /** Dims the label. The control itself must also be disabled. */
    disabled?: boolean;
}

const Root = React.forwardRef<React.ElementRef<typeof LabelPrimitive.Root>, LabelRootProps>(({ className, disabled, ...props }, ref) => {
    return (
        <LabelPrimitive.Root
            ref={ref}
            data-disabled={disabled || undefined}
            className={cn(
                "group flex cursor-pointer items-center gap-px size-p-sm text-foreground",
                "data-[disabled]:pointer-events-none data-[disabled]:text-muted-foreground/60",
                className
            )}
            {...props}
        />
    );
});
Root.displayName = "Label.Root";

// Visual required marker. Screen readers get "required" from the control (required / aria-required).
const Asterisk = React.forwardRef<HTMLSpanElement, React.HTMLAttributes<HTMLSpanElement>>(({ className, children, ...props }, ref) => (
    <span ref={ref} aria-hidden="true" className={cn("text-primary-strong group-data-[disabled]:text-muted-foreground/60", className)} {...props}>
        {children || "*"}
    </span>
));
Asterisk.displayName = "Label.Asterisk";

const Sub = React.forwardRef<HTMLSpanElement, React.HTMLAttributes<HTMLSpanElement>>(({ className, ...props }, ref) => (
    <span ref={ref} className={cn("size-p-xs text-muted-foreground group-data-[disabled]:text-muted-foreground/60", className)} {...props} />
));
Sub.displayName = "Label.Sub";

export { Root, Asterisk, Sub };

Test Your Component

  • 1. Add the component to a test page
  • 2. Check all props and variants work as expected
  • 3. Verify responsive behavior
  • 4. Test with different content/data
  • 5. Ensure accessibility features work correctly

With Asterisk

Labels can include an asterisk to indicate required fields.

With Sublabel

Labels can include a sublabel to provide additional context or instructions.

Disabled State

Labels can be disabled to match disabled form controls.

Complete Example

A complete example showing all Label features together.

API Reference

The Label component is built on top of the Radix UI Label primitive and extends its functionality with additional styling options and features.

Label.Root

The main label component that provides accessible labeling for form controls.

See Radix UI docs for base props.

PropTypeDefaultDescription
disabled"boolean""false"

When true, applies disabled styling to the label.

htmlFor"string""undefined"

The id of the element the label is associated with.

className"string""undefined"

Additional CSS classes to apply to the label.

Label.Asterisk

A component to display an asterisk or other indicator for required fields.

PropTypeDefaultDescription
className"string""undefined"

Additional CSS classes to apply to the asterisk.

children"ReactNode""*"

The content to display as the asterisk. Defaults to '*'.

Label.Sub

A component to display additional descriptive text below the main label.

PropTypeDefaultDescription
className"string""undefined"

Additional CSS classes to apply to the sublabel.

children"ReactNode""undefined"

The content to display as the sublabel.

Used in these blocks

Block families with at least one block built on this component. Each page previews its blocks and lists the components and packages they use.

Application