Label
字段标签:13/600 的 ink2,放在控件上方;控件禁用时标签变灰而不是变淡。
label-demo.tsx
import { Checkbox } from "@/components/ui/checkbox"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
export default function LabelDemo() {
return (
<div className="grid w-full max-w-sm gap-4">
<div className="grid gap-1.5">
<Label htmlFor="label-demo-group">字幕组名称</Label>
<Input id="label-demo-group" defaultValue="喵萌奶茶屋" />
</div>
<div className="flex items-center gap-2">
<Checkbox id="label-demo-only" defaultChecked />
{/* Option text beside a checkbox is body copy, not a field label. */}
<Label htmlFor="label-demo-only" className="font-normal text-foreground">
只看简体中文
</Label>
</div>
<div className="flex items-center gap-2">
<Checkbox id="label-demo-locked" disabled />
<Label htmlFor="label-demo-locked" className="font-normal">
同步时覆盖封面(已锁定)
</Label>
</div>
</div>
)
}安装
npx shadcn@latest add @anibt/label第一次用之前:在 components.json 里登记 @anibt,再单独运行一次 npx shadcn@latest add @anibt/theme(见快速开始)。只有单独安装主题时,CLI 才会把 shadcn 的标准颜色换成白樱 / 夜樱;随组件带进来的主题只补上 AniBT 自己的变量和配方。
手动安装
- 1. 先装主题组件只读
@anibt/theme的 token 和配方,没有主题就没有样子。npx shadcn@latest add @anibt/theme - 2. 安装依赖
npm install radix-ui - 3. 把下面的文件放进项目components/ui/label.tsx
"use client" import * as React from "react" import { Label as LabelPrimitive } from "radix-ui" import { cn } from "@/lib/utils" /** * Label — the head of AniBT's form grammar (A 「贴纸手账」). * * 13/600 in `--anibt-ink2`: one step quieter than the value under it and one * step louder than a hint. It sits above its control, 6px over it (`Field` * owns that gap), and never shares the control's row. `items-center gap-1.5` * keeps a required mark or a glyph on the label's line. * * A disabled control greys its label to `--muted-foreground` instead of * dimming it with opacity, so the copy stays legible. Option text beside a * checkbox or a switch is not a field label: `Field orientation="horizontal"` * turns it into 13/400 body ink, or pass `font-normal text-foreground`. */ function Label({ className, ...props }: React.ComponentProps<typeof LabelPrimitive.Root>) { return ( <LabelPrimitive.Root data-slot="label" className={cn( "flex items-center gap-1.5 text-[13px] leading-5 font-semibold text-anibt-ink2 select-none group-data-[disabled=true]:pointer-events-none group-data-[disabled=true]:text-muted-foreground peer-disabled:cursor-not-allowed peer-disabled:text-muted-foreground", className )} {...props} /> ) } export { Label }
用法
用法
import { Label } from "@/components/ui/label"
<Label htmlFor="group">字幕组名称</Label>标签比下面的值轻一档、比说明重一档:13/600 ink2。它在控件上方,离控件 6px(Field 管这个间距),不和控件挤在一行。
勾选框、开关旁边的选项文字不是字段标签,是正文:13/400 墨色。用 Field orientation="horizontal" 时会自动换成这一档;单独用时传 className="font-normal text-foreground"。
禁用
紧跟在禁用控件后面(peer-disabled),或在 data-disabled="true" 的组里,标签换成 muted-foreground,鼠标变成禁止。不用透明度,禁用的文字仍然读得清。
属性
Label
htmlForstring默认 —指向的控件 id
asChildboolean默认 false把样式交给唯一的子元素(例如给一组控件命名的 span)
基于 Radix Label:双击标签不会选中文字,其余属性透传给 <label>。
