Skip to content

Commit

Permalink
feat: Add SettingsSwitch component
Browse files Browse the repository at this point in the history
  • Loading branch information
Reynard-G committed Oct 2, 2024
1 parent f307889 commit 3180651
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
30 changes: 30 additions & 0 deletions app/components/Switch/SettingsSwitch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Switch } from "~/components/ui/switch";

interface SettingsSwitchProps {
name: string;
label: string;
defaultChecked?: boolean;
description?: string;
disabled?: boolean;
}

export default function SettingsSwitch({
name,
label,
defaultChecked = false,
description,
disabled = false,
}: SettingsSwitchProps) {
return (
<div className="flex flex-row items-center justify-between space-y-2 rounded-lg border p-4">
<div className="space-y-0.5">
<label className="text-base font-semibold peer-disabled:cursor-not-allowed peer-disabled:opacity-70">
{label}
</label>
<p className="text-sm text-muted-foreground">{description}</p>
</div>

<Switch name={name} defaultChecked={defaultChecked} disabled={disabled} />
</div>
);
}
27 changes: 27 additions & 0 deletions app/components/ui/switch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as React from "react";
import * as SwitchPrimitives from "@radix-ui/react-switch";

import { cn } from "~/lib/utils/cn";

const Switch = React.forwardRef<
React.ElementRef<typeof SwitchPrimitives.Root>,
React.ComponentPropsWithoutRef<typeof SwitchPrimitives.Root>
>(({ className, ...props }, ref) => (
<SwitchPrimitives.Root
className={cn(
"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",
className,
)}
{...props}
ref={ref}
>
<SwitchPrimitives.Thumb
className={cn(
"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",
)}
/>
</SwitchPrimitives.Root>
));
Switch.displayName = SwitchPrimitives.Root.displayName;

export { Switch };
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@radix-ui/react-select": "^2.1.2",
"@radix-ui/react-separator": "^1.1.0",
"@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-switch": "^1.1.1",
"@radix-ui/react-tabs": "^1.1.1",
"@radix-ui/react-visually-hidden": "^1.1.0",
"@remix-run/node": "^2.12.1",
Expand Down
31 changes: 31 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3180651

Please sign in to comment.