'use client' import { forwardRef, useEffect, useRef, type InputHTMLAttributes } from 'react' import { cn } from '../../lib/cn' export type CheckboxSize = 'sm' | 'md' const SIZES: Record = { sm: 'size-4', md: 'size-[18px]', } export type CheckboxProps = Omit, 'size' | 'type'> & { boxSize?: CheckboxSize /** Neither checked nor unchecked — the mixed state of a parent checkbox. */ indeterminate?: boolean invalid?: boolean } /** * A native checkbox with the box drawn on it. It carries no label and no * layout: pair it with `Label`, or let Field do the wiring. */ export const Checkbox = forwardRef(function Checkbox( { className, boxSize = 'md', indeterminate = false, invalid = false, ...props }, forwardedRef, ) { const innerRef = useRef(null) useEffect(() => { if (innerRef.current) innerRef.current.indeterminate = indeterminate }, [indeterminate]) return ( { innerRef.current = node if (typeof forwardedRef === 'function') forwardedRef(node) else if (forwardedRef) { ;(forwardedRef as React.MutableRefObject).current = node } }} aria-invalid={invalid || undefined} className={cn( 'peer size-full cursor-pointer appearance-none rounded-[var(--radius-6)] border bg-surface transition-colors', 'border-line-strong hover:border-ink-faint', 'checked:border-accent-strong checked:bg-accent-strong', 'indeterminate:border-accent-strong indeterminate:bg-accent-strong', 'disabled:cursor-not-allowed disabled:opacity-40 disabled:hover:border-line-strong', invalid && 'border-danger hover:border-danger', className, )} {...props} /> ) })