@lattice-ui/switch Part of the stable direction toward v1.0.
What It Is For
Use Switch for immediate on/off settings where the control communicates a binary system state rather than membership in a list.
It is the simplest boolean primitive in the library and works well for preferences panels and toggles.
import { Switch } from "@lattice-ui/switch";
import React from "@rbxts/react";
export function SwitchExample() {
const [checked, setChecked] = React.useState(false);
return (
<frame BackgroundTransparency={1} Size={UDim2.fromOffset(360, 150)}>
<Switch.Root asChild checked={checked} onCheckedChange={setChecked}>
<textbutton
AutoButtonColor={false}
BackgroundColor3={Color3.fromRGB(34, 41, 54)}
BorderSizePixel={0}
Size={UDim2.fromOffset(280, 44)}
Text=""
>
<uicorner CornerRadius={new UDim(0, 8)} />
<frame
BackgroundColor3={
checked
? Color3.fromRGB(53, 104, 196)
: Color3.fromRGB(60, 76, 104)
}
BorderSizePixel={0}
Position={UDim2.fromOffset(10, 10)}
Size={UDim2.fromOffset(46, 24)}
>
<uicorner CornerRadius={new UDim(1, 0)} />
<Switch.Thumb asChild>
<frame
BackgroundColor3={Color3.fromRGB(240, 244, 250)}
BorderSizePixel={0}
Position={
checked ? UDim2.fromOffset(24, 2) : UDim2.fromOffset(2, 2)
}
Size={UDim2.fromOffset(20, 20)}
>
<uicorner CornerRadius={new UDim(1, 0)} />
</frame>
</Switch.Thumb>
</frame>
<textlabel
BackgroundTransparency={1}
Position={UDim2.fromOffset(68, 0)}
Size={UDim2.fromOffset(180, 44)}
Text={checked ? "Notifications on" : "Notifications off"}
TextColor3={Color3.fromRGB(236, 240, 248)}
TextSize={14}
TextXAlignment={Enum.TextXAlignment.Left}
/>
</textbutton>
</Switch.Root>
</frame>
);
} Live demo is experimental and may contain bugs.
Install
Global CLI command: lattice add switch
Monorepo local script
Use your package manager wrapper when running the local lattice command.
pnpm lattice add switchPublic Exports
-
Switch -
Switch.Root -
Switch.Thumb
State Model
Switch.Roottracks a booleancheckedstate with controlled and uncontrolled entry points.disabledis enforced at the root, andSwitch.Thumbreads that shared state rather than maintaining any local state.- There is no indeterminate or mixed state in this package.
Key API
Switch.Root
Use checked, defaultChecked, onCheckedChange, and disabled to connect the control to your settings model.
Switch.Root asChild
Use asChild when you already have a visual host and only need switch semantics attached to it.
Switch.Thumb
Render the moving or highlighted interior affordance as a separate piece without re-implementing checked-state wiring.
Composition Patterns
Preference toggles
Combine a switch with nearby helper text when a setting should apply immediately and remain visually compact.
Custom host surfaces
Use asChild for pill, card, or toolbar-based visuals that still need the same binary state semantics.
Cautions / Limits
- Use
Checkboxinstead when the control represents selection or needs anindeterminatestate. - The package provides state semantics; thumb response should come from
motion, while you still own the track and skin.
Decision Guides
- Choosing between controls
Compare Switch vs Checkbox for immediate settings vs selection membership.
- Overlay and state pitfalls
Review controlled/uncontrolled transitions before synchronizing switch state with settings models.