组件

Checkbox

一个 React 复选框,采用 Liquid Glass 方块、勾选动画与不确定状态。

Checkbox 渲染玻璃方块、勾选/横线指示器、可选标签与描述,以及用于表单提交的隐藏原生复选框。

方块在未选中时保持玻璃表面;选中后由 --ui-ink 填充,并通过缩放动画显示标记。支持 indeterminate 混合状态。

基础示例

label 与 description 会渲染在方块右侧,整行都可以点击。未使用 checked 时是非受控组件,可用 defaultChecked 设置初始状态。

Product updatesReceive an email when a new release ships.Security alertsGet notified about sign-ins from new devices.Research invitesOccasional invitations to product interviews.

受控用法

用 checked 与 onCheckedChange 控制状态。onCheckedChange 也会收到事件详情。

Enable two-factor authenticationRequire a verification code when signing in.

Two-factor authentication is off.

不确定状态

indeterminate 会把复选框显示为混合状态,并渲染横线标记。它通常用于“全选”父级:当部分子项被选中时设置 indeterminate,全部选中时设置 checked。

Email notificationsChoose the channels you want to hear about.
CommentsReplies and mentions on your projects.ReleasesNew versions published by your team.BillingInvoices, receipts, and payment failures.

尺寸

提供 xs、sm、md、lg、xl 五种尺寸;标签与描述的字号会随尺寸调整。

Size xsSize smSize mdSize lgSize xl

禁用与只读

disabled 会停止交互并淡化整行;readOnly 保留焦点,但不允许修改选中状态。

DisabledThe checked value is visible but cannot change.Read-onlyFocus and selection work, but the value cannot change.

无障碍

Checkbox 渲染带 role="checkbox" 与 aria-checked 的可聚焦元素(默认是 <span>),并包含用于表单提交的隐藏原生复选框。可见的 label 为其命名,description 通过 aria-describedby 关联;显式 ARIA 名称优先。

Space 切换当前控件。indeterminate 会设置 aria-checked="mixed"。必须勾选时请设置 required;表单模式见可访问性指南。

API 参考

属性类型默认值说明
labelReactNode—可见标签;未提供 aria-label 或 aria-labelledby 时会成为可访问名称。
descriptionReactNode—标签下方的说明文字;控件有名称时会通过 aria-describedby 关联。
size"xs" | "sm" | "md" | "lg" | "xl""md"方块、图标与文字尺寸。
checkedboolean—受控的选中状态。
defaultCheckedbooleanfalse非受控时的初始选中状态。
indeterminatebooleanfalse混合状态;为 true 时设置 aria-checked="mixed"。
onCheckedChange(checked: boolean, details) => void—选中状态变化时调用。
disabledbooleanfalse禁用交互。
readOnlybooleanfalse只读,不可修改,但仍可聚焦。
requiredbooleanfalse表单提交前必须选中。
namestring—表单字段名。
formstring—拥有隐藏输入框的表单 ID。
valuestring—选中时随表单提交的值。
uncheckedValuestring—未选中时随表单提交的值。
parentbooleanfalse是否作为 CheckboxGroup 中一组子复选框的父复选框。
idstring—隐藏输入框的 ID;当 nativeButton 为 true 时改为根元素的 ID。
inputRefRef<HTMLInputElement>—指向隐藏原生输入框的 ref。
nativeButtonbooleanfalse是否渲染原生 <button>。
renderReactElement | ((props, state) => ReactElement)—替换根元素。
refRef<HTMLElement>—转发到根元素的 ref。
classNamestring—合并到复选框根元素的 Tailwind 类名。
styleCSSProperties—内联样式,合并到与 className 相同的元素。
ui{ root?: ClassValue; box?: ClassValue; fill?: ClassValue; glass?: ClassValue; overlay?: ClassValue; indicator?: ClassValue; indicatorIcon?: ClassValue; wrapper?: ClassValue; label?: ClassValue; description?: ClassValue }—按 data-slot 组织的内部槽位类名。
属性说明
data-checked复选框选中时存在。
data-unchecked复选框未选中时存在。
data-indeterminate复选框处于混合状态时存在。
data-disabled复选框禁用时存在。
data-readonly复选框只读时存在。
data-required复选框必填时存在。
data-valid复选框包裹在 Field 中且有效时存在。
data-invalid复选框包裹在 Field 中且无效时存在。
data-touched复选框包裹在 Field 中并被触碰后存在。
data-dirty复选框包裹在 Field 中且值变化后存在。
data-filled复选框包裹在 Field 中且选中时存在。
data-focused复选框包裹在 Field 中且聚焦时存在。
data-starting-style指示器开始进入动画时存在。
data-ending-style指示器正在退出动画时存在。
data-slot标记内部部件(root、box、fill、glass、overlay、indicator、indicator-icon、wrapper、label、description),用于样式选择。

主题

组件使用共享玻璃材质。可以在父级调整 --ui-glass-* token,用 className 控制布局,并通过 ui 按 data-slot 调整内部槽位。组件级数值位于 src/theme/checkbox.ts。Token 分层与预设见主题指南。

相关组件

  • Switch — 立即生效的设置。
  • Radio Group — 从少量选项中选择一个。
  • Toggle — 一次性的按下操作。