Switch

A toggle control enabling users to switch between "on" and "off" states.

	<script lang="ts">
  import { Label, Switch } from "bits-ui";
</script>
 
<div class="flex items-center space-x-3">
  <Switch.Root
    id="dnd"
    class="peer inline-flex h-[36px] min-h-[36px] w-[60px] shrink-0 cursor-pointer items-center rounded-full px-[3px] transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-foreground focus-visible:ring-offset-2 focus-visible:ring-offset-background disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-foreground data-[state=unchecked]:bg-dark-10 data-[state=unchecked]:shadow-mini-inset dark:data-[state=checked]:bg-foreground"
  >
    <Switch.Thumb
      class="pointer-events-none block size-[30px] shrink-0 rounded-full bg-background transition-transform data-[state=checked]:translate-x-6 data-[state=unchecked]:translate-x-0 data-[state=unchecked]:shadow-mini dark:border dark:border-background/30 dark:bg-foreground dark:shadow-popover dark:data-[state=unchecked]:border"
    />
  </Switch.Root>
  <Label.Root for="dnd" class="text-sm font-medium">Do not disturb</Label.Root>
</div>

Structure

	<script lang="ts">
	import { Switch } from "bits-ui";
</script>
 
<Switch.Root>
	<Switch.Thumb />
	<Switch.Input />
</Switch.Root>

API Reference

Switch.Root

The root switch component used to set and manage the state of the switch.

Property Type Description
checked bindable prop
boolean

Whether or not the switch is checked.

Default: false
onCheckedChange
function

A callback function called when the checked state of the switch changes.

Default: undefined
disabled
boolean

Whether or not the switch is disabled.

Default: false
name
string

The name of the hidden input element, used to identify the input in form submissions.

Default: undefined
required
boolean

Whether or not the switch is required to be checked.

Default: false
value
string

The value of the hidden input element to be used in form submissions when the switch is checked.

Default: undefined
ref bindable prop
HTMLButtonElement

The underlying DOM element being rendered. You can bind to this to get a reference to the element.

Default: undefined
children
Snippet

The children content to render.

Default: undefined
child
Snippet

Use render delegation to render your own element. See delegation docs for more information.

Default: undefined
Data Attribute Value Description
data-state
enum

The switch's checked state.

data-checked
''

Present when the switch is checked.

data-disabled
''

Present when the switch is disabled.

data-switch-root
''

Present on the root element.

Switch.Thumb

The thumb on the switch used to indicate the switch's state.

Property Type Description
ref bindable prop
HTMLSpanElement

The underlying DOM element being rendered. You can bind to this to get a reference to the element.

Default: undefined
children
Snippet

The children content to render.

Default: undefined
child
Snippet

Use render delegation to render your own element. See delegation docs for more information.

Default: undefined
Data Attribute Value Description
data-state
enum

The switch's checked state.

data-checked
''

Present when the switch is checked.

data-switch-thumb
''

Present on the thumb element.