Components
- AccordionNew
- Alert
- Alert DialogNew
- AutocompleteNew
- AvatarNew
- Badge
- Breadcrumb
- Button
- CalendarNew
- CardNew
- Checkbox
- Checkbox GroupNew
- CollapsibleNew
- ComboboxNew
- CommandNew
- Context MenuNew
- Date PickerNew
- DialogNew
- DrawerNew
- EmptyNew
- FieldNew
- FieldsetNew
- FormNew
- FrameNew
- GroupNew
- Input
- Input GroupNew
- KbdNew
- Label
- MenuNew
- MeterNew
- Number Field
- OTP FieldNew
- Pagination
- PopoverNew
- Preview CardNew
- Progress
- Radio Group
- Scroll AreaNew
- Select
- Separator
- SheetNew
- SkeletonNew
- Slider
- Spinner
- StepperNew
- Switch
- Table
- TabsNew
- Textarea
- ToastNew
- ToggleNew
- Toggle GroupNew
- ToolbarNew
- Tooltip
Switch
A control that indicates whether a setting is on or off.
import { Label } from "mako-ui/components/label";
import { Switch } from "mako-ui/components/switch";
export default function Particle() {
return (
<Label>
<Switch />
Marketing emails
</Label>
);
}
Usage
import { Switch } from "mako-ui/components/switch"<Switch />API Reference
Switch
A switch with a built-in thumb. Size is controlled via the --thumb-size CSS variable.
Examples
For accessible labelling and validation, prefer using the Field component to wrap checkboxes. See the related example: Switch field.
Disabled
import { Label } from "mako-ui/components/label";
import { Switch } from "mako-ui/components/switch";
export default function Particle() {
return (
<Label>
<Switch disabled />
Marketing emails
</Label>
);
}
With Description
By enabling marketing emails, you agree to receive emails.
import { Label } from "mako-ui/components/label";
import { Switch } from "mako-ui/components/switch";
import { useId } from "react";
export default function Particle() {
const id = useId();
return (
<div className="flex items-start gap-2">
<Switch defaultChecked id={id} />
<div className="flex flex-col gap-1">
<Label htmlFor={id}>Marketing emails</Label>
<p className="text-muted-foreground text-xs">
By enabling marketing emails, you agree to receive emails.
</p>
</div>
</div>
);
}
Customizing Size
The switch size is controlled by the --thumb-size CSS variable. By default, the switch uses responsive sizing with [--thumb-size:--spacing(5)] sm:[--thumb-size:--spacing(4)] classes, making it slightly larger on mobile devices (like other interactive elements).
You can customize the size by overriding these CSS variable classes on the component. The --thumb-size value can be expressed using:
- The spacing scale:
--spacing(3),--spacing(4),--spacing(5), etc. - Fixed units:
1rem,16px, etc.
import { Switch } from "mako-ui/components/switch";
export default function Particle() {
return (
<Switch
aria-label="Enable notifications"
className="[--thumb-size:--spacing(4)] sm:[--thumb-size:--spacing(3)]"
/>
);
}
Card Style
import { Label } from "mako-ui/components/label";
import { Switch } from "mako-ui/components/switch";
import { useId } from "react";
export default function Particle() {
const id = useId();
return (
<Label
className="flex items-center gap-6 rounded-lg border p-3 hover:bg-accent/50 has-data-checked:border-primary/48 has-data-checked:bg-accent/50"
htmlFor={id}
>
<div className="flex flex-col gap-1">
<p>Enable notifications</p>
<p className="text-muted-foreground text-xs">
You can enable or disable notifications at any time.
</p>
</div>
<Switch
className="[--thumb-size:--spacing(4)] sm:[--thumb-size:--spacing(3)]"
defaultChecked
id={id}
/>
</Label>
);
}
Form Integration
"use client";
import { Button } from "mako-ui/components/button";
import { Field, FieldLabel } from "mako-ui/components/field";
import { Form } from "mako-ui/components/form";
import { Switch } from "mako-ui/components/switch";
import type { FormEvent } from "react";
import { useState } from "react";
export default function Particle() {
const [loading, setLoading] = useState(false);
const onSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
const formData = new FormData(e.currentTarget);
setLoading(true);
await new Promise((r) => setTimeout(r, 800));
setLoading(false);
const enabled = formData.has("marketing");
alert(`Marketing emails: ${enabled ? "enabled" : "disabled"}`);
};
return (
<Form className="flex flex-col gap-4" onSubmit={onSubmit}>
<Field name="marketing">
<FieldLabel>
<Switch defaultChecked name="marketing" />
Enable marketing emails
</FieldLabel>
</Field>
<Button loading={loading} type="submit">
Submit
</Button>
</Form>
);
}