Skip to content

Checkbox

The Checkbox component provides a customizable checkbox input with support for three states: checked, unchecked, and indeterminate.

This component has no additional dependencies beyond the core Metro UI library.

Metro UI rewrites the default appearance of checkboxes to provide a consistent look and feel across different browsers.

<input type="checkbox" />

You can use checkbox with additional features. To get extended checkbox, the component can be initialized using the data-role attribute.

<input type="checkbox" data-role="checkbox">

You can add a caption to the checkbox using the data-caption attribute. The caption will be displayed next to the checkbox.

<input type="checkbox" data-role="checkbox" data-caption="Checkbox with Caption">

To change the state of the checkbox to checked, you can use the standard checked attribute.

<input type="checkbox" data-role="checkbox" data-caption="Checkbox" checked>

To disable the checkbox, use the disabled attribute. The checkbox will be visually styled to indicate that it is disabled.

<input type="checkbox" data-role="checkbox" data-caption="Checkbox" disabled>

To create a three-state checkbox (checked, unchecked, indeterminate), you can use the data-three-state attribute. This allows the checkbox to toggle between three states.

<input type="checkbox" data-role="checkbox" data-three-state="true" data-caption="Three-state Checkbox">

Alternatively, you can use the data-role="checkbox-three-state" attribute and set the initial state with data-state:

<input type="checkbox" data-role="checkbox-three-state" data-caption="Checked" data-state="checked">
<input type="checkbox" data-role="checkbox-three-state" data-caption="Unchecked" data-state="unchecked">
<input type="checkbox" data-role="checkbox-three-state" data-caption="Indeterminate" data-state="indeterminate">

You can add text or HTML content before (prepend) or after (append) the checkbox using the data-prepend and data-append attributes.

<input type="checkbox" data-role="checkbox" data-prepend="Before" data-append="After">
<input type="checkbox" data-role="checkbox" data-caption="Custom Classes" data-cls-checkbox="custom-checkbox" data-cls-caption="custom-caption">

You can customize the appearance of the checkbox using CSS variables:

<div style="--checkbox-color: red;">
<input type="checkbox" data-role="checkbox" data-caption="Red Checkbox">
</div>
ParameterTypeDefaultDescription
checkboxDeferrednumber0Delay for initialization in milliseconds
statenumber/string-1 (UNCHECKED)Initial state of the checkbox. Possible values: -1/unchecked, 0/indeterminate, 1/checked
threeStatebooleanfalseWhether the checkbox supports three states
prependstring""Content to prepend to the checkbox
appendstring""Content to append to the checkbox
captionstring""Caption for the checkbox (alternative to append)
clsCheckboxstring""Additional CSS class for the checkbox container
clsCaptionstring""Additional CSS class for the caption
clsPrependstring""Additional CSS class for prepended content
clsAppendstring""Additional CSS class for appended content
  • check() - Sets the checkbox to checked state.
  • uncheck() - Sets the checkbox to unchecked state.
  • indeterminate() - Sets the checkbox to indeterminate state.
  • setCheckState(state) - Sets the checkbox to a specific state. Possible values: -1/unchecked, 0/indeterminate, 1/checked.
  • getCheckState(asString) - Gets the current state of the checkbox. If asString is true, returns the state as a string (‘checked’, ‘unchecked’, or ‘indeterminate’).
  • toggle() - Toggles the checkbox state.
const checkbox = Metro.getPlugin('#myCheckbox', 'checkbox');
// Check the checkbox
checkbox.check();
// Uncheck the checkbox
checkbox.uncheck();
// Set to indeterminate state
checkbox.indeterminate();
// Toggle the state
checkbox.toggle();
// Get the current state
const state = checkbox.getCheckState();
const stateAsString = checkbox.getCheckState(true);
EventDescription
onCheckboxCreateTriggered when the checkbox is created
VariableDefault (Light)Dark ModeDescription
--checkbox-height36px36pxHeight of the checkbox
--checkbox-border-radius4px4pxBorder radius of the checkbox
--checkbox-color#575757#a6a8a7Color of the checkbox
--checkbox-color-disabled#c3c3c3#505050Color of the disabled checkbox
--checkbox-background-disabled#efefef#323030Background color of the disabled checkbox
--checkbox-focus-color#e8e8e8#191919Focus color of the checkbox
/* Custom styling example */
#my-checkbox {
--checkbox-color: #0078d7;
--checkbox-border-radius: 2px;
--checkbox-focus-color: rgba(0, 120, 215, 0.3);
}
  • .checkbox - The main container class for the checkbox component
  • .caption-prepend - Class for prepended content
  • .caption-append - Class for appended content
  • .checkbox-{color} - Color variants for the checkbox (using the normalColors variable)
<input type="checkbox" data-role="checkbox" class="checkbox-primary" data-caption="Primary Color">

The Checkbox component includes proper semantics for improved accessibility:

  • Uses native checkbox elements for proper keyboard and screen reader support
  • Properly conveys checked, unchecked, and indeterminate states
  • Supports disabled state with appropriate styling
  1. Always provide descriptive text for checkboxes using the data-caption, data-prepend, or data-append attributes
  2. Use the three-state mode only when an indeterminate state makes sense in your UI
  3. Group related checkboxes together for better organization
  4. Consider using custom styling to match your application’s design system
  5. Ensure sufficient contrast between the checkbox and its background for better visibility