<cs-checkbox>
StableFormsSince 0.1

Checkboxes let users toggle an option on or off, or select multiple items from a list. They also support an indeterminate state for partial selections in groups.

I agree to the terms and conditions
Remember me

This component works with standard <form> elements. See form controls for form submission and client-side validation.

Examples

Initial Value

Use the checked attribute to activate the checkbox.

Remember me

checked sets the initial value, not the current state.
Consistent with native checkboxes, it doesn’t reflect later changes. To toggle the checked state with JavaScript, use the checked property instead. To target checked checkboxes with CSS, use the :state(checked) selector.

Hint

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

Subscribe to the newsletter

Indeterminate

Use the indeterminate attribute to make the checkbox indeterminate. This is typically used for a “select all” control when its associated checkboxes have a mix of checked and unchecked states.

Select all

Disabled

Use the disabled attribute to disable the checkbox.

I accept marketing emails

Size

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

Extra Small Small Medium Large Extra Large

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

API

Importing

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

npm Self-Hosted React
import '@cruglobal/cornerstone-components/components/checkbox/checkbox.js';
import './cornerstone/components/checkbox/checkbox.js';
import CsCheckbox from '@cruglobal/cornerstone-components/react/checkbox/index.js';

Slots

Learn more about using slots.

NameDescription
(default)The checkbox's label.
hintText that describes how to use the checkbox. Alternatively, you can use the hint attribute.

Attributes & Properties

Learn more about attributes and properties.

PropertyAttributeDescriptionTypeDefaultReflects
checkedDraws the checkbox in a checked state.
defaultCheckedcheckedThe default value of the form control. Primarily used for resetting the form control.booleanYes
disableddisabledDisables the checkbox.booleanfalse
formBy 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.HTMLFormElement | null
hinthintThe checkbox's hint. If you need to display HTML, use the hint slot instead.string''
indeterminateindeterminateDraws 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.booleanfalseYes
namenameThe name of the input, submitted as a name/value pair with form data.string | nullnullYes
requiredrequiredMakes the checkbox a required field.booleanfalseYes
sizesizeThe checkbox's size.'xs' | 's' | 'm' | 'l' | 'xl''m'Yes
ssrHintssr-hintOnly required for SSR. Set to true if you're slotting in a hint element.booleanfalse
validationTargetOverride this to change where constraint validation popups are anchored.undefined | HTMLElement
validatorsValidators are static because they have observedAttributes, essentially attributes to "watch" for changes. Whenever these attributes change, we want to be notified and update the validator.Validator[][]
valuevalueThe value of the checkbox, submitted as a name/value pair with form data.string | nullYes

Methods

Learn more about methods.

NameDescriptionArguments
blur()Removes focus from the checkbox.
click()Simulates a click on the checkbox.
focus()Sets focus on the checkbox.options: FocusOptions
formStateRestoreCallback()Called when the browser is trying to restore element’s state to state in which case reason is "restore", or when the browser is trying to fulfill autofill on behalf of user in which case reason is "autocomplete". In the case of "restore", state is a string, File, or FormData object previously set as the second argument to setFormValue.state: string | File | FormData | null, reason: 'autocomplete' | 'restore'
resetValidity()Reset validity is a way of removing manual custom errors and native validation.
setCustomValidity()Do not use this when creating a "Validator". This is intended for end users of components. We track manually defined custom errors so we don't clear them on accident in our validators.message: string

Events

Learn more about events.

NameDescription
blurEmitted when the checkbox loses focus.
changeEmitted when the checked state changes.
cs-invalidEmitted when the form control has been checked for validity and its constraints aren't satisfied.
focusEmitted when the checkbox gains focus.
inputEmitted when the checkbox receives input.

CSS Custom Properties

Learn more about CSS custom properties.

NameDescriptionDefault
--checked-icon-colorThe color of the checked and indeterminate icons.
--checked-icon-scaleThe size of the checked and indeterminate icons relative to the checkbox.

Custom States

Learn more about custom states.

NameDescriptionCSS selector
checkedApplied when the checkbox is checked.:state(checked)
disabledApplied when the checkbox is disabled.:state(disabled)
indeterminateApplied when the checkbox is in an indeterminate state.:state(indeterminate)

CSS Parts

Learn more about CSS parts.

NameDescriptionCSS selector
checkboxThe component's outer wrapper.::part(checkbox)
checked-iconThe checked icon, a <cs-icon> element.::part(checked-icon)
controlThe square container that wraps the checkbox's checked state.::part(control)
hintThe hint's wrapper.::part(hint)
iconWhichever state icon is currently rendered, checked or indeterminate.::part(icon)
indeterminate-iconThe indeterminate icon, a <cs-icon> element.::part(indeterminate-icon)
labelThe container that wraps the checkbox's label.::part(label)

Dependencies

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