'use client' import { Checkbox as GuiCheckbox, Label, Text, View, VisuallyHidden } from '@hanzo/gui'; import * as React from 'react'; /** Tri-state, as the Chakra/Radix contract consumers already code against. */ type CheckedState = boolean | 'indeterminate'; const NOOP = () => { /* noop */ }; // ─── CheckboxGroup context ─────────────────────────────────────────── interface CheckboxGroupContextValue { value: Array; toggle: (itemValue: string) => void; } const CheckboxGroupContext = React.createContext(null); function useCheckboxGroupContext(): CheckboxGroupContextValue | null { return React.useContext(CheckboxGroupContext); } // ─── CheckboxGroup ─────────────────────────────────────────────────── export interface CheckboxGroupProps extends React.ComponentPropsWithoutRef<'div'> { value?: Array; defaultValue?: Array; onValueChange?: (value: Array) => void; orientation?: 'vertical' | 'horizontal'; name?: string; disabled?: boolean; readOnly?: boolean; } const CheckboxGroupBase = React.forwardRef( function CheckboxGroup(props, ref) { const { children, value: controlledValue, defaultValue, onValueChange, orientation = 'vertical', name: _name, ...rest } = props; const [ uncontrolledValue, setUncontrolledValue ] = React.useState>(defaultValue ?? []); const isControlled = controlledValue !== undefined; const value = isControlled ? controlledValue : uncontrolledValue; const toggle = React.useCallback( (itemValue: string) => { const next = value.includes(itemValue) ? value.filter((v) => v !== itemValue) : [ ...value, itemValue ]; if (!isControlled) { setUncontrolledValue(next); } onValueChange?.(next); }, [ value, isControlled, onValueChange ], ); // Sync uncontrolled state when controlled value changes React.useEffect(() => { if (isControlled) { setUncontrolledValue(controlledValue); } }, [ isControlled, controlledValue ]); const ctx = React.useMemo(() => ({ value, toggle }), [ value, toggle ]); return ( { children } ); }, ); export const CheckboxGroup = CheckboxGroupBase; // ─── Checkbox ──────────────────────────────────────────────────────── export interface CheckboxProps extends Omit, 'onChange'> { icon?: React.ReactNode; inputProps?: React.InputHTMLAttributes; rootRef?: React.Ref; checked?: boolean | 'indeterminate'; defaultChecked?: boolean; onCheckedChange?: (checked: CheckedState) => void; onChange?: React.FormEventHandler; disabled?: boolean; readOnly?: boolean; value?: string; size?: 'sm' | 'md'; name?: string; required?: boolean; } const SIZE = { sm: { rootGap: 6, control: 14, radius: 2, label: 12, icon: 12 }, md: { rootGap: 8, control: 16, radius: 4, label: 14, icon: 14 }, } as const; const INK = 'var(--color-text-primary)' as never; const ON_INK = 'var(--color-bg-body)' as never; const RESTING_BORDER = 'var(--color-input-border)' as never; const OUTLINE = 'var(--color-gray-500)' as never; const CheckIcon = ({ size, color }: { readonly size: number; readonly color: string }) => ( ); const IndeterminateIcon = ({ size, color }: { readonly size: number; readonly color: string }) => ( ); const CheckboxBase = React.forwardRef( function Checkbox(props, ref) { const { icon, children, inputProps, rootRef, checked: checkedProp, defaultChecked, onCheckedChange, onChange, disabled = false, readOnly = false, value, size = 'md', name, required, ...rest } = props; const group = useCheckboxGroupContext(); const hiddenInputRef = React.useRef(null); // Merge external ref with internal ref const setHiddenInputRef = React.useCallback( (node: HTMLInputElement | null) => { hiddenInputRef.current = node; if (typeof ref === 'function') { ref(node); } else if (ref) { (ref as React.MutableRefObject).current = node; } }, [ ref ], ); // Determine checked state const isInGroup = group !== null && value !== undefined; const groupChecked = isInGroup ? group.value.includes(value) : undefined; const [ internalChecked, setInternalChecked ] = React.useState( defaultChecked ?? false, ); const isControlled = checkedProp !== undefined || isInGroup; let checkedState: CheckedState; if (checkedProp !== undefined) { checkedState = checkedProp; } else if (isInGroup) { checkedState = groupChecked ?? false; } else { checkedState = internalChecked; } const handleCheckedChange = React.useCallback( (nextChecked: CheckedState) => { if (readOnly) return; if (!isControlled) { setInternalChecked(nextChecked); } if (isInGroup) { group.toggle(value); } onCheckedChange?.(nextChecked); // Dispatch a native change event from the hidden input so that // onChange handlers on the label (which listen for bubbling change // events) continue to work as they did with the Chakra version. if (onChange && hiddenInputRef.current) { const input = hiddenInputRef.current; // Set the checked property to the new state before dispatching const nativeInputValueSetter = Object.getOwnPropertyDescriptor( HTMLInputElement.prototype, 'checked', )?.set; nativeInputValueSetter?.call(input, nextChecked === true); const event = new Event('change', { bubbles: true }); input.dispatchEvent(event); } }, [ readOnly, isControlled, isInGroup, group, value, onCheckedChange, onChange ], ); const s = SIZE[size]; const isChecked = checkedState !== false; return ( ); }, ); export const Checkbox = CheckboxBase;