Skip to main content
Light Dark System

Checkbox

<zn-checkbox> | ZnCheckbox
Since 1.0 experimental

Short summary of the component’s intended use.

Examples

Basic Checkbox

Financial products access
<zn-checkbox>Financial products access</zn-checkbox>

Description

Add descriptive help text to individual checkbox items with the description attribute. For descriptions that contain HTML, use the description slot instead.

Financial products access
<zn-checkbox description="Grants access to cash account and charge card features">Financial products access</zn-checkbox>

Contained

Add the contained attribute to draw a card-like container around a checkbox. Add to a Checkbox Group to draw a container around each checkbox in the group. This style is useful for giving more emphasis to a checkbox or list of checkboxes.

Financial products access

Initiate outbound transfers Approve outbound transfers Export transactions
<zn-checkbox description="Grants access to cash account and charge card features" contained>Financial products access</zn-checkbox>
<br/>
<br/>
<zn-checkbox-group label="Financial products permissions" contained>
  <zn-checkbox description="Requires separate initiators and approvers">Initiate outbound transfers</zn-checkbox>
  <zn-checkbox description="Requires separate initiators and approvers">Approve outbound transfers </zn-checkbox>
  <zn-checkbox description="Applies to both cash account and charge card" disabled>Export transactions</zn-checkbox>
<zn-checkbox-group>

Selected Content

Use the selected-content slot to display additional content (such as an input field) inside a contained checkbox when it is checked. The slot is unstyled by default. Use ::part(selected-content) to style the content as needed.

Grant financial products access

A mobile number is required to grant this user access to financial products. The number will be used for login verification.

<zn-checkbox style="width:100%" contained>Grant financial products access
  <div slot="selected-content">
    <p>A mobile number is required to grant this user access to financial products. The number will be used for login verification.</p>
    <zn-input style="width: 280px;" label="Mobile number" type="tel" required optional-icon></div>
</zn-checkbox>
<style>
 zn-checkbox::part(selected-content) {
    font-size: 14px;
    font-weight: normal;
    color: #6D7176;
  }
</style>

Checked

Use the checked attribute to activate the checkbox.

Financial products access
<zn-checkbox checked>Financial products access</zn-checkbox>

Indeterminate

Use the indeterminate attribute to make the checkbox indeterminate.

Indeterminate
<zn-checkbox indeterminate>Indeterminate</zn-checkbox>

Disabled

Use the disabled attribute to disable the checkbox.

Disabled
<zn-checkbox disabled>Disabled</zn-checkbox>

Sizes

Use the size attribute to change a checkboxs size.

Small
Medium
Large
<zn-checkbox size="small">Small</zn-checkbox>
<br />
<zn-checkbox size="medium">Medium</zn-checkbox>
<br />
<zn-checkbox size="large">Large</zn-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">
  <zn-checkbox>Check me</zn-checkbox>
  <br />
  <zn-button type="submit" variant="primary" style="margin-top: 1rem;">Submit</zn-button>
</form>
<script type="module">
  const form = document.querySelector('.custom-validity');
  const checkbox = form.querySelector('zn-checkbox');
  const errorMessage = `Do not forget to check me!`;

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

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

  // Wait for controls to be defined before attaching form listeners
  await Promise.all([
    customElements.whenDefined(zn-checkbox'),
  ]).then(() => {
    form.addEventListener('submit', event => {
      event.preventDefault();
      alert('All fields are valid!');
    });
  });
</script>

Importing

If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.

To import this component from the CDN using a script tag:

<script type="module" src="https://cdn.jsdelivr.net/npm/@kubex/zinc@1.0.0/dist/components/checkbox/checkbox.js"></script>

To import this component from the CDN using a JavaScript import:

import 'https://cdn.jsdelivr.net/npm/@kubex/zinc@1.0.0/dist/components/checkbox/checkbox.js';

To import this component using a bundler:

import '@kubex/zinc/dist/components/checkbox/checkbox.js';

Slots

Name Description
(default) The checkbox’s label.
description A description of the checkbox’s label. Serves as help text for a checkbox item. Alternatively, you can use the description attribute.
selected-content Use to nest rich content (like an input) inside a selected checkbox item. Use only with the contained style.

Learn more about using slots.

Properties

Name Description Reflects Type Default
name The name of the checkbox, submitted as a name/value pair with form data. string ''
value The current value of the checkbox, submitted as a name/value pair with form data. string -
size The checkbox’s size. 'small' | 'medium' | 'large' 'medium'
disabled Disables the checkbox. boolean false
checked Draws the checkbox in a checked state. boolean false
indeterminate Draws the checkbox in an indeterminate state. This is usually applied to checkboxes that represents a “select all/none” behavior when associated checkboxes have a mix of checked and unchecked states. boolean false
contained Draws a container around the checkbox. boolean false
horizontal Applies styles relevant to checkboxes in a horizontal layout. boolean false
defaultChecked The default value of the form control. Primarily used for resetting the form control. boolean false
form By default, form controls are associated with the nearest containing <form> element. This attribute allows you to place the form control outside of a form and associate it with the form that has this id. The form must be in the same document or shadow root for this to work. string ''
required Makes the checkbox a required field. boolean false
description The checkbox’s help text. If you need to display HTML, use the description slot instead. string ''
validity Gets the validity state object - -
validationMessage Gets the validation message - -
updateComplete A read-only promise that resolves when the component has finished updating.

Learn more about attributes and properties.

Events

Name React Event Description Event Detail
zn-blur Emitted when the checkbox loses focus. -
zn-change Emitted when the checked state changes. -
zn-focus Emitted when the checkbox gains focus. -
zn-input Emitted when the checkbox receives input. -
zn-invalid Emitted when the form control has been checked for validity and its constraints aren’t satisfied. -

Learn more about events.

Methods

Name Description Arguments
click() Simulates a click on the checkbox. -
focus() Sets focus on the checkbox. options: FocusOptions
blur() Removes focus from the checkbox. -
checkValidity() Checks for validity but does not show a validation message. Returns true when valid and false when invalid. -
getForm() Gets the associated form, if one exists. -
reportValidity() Checks for validity and shows the browser’s validation message if the control is invalid. -
setCustomValidity() Sets a custom validation message. The value provided will be shown to the user when the form is submitted. To clear the custom validation message, call this method with an empty string. message: string

Learn more about methods.

Parts

Name Description
base The component’s base wrapper.
control The square container that wraps the checkbox’s checked state.
control--checked Matches the control part when the checkbox is checked.
control--indeterminate Matches the control part when the checkbox is indeterminate.
checked-icon The checked icon, an <zn-icon> element.
indeterminate-icon The indeterminate icon, an <zn-icon> element.
label The container that wraps the checkbox’s label.
description The container that wraps the checkbox’s description.
selected-content The container that wraps optional content that appears when a checkbox is checked.

Learn more about customizing CSS parts.

Dependencies

This component automatically imports the following dependencies.

  • <zn-example>
  • <zn-icon>