Menu

Menu

On this page

On this page

Checkbox

Allow users to select multiple options from a set.

npm install @vrembem/checkbox
Copied!
SCSS
@use "@vrembem/checkbox";
Copied!

Options

Checkbox uses design token CSS variables in a reference and fallback pattern. Variables are referenced in the component's styles but are not directly defined; instead, they are provided with sensible fallbacks.

VariableFallback
--vb-checkbox-color
Copied!
--vb-checkbox-accent
Copied!
--vb-checkbox-fill
Copied!
--vb-checkbox-font-size
Copied!
--vb-checkbox-border-width
Copied!
2px
Copied!
--vb-checkbox-border-radius
Copied!
0.25em
Copied!
--vb-checkbox-icon-size
Copied!
0.8em
Copied!

Basic usage

Checkbox form controls are composed using the <input type="checkbox"> element along with the .checkbox class.

<input class="checkbox" type="checkbox" />
Copied!
// Get checkboxes with "mixed" status
const checkboxes = document.querySelectorAll(
  "input[type='checkbox'][aria-checked='mixed']"
);

// Apply the indeterminate state
checkboxes.forEach((checkbox) => checkbox.indeterminate = true);
Copied!

Indeterminate checkboxes need to be set in JavaScript though styling is provided by Vrembem for the indeterminate states (see example above). Review MDN documentation for more details.

Labels

For checkboxes with labels, wrap the checkbox element along with the text label using the <label> element.



HTML
<label>
  <input class="checkbox" type="checkbox" />
  <span>Checkbox with a label</span>
</label>
Copied!

Disabled

Adding the boolean disabled attribute to the input element will provide visual indication that the checkbox is not available for use.



HTML
<input class="checkbox" type="checkbox" disabled />
Copied!

Invalid

Adding the is-invalid class or aria-invalid="true" attribute to the checkbox component will provide visual indication that the form-control value is invalid. Invalid state is also triggered by the native :user-invalid state.

HTML
<input class="checkbox is-invalid" type="checkbox" />
Copied!