Switch

Switch is used to users to toggle between two states.

Import

import { Switch } from '@ma9pie/lite-ui';

Source

components/Switch.tsx

Examples

off

import { Switch } from '@ma9pie/lite-ui';
import React, { useState } from 'react';

const App = () => {
  const [checked, setChecked] = useState(false);

  const handleChange = (value: boolean) => {
    setChecked(value);
  };

  return (
    <div className="flex items-center gap-2">
      <Switch onChange={handleChange}></Switch>
      <p>{checked ? 'on' : 'off'}</p>
    </div>
  );
};

export default App;

Size

import { Switch } from '@ma9pie/lite-ui';
import React from 'react';

const App = () => {
  return (
    <div className="flex items-center gap-2 flex-wrap">
      <Switch size={24}></Switch>
      <Switch></Switch>
      <Switch size={40}></Switch>
    </div>
  );
};

export default App;

Track padding

import { Switch } from '@ma9pie/lite-ui';
import React from 'react';

const App = () => {
  return (
    <div className="flex items-center gap-2 flex-wrap">
      <Switch trackPadding={2}></Switch>
      <Switch></Switch>
      <Switch trackPadding={4}></Switch>
    </div>
  );
};

export default App;

Track color

import { Switch } from '@ma9pie/lite-ui';
import React from 'react';

const App = () => {
  return (
    <div className="flex items-center gap-2 flex-wrap">
      <Switch defaultChecked trackColor="#ef4444"></Switch>
      <Switch defaultChecked trackColor="#eab308"></Switch>
      <Switch defaultChecked trackColor="#22c55e"></Switch>
      <Switch defaultChecked trackColor="#3b82f6"></Switch>
      <Switch defaultChecked trackColor="#8b5cf6"></Switch>
    </div>
  );
};

export default App;

Duration

import { Switch } from '@ma9pie/lite-ui';
import React from 'react';

const App = () => {
  return (
    <div className="flex items-center gap-2 flex-wrap">
      <Switch duration={0}></Switch>
      <Switch></Switch>
      <Switch duration={300}></Switch>
    </div>
  );
};

export default App;

Disabled

import { Switch } from '@ma9pie/lite-ui';
import React from 'react';

const App = () => {
  return (
    <div className="flex items-center gap-2">
      <Switch></Switch>
      <Switch disabled></Switch>
      <Switch defaultChecked disabled></Switch>
    </div>
  );
};

export default App;

API

Switch props

Property
Type
Description
Default
defaultChecked
boolean
Initial state.
false
size
number
Switch's height.
32
trackPadding
number
Switch's track padding size.
4
trackColor
string
Switch's track background color.
"#525252"
duration
number
Animation duration.
150
disabled
boolean
Disabled switch.
false
onChange
(value: boolean) => void
onChange event.
-

Default props

Property
Type
Description
Default
key
string
Unique key that identifies repeated elements.
-
className
string
Attribute used to add classes to HTML elements.
-
style
CSSProperties
Attribute used to apply CSS styles directly to HTML elements.
-

Made by ma9pie

Deployed on

vercel