Skip to content

Custom Checkbox

The Custom Checkbox component provides a highly customizable checkbox that allows you to define custom icons and captions for checked and unchecked states. It’s designed to be flexible and easy to style.

  • Metro UI Core
  • Dom
<input type="checkbox" data-role="custom-checkbox">
<input type="checkbox" data-role="custom-checkbox"
data-state-on="✓"
data-state-off="✗">
<input type="checkbox" data-role="custom-checkbox"
data-caption-on="Enabled"
data-caption-off="Disabled">
<input type="checkbox" data-role="custom-checkbox"
data-caption-on="Enabled"
data-caption-off="Disabled"
data-caption-position="left">
<input type="checkbox" data-role="custom-checkbox"
data-state-on="<span class='mif-star-full fg-orange'>"
data-state-off="<span class='mif-star-empty'>">
ParameterTypeDefaultDescription
stateOnString”✓“Symbol or HTML for the checked state
stateOffString”✗“Symbol or HTML for the unchecked state
captionOnString""Text displayed when checkbox is checked
captionOffString""Text displayed when checkbox is unchecked
captionPositionString”right”Position of the caption (“right” or “left”)
clsCustomCheckboxString""Additional CSS class for the checkbox container
clsIconString""Additional CSS class for the icon element
clsIconOnString""Additional CSS class for the checked state icon
clsIconOffString""Additional CSS class for the unchecked state icon
clsCaptionString""Additional CSS class for the caption element
clsCaptionOnString""Additional CSS class for the checked state caption
clsCaptionOffString""Additional CSS class for the unchecked state caption
EventDescription
onCustomCheckboxCreateTriggered after the custom checkbox is created
  • disable() - Disables the checkbox.
  • enable() - Enables the checkbox.
  • toggleState() - Toggles between enabled and disabled states.
  • toggle([value]) - Toggles the checked state. If value is provided (0 or 1), sets the state accordingly.
// Get the component instance
const checkbox = Metro.getPlugin('#myCheckbox', 'custom-checkbox');
// Disable the checkbox
checkbox.disable();
// Enable the checkbox
checkbox.enable();
// Toggle the checked state
checkbox.toggle();
// Set to checked state
checkbox.toggle(1);
// Set to unchecked state
checkbox.toggle(0);
VariableDefault (Light)Dark ModeDescription
--custom-checkbox-size36px36pxSize of the checkbox
--custom-checkbox-icon-size16px16pxSize of the icon
--custom-checkbox-color#575757rgb(166, 166, 166)Color of the checkbox
--custom-checkbox-backgroundtransparenttransparentBackground of the checkbox
--custom-checkbox-caption-color#191919#e8e8e8Color of the caption
--custom-checkbox-background-disabled#e8e8e8#353535Background when disabled
--custom-checkbox-color-disabled#c3c3c3#6c6c6cColor when disabled
/* Custom styling for a specific checkbox */
#myCustomCheckbox {
--custom-checkbox-size: 42px;
--custom-checkbox-icon-size: 20px;
--custom-checkbox-color: #2196F3;
--custom-checkbox-caption-color: #0D47A1;
}
  • .custom-checkbox - Main container class for the custom checkbox
  • .icon - Container for the checkbox icon
  • .caption - Container for the checkbox caption
  • .caption-left - Positions the caption to the left of the checkbox
  • .disabled - Applied when the checkbox is disabled
  • .state-on - Element shown when checkbox is checked
  • .state-off - Element shown when checkbox is unchecked
  • .caption-state-on - Caption element shown when checkbox is checked
  • .caption-state-off - Caption element shown when checkbox is unchecked

You can set global defaults for all Custom Checkbox components using:

Metro.customCheckboxSetup({
stateOn: "✓",
stateOff: "✗",
captionPosition: "left"
});

The Custom Checkbox component maintains the native checkbox element’s accessibility features:

  • It preserves the native checkbox’s functionality while enhancing its appearance
  • It respects the disabled state of the checkbox
  • It uses proper label elements for better screen reader support
  1. Use meaningful icons that clearly indicate the checked and unchecked states
  2. Provide clear captions when the checkbox’s purpose might not be obvious from context
  3. Maintain sufficient contrast between the checkbox and its background for better visibility
  4. Consider using the left caption position when checkboxes are aligned to the right side of a form