Forms & Inputs
@hacline/ui/switchSwitch
A toggle switch built on @ui-sdk/react/switch with controlled and uncontrolled state, sm/md sizes, Space key toggling, and Field form integration.
Import
import { Switch, SwitchThumb } from "@/components/ui/hacline/components/switch";Basic Usage
Pair with Field (orientation="horizontal") and FieldLabel to bind the label automatically, allowing users to toggle the switch by clicking the label.
Status: Enabled
const [enabled, setEnabled] = useState(true);
<div className="flex w-full max-w-sm flex-col gap-4">
<Field name="airplane" orientation="horizontal">
<Switch checked={enabled} onCheckedChange={setEnabled} />
<div className="flex flex-col gap-0.5">
<FieldLabel>Auto-sync settings</FieldLabel>
<FieldDescription>Status: {enabled ? "On" : "Off"}</FieldDescription>
</div>
</Field>
<Field disabled name="readonly-switch" orientation="horizontal">
<Switch defaultChecked size="sm" />
<FieldLabel>Small disabled</FieldLabel>
</Field>
</div>;Submitting Boolean Settings
Associate the switch with a form field. Your app saves the switch state based on whether the field is present in the submitted form data.
"use client";
import { useState } from "react";
import { Button } from "@/components/ui/hacline/components/button";
import { Field, FieldLabel } from "@/components/ui/hacline/components/field";
import { Switch } from "@/components/ui/hacline/components/switch";
export const Example = () => {
const [saved, setSaved] = useState("");
return (
<form
action={(data) =>
setSaved(
data.has("weekly")
? "Weekly digest enabled"
: "Weekly digest disabled"
)
}
className="grid justify-items-start gap-3"
>
<Field name="weekly" orientation="horizontal">
<Switch />
<FieldLabel>Receive weekly digest</FieldLabel>
</Field>
<Button size="sm" type="submit" variant="outline">
Save email preferences
</Button>
{saved && <output className="text-hl-fg-muted text-xs">{saved}</output>}
</form>
);
};Props
Prop
Type