Skip to main content

Checkbox

The checkbox component provides a clear way for users to make selections, such as agreeing to terms, enabling settings, or choosing multiple items from a list. Use checkbox to create binary on/off controls or multi-select interfaces where users can select any combination of options.

Checkboxes support labels and error states. For settings that take effect immediately, use switch instead.

Support
Targets (29)

Configure the following properties on the checkbox component.

Anchor to accessibilityLabel
accessibilityLabel
string

A label used for users using assistive technologies like screen readers. When set, any children or label supplied will not be announced. This can also be used to display a control without a visual label, while still providing context to users using screen readers.

Anchor to checked
checked
boolean
Default: false

Whether the control is currently checked.

Anchor to command
command
'--auto' | '--show' | '--hide' | '--toggle'
Default: '--auto'

Sets the action the commandFor target should take when this component is activated. Available options:

  • '--auto': Performs the default action appropriate for the target component.
  • '--show': Displays the target component if it's currently hidden.
  • '--hide': Conceals the target component from view.
  • '--toggle': Alternates the target component between visible and hidden states.
  • '--copy': Copies the target clipboard item.

The supported actions vary by target component type. Learn more about the command attribute.

Anchor to commandFor
commandFor
string

The ID of the component to control when this component is activated. Pair with the command property to specify what action to perform on the target component. Learn more about the commandFor attribute.

When both commandFor and href are set, commandFor takes precedence. The command runs and the link doesn't navigate.

Anchor to defaultChecked
defaultChecked
boolean
Default: false

Whether the control is checked by default.

Anchor to disabled
disabled
boolean
Default: false

Whether the control is disabled, preventing any user interaction.

Anchor to error
error
string

An error message displayed below the field to indicate validation problems. When set, the field is styled with error indicators and the message is announced to screen readers.

string

A unique identifier for the element. Use this to reference the element in JavaScript, link labels to form controls, or target specific elements for styling or scripting.

Anchor to label
label
string

The visual content to use as the control label. Use a string to provide a simple text label displayed to the user.

If a label slot is also provided, the slot content takes precedence. Learn more about slots.

string

The name attribute for the control, used to identify its value when the form is submitted. Must be unique within the nearest containing form.

Anchor to required
required
boolean
Default: false

Whether the field needs a value. This requirement adds semantic value to the field, but it will not cause an error to appear automatically. If you want to present an error when this field is empty, you can do so with the error property.

Adds semantic meaning for accessibility. Doesn't trigger automatic validation or display an error. Implement validation logic yourself and use the error prop to show results.

Anchor to value
value
string

The value used in form data when the control is checked.

The checkbox component provides event callbacks for handling user interactions. Learn more about handling events.

Anchor to change
change
<typeof tagName>

A callback fired when the checkbox value changes.

Learn more about the change event.


Anchor to Opt in to email marketingOpt in to email marketing

Pre-fill a marketing opt-in checkbox with a checked default state. This example displays an s-checkbox with checked and a descriptive label for email subscriptions.

Opt in to email marketing

A rendered example of the checkbox component

html

<s-checkbox checked label="Email me with news and offers"></s-checkbox>

Anchor to Require agreement before checkoutRequire agreement before checkout

Enforce agreement to terms of service before checkout can proceed. This example uses an s-checkbox with required and an error message when the buyer hasn't accepted.

html

<s-checkbox
label="I agree to the terms of service"
required
error="You must accept the terms to continue"
></s-checkbox>

Anchor to Disable an unavailable optionDisable an unavailable option

Disable a checkbox when the corresponding option isn't currently available. This example displays an s-checkbox with the disabled attribute for a newsletter subscription.

html

<s-checkbox label="Subscribe to newsletter" disabled></s-checkbox>

  • Write descriptive labels: Use clear, specific text that explains what the buyer is agreeing to or selecting.
  • Pre-check thoughtfully: Only use checked for options that benefit the buyer, such as marketing opt-in. Don't pre-check options that add cost.
  • Show validation errors inline: Use the error property to display messages directly below the checkbox for required fields.
  • Use switch for instant effects: If a selection takes effect immediately without a form submission, use switch instead.
  • Pair with forms: Use checkboxes inside a form when selections need to be submitted together with other input.

Was this page helpful?