Skip to content

Commit 3180651

Browse files
committed
feat: Add SettingsSwitch component
1 parent f307889 commit 3180651

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Switch } from "~/components/ui/switch";
2+
3+
interface SettingsSwitchProps {
4+
name: string;
5+
label: string;
6+
defaultChecked?: boolean;
7+
description?: string;
8+
disabled?: boolean;
9+
}
10+
11+
export default function SettingsSwitch({
12+
name,
13+
label,
14+
defaultChecked = false,
15+
description,
16+
disabled = false,
17+
}: SettingsSwitchProps) {
18+
return (
19+
<div className="flex flex-row items-center justify-between space-y-2 rounded-lg border p-4">
20+
<div className="space-y-0.5">
21+
<label className="text-base font-semibold peer-disabled:cursor-not-allowed peer-disabled:opacity-70">
22+
{label}
23+
</label>
24+
<p className="text-sm text-muted-foreground">{description}</p>
25+
</div>
26+
27+
<Switch name={name} defaultChecked={defaultChecked} disabled={disabled} />
28+
</div>
29+
);
30+
}

app/components/ui/switch.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as React from "react";
2+
import * as SwitchPrimitives from "@radix-ui/react-switch";
3+
4+
import { cn } from "~/lib/utils/cn";
5+
6+
const Switch = React.forwardRef<
7+
React.ElementRef<typeof SwitchPrimitives.Root>,
8+
React.ComponentPropsWithoutRef<typeof SwitchPrimitives.Root>
9+
>(({ className, ...props }, ref) => (
10+
<SwitchPrimitives.Root
11+
className={cn(
12+
"peer inline-flex h-5 w-9 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent shadow-sm transition-colors data-[state=checked]:bg-primary data-[state=unchecked]:bg-input focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background disabled:cursor-not-allowed disabled:opacity-50",
13+
className,
14+
)}
15+
{...props}
16+
ref={ref}
17+
>
18+
<SwitchPrimitives.Thumb
19+
className={cn(
20+
"pointer-events-none block h-4 w-4 rounded-full bg-background shadow-lg ring-0 transition-transform data-[state=checked]:translate-x-4 data-[state=unchecked]:translate-x-0",
21+
)}
22+
/>
23+
</SwitchPrimitives.Root>
24+
));
25+
Switch.displayName = SwitchPrimitives.Root.displayName;
26+
27+
export { Switch };

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@radix-ui/react-select": "^2.1.2",
2828
"@radix-ui/react-separator": "^1.1.0",
2929
"@radix-ui/react-slot": "^1.1.0",
30+
"@radix-ui/react-switch": "^1.1.1",
3031
"@radix-ui/react-tabs": "^1.1.1",
3132
"@radix-ui/react-visually-hidden": "^1.1.0",
3233
"@remix-run/node": "^2.12.1",

pnpm-lock.yaml

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)