Checkbox
A native checkbox, styled.
Also uses Label.
checkbox-demo.tsx
import { Checkbox } from './ui/checkbox.tsx'
import { Label } from './ui/label.tsx'
export default () => (
<div style="display: grid; gap: 1.5rem">
<div style="display: flex; align-items: center; gap: 0.75rem">
<Checkbox id="terms" />
<Label for="terms">Accept terms and conditions</Label>
</div>
<div style="display: flex; align-items: center; gap: 0.75rem">
<Checkbox id="newsletter" checked />
<Label for="newsletter">Subscribe to the newsletter</Label>
</div>
<div style="display: flex; align-items: center; gap: 0.75rem">
<Checkbox id="disabled" disabled />
<Label for="disabled">Enable notifications</Label>
</div>
</div>
)
Installation
dui add checkboxOr copy ui/checkbox.tsx and ui/checkbox.css into your project, with lib/jsx/jsx-runtime.ts, lib/jsx/jsx-dev-runtime.ts, lib/css.d.ts, and lib/theme.css.
Examples
With a description
By checking this, you agree to the terms of service.
Also uses Field.
checkbox-description.tsx
import { Checkbox } from './ui/checkbox.tsx'
import { Field, FieldContent, FieldDescription, FieldLabel } from './ui/field.tsx'
export default () => (
<Field orientation="horizontal" style="max-width: 24rem">
<Checkbox id="terms-2" name="terms" checked />
<FieldContent>
<FieldLabel for="terms-2">Accept terms and conditions</FieldLabel>
<FieldDescription>By checking this, you agree to the terms of service.</FieldDescription>
</FieldContent>
</Field>
)
With Datastar
checkbox-datastar.tsx
import { Button } from './ui/button.tsx'
import { Checkbox } from './ui/checkbox.tsx'
import { Label } from './ui/label.tsx'
// data-bind keeps the box and the $agreed signal in step.
export default () => (
<div data-signals:agreed="false" style="display: grid; gap: 1rem; justify-items: start">
<Label>
<Checkbox data-bind:agreed /> I've read the terms
</Label>
<Button data-attr:disabled="!$agreed" disabled>
Continue
</Button>
</div>
)
Compared to shadcn/ui
- A native
<input type="checkbox">instead of Radix's button and hidden input: it's sent with its form and works without JavaScript. checkedis the attribute;onCheckedChangeisdata-bindordata-on:change.- Indeterminate is the input's property, not an attribute: set it with Datastar, like
data-effect="el.indeterminate = $some && !$all".