Component
Checkbox Indeterminate
Real indeterminate checkbox that sets the HTMLInputElement.indeterminate property imperatively.
- Component
- React
- TSX
- Tailwind CSS
- MIT
What it does
Capabilities
- Semantic HTML
- Native form control
- Native checkbox
- Associated labels
- ARIA
Preview
Live component
9:41 100%
devsnips.site/library/react/components/checkboxes/checkbox-indeterminate/ fluid · no fixed width 100%
Install
Add to your project
npx devsnips add React/Components/Checkboxes/checkbox-indeterminate React/Components/Checkboxes/checkbox-indeterminate import type { ChangeEvent, ReactNode } from "react";
import { useEffect, useId, useRef, useState } from "react";
function cx(...parts: Array<string | false | null | undefined>): string {
return parts.filter(Boolean).join(" ");
}
export interface CheckboxIndeterminateProps {
label?: ReactNode;
checked?: boolean;
defaultChecked?: boolean;
indeterminate?: boolean;
onChange?: (checked: boolean, event: ChangeEvent<HTMLInputElement>) => void;
disabled?: boolean;
required?: boolean;
invalid?: boolean;
name?: string;
value?: string | number | readonly string[];
id?: string;
"aria-describedby"?: string;
className?: string;
}
/**
* Checkbox that supports a REAL indeterminate state. The underlying
* `HTMLInputElement.indeterminate` property is set imperatively on the DOM
* node (there is no HTML attribute for it) via a layout-effect that runs
* whenever `indeterminate` changes. The indeterminate indicator is a
* horizontal dash, distinct from the checked check mark. Built on the native
* `<input type="checkbox">`.
*/
export function CheckboxIndeterminate({
label,
checked,
defaultChecked,
indeterminate = false,
onChange,
disabled,
required,
invalid,
name,
value,
id,
"aria-describedby": ariaDescribedby,
className,
}: CheckboxIndeterminateProps) {
const generatedId = useId();
const inputId = id ?? `checkbox-${generatedId}`;
const ref = useRef<HTMLInputElement>(null);
const isControlled = checked !== undefined;
const [internal, setInternal] = useState<boolean>(defaultChecked ?? false);
const isChecked = isControlled ? checked : internal;
// Imperatively set the indeterminate IDL property — no HTML attribute.
useEffect(() => {
if (ref.current) ref.current.indeterminate = indeterminate;
}, [indeterminate]);
function handleChange(event: ChangeEvent<HTMLInputElement>) {
if (!isControlled) setInternal(event.target.checked);
onChange?.(event.target.checked, event);
}
const control = (
<span className="relative inline-flex size-[18px] shrink-0 items-center justify-center">
<input
ref={ref}
id={inputId}
type="checkbox"
className={cx(
"size-[18px] cursor-pointer appearance-none rounded-[var(--ds-radius-xs)] border bg-[var(--ds-color-input)] transition-colors duration-150 ease-out hover:border-[var(--ds-color-border-strong)] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--ds-color-focus-ring)] disabled:cursor-not-allowed disabled:opacity-50 motion-reduce:transition-none",
invalid
? "border-[var(--ds-color-destructive)]"
: "border-[var(--ds-color-border)]",
(indeterminate || isChecked) && !invalid
? "bg-[var(--ds-color-primary)] border-[var(--ds-color-primary)]"
: "",
(indeterminate || isChecked) && invalid
? "bg-[var(--ds-color-destructive)] border-[var(--ds-color-destructive)]"
: "",
)}
checked={isControlled ? isChecked : undefined}
defaultChecked={isControlled ? undefined : defaultChecked}
disabled={disabled}
required={required}
aria-invalid={invalid ? true : undefined}
aria-describedby={ariaDescribedby}
name={name}
value={value}
onChange={handleChange}
/>
<span
aria-hidden="true"
className={cx(
"pointer-events-none absolute inset-0 flex items-center justify-center text-[var(--ds-color-primary-foreground)] transition-opacity duration-150 motion-reduce:transition-none",
indeterminate || isChecked ? "opacity-100" : "opacity-0",
)}
>
{indeterminate ? (
<span className="block h-[2px] w-[10px] rounded-full bg-current" />
) : (
<svg className="size-[12px]" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={3.5} strokeLinecap="round" strokeLinejoin="round">
<path d="M20 6 9 17l-5-5" />
</svg>
)}
</span>
</span>
);
if (!label) return control;
return (
<label
htmlFor={inputId}
className={cx(
"inline-flex items-center gap-2.5 text-sm leading-5 text-[var(--ds-color-foreground)]",
disabled ? "cursor-not-allowed opacity-60" : "cursor-pointer",
className,
)}
>
{control}
<span className="select-none">
{label}
{required ? (
<span aria-hidden="true" className="ml-0.5 text-[var(--ds-color-destructive)]">*</span>
) : null}
</span>
</label>
);
}
export default CheckboxIndeterminate; /* DevSnips React — JavaScript parity build.
* Same API, behavior, and classes as code.tsx; TypeScript types removed.
* Regenerated from code.tsx — edit code.tsx and re-run the generator.
*/
import { useEffect, useId, useRef, useState } from "react";
function cx(...parts) {
return parts.filter(Boolean).join(" ");
}
export function CheckboxIndeterminate({
label,
checked,
defaultChecked,
indeterminate = false,
onChange,
disabled,
required,
invalid,
name,
value,
id,
"aria-describedby": ariaDescribedby,
className
}) {
const generatedId = useId();
const inputId = id ?? `checkbox-${generatedId}`;
const ref = useRef(null);
const isControlled = checked !== undefined;
const [internal, setInternal] = useState(defaultChecked ?? false);
const isChecked = isControlled ? checked : internal;
useEffect(() => {
if (ref.current) ref.current.indeterminate = indeterminate;
}, [indeterminate]);
function handleChange(event) {
if (!isControlled) setInternal(event.target.checked);
onChange?.(event.target.checked, event);
}
const control = <span className="relative inline-flex size-[18px] shrink-0 items-center justify-center">
<input
ref={ref}
id={inputId}
type="checkbox"
className={cx(
"size-[18px] cursor-pointer appearance-none rounded-[var(--ds-radius-xs)] border bg-[var(--ds-color-input)] transition-colors duration-150 ease-out hover:border-[var(--ds-color-border-strong)] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--ds-color-focus-ring)] disabled:cursor-not-allowed disabled:opacity-50 motion-reduce:transition-none",
invalid ? "border-[var(--ds-color-destructive)]" : "border-[var(--ds-color-border)]",
(indeterminate || isChecked) && !invalid ? "bg-[var(--ds-color-primary)] border-[var(--ds-color-primary)]" : "",
(indeterminate || isChecked) && invalid ? "bg-[var(--ds-color-destructive)] border-[var(--ds-color-destructive)]" : ""
)}
checked={isControlled ? isChecked : undefined}
defaultChecked={isControlled ? undefined : defaultChecked}
disabled={disabled}
required={required}
aria-invalid={invalid ? true : undefined}
aria-describedby={ariaDescribedby}
name={name}
value={value}
onChange={handleChange}
/>
<span
aria-hidden="true"
className={cx(
"pointer-events-none absolute inset-0 flex items-center justify-center text-[var(--ds-color-primary-foreground)] transition-opacity duration-150 motion-reduce:transition-none",
indeterminate || isChecked ? "opacity-100" : "opacity-0"
)}
>
{indeterminate ? <span className="block h-[2px] w-[10px] rounded-full bg-current" /> : <svg className="size-[12px]" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={3.5} strokeLinecap="round" strokeLinejoin="round">
<path d="M20 6 9 17l-5-5" />
</svg>}
</span>
</span>;
if (!label) return control;
return <label
htmlFor={inputId}
className={cx(
"inline-flex items-center gap-2.5 text-sm leading-5 text-[var(--ds-color-foreground)]",
disabled ? "cursor-not-allowed opacity-60" : "cursor-pointer",
className
)}
>
{control}
<span className="select-none">
{label}
{required ? <span aria-hidden="true" className="ml-0.5 text-[var(--ds-color-destructive)]">*</span> : null}
</span>
</label>;
}
export default CheckboxIndeterminate; # Checkbox Indeterminate
Real indeterminate checkbox that sets the HTMLInputElement.indeterminate property imperatively.
## Usage
```tsx
<CheckboxIndeterminate label="Notifications" indeterminate />
```
## JavaScript
A `code.jsx` build is provided for projects that ship plain JSX. It exposes the same API and behavior as `code.tsx` — only the TypeScript types are removed.
```jsx
<CheckboxIndeterminate label="Notifications" indeterminate />
```
## Props
| Name | Type | Default | Description |
|---|---|---:|---|
| `label` | `ReactNode` | — | Visible label (omit for an icon-only / aria-label control). |
| `checked` / `defaultChecked` | `boolean` | `false` | Controlled / uncontrolled checked state. |
| `onChange` | `(checked, event) => void` | — | Change callback. |
| `disabled` | `boolean` | — | Disables the control. |
| `readOnly` | `boolean` | — | Read-only (blocks toggling). |
| `required` | `boolean` | — | Marks the field required (renders `*`). |
| `invalid` | `boolean` | — | Sets `aria-invalid` + destructive styling. |
| `name` / `value` | `string` | — | Native form name/value. |
| `id` | `string` | generated | Input id (also the label `htmlFor`). |
| `aria-label` / `aria-labelledby` / `aria-describedby` | `string` | — | Override association. |
| `indeterminate` | `boolean` | `false` | Sets the native `.indeterminate` IDL property (imperative). Renders a dash indicator. |
## States
A REAL indeterminate checkbox. The underlying `HTMLInputElement.indeterminate` property is set imperatively on the DOM node (there is no HTML attribute for it) via an effect that runs whenever `indeterminate` changes. The indeterminate indicator is a horizontal dash, distinct from the checked check mark. `checked` and `indeterminate` are independent.
## Accessibility
Native `<input type="checkbox">` with the `.indeterminate` IDL property set on the DOM node. Keyboard behavior is native. `aria-invalid` when `invalid`, visible `focus-visible` ring.
## Styling
Built with React, Tailwind CSS, and DevSnips design tokens. The component consumes the `--ds-*` semantic tokens via arbitrary values (for example `bg-[var(--ds-color-primary)]`). Define the tokens once in your theme — no component-specific CSS file is required.
## Design Tokens
See [React/DESIGN_TOKENS.md](../../../DESIGN_TOKENS.md) for the authoritative token specification. This checkbox uses the semantic color, radius, spacing, and motion tokens.
## Notes
This is the primitive behind `checkbox-with-select-all`. Browsers have no HTML attribute for indeterminate — it must be set in JS on the DOM node, which is why an effect + ref is used rather than a prop-to-attribute mapping. 132 lines UTF-8 · LF · Spaces: 2
DevSnips AI ready Claude · Cursor · Windsurf
Agent skill & Model Context Protocol
Pass this component straight into your AI workflow. DevSnips exposes each resource as a structured, addressable registry entry, so your agent installs the real files instead of approximating them.
devsnips://components/checkboxes/checkbox-indeterminate I want to use this DevSnips resource in production: Page: https://devsnips.site/library/react/components/checkboxes/checkbox-indeterminate/ Install: npx devsnips add React/Components/Checkboxes/checkbox-indeterminate This is the Checkbox Indeterminate — Real indeterminate checkbox that sets the HTMLInputElement.indeterminate property imperatively. Use this DevSnips resource as the starting point for my project. Adapt it to my project's stack and conventions, fix any issues, and make it production-ready while preserving its intended design and behavior.
Paste into ChatGPT, Claude or your agent — it already carries the page URL and the exact install path.
Resource URI
devsnips://components/checkboxes/checkbox-indeterminate