> ## 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.

# Conditional Rendering

> Toggle between UI states with If/Else and reactive switches.

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>;
};

Flip a switch and the UI changes. `If` and `Else` render different components based on reactive state, with no server round-trip.

<ComponentPreview json={{"view":{"cssClass":"w-full","type":"Card","children":[{"type":"CardHeader","children":[{"type":"CardTitle","content":"Preferences"}]},{"type":"CardContent","children":[{"cssClass":"gap-4","type":"Column","children":[{"name":"dark_mode","value":false,"type":"Switch","label":"Dark Mode","size":"default","disabled":false,"required":false},{"name":"notifications","value":true,"type":"Switch","label":"Notifications","size":"default","disabled":false,"required":false},{"type":"Condition","cases":[{"when":"{{ dark_mode }}","children":[{"type":"Alert","variant":"info","children":[{"type":"AlertTitle","content":"Dark Mode"},{"type":"AlertDescription","content":"Your eyes will thank you."}]}]}],"else":[{"type":"Alert","variant":"warning","children":[{"type":"AlertTitle","content":"Light Mode"},{"type":"AlertDescription","content":"Living dangerously, I see."}]}]},{"type":"Condition","cases":[{"when":"{{ notifications }}","children":[{"cssClass":"text-sm text-muted-foreground","content":"You'll receive alerts for all events.","type":"Text"}]}],"else":[{"cssClass":"text-sm text-muted-foreground","content":"Notifications are off. You're on your own.","type":"Text"}]}]}]}]},"state":{"dark_mode":false,"notifications":true}}} playground="ZnJvbSBwcmVmYWJfdWkuY29tcG9uZW50cyBpbXBvcnQgKAogICAgQWxlcnQsIEFsZXJ0RGVzY3JpcHRpb24sIEFsZXJ0VGl0bGUsCiAgICBDYXJkLCBDYXJkQ29udGVudCwgQ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JIHNlZS4iKQogICAgICAgICAgICB3aXRoIElmKG5vdGlmaWNhdGlvbnMpOgogICAgICAgICAgICAgICAgVGV4dCgKICAgICAgICAgICAgICAgICAgICAiWW91J2xsIHJlY2VpdmUgYWxlcnRzIGZvciBhbGwgZXZlbnRzLiIsCiAgICAgICAgICAgICAgICAgICAgY3NzX2NsYXNzPSJ0ZXh0LXNtIHRleHQtbXV0ZWQtZm9yZWdyb3VuZCIsCiAgICAgICAgICAgICAgICApCiAgICAgICAgICAgIHdpdGggRWxzZSgpOgogICAgICAgICAgICAgICAgVGV4dCgKICAgICAgICAgICAgICAgICAgICAiTm90aWZpY2F0aW9ucyBhcmUgb2ZmLiBZb3UncmUgb24geW91ciBvd24uIiwKICAgICAgICAgICAgICAgICAgICBjc3NfY2xhc3M9InRleHQtc20gdGV4dC1tdXRlZC1mb3JlZ3JvdW5kIiwKICAgICAgICAgICAgICAgICkK">
  <CodeGroup>
    ```python icon="python" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    from prefab_ui.components import (
        Alert, AlertDescription, AlertTitle,
        Card, CardContent, CardHeader, CardTitle,
        Column, Switch, Text,
    )
    from prefab_ui.components.control_flow import Else, If
    from prefab_ui.rx import Rx

    dark_mode = Rx("dark_mode")
    notifications = Rx("notifications")

    with Card(css_class="w-full"):
        with CardHeader():
            CardTitle("Preferences")
        with CardContent():
            with Column(gap=4):
                Switch(label="Dark Mode", name="dark_mode", value=False)
                Switch(label="Notifications", name="notifications", value=True)
                with If(dark_mode):
                    with Alert(variant="info"):
                        AlertTitle("Dark Mode")
                        AlertDescription("Your eyes will thank you.")
                with Else():
                    with Alert(variant="warning"):
                        AlertTitle("Light Mode")
                        AlertDescription("Living dangerously, I see.")
                with If(notifications):
                    Text(
                        "You'll receive alerts for all events.",
                        css_class="text-sm text-muted-foreground",
                    )
                with Else():
                    Text(
                        "Notifications are off. You're on your own.",
                        css_class="text-sm text-muted-foreground",
                    )
    ```

    ```json Protocol icon="brackets-curly" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    {
      "view": {
        "cssClass": "w-full",
        "type": "Card",
        "children": [
          {
            "type": "CardHeader",
            "children": [{"type": "CardTitle", "content": "Preferences"}]
          },
          {
            "type": "CardContent",
            "children": [
              {
                "cssClass": "gap-4",
                "type": "Column",
                "children": [
                  {
                    "name": "dark_mode",
                    "value": false,
                    "type": "Switch",
                    "label": "Dark Mode",
                    "size": "default",
                    "disabled": false,
                    "required": false
                  },
                  {
                    "name": "notifications",
                    "value": true,
                    "type": "Switch",
                    "label": "Notifications",
                    "size": "default",
                    "disabled": false,
                    "required": false
                  },
                  {
                    "type": "Condition",
                    "cases": [
                      {
                        "when": "{{ dark_mode }}",
                        "children": [
                          {
                            "type": "Alert",
                            "variant": "info",
                            "children": [
                              {"type": "AlertTitle", "content": "Dark Mode"},
                              {"type": "AlertDescription", "content": "Your eyes will thank you."}
                            ]
                          }
                        ]
                      }
                    ],
                    "else": [
                      {
                        "type": "Alert",
                        "variant": "warning",
                        "children": [
                          {"type": "AlertTitle", "content": "Light Mode"},
                          {"type": "AlertDescription", "content": "Living dangerously, I see."}
                        ]
                      }
                    ]
                  },
                  {
                    "type": "Condition",
                    "cases": [
                      {
                        "when": "{{ notifications }}",
                        "children": [
                          {
                            "cssClass": "text-sm text-muted-foreground",
                            "content": "You'll receive alerts for all events.",
                            "type": "Text"
                          }
                        ]
                      }
                    ],
                    "else": [
                      {
                        "cssClass": "text-sm text-muted-foreground",
                        "content": "Notifications are off. You're on your own.",
                        "type": "Text"
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      },
      "state": {"dark_mode": false, "notifications": true}
    }
    ```
  </CodeGroup>
</ComponentPreview>

## How It Works

Two boolean [state](/concepts/state) keys drive everything: `dark_mode` (starting `False`) and `notifications` (starting `True`). Each `Switch` binds to one of them through its `name`, so flipping a switch writes `True` or `False` back to state and re-renders anything that reads it.

`dark_mode = Rx("dark_mode")` and `notifications = Rx("notifications")` create reactive references to those keys. Passing one to [`If`](/components/conditional) registers a condition that the renderer re-evaluates whenever state changes. `with If(dark_mode):` renders its children only while `dark_mode` is truthy; the paired `with Else():` renders the alternative the rest of the time. The two branches are mutually exclusive — exactly one alert is mounted at any moment, swapping the moment the switch moves.

The pattern composes. The second `If`/`Else` pair reads `notifications` independently and toggles between two status lines, so the card holds two unrelated conditional regions side by side. Each `If`/`Else` only needs the reactive reference for its condition — there is no manual subscription or re-render logic, because [conditional rendering](/concepts/expressions) reacts to the same client-side state the switches write to. Flipping either switch updates only the branch that depends on it.
