Controlsradix-ui
Label
An accessible label.
Installation
pnpm dlx shadcn@latest add https://glasscn.app/r/label.jsonReact Native (iOS, Android and web through react-native-web) — same parts and props:
pnpm dlx shadcn@latest add https://glasscn.app/r/native-label.jsonOr with the namespace set up: shadcn add @glasscn-app/label
Props
| Prop | Type | Default |
|---|---|---|
| required | booleanA marker after the text (hidden from screen readers — mark the control required too). | false |
| optional | boolean | stringA quiet "(optional)" hint, or your own text. | false |
Source
components/glass/label.tsx
"use client"
import * as React from "react"
import { cn } from "@/lib/glass-cn"
import { Label as LabelPrimitive } from "radix-ui"
/**
* An accessible label. `required` adds a marker (hidden from assistive tech — mark the control
* `required` too); `optional` adds a quiet "(optional)" hint.
*/
function Label({
className,
required,
optional,
children,
...props
}: React.ComponentProps<typeof LabelPrimitive.Root> & { required?: boolean; optional?: boolean | string }) {
// markers only when asked: with asChild, Radix needs a single child
const content =
required || optional ? (
<>
{children}
{required ? (
<span aria-hidden data-slot="label-required" className="-ms-1.5 text-destructive-ink">
*
</span>
) : null}
{optional ? (
<span data-slot="label-optional" className="font-normal text-muted-foreground">
{typeof optional === "string" ? optional : "(optional)"}
</span>
) : null}
</>
) : (
children
)
return (
<LabelPrimitive.Root
data-slot="label"
className={cn(
"flex items-center gap-2 text-sm leading-snug font-medium select-none group-data-[disabled=true]:pointer-events-none group-data-[disabled=true]:text-glass-label-tertiary peer-disabled:cursor-not-allowed peer-disabled:text-glass-label-tertiary",
className
)}
{...props}
>
{content}
</LabelPrimitive.Root>
)
}
export { Label }