{ "name": "checkbox", "displayName": "Checkbox", "type": "component", "files": [ { "name": "Checkbox.tsx", "content": "import {\n createContext,\n forwardRef,\n useContext,\n useEffect,\n useId,\n useRef,\n type InputHTMLAttributes,\n type ReactNode,\n} from 'react';\nimport { cn, vc } from '../../utils/cn';\nimport styles from './Checkbox.module.css';\n\n// -------------------- CheckboxGroupContext --------------------\n\ninterface CheckboxGroupContextValue {\n value: string[];\n onToggle: (v: string) => void;\n disabled?: boolean;\n}\nconst CheckboxGroupContext = createContext(\n null,\n);\n\n// -------------------- Checkbox --------------------\n\nexport interface CheckboxProps extends Omit<\n InputHTMLAttributes,\n 'type' | 'onChange' | 'size'\n> {\n label: string;\n /** Value used when part of a CheckboxGroup. */\n value?: string;\n checked?: boolean;\n defaultChecked?: boolean;\n indeterminate?: boolean;\n onChange?: (\n checked: boolean,\n event: React.ChangeEvent,\n ) => void;\n}\n\nexport const Checkbox = forwardRef(\n function Checkbox(\n {\n label,\n value,\n checked,\n defaultChecked,\n indeterminate = false,\n disabled,\n id,\n className,\n onChange,\n 'data-testid': dataTestId = 'tatva-checkbox',\n ...rest\n }: CheckboxProps & { 'data-testid'?: string },\n ref,\n ) {\n const generatedId = useId();\n const inputId = id ?? generatedId;\n const group = useContext(CheckboxGroupContext);\n const innerRef = useRef(null);\n\n const inGroup = group !== null && value !== undefined;\n const effectiveChecked = inGroup ? group.value.includes(value) : checked;\n const effectiveDisabled = disabled ?? group?.disabled;\n\n useEffect(() => {\n if (innerRef.current) innerRef.current.indeterminate = indeterminate;\n }, [indeterminate]);\n\n return (\n \n {\n innerRef.current = node;\n if (typeof ref === 'function') ref(node);\n else if (ref)\n (ref as React.MutableRefObject).current =\n node;\n }}\n id={inputId}\n type=\"checkbox\"\n className={styles.input}\n checked={effectiveChecked}\n defaultChecked={inGroup ? undefined : defaultChecked}\n disabled={effectiveDisabled}\n aria-checked={indeterminate ? 'mixed' : effectiveChecked}\n data-testid={dataTestId}\n onChange={(e) => {\n if (inGroup && value !== undefined) group.onToggle(value);\n onChange?.(e.target.checked, e);\n }}\n {...rest}\n />\n \n {indeterminate ? (\n \n \n \n ) : (\n \n \n \n )}\n \n {label}\n \n );\n },\n);\n\n// -------------------- CheckboxGroup --------------------\n\nexport interface CheckboxGroupProps {\n value: string[];\n onChange: (value: string[]) => void;\n orientation?: 'horizontal' | 'vertical';\n disabled?: boolean;\n children: ReactNode;\n className?: string;\n label?: string;\n}\n\nexport function CheckboxGroup({\n value,\n onChange,\n orientation = 'vertical',\n disabled,\n children,\n className,\n label,\n}: CheckboxGroupProps) {\n const onToggle = (v: string) => {\n onChange(value.includes(v) ? value.filter((x) => x !== v) : [...value, v]);\n };\n return (\n \n \n {children}\n \n \n );\n}\n" }, { "name": "Checkbox.module.css", "content": ".wrapper {\n display: inline-flex;\n align-items: center;\n gap: var(--tatva-space-2);\n cursor: pointer;\n font-family: var(--tatva-font-family);\n font-size: var(--tatva-font-size-md);\n color: var(--tatva-color-fg);\n user-select: none;\n}\n\n.disabled {\n opacity: 0.55;\n cursor: not-allowed;\n}\n\n.input {\n position: absolute;\n opacity: 0;\n width: 1px;\n height: 1px;\n margin: 0;\n}\n\n.box {\n display: inline-flex;\n align-items: center;\n justify-content: center;\n width: 18px;\n height: 18px;\n border: 1px solid var(--tatva-color-border-strong);\n border-radius: var(--tatva-radius-sm);\n background-color: var(--tatva-color-bg);\n color: transparent;\n transition:\n background-color var(--tatva-transition-fast),\n border-color var(--tatva-transition-fast),\n color var(--tatva-transition-fast);\n flex: 0 0 auto;\n}\n\n.input:focus-visible + .box {\n border-color: var(--tatva-color-primary-500);\n box-shadow: 0 0 0 3px var(--tatva-color-primary-200);\n}\n\n.input:checked + .box,\n.input[aria-checked='mixed'] + .box {\n background-color: var(--tatva-color-primary-600);\n border-color: var(--tatva-color-primary-600);\n color: #ffffff;\n}\n\n.label {\n line-height: var(--tatva-line-height-normal);\n}\n\n.group {\n display: flex;\n gap: var(--tatva-space-3);\n}\n.orientationVertical {\n flex-direction: column;\n align-items: flex-start;\n}\n.orientationHorizontal {\n flex-direction: row;\n flex-wrap: wrap;\n align-items: center;\n}\n" }, { "name": "index.ts", "content": "export { Checkbox, CheckboxGroup } from './Checkbox';\nexport type { CheckboxProps, CheckboxGroupProps } from './Checkbox';\n" } ], "registryDependencies": [], "dependencies": [], "devDependencies": [] }