Label

Names any control, and moves focus into it when pressed.

A plain <label> only reaches native inputs. Label names any control by its htmlFor, including ui-sdk ones like a switch or a radio group. Wrapping a control in a Label works too, as the checkbox examples show.

Preview
basic.tsx
import { Label } from "~/components/ui/label"
import { Switch } from "~/components/ui/switch"

export default function Example() {
  return (
    <div className="flex items-center gap-md">
      <Label htmlFor="notifications">Email notifications</Label>
      <Switch id="notifications" defaultChecked />
    </div>
  )
}

Dependencies

What it's built from and what's built from it in Mocha, read from the imports in its source.

UsesTextprimitive
Used byFieldform itemSelectform item
Packages @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20tailwind-variants@4.1.0

Installation

Set the project up for Mocha once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/mocha/label.json

Manually

Install its packages:

terminal
npm install @ui-sdk/core@0.1.0-beta.20 @ui-sdk/react@0.1.0-beta.20 tailwind-variants@4.1.0

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/ui/label.tsx
import { Label as LabelPrimitive } from "@ui-sdk/react/label"
import type * as React from "react"
import { tv } from "tailwind-variants"

import { textVariants } from "~/components/ui/text"
import { cnState } from "~/lib/utils"

/** Names any control by its `htmlFor`, and moves focus into it when pressed. */
/** A label's look, shared with `FieldLabel`. Dims with the control it names when that's disabled. */
const labelVariants = tv({
  base: [
    textVariants({ size: "sm", weight: "bold", leading: "none" }),
    "items-center gap-sm text-foreground select-none has-data-disabled:opacity-50 data-disabled:opacity-50",
  ],
})

function Label({ className, ...props }: React.ComponentProps<typeof LabelPrimitive>) {
  return (
    <LabelPrimitive
      className={cnState([labelVariants(), "inline-flex cursor-default"], className)}
      {...props}
    />
  )
}

export { Label, labelVariants }

API reference

Generated from Mocha's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

Label

className

Type string | ((state: {}) => string)

debug

Logs every state change with its reason to the console, in development builds.

Type boolean

render

Type RenderProp<{}>

style

Type CSSProperties | ((state: {}) => CSSProperties)