Switch

The Switch is a motion component that changes the color of the background when the user scrolls.

Importation

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

import { Switch } from "pol-ui";

Default Switch

Default Switch

import { Switch, useBoolean } from "pol-ui";
 
export const SwitchComponent = (): JSX.Element => {
  const { value, toggle } = useBoolean(false);
  return <Switch checked={value} onChange={toggle} />;
};

Props

Prop nameTypeDefaultDescription
checkedbooleanfalseThe state of the switch
colorColorsColorsEnum.secondaryThe color of the switch
sizeMainSizesMainSizesEnum.mdThe size of the switch
labelstring-The label of the switch
onChange(checked: boolean) => void-The function to call when the switch is clicked
themeSwitchTheme-The theme of the switch

Examples

All colors

All colors

import { Switch, useBoolean, ColorsEnum } from "pol-ui";
 export const AllColors = (): JSX.Element => (
  const { value, toggle } = useBoolean(false);
 
  <div className="flex flex-wrap gap-6">
    {Object.keys(ColorsEnum).map(v => (
      <div key={v} className="flex flex-col items-center justify-center">
        <Switch checked={value} onChange={toggle} color={v as Colors} label={v} />
      </div>
    ))}
    <div className="flex flex-wrap gap-6 dark pt-10">
      Dark Mode
      <div className="bg-secondary-900 text-secondary-50 flex gap-2 flex-wrap p-4">
        {Object.keys(ColorsEnum).map(v => (
          <div key={v} className="flex flex-col items-center justify-center">
            <Switch checked={value} onChange={toggle} color={v as Colors} label={v} />
          </div>
        ))}
      </div>
    </div>
  </div>
)

All sizes

All sizes

import { Switch, MainSizesEnum, MainSizes } from "pol-ui";
export const AllSizes = (): JSX.Element => (
  <div className="flex flex-wrap gap-6">
    {Object.keys(MainSizesEnum).map(v => (
      <div key={v} className="flex flex-col items-center justify-center">
        <Switch size={v as MainSizes} checked={true} onChange={() => {}} label={v} />
      </div>
    ))}
  </div>
)

Theme

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

interface SwitchTheme {
  root: {
    base: string;
    active: IBoolean;
    label: string;
  };
  toggle: {
    base: string;
    sizes: MainSizesType;
    color: ColorsType;
    checked: IBoolean;
    handler: {
      base: string;
      sizes: MainSizesType;
    };
  };
}

Since March 1, 2024

Proudly Open Source
  • Npm

  • Github

  • Creator

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