Skip to content

Commit a047290

Browse files
feat: add switch component (#74)
1 parent 6f1fb53 commit a047290

File tree

7 files changed

+115
-0
lines changed

7 files changed

+115
-0
lines changed

.changeset/neat-mangos-tickle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@zenml-io/react-component-library": minor
3+
---
4+
5+
add switch component

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
"@radix-ui/react-dropdown-menu": "^2.0.6",
9292
"@radix-ui/react-select": "^2.0.0",
9393
"@radix-ui/react-slot": "^1.0.2",
94+
"@radix-ui/react-switch": "^1.0.3",
9495
"@radix-ui/react-tabs": "^1.0.4",
9596
"@radix-ui/react-toast": "^1.1.5",
9697
"@radix-ui/react-tooltip": "^1.0.7",

pnpm-lock.yaml

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { Meta } from "@storybook/react";
2+
import { Switch } from "./index";
3+
import { StoryObj } from "@storybook/react";
4+
5+
const meta = {
6+
title: "Elements/Switch",
7+
component: Switch,
8+
argTypes: {
9+
disabled: {
10+
control: "boolean",
11+
defaultValue: false,
12+
description: "if true, the switch is disabled"
13+
},
14+
defaultChecked: {
15+
control: "boolean",
16+
defaultValue: false,
17+
description: "if true, the switch is checked by default"
18+
},
19+
half: {
20+
control: "boolean",
21+
defaultValue: false
22+
}
23+
},
24+
parameters: {
25+
layout: "centered"
26+
},
27+
28+
tags: ["autodocs"]
29+
} satisfies Meta<typeof Switch>;
30+
31+
export default meta;
32+
33+
type Story = StoryObj<typeof meta>;
34+
35+
export const defaultVariant: Story = {
36+
name: "default",
37+
args: {
38+
defaultChecked: true,
39+
half: false,
40+
disabled: false
41+
}
42+
};
43+
44+
export const half: Story = {
45+
name: "Half",
46+
args: {
47+
half: true,
48+
defaultChecked: true,
49+
disabled: false
50+
}
51+
};

src/components/Switch/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+
import { cn } from "../../utilities";
4+
5+
const Switch = React.forwardRef<
6+
React.ElementRef<typeof SwitchPrimitives.Root>,
7+
React.ComponentPropsWithoutRef<typeof SwitchPrimitives.Root> & { half?: boolean }
8+
>(({ className, half, ...props }, ref) => (
9+
<SwitchPrimitives.Root
10+
data-half={half}
11+
className={cn(
12+
"peer inline-flex h-4 w-[36px] shrink-0 cursor-pointer items-center rounded-rounded border-2 border-transparent transition-colors 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 data-[state=checked]:disabled:bg-primary-400/30 data-[state=checked]:data-[half=true]:bg-primary-50 disabled:data-[state=checked]:data-[half=true]:bg-primary-50/30 data-[state=checked]:bg-primary-400 disabled:data-[state=unchecked]:bg-neutral-100/30 data-[state=unchecked]:bg-neutral-100",
13+
className
14+
)}
15+
{...props}
16+
ref={ref}
17+
>
18+
<SwitchPrimitives.Thumb
19+
className={cn(
20+
"pointer-events-none block h-3 w-3 rounded-rounded bg-white shadow-sm ring-0 transition-transform data-[state=checked]:translate-x-3 data-[state=unchecked]:translate-x-0"
21+
)}
22+
/>
23+
</SwitchPrimitives.Root>
24+
));
25+
Switch.displayName = SwitchPrimitives.Root.displayName;
26+
27+
export { Switch };

src/components/Switch/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./Switch";

src/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ export * from "./Toast";
1818
export * from "./Dialog";
1919
export * from "./Progress";
2020
export * from "./Select";
21+
export * from "./Switch";

0 commit comments

Comments
 (0)