Checkbox

<wa-checkbox> Since 2.0 stable

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>

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.

Small
Medium
Large
<wa-checkbox size="small">Small</wa-checkbox>
<br />
<wa-checkbox size="medium">Medium</wa-checkbox>
<br />
<wa-checkbox size="large">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 type="submit" variant="brand" 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>

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.

Importing

The autoloader is the recommended way to import components. If you prefer to do it manually, use one of the following code snippets.

CDN npm React

To manually import this component from the CDN, use the following code.

import 'https://early.webawesome.com/webawesome@3.0.0-beta.2/dist/components/checkbox/checkbox.js';

To manually import this component from NPM, use the following code.

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

To manually import this component from React, use the following code.

import '@awesome.me/webawesome/dist/react/checkbox';
Need a hand? Report a bug Ask for help
    No results