agentic-ui logoagentic-ui

Switch

Toggle for boolean settings. Use the wrapper or compose SwitchRoot + SwitchThumb with a raw base-ui Root.

High-level wrapper

import { Switch } from "@brijbyte/agentic-ui/switch";

import "@brijbyte/agentic-ui/switch.css";

export default function HighLevelDemo() {
  return (
    <>
      <Switch>Notifications</Switch>
      <Switch defaultChecked>Auto-save (on by default)</Switch>
      <Switch disabled>Disabled off</Switch>
      <Switch defaultChecked disabled>
        Disabled on
      </Switch>
    </>
  );
}

Controlled

import { useState } from "react";
import { Switch } from "@brijbyte/agentic-ui/switch";

import "@brijbyte/agentic-ui/switch.css";

export default function ControlledDemo() {
  const [val, setVal] = useState(false);

  return (
    <Switch checked={val} onCheckedChange={(c) => setVal(c as boolean)}>
      Dark mode: {val ? "on" : "off"}
    </Switch>
  );
}

Composed: base-ui Root + SwitchThumb

import { useId } from "react";
import { SwitchRoot, SwitchThumb } from "@brijbyte/agentic-ui/switch";

import "@brijbyte/agentic-ui/switch.css";

export default function ComposedDemo() {
  const id = useId();
  return (
    <label
      htmlFor={id}
      style={{
        display: "flex",
        alignItems: "center",
        gap: "var(--space-2)",
        cursor: "pointer",
        fontFamily: "var(--font-mono)",
        fontSize: "var(--font-size-sm)",
        color: "var(--color-primary)",
      }}
    >
      <SwitchRoot id={id}>
        <SwitchThumb />
      </SwitchRoot>
      Styled Root + Thumb
    </label>
  );
}

API Reference

Toggle for boolean settings. Renders a track with a sliding thumb and
an optional label. Wraps @base-ui/react Switch.

PropTypeDefault
checkedboolean—

Controlled checked state.

defaultCheckedboolean—

Initial checked state (uncontrolled).

disabledboolean—

Prevent interaction.

requiredboolean—

Mark the field as required for form validation.

namestring—

HTML name attribute for form submission.

valuestring—

HTML value attribute for form submission.

onCheckedChange((checked: boolean, eventDetails: unknown) => void)—

Called when checked state changes. eventDetails is the base-ui event details object.

childrenReactNode—

Label content rendered next to the switch.

idstring—

Override the auto-generated element id.

classNamestring—

Styled Parts

base-ui docs ↗

Pre-styled wrappers around the corresponding base-ui parts. All base-ui props are forwarded.

CSS Class Names

Available as keys on the SwitchStyles object. Each key maps to a hashed CSS module class name at runtime.

labelrootthumbthumb-track