Skip to content
UI

Switch

A control that allows toggling between two states, on and off.

<script setup lang="ts">
import { Switch } from '@destyler-ui/vue'
</script>

<template>
  <Switch.Root>
    <Switch.Control>
      <Switch.Thumb />
    </Switch.Control>
    <Switch.Label>Label</Switch.Label>
    <Switch.HiddenInput />
  </Switch.Root>
</template>
import { Switch } from '@destyler-ui/react'

export function Basic(props: Switch.RootProps) {
  return (
    <Switch.Root {...props}>
      <Switch.Control>
        <Switch.Thumb />
      </Switch.Control>
      <Switch.Label>Label</Switch.Label>
      <Switch.HiddenInput />
    </Switch.Root>
  )
}
import { Switch } from '@destyler-ui/solid/switch'

export function Basic() {
  return (
    <Switch.Root>
      <Switch.Control>
        <Switch.Thumb />
      </Switch.Control>
      <Switch.Label>Label</Switch.Label>
      <Switch.HiddenInput />
    </Switch.Root>
  )
}
<script module lang="ts">
  import type { SwitchRootProps } from '@destyler-ui/svelte'

  export interface BasicProps extends SwitchRootProps {}
</script>

<script lang="ts">
  import { Switch } from '@destyler-ui/svelte'

  const props: BasicProps = $props()
</script>

<Switch.Root {...props}>
  <Switch.Control>
    <Switch.Thumb />
  </Switch.Control>
  <Switch.Label>Label</Switch.Label>
  <Switch.HiddenInput />
</Switch.Root>
<script setup lang="ts">
import { Switch } from '@destyler-ui/vue'
</script>
<template>
<Switch.Root>
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Label />
<Switch.HiddenInput />
</Switch.Root>
</template>
import { Switch } from '@destyler-ui/react'
export default function Basic() {
return (
<Switch.Root>
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Label />
<Switch.HiddenInput />
</Switch.Root>
)
}
import { Switch } from '@destyler-ui/solid'
export default function Basic() {
return (
<Switch.Root>
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Label />
<Switch.HiddenInput />
</Switch.Root>
)
}
<script lang="ts">
import { Switch } from '@destyler-ui/svelte'
</script>
<Switch.Root>
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.Label>Notifications</Switch.Label>
<Switch.HiddenInput />
</Switch.Root>
<script setup lang="ts">
import { Switch } from '@destyler-ui/vue'
</script>

<template>
  <Switch.Root>
    <Switch.Control>
      <Switch.Thumb />
    </Switch.Control>
    <Switch.Label>Label</Switch.Label>
    <Switch.HiddenInput />
  </Switch.Root>
</template>
import { Switch } from '@destyler-ui/react'

export function Basic(props: Switch.RootProps) {
  return (
    <Switch.Root {...props}>
      <Switch.Control>
        <Switch.Thumb />
      </Switch.Control>
      <Switch.Label>Label</Switch.Label>
      <Switch.HiddenInput />
    </Switch.Root>
  )
}
import { Switch } from '@destyler-ui/solid/switch'

export function Basic() {
  return (
    <Switch.Root>
      <Switch.Control>
        <Switch.Thumb />
      </Switch.Control>
      <Switch.Label>Label</Switch.Label>
      <Switch.HiddenInput />
    </Switch.Root>
  )
}
<script module lang="ts">
  import type { SwitchRootProps } from '@destyler-ui/svelte'

  export interface BasicProps extends SwitchRootProps {}
</script>

<script lang="ts">
  import { Switch } from '@destyler-ui/svelte'

  const props: BasicProps = $props()
</script>

<Switch.Root {...props}>
  <Switch.Control>
    <Switch.Thumb />
  </Switch.Control>
  <Switch.Label>Label</Switch.Label>
  <Switch.HiddenInput />
</Switch.Root>

Control the switch state programmatically.

<script setup lang="ts">
import { Switch } from '@destyler-ui/vue'
import { ref } from 'vue'

const checked = ref(true)
</script>

<template>
  <Switch.Root v-model:checked="checked">
    <Switch.Control>
      <Switch.Thumb />
    </Switch.Control>
    <Switch.Label>Label</Switch.Label>
    <Switch.HiddenInput />
  </Switch.Root>
</template>
import { useState } from 'react'
import { Switch } from '@destyler-ui/react'

export function Controlled() {
  const [checked, setChecked] = useState(true)

  return (
    <Switch.Root checked={checked} onCheckedChange={e => setChecked(e.checked)}>
      <Switch.Control>
        <Switch.Thumb />
      </Switch.Control>
      <Switch.Label>Label</Switch.Label>
      <Switch.HiddenInput />
    </Switch.Root>
  )
}
import { Switch } from '@destyler-ui/solid/switch'
import { createSignal } from 'solid-js'

export function Controlled() {
  const [checked, setChecked] = createSignal(false)

  return (
    <Switch.Root checked={checked()} onCheckedChange={e => setChecked(e.checked)}>
      <Switch.Control>
        <Switch.Thumb />
      </Switch.Control>
      <Switch.Label>Label</Switch.Label>
      <Switch.HiddenInput />
    </Switch.Root>
  )
}
<script lang="ts">
  import { Switch } from '@destyler-ui/svelte'

  let checked = $state(true)
</script>

<Switch.Root bind:checked>
  <Switch.Control><Switch.Thumb /></Switch.Control>
  <Switch.Label>Label</Switch.Label>
  <Switch.HiddenInput />
</Switch.Root>

Set the initial checked state.

<script setup lang="ts">
import { Switch } from '@destyler-ui/vue'
</script>

<template>
  <Switch.Root :defaultChecked="true">
    <Switch.Control>
      <Switch.Thumb />
    </Switch.Control>
    <Switch.Label>Label</Switch.Label>
    <Switch.HiddenInput />
  </Switch.Root>
</template>
import { Switch } from '@destyler-ui/react'

export function InitialValue() {
  return (
    <Switch.Root defaultChecked>
      <Switch.Control>
        <Switch.Thumb />
      </Switch.Control>
      <Switch.Label>Label</Switch.Label>
      <Switch.HiddenInput />
    </Switch.Root>
  )
}
import { Switch } from '@destyler-ui/solid/switch'

export function InitialValue() {
  return (
    <Switch.Root checked>
      <Switch.Control>
        <Switch.Thumb />
      </Switch.Control>
      <Switch.Label>Label</Switch.Label>
      <Switch.HiddenInput />
    </Switch.Root>
  )
}
<script lang="ts">
  import { Switch } from '@destyler-ui/svelte'
</script>

<Switch.Root defaultChecked>
  <Switch.Control>
    <Switch.Thumb />
  </Switch.Control>
  <Switch.Label>Label</Switch.Label>
  <Switch.HiddenInput />
</Switch.Root>

Display a disabled switch.

<script setup lang="ts">
import { Switch } from '@destyler-ui/vue'
</script>

<template>
  <Switch.Root disabled>
    <Switch.Control>
      <Switch.Thumb />
    </Switch.Control>
    <Switch.Label>Label</Switch.Label>
    <Switch.HiddenInput />
  </Switch.Root>
</template>
import { Switch } from '@destyler-ui/react'

export function Disabled() {
  return (
    <Switch.Root disabled>
      <Switch.Control>
        <Switch.Thumb />
      </Switch.Control>
      <Switch.Label>Label</Switch.Label>
      <Switch.HiddenInput />
    </Switch.Root>
  )
}
import { Switch } from '@destyler-ui/solid/switch'

export function Disabled() {
  return (
    <Switch.Root disabled>
      <Switch.Control>
        <Switch.Thumb />
      </Switch.Control>
      <Switch.Label>Label</Switch.Label>
      <Switch.HiddenInput />
    </Switch.Root>
  )
}
<script lang="ts">
  import { Switch } from '@destyler-ui/svelte'
</script>

<Switch.Root disabled>
  <Switch.Control>
    <Switch.Thumb />
  </Switch.Control>
  <Switch.Label>Label</Switch.Label>
  <Switch.HiddenInput />
</Switch.Root>

Use render prop for custom switch rendering.

<script setup lang="ts">
import { Switch } from '@destyler-ui/vue'
</script>

<template>
  <Switch.Root>
    <Switch.Control>
      <Switch.Thumb />
    </Switch.Control>
    <Switch.Context v-slot="api">
      <Switch.Label>Feature is {{ api.checked ? 'enabled' : 'disabled' }}</Switch.Label>
    </Switch.Context>
    <Switch.HiddenInput />
  </Switch.Root>
</template>
import { Switch } from '@destyler-ui/react'

export function RenderProp() {
  return (
    <Switch.Root>
      <Switch.Control>
        <Switch.Thumb />
      </Switch.Control>
      <Switch.Context>
        {api => (
          <Switch.Label>Feature is {api.checked ? 'enabled' : 'disabled'}</Switch.Label>
        )}
      </Switch.Context>
      <Switch.HiddenInput />
    </Switch.Root>
  )
}
import { Switch } from '@destyler-ui/solid/switch'

export function RenderProp() {
  return (
    <Switch.Root>
      <Switch.Control>
        <Switch.Thumb />
      </Switch.Control>
      <Switch.Context>
        {context => (
          <Switch.Label>Feature is {context().checked ? 'enabled' : 'disabled'}</Switch.Label>
        )}
      </Switch.Context>
      <Switch.HiddenInput />
    </Switch.Root>
  )
}
<script lang="ts">
  import { Switch } from '@destyler-ui/svelte'
</script>

<Switch.Root>
  <Switch.Control><Switch.Thumb /></Switch.Control>
  <Switch.Context>
    {#snippet render(api)}
      <Switch.Label>Feature is {api().checked ? 'enabled' : 'disabled'}</Switch.Label>
    {/snippet}
  </Switch.Context>
  <Switch.HiddenInput />
</Switch.Root>

Combine with a form field for validation and labeling.

<script setup lang="ts">
import { Field } from '@destyler-ui/vue'
import { Switch } from '@destyler-ui/vue'
</script>

<template>
  <Field.Root>
    <Switch.Root>
      <Switch.Control>
        <Switch.Thumb />
      </Switch.Control>
      <Switch.Label>Label</Switch.Label>
      <Switch.HiddenInput />
    </Switch.Root>
    <Field.HelperText>Additional Info</Field.HelperText>
    <Field.ErrorText>Error Info</Field.ErrorText>
  </Field.Root>
</template>
import { Field } from '@destyler-ui/react'
import { Switch } from '@destyler-ui/react'

export function WithField(props: Field.RootProps) {
  return (
    <Field.Root {...props}>
      <Switch.Root>
        <Switch.Control>
          <Switch.Thumb />
        </Switch.Control>
        <Switch.Label>Label</Switch.Label>
        <Switch.HiddenInput />
      </Switch.Root>
      <Field.HelperText>Additional Info</Field.HelperText>
      <Field.ErrorText>Error Info</Field.ErrorText>
    </Field.Root>
  )
}
import { Field } from '@destyler-ui/solid/field'
import { Switch } from '@destyler-ui/solid/switch'

export function WithField(props: Field.RootProps) {
  return (
    <Field.Root {...props}>
      <Switch.Root>
        <Switch.Control>
          <Switch.Thumb />
        </Switch.Control>
        <Switch.Label>Label</Switch.Label>
        <Switch.HiddenInput />
      </Switch.Root>
      <Field.HelperText>Additional Info</Field.HelperText>
      <Field.ErrorText>Error Info</Field.ErrorText>
    </Field.Root>
  )
}
<script module lang="ts">
  import type { FieldRootProps } from '@destyler-ui/svelte'

  export interface WithFieldProps extends FieldRootProps {}
</script>

<script lang="ts">
  import { Field } from '@destyler-ui/svelte'
  import { Switch } from '@destyler-ui/svelte'

  const props: WithFieldProps = $props()
</script>

<Field.Root {...props}>
  <Switch.Root>
    <Switch.Control><Switch.Thumb /></Switch.Control>
    <Switch.Label>Label</Switch.Label>
    <Switch.HiddenInput />
  </Switch.Root>
  <Field.HelperText>Additional Info</Field.HelperText>
  <Field.ErrorText>Error Info</Field.ErrorText>
</Field.Root>
<script setup lang="ts">
import { Switch, useSwitch } from '@destyler-ui/vue'

const switchApi = useSwitch()
</script>

<template>
  <button @click="switchApi.toggleChecked()">Toggle</button>

  <Switch.RootProvider :value="switchApi">
    <Switch.Control>
      <Switch.Thumb />
    </Switch.Control>
    <Switch.Label>Label</Switch.Label>
    <Switch.HiddenInput />
  </Switch.RootProvider>
</template>
import { Switch, useSwitch } from '@destyler-ui/react'

export function RootProvider() {
  const switchApi = useSwitch()

  return (
    <>
      <button onClick={() => switchApi.toggleChecked()}>Toggle</button>

      <Switch.RootProvider value={switchApi}>
        <Switch.Control>
          <Switch.Thumb />
        </Switch.Control>
        <Switch.Label>Label</Switch.Label>
        <Switch.HiddenInput />
      </Switch.RootProvider>
    </>
  )
}
import { Switch, useSwitch } from '@destyler-ui/solid/switch'

export function RootProvider() {
  const switchApi = useSwitch()

  return (
    <>
      <button onClick={() => switchApi().toggleChecked()}>Toggle</button>

      <Switch.RootProvider value={switchApi}>
        <Switch.Control>
          <Switch.Thumb />
        </Switch.Control>
        <Switch.Label>Label</Switch.Label>
        <Switch.HiddenInput />
      </Switch.RootProvider>
    </>
  )
}
<script lang="ts">
  import { Switch, useSwitch } from '@destyler-ui/svelte'

  const id = $props.id()
  const switchApi = useSwitch({ id })
</script>

<button type="button" onclick={() => switchApi().toggleChecked()}>Toggle</button>

<Switch.RootProvider value={switchApi}>
  <Switch.Control><Switch.Thumb /></Switch.Control>
  <Switch.Label>Label</Switch.Label>
  <Switch.HiddenInput />
</Switch.RootProvider>

Root

PropDefaultType
asChildfalse | true

Use the provided child element as the default rendered element, combining their props and behavior.

checkedfalse | true

Whether the switch is checked.

defaultCheckedfalse | true

The checked state of the switch when it is first rendered. Use this when you do not need to control the state of the switch.

disabledfalse | true

Whether the switch is disabled.

formstring

The id of the form that the switch belongs to

idstring

The unique identifier of the machine.

idsPartial<{ root: string; hiddenInput: string; control: string; label: string; thumb: string; }>

The ids of the elements in the switch. Useful for composition.

invalidfalse | true

If `true`, the switch is marked as invalid.

labelstring

Specifies the localized strings that identifies the accessibility elements and their states

namestring

The name of the input field in a switch (Useful for form submission).

readOnlyfalse | true

Whether the switch is read-only

requiredfalse | true

If `true`, the switch input is marked as required,

value"on"string | number

The value of switch input. Useful for form submission.

EmitEvent
checkedChange[details: CheckedChangeDetails]

Function to call when the switch is clicked.

update:checked[checked: boolean]

The callback fired when the model value changes.

Context

No serializable props or emits are declared for this part.

Control

PropDefaultType
asChildfalse | true

Use the provided child element as the default rendered element, combining their props and behavior.

HiddenInput

PropDefaultType
asChildfalse | true

Use the provided child element as the default rendered element, combining their props and behavior.

Label

PropDefaultType
asChildfalse | true

Use the provided child element as the default rendered element, combining their props and behavior.

RootProvider

PropDefaultType
value*MachineApi<PropTypes>
asChildfalse | true

Use the provided child element as the default rendered element, combining their props and behavior.

Thumb

PropDefaultType
asChildfalse | true

Use the provided child element as the default rendered element, combining their props and behavior.

Root

PropDefaultType
asChildfalse | true

Use the provided child element as the default rendered element, combining their props and behavior.

checkedfalse | true

Whether the switch is checked.

disabledfalse | true

Whether the switch is disabled.

idsPartial<{ root: string; hiddenInput: string; control: string; label: string; thumb: string; }>

The ids of the elements in the switch. Useful for composition.

invalidfalse | true

If `true`, the switch is marked as invalid.

labelstring

Specifies the localized strings that identifies the accessibility elements and their states

namestring

The name of the input field in a switch (Useful for form submission).

onCheckedChange(details: CheckedChangeDetails) => void

Function to call when the switch is clicked.

readOnlyfalse | true

Whether the switch is read-only

requiredfalse | true

If `true`, the switch input is marked as required,

value"on"string | number

The value of switch input. Useful for form submission.

Context

PropDefaultType
children*(context: UseSwitchContext) => ReactNode

Control

PropDefaultType
asChildfalse | true

Use the provided child element as the default rendered element, combining their props and behavior.

HiddenInput

PropDefaultType
asChildfalse | true

Use the provided child element as the default rendered element, combining their props and behavior.

Label

PropDefaultType
asChildfalse | true

Use the provided child element as the default rendered element, combining their props and behavior.

RootProvider

PropDefaultType
value*UseSwitchReturn
asChildfalse | true

Use the provided child element as the default rendered element, combining their props and behavior.

Thumb

PropDefaultType
asChildfalse | true

Use the provided child element as the default rendered element, combining their props and behavior.

Root

PropDefaultType
asChild(props: (userProps?: solid_js185.JSX.LabelHTMLAttributes<HTMLLabelElement> | undefined) => JSX.HTMLAttributes<any>) => JSX.Element

Use the provided child element as the default rendered element, combining their props and behavior.

checkedfalse | true

Whether the switch is checked.

defaultCheckedfalse | true

The checked state of the switch when it is first rendered. Use this when you do not need to control the state of the switch.

disabledfalse | true

Whether the switch is disabled.

formstring

The id of the form that the switch belongs to

idsPartial<{ root: string; hiddenInput: string; control: string; label: string; thumb: string; }>

The ids of the elements in the switch. Useful for composition.

invalidfalse | true

If `true`, the switch is marked as invalid.

labelstring

Specifies the localized strings that identifies the accessibility elements and their states

namestring

The name of the input field in a switch (Useful for form submission).

onCheckedChange(details: CheckedChangeDetails) => void

Function to call when the switch is clicked.

readOnlyfalse | true

Whether the switch is read-only

requiredfalse | true

If `true`, the switch input is marked as required,

value"on"string | number

The value of switch input. Useful for form submission.

Context

PropDefaultType
children*(context: UseSwitchContext) => Element

Control

PropDefaultType
asChild(props: (userProps?: solid_js185.JSX.HTMLAttributes<HTMLSpanElement> | undefined) => JSX.HTMLAttributes<any>) => JSX.Element

Use the provided child element as the default rendered element, combining their props and behavior.

HiddenInput

PropDefaultType
asChild(props: (userProps?: solid_js185.JSX.InputHTMLAttributes<HTMLInputElement> | undefined) => JSX.HTMLAttributes<any>) => JSX.Element

Use the provided child element as the default rendered element, combining their props and behavior.

Label

PropDefaultType
asChild(props: (userProps?: solid_js185.JSX.HTMLAttributes<HTMLSpanElement> | undefined) => JSX.HTMLAttributes<any>) => JSX.Element

Use the provided child element as the default rendered element, combining their props and behavior.

RootProvider

PropDefaultType
value*UseSwitchReturn
asChild(props: (userProps?: solid_js185.JSX.LabelHTMLAttributes<HTMLLabelElement> | undefined) => JSX.HTMLAttributes<any>) => JSX.Element

Use the provided child element as the default rendered element, combining their props and behavior.

Thumb

PropDefaultType
asChild(props: (userProps?: solid_js185.JSX.HTMLAttributes<HTMLSpanElement> | undefined) => JSX.HTMLAttributes<any>) => JSX.Element

Use the provided child element as the default rendered element, combining their props and behavior.

Root

PropDefaultType
asChildimport("svelte").Snippet<[import("/home/runner/work/ui/ui/packages/svelte/dist/types").PropsFn<"label">]>
checkedfalse | true

Whether the switch is checked.

childrenSnippet<[]> | undefined
defaultCheckedfalse | true
disabledfalse | true

Whether the switch is disabled.

formstring

The id of the form that the switch belongs to

idstring
idsPartial<{ root: string; hiddenInput: string; control: string; label: string; thumb: string; }>

The ids of the elements in the switch. Useful for composition.

invalidfalse | true

If `true`, the switch is marked as invalid.

labelstring

Specifies the localized strings that identifies the accessibility elements and their states

namestring

The name of the input field in a switch (Useful for form submission).

onCheckedChange(details: import("/home/runner/work/ui/ui/packages/svelte/dist/index").SwitchCheckedChangeDetails) => void

Function to call when the switch is clicked.

readOnlyfalse | true

Whether the switch is read-only

requiredfalse | true

If `true`, the switch input is marked as required,

value"on"string | number

The value of switch input. Useful for form submission.

Context

PropDefaultType
render*Snippet<[UseSwitchContext]>

Control

PropDefaultType
asChildimport("svelte").Snippet<[import("/home/runner/work/ui/ui/packages/svelte/dist/types").PropsFn<"span">]>
childrenSnippet<[]> | undefined

HiddenInput

PropDefaultType
asChildimport("svelte").Snippet<[import("/home/runner/work/ui/ui/packages/svelte/dist/types").PropsFn<"input">]>
childrenSnippet<[]> | undefined

Label

PropDefaultType
asChildimport("svelte").Snippet<[import("/home/runner/work/ui/ui/packages/svelte/dist/types").PropsFn<"span">]>
childrenSnippet<[]> | undefined

RootProvider

PropDefaultType
value*UseSwitchReturn
asChildimport("svelte").Snippet<[import("/home/runner/work/ui/ui/packages/svelte/dist/types").PropsFn<"label">]>
childrenSnippet<[]> | undefined

Thumb

PropDefaultType
asChildimport("svelte").Snippet<[import("/home/runner/work/ui/ui/packages/svelte/dist/types").PropsFn<"span">]>
childrenSnippet<[]> | undefined