'use client'
import { Switch } from '@saas-ui/react'
export const SwitchBasic = () => {
  return <Switch>Activate Chakra</Switch>
}
import { Switch } from '@saas-ui/react/switch'<Switch>Label</Switch>Use the size prop to change the size of the switch component.
'use client'
import { HStack } from '@chakra-ui/react'
import { Switch } from '@saas-ui/react'
export const SwitchWithSizes = () => {
  return (
    <HStack gap="8">
      <Switch size="xs" />
      <Switch size="sm" />
      <Switch size="md" />
      <Switch size="lg" />
    </HStack>
  )
}
Use the variant prop to change the visual style of the switch.
'use client'
import { HStack } from '@chakra-ui/react'
import { Switch } from '@saas-ui/react'
export const SwitchWithVariants = () => {
  return (
    <HStack gap="8">
      <Switch variant="raised" />
      <Switch variant="solid" />
    </HStack>
  )
}
Use the colorPalette prop to change the color scheme of the component.
gray
red
green
blue
teal
pink
purple
cyan
orange
yellow
'use client'
import { Stack, Text } from '@chakra-ui/react'
import { Switch } from '@saas-ui/react'
import { colorPalettes } from '../lib/color-palettes'
export const SwitchWithColors = () => {
  return (
    <Stack gap="2" align="flex-start">
      {colorPalettes.map((colorPalette) => (
        <Stack
          align="center"
          key={colorPalette}
          direction="row"
          gap="10"
          px="4"
        >
          <Text minW="8ch">{colorPalette}</Text>
          <Switch colorPalette={colorPalette} />
          <Switch colorPalette={colorPalette} defaultChecked />
        </Stack>
      ))}
    </Stack>
  )
}
Use the checked and onCheckedChange prop to control the state of the switch.
'use client'
import { useState } from 'react'
import { Switch } from '@saas-ui/react'
export const SwitchControlled = () => {
  const [checked, setChecked] = useState(false)
  return (
    <Switch checked={checked} onCheckedChange={(e) => setChecked(e.checked)} />
  )
}
Here's an example of integrating the switch with react-hook-form.
'use client'
import { Button, Stack } from '@chakra-ui/react'
import { zodResolver } from '@hookform/resolvers/zod'
import { Field, Switch } from '@saas-ui/react'
import { Controller, useForm } from 'react-hook-form'
import { z } from 'zod'
const formSchema = z.object({
  active: z.boolean({ message: 'Active is required' }),
})
type FormData = z.infer<typeof formSchema>
export const SwitchWithHookForm = () => {
  const {
    handleSubmit,
    control,
    formState: { errors },
  } = useForm<FormData>({
    resolver: zodResolver(formSchema),
  })
  return (
    <form onSubmit={handleSubmit((data) => console.log(data))}>
      <Stack align="flex-start">
        <Controller
          name="active"
          control={control}
          render={({ field }) => (
            <Field.Root invalid={!!errors.active}>
              <Field.Label>Active</Field.Label>
              <Switch
                name={field.name}
                checked={field.value}
                onCheckedChange={({ checked }) => field.onChange(checked)}
                inputProps={{ onBlur: field.onBlur }}
              >
                Activate Chakra
              </Switch>
              <Field.ErrorText>{errors.active?.message}</Field.ErrorText>
            </Field.Root>
          )}
        />
        <Button size="sm" type="submit" mt="4">
          Submit
        </Button>
      </Stack>
    </form>
  )
}
Use the disabled prop to disable the switch.
'use client'
import { Switch } from '@saas-ui/react'
export const SwitchWithDisabled = () => {
  return <Switch disabled>Activate Chakra</Switch>
}
Use the invalid prop to indicate an error state for the switch.
'use client'
import { Switch } from '@saas-ui/react'
export const SwitchWithInvalid = () => {
  return <Switch invalid>Activate Chakra</Switch>
}
Here's an example of a switch with a tooltip.
'use client'
import { useId } from 'react'
import { Switch, Tooltip } from '@saas-ui/react'
export const SwitchWithTooltip = () => {
  const id = useId()
  return (
    <Tooltip ids={{ trigger: id }} content="This is a tooltip">
      <Switch ids={{ root: id }}>Switch with tooltip </Switch>
    </Tooltip>
  )
}
Use the trackLabel prop to display different label based on the checked state.
'use client'
import { Icon } from '@chakra-ui/react'
import { Switch } from '@saas-ui/react'
import { FaMoon, FaSun } from 'react-icons/fa'
export const SwitchWithTrackLabel = () => {
  return (
    <Switch
      colorPalette="blue"
      size="lg"
      trackLabel={{
        on: (
          <Icon color="yellow.400">
            <FaSun />
          </Icon>
        ),
        off: (
          <Icon color="gray.400">
            <FaMoon />
          </Icon>
        ),
      }}
    />
  )
}
Use the thumbLabel prop to add an icon to the switch thumb.
'use client'
import { Switch } from '@saas-ui/react'
import { HiCheck, HiX } from 'react-icons/hi'
export const SwitchWithThumbLabel = () => {
  return (
    <Switch size="lg" thumbLabel={{ on: <HiCheck />, off: <HiX /> }}>
      Switch me
    </Switch>
  )
}
| Prop | Default | Type | 
|---|---|---|
| value  | '\'on\'' | string | numberThe value of switch input. Useful for form submission. | 
| colorPalette  | 'gray' | 'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink' | 'accent'The color palette of the component | 
| variant  | 'solid' | 'solid' | 'raised'The variant of the component | 
| size  | 'md' | 'xs' | 'sm' | 'md' | 'lg'The size of the component | 
| asChild  | booleanUse the provided child element as the default rendered element, combining their props and behavior.For more details, read our Composition guide. | |
| checked  | booleanWhether the switch is checked. | |
| disabled  | booleanWhether the switch is disabled. | |
| ids  | Partial<{
  root: string
  hiddenInput: string
  control: string
  label: string
  thumb: string
}>The ids of the elements in the switch. Useful for composition. | |
| invalid  | booleanIf `true`, the switch is marked as invalid. | |
| label  | stringSpecifies the localized strings that identifies the accessibility elements and their states | |
| name  | stringThe name of the input field in a switch (Useful for form submission). | |
| onCheckedChange  | (details: CheckedChangeDetails) => voidFunction to call when the switch is clicked. | |
| readOnly  | booleanWhether the switch is read-only | |
| required  | booleanIf `true`, the switch input is marked as required, | |
| as  | React.ElementTypeThe underlying element to render. | |
| unstyled  | booleanWhether to remove the component's style. |