> ## Documentation Index
> Fetch the complete documentation index at: https://prefab.prefect.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Switch

> Toggle switch for on/off states.

export const ComponentPreview = ({json, height, resizable, bare, hideJson, playground, children}) => {
  const hostRef = React.useRef(null);
  const handleRef = React.useRef(null);
  const cardRef = React.useRef(null);
  const containerRef = React.useRef(null);
  const [previewWidth, setPreviewWidth] = React.useState(null);
  const [isDragging, setIsDragging] = React.useState(false);
  const [playgroundHref, setPlaygroundHref] = React.useState(null);
  React.useEffect(function () {
    if (!playground) return;
    var isLocal = location.hostname === "localhost" || location.hostname === "127.0.0.1";
    var path = isLocal ? "/playground" : "/docs/playground";
    var url = new URL(path, window.location.href);
    url.hash = "code=" + playground;
    setPlaygroundHref(url.href);
  }, [playground]);
  var jsonStr = typeof json === "string" ? json : JSON.stringify(json, null, 2);
  React.useEffect(function () {
    if (cardRef.current) {
      var card = cardRef.current.closest(".group");
      if (card) card.classList.remove("group");
    }
    var host = hostRef.current;
    if (!host || !json) return;
    function mount() {
      if (!window.__prefab || !host) return;
      var dark = document.documentElement.classList.contains("dark");
      handleRef.current = window.__prefab.mountPreview(host, jsonStr, {
        dark: dark
      });
      if (bare && host.shadowRoot) {
        var m = host.shadowRoot.querySelector("[data-prefab-mount]");
        if (m) m.style.background = "transparent";
      }
    }
    if (window.__prefab) {
      mount();
    } else {
      if (!window.__prefabLoading) {
        if (!window.__prefabReady) {
          var s = document.createElement("script");
          s.src = "/renderer.js";
          document.head.appendChild(s);
        }
        window.__prefabLoading = new Promise(function (resolve) {
          function check() {
            if (window.__prefabReady) {
              window.__prefabReady.then(resolve);
            } else {
              setTimeout(check, 10);
            }
          }
          check();
        });
      }
      window.__prefabLoading.then(mount);
    }
    var observer = new MutationObserver(function () {
      var dark = document.documentElement.classList.contains("dark");
      if (handleRef.current) handleRef.current.setDark(dark);
    });
    observer.observe(document.documentElement, {
      attributes: true,
      attributeFilter: ["class"]
    });
    return function () {
      observer.disconnect();
      if (handleRef.current) {
        handleRef.current.unmount();
        handleRef.current = null;
      }
    };
  }, [json]);
  function startResize(e) {
    e.preventDefault();
    var startX = e.clientX;
    var startW = hostRef.current ? hostRef.current.offsetWidth : 0;
    var maxW = containerRef.current ? containerRef.current.offsetWidth - 10 : startW;
    setIsDragging(true);
    function onMove(ev) {
      var w = Math.max(260, Math.min(startW + (ev.clientX - startX), maxW));
      setPreviewWidth(w);
    }
    function onUp() {
      document.removeEventListener("mousemove", onMove);
      document.removeEventListener("mouseup", onUp);
      setIsDragging(false);
    }
    document.addEventListener("mousemove", onMove);
    document.addEventListener("mouseup", onUp);
  }
  if (bare) {
    return <div ref={hostRef} />;
  }
  if (resizable) {
    return <Card>
        <div ref={cardRef} />
        <div ref={containerRef} style={{
      display: "flex",
      position: "relative",
      overflow: "hidden"
    }}>
          <div ref={hostRef} style={{
      flex: previewWidth ? "none" : 1,
      width: previewWidth ? previewWidth + "px" : undefined,
      minWidth: 0
    }} />
          <div onMouseDown={startResize} style={{
      width: "14px",
      flexShrink: 0,
      cursor: "col-resize",
      background: isDragging ? "var(--border, #e5e7eb)" : "color-mix(in srgb, var(--border, #e5e7eb) 40%, transparent)",
      display: "flex",
      alignItems: "center",
      justifyContent: "center",
      transition: "background 0.15s",
      userSelect: "none"
    }}>
            <svg width="6" height="24" style={{
      opacity: 0.5
    }}>
              <circle cx="2" cy="4" r="1.2" fill="currentColor" />
              <circle cx="2" cy="9" r="1.2" fill="currentColor" />
              <circle cx="2" cy="14" r="1.2" fill="currentColor" />
              <circle cx="2" cy="19" r="1.2" fill="currentColor" />
              <circle cx="5" cy="4" r="1.2" fill="currentColor" />
              <circle cx="5" cy="9" r="1.2" fill="currentColor" />
              <circle cx="5" cy="14" r="1.2" fill="currentColor" />
              <circle cx="5" cy="19" r="1.2" fill="currentColor" />
            </svg>
          </div>
          <div style={{
      flex: previewWidth ? 1 : "0 0 24px",
      minWidth: "24px",
      background: "repeating-linear-gradient(-45deg, transparent, transparent 3px, var(--border, #e5e7eb) 3px, var(--border, #e5e7eb) 4px)",
      opacity: 0.4
    }} />
          {previewWidth && <div style={{
      position: "absolute",
      bottom: "8px",
      right: "8px",
      background: "var(--background, white)",
      border: "1px solid var(--border, #e5e7eb)",
      borderRadius: "4px",
      padding: "1px 6px",
      fontSize: "11px",
      fontFamily: "monospace",
      color: "var(--muted-foreground, #6b7280)",
      pointerEvents: "none"
    }}>
              {previewWidth}px
            </div>}
        </div>
        {playgroundHref && <div style={{
      display: "flex",
      justifyContent: "flex-end",
      padding: "4px 0 8px"
    }}>
            <a href={playgroundHref} target="_blank" rel="noopener noreferrer" style={{
      fontSize: "11px",
      opacity: 0.4,
      textDecoration: "none",
      textDecorationLine: "none",
      borderBottom: "none",
      boxShadow: "none",
      display: "inline-flex",
      alignItems: "center",
      gap: "3px",
      transition: "opacity 0.15s",
      fontWeight: 400
    }} onMouseOver={function (e) {
      e.currentTarget.style.opacity = "0.6";
    }} onMouseOut={function (e) {
      e.currentTarget.style.opacity = "0.4";
    }}>
              Edit in Playground
              <svg xmlns="http://www.w3.org/2000/svg" width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M15 3h6v6" /><path d="M10 14 21 3" /><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6" /></svg>
            </a>
          </div>}
        <div style={{
      marginBottom: "-2rem"
    }}>{children}</div>
      </Card>;
  }
  return <Card>
      <div ref={cardRef} />
      <div ref={hostRef} />
      {playgroundHref && <div style={{
    display: "flex",
    justifyContent: "flex-end",
    padding: "4px 0 8px"
  }}>
          <a href={playgroundHref} target="_blank" rel="noopener noreferrer" style={{
    fontSize: "11px",
    opacity: 0.4,
    textDecoration: "none",
    textDecorationLine: "none",
    borderBottom: "none",
    boxShadow: "none",
    display: "inline-flex",
    alignItems: "center",
    gap: "3px",
    transition: "opacity 0.15s",
    fontWeight: 400
  }} onMouseOver={function (e) {
    e.currentTarget.style.opacity = "0.6";
  }} onMouseOut={function (e) {
    e.currentTarget.style.opacity = "0.4";
  }}>
            Edit in Playground
            <svg xmlns="http://www.w3.org/2000/svg" width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M15 3h6v6" /><path d="M10 14 21 3" /><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6" /></svg>
          </a>
        </div>}
      <div style={{
    marginBottom: "-2rem"
  }}>{children}</div>
    </Card>;
};

Switches provide a visual toggle for binary states, offering an alternative to checkboxes.

## Basic Usage

<ComponentPreview json={{"view":{"name":"switch_7","value":false,"type":"Switch","label":"Enable notifications","size":"default","disabled":false,"required":false},"state":{"switch_7":false}}} playground="ZnJvbSBwcmVmYWJfdWkuY29tcG9uZW50cyBpbXBvcnQgU3dpdGNoCgpTd2l0Y2gobGFiZWw9IkVuYWJsZSBub3RpZmljYXRpb25zIikK">
  <CodeGroup>
    ```python Python icon="python" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    from prefab_ui.components import Switch

    Switch(label="Enable notifications")
    ```

    ```json Protocol icon="brackets-curly" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    {
      "view": {
        "name": "switch_7",
        "value": false,
        "type": "Switch",
        "label": "Enable notifications",
        "size": "default",
        "disabled": false,
        "required": false
      },
      "state": {"switch_7": false}
    }
    ```
  </CodeGroup>
</ComponentPreview>

## Checked State

Pass `value=True` to render the switch in its on position from the start. Use this to reflect a setting that already defaults to enabled, so the control matches the state of the system the moment it appears.

<ComponentPreview json={{"view":{"name":"switch_8","value":true,"type":"Switch","label":"Dark mode","size":"default","disabled":false,"required":false},"state":{"switch_8":true}}} playground="ZnJvbSBwcmVmYWJfdWkuY29tcG9uZW50cyBpbXBvcnQgU3dpdGNoCgpTd2l0Y2gobGFiZWw9IkRhcmsgbW9kZSIsIHZhbHVlPVRydWUpCg">
  <CodeGroup>
    ```python Python icon="python" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    from prefab_ui.components import Switch

    Switch(label="Dark mode", value=True)
    ```

    ```json Protocol icon="brackets-curly" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    {
      "view": {
        "name": "switch_8",
        "value": true,
        "type": "Switch",
        "label": "Dark mode",
        "size": "default",
        "disabled": false,
        "required": false
      },
      "state": {"switch_8": true}
    }
    ```
  </CodeGroup>
</ComponentPreview>

## Sizes

Switches come in two sizes:

<ComponentPreview json={{"view":{"cssClass":"gap-4 w-fit mx-auto","type":"Column","children":[{"name":"switch_9","value":true,"type":"Switch","label":"Default size","size":"default","disabled":false,"required":false},{"name":"switch_10","value":true,"type":"Switch","label":"Small size","size":"sm","disabled":false,"required":false}]},"state":{"switch_9":true,"switch_10":true}}} playground="ZnJvbSBwcmVmYWJfdWkuY29tcG9uZW50cyBpbXBvcnQgKAogICAgQ29sdW1uLAogICAgU3dpdGNoLAopCgp3aXRoIENvbHVtbihnYXA9NCwgY3NzX2NsYXNzPSJ3LWZpdCBteC1hdXRvIik6CiAgICBTd2l0Y2gobGFiZWw9IkRlZmF1bHQgc2l6ZSIsIHZhbHVlPVRydWUpCiAgICBTd2l0Y2gobGFiZWw9IlNtYWxsIHNpemUiLCBzaXplPSJzbSIsIHZhbHVlPVRydWUpCg">
  <CodeGroup>
    ```python Python icon="python" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    from prefab_ui.components import (
        Column,
        Switch,
    )

    with Column(gap=4, css_class="w-fit mx-auto"):
        Switch(label="Default size", value=True)
        Switch(label="Small size", size="sm", value=True)
    ```

    ```json Protocol icon="brackets-curly" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    {
      "view": {
        "cssClass": "gap-4 w-fit mx-auto",
        "type": "Column",
        "children": [
          {
            "name": "switch_9",
            "value": true,
            "type": "Switch",
            "label": "Default size",
            "size": "default",
            "disabled": false,
            "required": false
          },
          {
            "name": "switch_10",
            "value": true,
            "type": "Switch",
            "label": "Small size",
            "size": "sm",
            "disabled": false,
            "required": false
          }
        ]
      },
      "state": {"switch_9": true, "switch_10": true}
    }
    ```
  </CodeGroup>
</ComponentPreview>

## With Actions

The `on_change` parameter runs actions when the switch is toggled. The `$event` variable is `true` when switched on, `false` when off. Here, toggling the switch updates a separate status message:

<ComponentPreview json={{"view":{"cssClass":"gap-4 w-fit mx-auto","type":"Column","children":[{"name":"notifications","value":false,"type":"Switch","label":"Enable notifications","size":"default","disabled":false,"required":false,"onChange":{"action":"setState","key":"status","value":"{{ $event ? 'Notifications enabled' : 'Notifications disabled' }}"}},{"cssClass":"text-muted-foreground","content":"{{ status | default:'Toggle the switch' }}","type":"Text"}]},"state":{"notifications":false}}} playground="ZnJvbSBwcmVmYWJfdWkuY29tcG9uZW50cyBpbXBvcnQgQ29sdW1uLCBTd2l0Y2gsIFRleHQKZnJvbSBwcmVmYWJfdWkuYWN0aW9ucyBpbXBvcnQgU2V0U3RhdGUKZnJvbSBwcmVmYWJfdWkucnggaW1wb3J0IEVWRU5ULCBSeAoKc3RhdHVzID0gUngoInN0YXR1cyIpCgp3aXRoIENvbHVtbihnYXA9NCwgY3NzX2NsYXNzPSJ3LWZpdCBteC1hdXRvIik6CiAgICBTd2l0Y2goCiAgICAgICAgbGFiZWw9IkVuYWJsZSBub3RpZmljYXRpb25zIiwKICAgICAgICBuYW1lPSJub3RpZmljYXRpb25zIiwKICAgICAgICBvbl9jaGFuZ2U9U2V0U3RhdGUoCiAgICAgICAgICAgICJzdGF0dXMiLAogICAgICAgICAgICBFVkVOVC50aGVuKCJOb3RpZmljYXRpb25zIGVuYWJsZWQiLCAiTm90aWZpY2F0aW9ucyBkaXNhYmxlZCIpLAogICAgICAgICksCiAgICApCiAgICBUZXh0KAogICAgICAgIHN0YXR1cy5kZWZhdWx0KCJUb2dnbGUgdGhlIHN3aXRjaCIpLAogICAgICAgIGNzc19jbGFzcz0idGV4dC1tdXRlZC1mb3JlZ3JvdW5kIiwKICAgICkK">
  <CodeGroup>
    ```python Python icon="python" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    from prefab_ui.components import Column, Switch, Text
    from prefab_ui.actions import SetState
    from prefab_ui.rx import EVENT, Rx

    status = Rx("status")

    with Column(gap=4, css_class="w-fit mx-auto"):
        Switch(
            label="Enable notifications",
            name="notifications",
            on_change=SetState(
                "status",
                EVENT.then("Notifications enabled", "Notifications disabled"),
            ),
        )
        Text(
            status.default("Toggle the switch"),
            css_class="text-muted-foreground",
        )
    ```

    ```json Protocol icon="brackets-curly" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    {
      "view": {
        "cssClass": "gap-4 w-fit mx-auto",
        "type": "Column",
        "children": [
          {
            "name": "notifications",
            "value": false,
            "type": "Switch",
            "label": "Enable notifications",
            "size": "default",
            "disabled": false,
            "required": false,
            "onChange": {
              "action": "setState",
              "key": "status",
              "value": "{{ $event ? 'Notifications enabled' : 'Notifications disabled' }}"
            }
          },
          {
            "cssClass": "text-muted-foreground",
            "content": "{{ status | default:'Toggle the switch' }}",
            "type": "Text"
          }
        ]
      },
      "state": {"notifications": false}
    }
    ```
  </CodeGroup>
</ComponentPreview>

## Disabled State

Set `disabled=True` to show a switch whose state is fixed and can't be toggled. This communicates that a setting is locked by something else — a plan tier, a permission, or another toggle — while still letting the user see whether it's currently on or off.

<ComponentPreview json={{"view":{"cssClass":"gap-3 w-fit mx-auto","type":"Column","children":[{"name":"switch_11","value":false,"type":"Switch","label":"Enabled switch","size":"default","disabled":false,"required":false},{"name":"switch_12","value":false,"type":"Switch","label":"Disabled off","size":"default","disabled":true,"required":false},{"name":"switch_13","value":true,"type":"Switch","label":"Disabled on","size":"default","disabled":true,"required":false}]},"state":{"switch_11":false,"switch_12":false,"switch_13":true}}} playground="ZnJvbSBwcmVmYWJfdWkuY29tcG9uZW50cyBpbXBvcnQgKAogICAgQ29sdW1uLAogICAgU3dpdGNoLAopCgp3aXRoIENvbHVtbihnYXA9MywgY3NzX2NsYXNzPSJ3LWZpdCBteC1hdXRvIik6CiAgICBTd2l0Y2gobGFiZWw9IkVuYWJsZWQgc3dpdGNoIikKICAgIFN3aXRjaChsYWJlbD0iRGlzYWJsZWQgb2ZmIiwgZGlzYWJsZWQ9VHJ1ZSkKICAgIFN3aXRjaChsYWJlbD0iRGlzYWJsZWQgb24iLCB2YWx1ZT1UcnVlLCBkaXNhYmxlZD1UcnVlKQo">
  <CodeGroup>
    ```python Python icon="python" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    from prefab_ui.components import (
        Column,
        Switch,
    )

    with Column(gap=3, css_class="w-fit mx-auto"):
        Switch(label="Enabled switch")
        Switch(label="Disabled off", disabled=True)
        Switch(label="Disabled on", value=True, disabled=True)
    ```

    ```json Protocol icon="brackets-curly" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    {
      "view": {
        "cssClass": "gap-3 w-fit mx-auto",
        "type": "Column",
        "children": [
          {
            "name": "switch_11",
            "value": false,
            "type": "Switch",
            "label": "Enabled switch",
            "size": "default",
            "disabled": false,
            "required": false
          },
          {
            "name": "switch_12",
            "value": false,
            "type": "Switch",
            "label": "Disabled off",
            "size": "default",
            "disabled": true,
            "required": false
          },
          {
            "name": "switch_13",
            "value": true,
            "type": "Switch",
            "label": "Disabled on",
            "size": "default",
            "disabled": true,
            "required": false
          }
        ]
      },
      "state": {"switch_11": false, "switch_12": false, "switch_13": true}
    }
    ```
  </CodeGroup>
</ComponentPreview>

## Reading the Value

Use `.rx` to get a reactive reference to the switch's boolean state. The `.then()` pipe converts `true`/`false` into display-friendly strings:

<ComponentPreview json={{"view":{"cssClass":"gap-4 items-center","type":"Row","children":[{"name":"switch_14","value":true,"type":"Switch","label":"Auto-save to the Guide","size":"default","disabled":false,"required":false},{"type":"Label","text":"Auto-save:","optional":false},{"name":"input_17","value":"{{ switch_14 ? 'On' : 'Off' }}","type":"Input","inputType":"text","disabled":false,"readOnly":true,"required":false}]},"state":{"switch_14":true}}} playground="ZnJvbSBwcmVmYWJfdWkuY29tcG9uZW50cyBpbXBvcnQgSW5wdXQsIExhYmVsLCBSb3csIFN3aXRjaAoKd2l0aCBSb3coZ2FwPTQsIGFsaWduPSJjZW50ZXIiKToKICAgIHN3ID0gU3dpdGNoKGxhYmVsPSJBdXRvLXNhdmUgdG8gdGhlIEd1aWRlIiwgdmFsdWU9VHJ1ZSkKICAgIExhYmVsKCJBdXRvLXNhdmU6IikKICAgIElucHV0KHZhbHVlPXN3LnJ4LnRoZW4oIk9uIiwgIk9mZiIpLCByZWFkX29ubHk9VHJ1ZSkK">
  <CodeGroup>
    ```python Python icon="python" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    from prefab_ui.components import Input, Label, Row, Switch

    with Row(gap=4, align="center"):
        sw = Switch(label="Auto-save to the Guide", value=True)
        Label("Auto-save:")
        Input(value=sw.rx.then("On", "Off"), read_only=True)
    ```

    ```json Protocol icon="brackets-curly" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    {
      "view": {
        "cssClass": "gap-4 items-center",
        "type": "Row",
        "children": [
          {
            "name": "switch_14",
            "value": true,
            "type": "Switch",
            "label": "Auto-save to the Guide",
            "size": "default",
            "disabled": false,
            "required": false
          },
          {"type": "Label", "text": "Auto-save:", "optional": false},
          {
            "name": "input_17",
            "value": "{{ switch_14 ? 'On' : 'Off' }}",
            "type": "Input",
            "inputType": "text",
            "disabled": false,
            "readOnly": true,
            "required": false
          }
        ]
      },
      "state": {"switch_14": true}
    }
    ```
  </CodeGroup>
</ComponentPreview>

Toggle the switch and the input updates to reflect the current state.

## API Reference

<Card icon="code" title="Switch Parameters">
  <ParamField body="label" type="str | None" default="None">
    Label text displayed next to the switch.
  </ParamField>

  <ParamField body="value" type="bool" default="False">
    Whether the switch is on.
  </ParamField>

  <ParamField body="size" type="str" default="default">
    Switch size: `"default"`, `"sm"`.
  </ParamField>

  <ParamField body="name" type="str | None" default="None">
    State key for the switch's checked state. Auto-generated if not provided. Use `.rx` to reference the switch's boolean state in other components without specifying the name explicitly.
  </ParamField>

  <ParamField body="disabled" type="bool" default="False">
    Whether the switch is non-interactive.
  </ParamField>

  <ParamField body="required" type="bool" default="False">
    Whether a selection is required for form submission.
  </ParamField>

  <ParamField body="on_change" type="Action | list[Action] | None" default="None">
    Action(s) to execute when toggled. `$event` is `true` when on, `false` when off.
  </ParamField>

  <ParamField body="css_class" type="str | None" default="None">
    Additional Tailwind CSS classes appended to the component's built-in styles.
  </ParamField>
</Card>

## Protocol Reference

```json Switch theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
{
  "type": "Switch",
  "id?": "string",
  "onMount?": "any",
  "name?": "string",
  "value?": "boolean | string",
  "label?": "string",
  "size?": "sm | default",
  "disabled?": false,
  "required?": false,
  "onChange?": "Action | Action[]",
  "cssClass?": "string"
}
```

For the complete protocol schema, see [Switch](/protocol/switch).
