Checkbox

<wa-checkbox> Stable Since 2.0

Checkboxes allow the user to toggle an option on or off.

Checkbox
<wa-checkbox>Checkbox</wa-checkbox>

This component works with standard <form> elements. Please refer to the section on form controls to learn more about form submission and client-side validation.

Examples

Checked

Use the checked attribute to activate the checkbox.

Checked
<wa-checkbox checked>Checked</wa-checkbox>

The checked attribute is the initial value and does not reflect changes, consistent with native checkboxes. To toggle the checked state with JavaScript, use the checked property instead. To target checked checkboxes with CSS, use the :state(checked) selector.

Indeterminate

Use the indeterminate attribute to make the checkbox indeterminate.

Indeterminate
<wa-checkbox indeterminate>Indeterminate</wa-checkbox>

Disabled

Use the disabled attribute to disable the checkbox.

Disabled
<wa-checkbox disabled>Disabled</wa-checkbox>

Sizes

Use the size attribute to change a checkbox's size.

Extra Small
Small
Medium
Large
Extra Large
<wa-checkbox size="xs">Extra Small</wa-checkbox>
<br />
<wa-checkbox size="s">Small</wa-checkbox>
<br />
<wa-checkbox size="m">Medium</wa-checkbox>
<br />
<wa-checkbox size="l">Large</wa-checkbox>
<br />
<wa-checkbox size="xl">Extra Large</wa-checkbox>

Hint

Add descriptive hint to a switch with the hint attribute. For hints that contain HTML, use the hint slot instead.

Label
<wa-checkbox hint="What should the user know about the checkbox?">Label</wa-checkbox>

Custom Validity

Use the setCustomValidity() method to set a custom validation message. This will prevent the form from submitting and make the browser display the error message you provide. To clear the error, call this function with an empty string.

Check me
Submit
<form class="custom-validity">
  <wa-checkbox>Check me</wa-checkbox>
  <br />
  <wa-button appearance="filled" type="submit" variant="neutral" style="margin-top: 1rem;">Submit</wa-button>
</form>
<script>
  const form = document.querySelector('.custom-validity');
  const checkbox = form.querySelector('wa-checkbox');
  const errorMessage = `Don't forget to check me!`;

  // Set initial validity as soon as the element is defined
  customElements.whenDefined('wa-checkbox').then(async () => {
    await checkbox.updateComplete;
    checkbox.setCustomValidity(errorMessage);
  });

  // Update validity on change
  checkbox.addEventListener('change', () => {
    checkbox.setCustomValidity(checkbox.checked ? '' : errorMessage);
  });

  // Handle submit
  customElements.whenDefined('wa-checkbox').then(() => {
    form.addEventListener('submit', event => {
      event.preventDefault();
      alert('All fields are valid!');
    });
  });
</script>

Importing

If you're using the autoloader or a hosted project, components load on demand — no manual import needed. To cherry-pick a component manually, use one of the following snippets.

CDN npm Self-Hosted React

Import this component directly from the CDN:

import 'https://ka-f.webawesome.com/webawesome@0.14.0/components/checkbox/checkbox.js';

After installing Web Awesome via npm, import this component:

import '@awesome.me/webawesome/dist/components/checkbox/checkbox.js';

If you're self-hosting Web Awesome, import this component from your server:

import './webawesome/dist/components/checkbox/checkbox.js';

To import this component for React 18 or below, use the following code:

import WaCheckbox from '@awesome.me/webawesome/dist/react/checkbox/index.js';

Slots

Learn more about using slots.

Attributes & Properties

Learn more about attributes and properties.

Methods

Learn more about methods.

Events

Learn more about events.

CSS custom properties

Learn more about CSS custom properties.

Custom States

Learn more about custom states.

CSS parts

Learn more about CSS parts.

Dependencies

This component automatically imports the following elements. Sub-dependencies, if any exist, will also be included in this list.

Need a hand? Report a bug Ask for help
    No results