Checkbox
An easily stylable checkbox component.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Checkbox;
use function Phem\Ui\CheckboxIndicator;
#[Component]
function CheckboxHeroDemo(): Node
{
return (
<label className="flex items-center gap-2 text-sm font-normal text-neutral-950 dark:text-white">
<Checkbox defaultChecked>
<CheckboxIndicator />
</Checkbox>
Enable notifications
</label>
);
}
Examples#
Labeling a checkbox#
An enclosing <label> is the simplest labeling pattern:
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Checkbox;
use function Phem\Ui\CheckboxIndicator;
#[Component]
function CheckboxLabelingDemo(): Node
{
return (
<label className="flex items-center gap-2 text-sm font-normal text-neutral-950 dark:text-white">
<Checkbox>
<CheckboxIndicator />
</Checkbox>
Accept terms and conditions
</label>
);
}
#
By default, Checkbox renders a <span> element to support enclosing labels. Prefer rendering the checkbox as a native button with nativeButton when using sibling labels (for/id).
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Checkbox;
use function Phem\Ui\CheckboxIndicator;
#[Component]
function CheckboxNativeButtonDemo(): Node
{
return (
<div className="flex items-center gap-2">
<label htmlFor="notifications-checkbox" className="text-sm text-neutral-950 dark:text-white">Enable notifications</label>
<Checkbox id="notifications-checkbox" nativeButton>
<CheckboxIndicator />
</Checkbox>
</div>
);
}
Form integration#
Use Field to handle label associations and form integration:
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\{Checkbox, Field};
use function Phem\Ui\{CheckboxIndicator, FieldLabel, Form};
#[Component]
function CheckboxFormDemo(): Node
{
return (
<Form>
<Field>
<FieldLabel className="flex items-center gap-2 font-normal">
<Checkbox name="stayLoggedIn">
<CheckboxIndicator />
</Checkbox>
Stay logged in for 7 days
</FieldLabel>
</Field>
</Form>
);
}
API reference#
Import the components of the family:
use Phem\Ui\Checkbox;
use Phem\Ui\CheckboxGroup;
use function Phem\Ui\CheckboxIndicator;Checkbox#
A box that the user checks and unchecks: Base UI Checkbox.Root, on the phem-toggle controller.
Put it in a <label> with its text. A hidden checkbox keeps the state for a form (name, value). Bind it to a bool state with phem:bind. With nativeButton, it is a <button> for a sibling <label for> (give it an id), and a hidden input keeps the state (a hidden checkbox when it is bound). In a CheckboxGroup, the group owns the state: give the checkbox a value, and parent to the checkbox that checks all the items.
| Prop | Type | Default |
|---|---|---|
defaultChecked | bool | false |
indeterminate | bool | false |
disabled | bool | false |
readOnly | bool | false |
required | bool | false |
parent | bool | false |
nativeButton | bool | false |
name | ?string | null |
value | ?string | null |
checked | ?Bound | null |
children | ?Html | null |
...attrs | array | [] |
CheckboxIndicator#
The check mark. It shows only when the checkbox is checked, or a dash when it is indeterminate.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
CheckboxGroup#
A group of checkboxes with one state: Base UI CheckboxGroup, on the phem-checkbox-group controller. defaultValue lists the values of the checked checkboxes, and allValues the values that a parent checkbox checks. Label it with aria-labelledby. With disabled, all its checkboxes are disabled. With a value, a group in a group is an item of the outer group: the outer parent checks all its checkboxes.
| Prop | Type | Default |
|---|---|---|
defaultValue | array | [] |
allValues | array | [] |
value | ?string | null |
disabled | bool | false |
children | ?Html | null |
...attrs | array | [] |
The source of the examples: examples/ui/Checkbox.phem.