# Switch

A pill pulled in one motion, shaded in with the pen as the thumb slides across.

Docs: https://ballpoint.st9wd.com/docs/switch

```bash
npx shadcn@latest add @ballpoint/switch
```

```tsx
import { Label } from "@/components/ui/label"
import { Switch } from "@/components/ui/switch"

<Label>
  <Switch defaultChecked />
  Draw things in
</Label>
```

Props:

- `size`: `"sm" | "default" | "lg"` (default `"default"`). Track size: 32, 44 or 56px wide.
- `seed`: `string | number`. Pins the drawing.

Pen settings: `draw`, `roughness`, `passes`, `fill`, `weight`, `speed` (see Pen settings).

Every other prop goes to Base UI's Switch: https://base-ui.com/react/components/switch

## Example

```tsx
import { Field, FieldContent, FieldDescription, FieldLabel } from "@/components/ui/field";
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";

export default function SwitchDemo() {
  return (
    <div className="flex w-full max-w-md flex-col gap-6">
      <Field orientation="horizontal">
        <FieldContent>
          <FieldLabel htmlFor="switch-draw">Draw things in</FieldLabel>
          <FieldDescription>Strokes draw themselves the first time they scroll into view.</FieldDescription>
        </FieldContent>
        <Switch id="switch-draw" defaultChecked seed="switch-draw" />
      </Field>
      <div className="flex flex-wrap items-center gap-6">
        <Label>
          <Switch seed="switch-off" />
          Off
        </Label>
        <Label>
          <Switch size="sm" defaultChecked seed="switch-sm" />
          Small
        </Label>
        <Label>
          <Switch fill="hatch" defaultChecked seed="switch-hatch" />
          Hatched
        </Label>
        <Label>
          <Switch fill="flat" defaultChecked seed="switch-flat" />
          Flat
        </Label>
        <Label>
          <Switch disabled seed="switch-disabled" />
          Disabled
        </Label>
      </div>
    </div>
  );
}
```

## In a card

A controlled switch in a settings card, and a disabled one beside it.

```tsx
"use client";

import { useState } from "react";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Field, FieldContent, FieldDescription, FieldGroup, FieldLabel } from "@/components/ui/field";
import { Switch } from "@/components/ui/switch";

export default function SwitchSettings() {
  const [reminders, setReminders] = useState(true);
  return (
    <Card className="w-full max-w-md" seed="ss-card">
      <CardHeader>
        <CardTitle>Notifications</CardTitle>
        <CardDescription>Reminders are {reminders ? "on" : "off"}.</CardDescription>
      </CardHeader>
      <CardContent>
        <FieldGroup className="gap-5">
          <Field orientation="horizontal">
            <FieldContent>
              <FieldLabel htmlFor="ss-reminders">Reminders</FieldLabel>
              <FieldDescription>An email the day before.</FieldDescription>
            </FieldContent>
            <Switch id="ss-reminders" checked={reminders} onCheckedChange={setReminders} seed="ss-reminders" />
          </Field>
          <Field orientation="horizontal" data-disabled>
            <FieldContent>
              <FieldLabel htmlFor="ss-digest">Weekly digest</FieldLabel>
              <FieldDescription>Coming soon.</FieldDescription>
            </FieldContent>
            <Switch id="ss-digest" disabled seed="ss-digest" />
          </Field>
        </FieldGroup>
      </CardContent>
    </Card>
  );
}
```

## Sizes and states

Three sizes, disabled on and off, and invalid.

```tsx
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";

const sizes = ["sm", "default", "lg"] as const;

export default function SwitchSizes() {
  return (
    <div className="flex flex-col gap-6">
      <div className="flex flex-wrap items-center gap-8">
        {sizes.map((size) => (
          <Label key={size}>
            <Switch size={size} defaultChecked seed={`ssz-${size}`} />
            {size}
          </Label>
        ))}
      </div>
      <div className="flex flex-wrap items-center gap-8">
        <Label>
          <Switch size="lg" disabled seed="ssz-off" />
          Disabled
        </Label>
        <Label>
          <Switch size="lg" disabled defaultChecked seed="ssz-on" />
          Disabled, on
        </Label>
        <Label>
          <Switch size="lg" aria-invalid seed="ssz-invalid" />
          Invalid
        </Label>
      </div>
    </div>
  );
}
```

## In a form

Switches with descriptions, one required, and an error until it is on.

```tsx
"use client";

import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Field, FieldContent, FieldDescription, FieldError, FieldGroup, FieldLabel } from "@/components/ui/field";
import { Switch } from "@/components/ui/switch";

export default function SwitchForm() {
  const [terms, setTerms] = useState(false);
  const [news, setNews] = useState(true);
  const [sent, setSent] = useState(false);
  return (
    <form
      className="w-full max-w-md"
      onSubmit={(event) => {
        event.preventDefault();
        setSent(terms);
      }}
    >
      <FieldGroup>
        <Field orientation="horizontal" data-invalid={!terms && undefined}>
          <FieldContent>
            <FieldLabel htmlFor="sf-terms">Accept the terms</FieldLabel>
            <FieldDescription>Required before we can post you a notebook.</FieldDescription>
            {!terms && <FieldError>You need to accept to continue.</FieldError>}
          </FieldContent>
          <Switch id="sf-terms" checked={terms} onCheckedChange={setTerms} aria-invalid={!terms} seed="sf-terms" />
        </Field>
        <Field orientation="horizontal">
          <FieldContent>
            <FieldLabel htmlFor="sf-news">Send me the newsletter</FieldLabel>
            <FieldDescription>One email a month, hand-written in spirit.</FieldDescription>
          </FieldContent>
          <Switch id="sf-news" checked={news} onCheckedChange={setNews} seed="sf-news" />
        </Field>
        <div className="flex items-center gap-4">
          <Button type="submit" seed="sf-submit">
            Continue
          </Button>
          {sent && <span className="text-ink-2">Thanks, it&apos;s on its way.</span>}
        </div>
      </FieldGroup>
    </form>
  );
}
```

## Pen settings

- `draw`: `"auto" | "mount" | "none"` (default `"auto"`). When the strokes draw themselves in: the first time they scroll into view, as soon as they render, or never (already drawn).
- `roughness`: `number` (default `1`). 0 is ruler-neat, 1 a quick confident hand, 2 a scrawl.
- `passes`: `1 | 2 | 3`. How many times an outline is gone over. Each component picks its own default.
- `fill`: `"shade" | "hatch" | "scribble" | "flat"`. How an area is coloured in. Solid parts default to shade, light ones to hatch.
- `weight`: `number` (default `1`). Line weight multiplier. Also settable in CSS as --ink-weight.
- `speed`: `number` (default `1`). Drawing speed multiplier. Also settable in CSS as --ink-speed.
