Switch

SwitchGitHub
A control that toggles between two states.

Usage

Use the v-model directive to control the checked state of the Switch.

<script setup lang="ts">
const value = ref(true)
</script>

<template>
  <PSwitch v-model="value" />
</template>

Use the default-value prop to set the initial value when you do not need to control its state.

<template>
  <PSwitch default-value />
</template>

Label

Use the label prop to set the label of the Switch.

<template>
  <PSwitch label="Check me" />
</template>

When using the required prop, an asterisk is added next to the label.

<template>
  <PSwitch required label="Check me" />
</template>

Description

Use the description prop to set the description of the Switch.

This is a checkbox.

<template>
  <PSwitch label="Check me" description="This is a checkbox." />
</template>

Icon

Use the checked-icon and unchecked-icon props to set the icons of the Switch when checked and unchecked.

<template>
  <PSwitch
    unchecked-icon="i-lucide:x"
    checked-icon="i-lucide:check"
    default-value
    label="Check me"
  />
</template>

Loading

Use the loading prop to show a loading icon on the Switch.

<template>
  <PSwitch loading default-value label="Check me" />
</template>

Loading Icon

Use the loading-icon prop to customize the loading icon. Defaults to i-lucide:loader-circle.

<template>
  <PSwitch loading loading-icon="i-lucide:loader" default-value label="Check me" />
</template>
You can customize this icon globally in your app.config.ts under pohon.icons.loading key.
You can customize this icon globally in your vite.config.ts under pohon.icons.loading key.

Color

Use the color prop to change the color of the Switch.

<template>
  <PSwitch color="neutral" default-value label="Check me" />
</template>

Size

Use the size prop to change the size of the Switch.

<template>
  <PSwitch size="xl" default-value label="Check me" />
</template>

Disabled

Use the disabled prop to disable the Switch.

<template>
  <PSwitch disabled label="Check me" />
</template>

API

Props

Prop Default Type
This component also supports all native <button> HTML attributes.

Slots

Slot Type

Emits

Event Type

Theme

We use unocss-variants to customize the theme. Read more about it in the theming guide.

Below is the theme configuration skeleton for the PSwitch. Since the component is provided unstyled by default, you will need to fill in these values to apply your own custom look and feel. If you prefer to use our pre-built, opinionated styling, you can instead use our UnoCSS preset, this docs is using it as well.

app.config.ts
export default defineAppConfig({
  pohon: {
    switch: {
      slots: {
        root: '',
        base: '',
        container: '',
        thumb: '',
        icon: '',
        wrapper: '',
        label: '',
        description: ''
      },
      variants: {
        color: {
          primary: {
            base: '',
            icon: ''
          },
          secondary: {
            base: '',
            icon: ''
          },
          success: {
            base: '',
            icon: ''
          },
          info: {
            base: '',
            icon: ''
          },
          warning: {
            base: '',
            icon: ''
          },
          error: {
            base: '',
            icon: ''
          },
          neutral: {
            base: '',
            icon: ''
          }
        },
        size: {
          xs: {
            base: '',
            container: '',
            thumb: '',
            wrapper: ''
          },
          sm: {
            base: '',
            container: '',
            thumb: '',
            wrapper: ''
          },
          md: {
            base: '',
            container: '',
            thumb: '',
            wrapper: ''
          },
          lg: {
            base: '',
            container: '',
            thumb: '',
            wrapper: ''
          },
          xl: {
            base: '',
            container: '',
            thumb: '',
            wrapper: ''
          }
        },
        checked: {
          true: {
            icon: ''
          }
        },
        unchecked: {
          true: {
            icon: ''
          }
        },
        loading: {
          true: {
            icon: ''
          }
        },
        required: {
          true: {
            label: ''
          }
        },
        disabled: {
          true: {
            root: '',
            base: '',
            label: '',
            description: ''
          }
        }
      },
      defaultVariants: {
        color: 'primary',
        size: 'md'
      }
    }
  }
};
vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import pohon from 'pohon-ui/vite'

export default defineAppConfig({
  pohon: {
    switch: {
      slots: {
        root: '',
        base: '',
        container: '',
        thumb: '',
        icon: '',
        wrapper: '',
        label: '',
        description: ''
      },
      variants: {
        color: {
          primary: {
            base: '',
            icon: ''
          },
          secondary: {
            base: '',
            icon: ''
          },
          success: {
            base: '',
            icon: ''
          },
          info: {
            base: '',
            icon: ''
          },
          warning: {
            base: '',
            icon: ''
          },
          error: {
            base: '',
            icon: ''
          },
          neutral: {
            base: '',
            icon: ''
          }
        },
        size: {
          xs: {
            base: '',
            container: '',
            thumb: '',
            wrapper: ''
          },
          sm: {
            base: '',
            container: '',
            thumb: '',
            wrapper: ''
          },
          md: {
            base: '',
            container: '',
            thumb: '',
            wrapper: ''
          },
          lg: {
            base: '',
            container: '',
            thumb: '',
            wrapper: ''
          },
          xl: {
            base: '',
            container: '',
            thumb: '',
            wrapper: ''
          }
        },
        checked: {
          true: {
            icon: ''
          }
        },
        unchecked: {
          true: {
            icon: ''
          }
        },
        loading: {
          true: {
            icon: ''
          }
        },
        required: {
          true: {
            label: ''
          }
        },
        disabled: {
          true: {
            root: '',
            base: '',
            label: '',
            description: ''
          }
        }
      },
      defaultVariants: {
        color: 'primary',
        size: 'md'
      }
    }
  }
};

Akar

With Pohon UI, you can achieve similar component functionality with less code and effort, as it comes with built-in styles mechanism and behaviors that are optimized for common use cases. Since it's using unocss-variants it adds a runtime cost, but it can be worth it if you prioritize development speed and ease of use over fine-grained control.

If this is a deal breaker for you, you can always stick to using Akar and build your own custom components on top of it.

Changelog

No recent changes