Web Components Checkbox Overview
The Web Components Checkbox is a component that lets you add checkboxes to your Web Components apps. It behaves as a standard HTML checkbox, enabling users to select basic checked and unchecked states or an additional indeterminate state. You also get full control over the styling of the Web Components checkbox component and ability to use it with forms.
Checkbox Example
Usage
At its core, the IgcCheckboxComponent
allows for a choice between selected/unselected state. The default styling is done according to the selection controls specification in the Material Design guidelines.
First, you need to install the Ignite UI for Web Components by running the following command:
npm install igniteui-webcomponents
You will then need to import the IgcCheckboxComponent
, its necessary CSS, and register its module, like so:
import { defineComponents, IgcCheckboxComponent } from "igniteui-webcomponents";
import 'igniteui-webcomponents/themes/light/bootstrap.css';
defineComponents(IgcCheckboxComponent);
For a complete introduction to the Ignite UI for Web Components, read the Getting Started topic.
The simplest way to start using the IgcCheckboxComponent
is as follows:
<igc-checkbox></igc-checkbox>
[!WARNING] The
IgcCheckboxComponent
component doesn't work with the standard<form>
element. UseForm
instead.
Examples
Label
To provide a meaningful label for the checkbox, simply place some text between the opening and closing tags:
<igc-checkbox>Label</igc-checkbox>
You can specify if the label should be positioned before or after the checkbox toggle by setting the label-position
attribute of the checkbox. Allowed values are before
and after
(default):
<igc-checkbox label-position="before">Label</igc-checkbox>
The checkbox can also be labelled by elements external to the checkbox. In this case, the user is given full control to position and style the label in accordance with their needs.
<span id="checkbox-label">Label</span>
<igc-checkbox aria-labelledby="checkbox-label"></igc-checkbox>
Checked
You can use the checked
attribute of the component to determine whether the checkbox should be toggled on or off by default.
<igc-checkbox checked></igc-checkbox>
Indeterminate
You can use the indeterminate
property of the component to set the checkbox's value to neither true nor false.
<igc-checkbox indeterminate></igc-checkbox>
Required
You can use the required
property to mark the checkbox as required.
<igc-checkbox required></igc-checkbox>
Invalid
You can use the invalid
attribute to mark the checkbox as invalid.
<igc-checkbox invalid></igc-checkbox>
Disabled
You can use the disabled
attribute to disable the checkbox.
<igc-checkbox disabled></igc-checkbox>
Forms
You can use the name
and value
attributes when using the checkbox with Form
.
<igc-checkbox name="wifi" value="enabled"></igc-checkbox>
Styling
The checkbox component exposes several CSS parts (base
, control
, indicator
, and label
) to give you full control over its styling.
igc-checkbox::part(indicator) {
&::after {
padding: 12px;
border-radius: 14px;
}
}
igc-checkbox::part(indicator checked) {
border-radius: 0;
&::after {
background: olive;
border-color: olive;
stroke: beige;
}
}