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

# Threshold Meter

> A progress bar whose color changes reactively based on value thresholds.

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

A slider drives a progress bar whose color shifts at each threshold. The slider's reactive reference is forward-declared with a lambda, so it can be used by components above it in the tree — the lambda resolves at render time, after the slider exists.

<ComponentPreview json={{"view":{"cssClass":"gap-6 max-w-sm mx-auto","type":"Column","children":[{"content":"Health Monitor","type":"H3"},{"content":"Drag the slider \u2014 the bar color changes at each threshold.","type":"Muted"},{"cssClass":"gap-4","type":"Column","children":[{"cssClass":"justify-between items-center","type":"Row","children":[{"content":"CPU Usage","type":"Text"},{"cssClass":"font-bold","content":"{{ slider_25 / 100 | percent }}","type":"Text"}]},{"cssClass":"pf-progress-flat","type":"Progress","value":"{{ slider_25 }}","variant":"default","size":"default","indicatorClass":"{{ slider_25 < 20 ? 'bg-red-500' : (slider_25 < 60 ? 'bg-primary' : (slider_25 < 75 ? 'bg-amber-500' : 'bg-green-500')) }}"},{"name":"slider_25","value":50.0,"type":"Slider","min":0.0,"max":100.0,"step":1.0,"disabled":false,"size":"default"}]}]},"state":{"slider_25":50.0}}} playground="ZnJvbSBwcmVmYWJfdWkuY29tcG9uZW50cyBpbXBvcnQgQ29sdW1uLCBIMywgTXV0ZWQsIFByb2dyZXNzLCBSb3csIFNsaWRlciwgVGV4dApmcm9tIHByZWZhYl91aS5yeCBpbXBvcnQgUngKCnZhbCA9IFJ4KGxhbWJkYTogc2xpZGVyKQoKd2l0aCBDb2x1bW4oZ2FwPTYsIGNzc19jbGFzcz0ibWF4LXctc20gbXgtYXV0byIpOgogICAgSDMoIkhlYWx0aCBNb25pdG9yIikKICAgIE11dGVkKCJEcmFnIHRoZSBzbGlkZXIg4oCUIHRoZSBiYXIgY29sb3IgY2hhbmdlcyBhdCBlYWNoIHRocmVzaG9sZC4iKQoKICAgIHdpdGggQ29sdW1uKGdhcD00KToKICAgICAgICB3aXRoIFJvdyhjc3NfY2xhc3M9Imp1c3RpZnktYmV0d2VlbiBpdGVtcy1jZW50ZXIiKToKICAgICAgICAgICAgVGV4dCgiQ1BVIFVzYWdlIikKICAgICAgICAgICAgVGV4dCgodmFsIC8gMTAwKS5wZXJjZW50KCksIGJvbGQ9VHJ1ZSkKICAgICAgICBQcm9ncmVzcygKICAgICAgICAgICAgdmFsdWU9dmFsLAogICAgICAgICAgICBpbmRpY2F0b3JfY2xhc3M9KHZhbCA8IDIwKS50aGVuKAogICAgICAgICAgICAgICAgImJnLXJlZC01MDAiLAogICAgICAgICAgICAgICAgKHZhbCA8IDYwKS50aGVuKAogICAgICAgICAgICAgICAgICAgICJiZy1wcmltYXJ5IiwKICAgICAgICAgICAgICAgICAgICAodmFsIDwgNzUpLnRoZW4oImJnLWFtYmVyLTUwMCIsICJiZy1ncmVlbi01MDAiKSwKICAgICAgICAgICAgICAgICksCiAgICAgICAgICAgICksCiAgICAgICAgKQogICAgICAgIHNsaWRlciA9IFNsaWRlcihtaW49MCwgbWF4PTEwMCwgc3RlcD0xLCB2YWx1ZT01MCkK">
  <CodeGroup>
    ```python Python icon="python" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    from prefab_ui.components import Column, H3, Muted, Progress, Row, Slider, Text
    from prefab_ui.rx import Rx

    val = Rx(lambda: slider)

    with Column(gap=6, css_class="max-w-sm mx-auto"):
        H3("Health Monitor")
        Muted("Drag the slider — the bar color changes at each threshold.")

        with Column(gap=4):
            with Row(css_class="justify-between items-center"):
                Text("CPU Usage")
                Text((val / 100).percent(), bold=True)
            Progress(
                value=val,
                indicator_class=(val < 20).then(
                    "bg-red-500",
                    (val < 60).then(
                        "bg-primary",
                        (val < 75).then("bg-amber-500", "bg-green-500"),
                    ),
                ),
            )
            slider = Slider(min=0, max=100, step=1, value=50)
    ```

    ```json Protocol icon="brackets-curly" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    {
      "view": {
        "cssClass": "gap-6 max-w-sm mx-auto",
        "type": "Column",
        "children": [
          {"content": "Health Monitor", "type": "H3"},
          {
            "content": "Drag the slider \u2014 the bar color changes at each threshold.",
            "type": "Muted"
          },
          {
            "cssClass": "gap-4",
            "type": "Column",
            "children": [
              {
                "cssClass": "justify-between items-center",
                "type": "Row",
                "children": [
                  {"content": "CPU Usage", "type": "Text"},
                  {
                    "cssClass": "font-bold",
                    "content": "{{ slider_25 / 100 | percent }}",
                    "type": "Text"
                  }
                ]
              },
              {
                "cssClass": "pf-progress-flat",
                "type": "Progress",
                "value": "{{ slider_25 }}",
                "variant": "default",
                "size": "default",
                "indicatorClass": "{{ slider_25 < 20 ? 'bg-red-500' : (slider_25 < 60 ? 'bg-primary' : (slider_25 < 75 ? 'bg-amber-500' : 'bg-green-500')) }}"
              },
              {
                "name": "slider_25",
                "value": 50.0,
                "type": "Slider",
                "min": 0.0,
                "max": 100.0,
                "step": 1.0,
                "disabled": false,
                "size": "default"
              }
            ]
          }
        ]
      },
      "state": {"slider_25": 50.0}
    }
    ```
  </CodeGroup>
</ComponentPreview>

## How It Works

The meter is driven by one piece of [state](/concepts/state): the value of the `Slider` at the bottom. Rather than passing `name="..."` and referencing a string key, this example uses an *anonymous* binding — the `Slider` auto-generates its own state key, and `val = Rx(lambda: slider)` captures a [reactive reference](/concepts/expressions) to it. The `lambda` defers the lookup, so `val` can be defined at the top of the column even though `slider` itself is created at the bottom. Everything above reads through `val`; the slider is the only thing that writes.

The color logic is a `.then()` chain that reads like nested `if`/`elif`. `(val < 20).then("bg-red-500", (val < 60).then("bg-primary", (val < 75).then("bg-amber-500", "bg-green-500")))` resolves to red below 20, the primary color below 60, amber below 75, and green above — each `.then(a, b)` is "use `a` if the condition holds, otherwise fall through to `b`." The chain compiles to a nested ternary in the wire format — `{{ slider_1 < 20 ? 'bg-red-500' : ... }}` — which the renderer re-evaluates on every state change. Passing it to `indicator_class` swaps the bar's Tailwind color class live as you drag.

The same reference feeds the other reactive props without extra work. `Progress(value=val)` drives the bar's fill, and the readout `Text((val / 100).percent(), bold=True)` divides the value into a fraction and formats it as a percentage. Because `value`, `indicator_class`, and the `Text` content all trace back to the one slider, a single drag updates the number, the fill, and the color in one pass.
