Skip to content
Shaken UI
Sign in

Label

Save

A form label with quiet required and optional markers that dims with a disabled control.

Category
Forms
Version
0.1.0
Frameworks
Next.js, Vite + React
Styling
Tailwind CSS v4

Preview

Label preview

Installation

npx @shakenui/cli add label

Review the plan first. A dry run lists every file and package without changing anything:

npx @shakenui/cli add label --dry-run

Usage

label-example.tsx
import { Label } from "@/components/shaken/ui/label";

// Any control works with Label; these inputs carry a few classes so the example stands alone.
const control =
  "h-9 w-full rounded-lg border border-zinc-300 bg-white px-3 text-sm text-zinc-900 shadow-sm placeholder:text-zinc-400 focus-visible:border-sky-500 focus-visible:ring-3 focus-visible:ring-sky-500/20 focus-visible:outline-none disabled:cursor-not-allowed disabled:opacity-60 dark:border-zinc-700 dark:bg-zinc-950 dark:text-zinc-100 dark:placeholder:text-zinc-500";

export default function LabelExample() {
  return (
    <form className="grid w-full max-w-sm gap-5">
      <div className="grid gap-1.5">
        <Label htmlFor="workspace-name" required>
          Workspace name
        </Label>
        <input id="workspace-name" required placeholder="Northwind Labs" className={control} />
      </div>
      <div className="grid gap-1.5">
        <Label htmlFor="vat-number" optional>
          VAT number
        </Label>
        <input id="vat-number" placeholder="DE 123 456 789" className={control} />
      </div>
      <Label className="gap-2.5 font-normal">
        <input type="checkbox" defaultChecked className="size-4 accent-zinc-950 dark:accent-white" />
        Send me the weekly usage report
      </Label>
    </form>
  );
}

API

PropTypeDefaultDescription
requiredbooleanfalseAdds an asterisk after the text; screen readers hear "required" instead. Set required on the control to enforce it.
optionalbooleanfalseAdds an "Optional" note after the text, for forms where most fields are required.
htmlForstring—The id of the control the label names. Not needed when the label wraps the control.
...propsComponentProps<"label">—Any native label attribute.

Accessibility

  • Renders a native <label>, so clicking it focuses or toggles the control it names.
  • The required asterisk is aria-hidden and paired with visually hidden text, so the accessible name reads "Workspace name required" rather than "star".
  • The optional note is plain text in the label, so it is part of the accessible name.

Files

Installed into src/components/shaken, or components/shaken when the project has no src directory.

  • src/components/shaken/ui/label.tsxlabel
  • npm packages: noneReact and Tailwind CSS v4 required

Source

The exact source the CLI installs, from /r/items/react/label/0.1.0.json.

ui/label.tsxShow
ui/label.tsx
import type { ComponentProps } from "react";

function cx(...classes: (string | false | null | undefined)[]) {
  return classes.filter(Boolean).join(" ");
}

export interface LabelProps extends ComponentProps<"label"> {
  /**
   * Adds a quiet asterisk after the text. Screen readers hear "required" instead of the symbol.
   * This only marks the label; set `required` on the control itself to enforce it.
   */
  required?: boolean;
  /** Adds an "Optional" note after the text, for forms where most fields are required. */
  optional?: boolean;
}

/**
 * A form label in the primary text color. Pass `htmlFor` to bind it to a control, or wrap the
 * control inside it. Dims itself when the control it labels is disabled (Base UI `data-disabled`).
 */
export function Label({ required = false, optional = false, className, children, ...props }: LabelProps) {
  return (
    <label
      className={cx(
        "inline-flex items-baseline gap-1.5 text-sm leading-5 font-medium text-zinc-950 select-none dark:text-white",
        "transition-opacity duration-150 ease-out motion-reduce:transition-none",
        "data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50",
        className,
      )}
      {...props}
    >
      {children}
      {required ? (
        <>
          <span aria-hidden="true" className="-ml-0.5 font-normal text-zinc-500 dark:text-zinc-500">
            *
          </span>
          <span className="sr-only">required</span>
        </>
      ) : optional ? (
        <span className="text-xs font-normal text-zinc-500 dark:text-zinc-500">Optional</span>
      ) : null}
    </label>
  );
}