Checkbox

Introduction

A checkbox is a component that allows the user to select one or more items from a set of options. This input could be super useful when you need to select multiple items from a short list of options.

You should use a checkbox when
You have multiple options to select from
You don't have more than 5 options
You need to select more than one option
You should not use a checkbox when
You have a long list of options -> Use a select input
You need to select only one option -> Use a radio input

Import the component from pol-ui to use the checkbox element:

Importation

import { Checkbox } from "pol-ui";

Default checkbox

The default checkbox component comes with a set of control buttons and indicators to navigate between the items.

Default checkbox

import { Checkbox } from "pol-ui";
 
import React from "react";
 
const CheckboxComponent = () => {
  return (
    <Checkbox
      label="Controlled checkbox"
      color={ColorsEnum.primary}
      checked={checked}
      onChange={() => setChecked(!checked)}
    />
  );
};
export default CheckboxComponent;

Props

The checkbox component comes with a set of props to customize the appearance and behavior of the component.

This props are a small amount, our goal is to keep the component simple and easy to use and be the most native possible.

You can also pass any prop that the native input checkbox accepts.

Prop nameTypeDefaultDescription
colorColorsprimarySpecify the color of the checkbox.
labelstringnullAdd a label to the checkbox.
themeCheckboxThemenullSpecify the theme of the checkbox.

Customization

Uncontrolled checkbox

You can always use the checkbox as an uncontrolled component, this means that the checkbox will manage its own state and then you can get the value of the checkbox using the ref prop, a form submit or any other way.

⚠️

See how when using the uncontrolled checkbox, the animation is disabled because there is not a direct way to know when the checkbox is checked.

Default checkbox

import { Checkbox } from "pol-ui";
 
import React from "react";
 
const CheckboxComponent = () => {
  return <Checkbox label="Uncontrolled checkbox" />;
};
export default CheckboxComponent;

All colors

As all the components in pol-ui, the checkbox component comes with a set of colors to customize the appearance of the component.

Default checkbox

import { Checkbox, ColorsEnum } from "pol-ui";
 
import React from "react";
 
const CheckboxComponent = () => {
  return (
    <div className="flex flex-wrap gap-8">
    {Object.keys(ColorsEnum).map(status => (
      <div key={status} className="flex flex-col items-center justify-center">
        <Checkbox color={status as keyof typeof ColorsEnum} defaultChecked label={status} />
      </div>
    ))}
  </div>
  );
};
export default CheckboxComponent;

Theme

To learn more about how to customize the appearance of components, please see the Theme docs.

interface CheckboxTheme {
  base: string;
  color: ColorsType;
  before: string;
  floating: {
    base: string;
    svg: string;
  };
  check: {
    base: string;
    color: ColorsType;
  };
}

Further reading

See more about when to use a checkbox

Since February 15, 2024

Proudly Open Source
  • Npm

  • Github

  • Creator

  • Made with love and Pol-ui by Pol Gubau Amores