---
layout: ~/layouts/MdxDocLayout.astro
title: "Switch"
description: "A two-state button that can be either on or off."
sourceFile: "components/switch"
baseUIComponent: "switch"
---

import ComponentExample from "~/components/docs/ComponentExample.astro";
import ComponentSection from "~/components/docs/ComponentSection.astro";
import CodeBlock from "~/components/docs/CodeBlock.astro";
import Heading from "~/components/docs/Heading.astro";
import PropsTable from "~/components/docs/PropsTable.astro";
import {
  SwitchBasicDemo,
  SwitchOffDemo,
  SwitchOnDemo,
} from "~/components/demos/SwitchDemo";

{/* Demo */}

<ComponentSection>
  <ComponentExample
    code={`<Switch label="Switch" checked={false} onCheckedChange={() => {}} />`}
  >
    <SwitchBasicDemo client:visible />
  </ComponentExample>
</ComponentSection>

{/* Installation */}

<ComponentSection>
  <Heading level={2}>Installation</Heading>
  <Heading level={3}>Barrel</Heading>
  <CodeBlock code={`import { Switch } from "@cloudflare/kumo";`} lang="tsx" />
  <Heading level={3}>Granular</Heading>
  <CodeBlock
    code={`import { Switch } from "@cloudflare/kumo/components/switch";`}
    lang="tsx"
  />
</ComponentSection>

{/* Usage */}

<ComponentSection>
  <Heading level={2}>Usage</Heading>
  <CodeBlock
    code={`import { Switch } from "@cloudflare/kumo";
import { useState } from "react";

export default function Example() {
  const [checked, setChecked] = useState(false);
  
  return (
    <Switch 
      checked={checked} 
      onCheckedChange={(val) => setChecked(val)} 
    />
  );
}`}
    lang="tsx"
  />
</ComponentSection>

{/* Examples */}

<ComponentSection>
  <Heading level={2}>Examples</Heading>

<Heading level={3}>Off State</Heading>
<ComponentExample
  code={`<Switch label="Switch" checked={false} onCheckedChange={() => {}} />`}
>
  <SwitchOffDemo client:visible />
</ComponentExample>

  <Heading level={3}>On State</Heading>
  <ComponentExample
    code={`<Switch label="Switch" checked={true} onCheckedChange={() => {}} />`}
  >
    <SwitchOnDemo client:visible />
  </ComponentExample>
</ComponentSection>

{/* API Reference */}

<ComponentSection>
  <Heading level={2}>API Reference</Heading>
  <PropsTable component="Switch" />
</ComponentSection>
