COMPONENTS/INPUTS
Checkbox.
Choose one thing. Or a few.
In practice
Compose CheckboxIndicator inside Checkbox. Supports checked, indeterminate, name, required and disabled.
Installation
Install Coal once. Components and TypeScript declarations are included. React 19 and React DOM 19 are peer dependencies.
Terminal · local package
npm install http://localhost:3100/downloads/chlohal-coal-ui-0.5.0.tgzThis installs the local package. It is not yet published on npm. See installation for stylesheet setup and integration.
Usage
example.tsx
"use client";
import * as React from "react";
import "@chlohal/coal-ui/styles.css";
import { Check } from "lucide-react";
import { Checkbox, CheckboxIndicator } from "@chlohal/coal-ui";
export default function Example() {
return (<div>
{[
"A little more intention",
"A little less noise",
"Make it yours",
].map((s, i) => (<label key={s}>
<Checkbox defaultChecked={i === 0}>
<CheckboxIndicator>
<Check size={12}/>
</CheckboxIndicator>
</Checkbox>
{s}
</label>))}
</div>);
}
Source
The original Coal implementation. Use the package export to integrate it.
packages/react/src/checkbox.tsx
"use client";
import * as React from "react";
import { cn } from "./internal.js";
export type CheckboxProps = Omit<
React.ComponentPropsWithRef<"input">,
"type" | "size"
> & { indeterminate?: boolean; onCheckedChange?: (checked: boolean) => void };
export function Checkbox({
checked,
defaultChecked = false,
onCheckedChange,
onChange,
indeterminate = false,
children,
className,
ref,
...props
}: CheckboxProps) {
const input = React.useRef<HTMLInputElement | null>(null);
React.useEffect(() => {
if (input.current) input.current.indeterminate = indeterminate;
}, [indeterminate]);
return (
<span
className={cn("coal-checkbox", className)}
data-indeterminate={indeterminate || undefined}
data-disabled={props.disabled || undefined}
>
<input
{...props}
ref={(node) => {
input.current = node;
if (typeof ref === "function") ref(node);
else if (ref) ref.current = node;
}}
type="checkbox"
checked={checked}
defaultChecked={checked === undefined ? defaultChecked : undefined}
onChange={(e) => {
onChange?.(e);
if (!e.defaultPrevented) onCheckedChange?.(e.target.checked);
}}
/>
{children ?? (
<CheckboxIndicator>{indeterminate ? "−" : "✓"}</CheckboxIndicator>
)}
</span>
);
}
export function CheckboxIndicator({
className,
...props
}: React.ComponentPropsWithRef<"span">) {
return (
<span
aria-hidden="true"
className={cn("coal-checkbox-indicator", className)}
{...props}
/>
);
}
Accessibility checklist
- Provide an accessible name for controls, including icon-only buttons.
- Check keyboard navigation, visible focus and disabled states in your application.
- Do not rely on color alone to communicate status.