From 33ab5456b56172005efb12b0bcbee4b353223e24 Mon Sep 17 00:00:00 2001 From: BrickheadJohnny <92519134+BrickheadJohnny@users.noreply.github.com> Date: Fri, 27 Sep 2024 10:22:05 +0200 Subject: [PATCH] feat: add `Switch` component & story (#1512) * feat: initialize shadcn/ui switch component * cleanup: remove `* as React` imports * feat: add proper styling & storybook --- package-lock.json | 30 ++++++++++++++++ package.json | 1 + src/v2/components/ui/Progress.tsx | 22 ++++++------ src/v2/components/ui/ScrollArea.tsx | 17 +++++---- src/v2/components/ui/Select.tsx | 47 ++++++++++++------------- src/v2/components/ui/Switch.stories.tsx | 31 ++++++++++++++++ src/v2/components/ui/Switch.tsx | 30 ++++++++++++++++ src/v2/components/ui/Textarea.tsx | 8 ++--- 8 files changed, 138 insertions(+), 48 deletions(-) create mode 100644 src/v2/components/ui/Switch.stories.tsx create mode 100644 src/v2/components/ui/Switch.tsx diff --git a/package-lock.json b/package-lock.json index cdfa808e6d..a2d02a409c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -48,6 +48,7 @@ "@radix-ui/react-select": "^2.1.1", "@radix-ui/react-separator": "^1.1.0", "@radix-ui/react-slot": "^1.1.0", + "@radix-ui/react-switch": "^1.1.0", "@radix-ui/react-toast": "^1.2.1", "@radix-ui/react-toggle": "^1.1.0", "@radix-ui/react-toggle-group": "^1.1.0", @@ -9063,6 +9064,35 @@ } } }, + "node_modules/@radix-ui/react-switch": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@radix-ui/react-switch/-/react-switch-1.1.0.tgz", + "integrity": "sha512-OBzy5WAj641k0AOSpKQtreDMe+isX0MQJ1IVyF03ucdF3DunOnROVrjWs8zsXUxC3zfZ6JL9HFVCUlMghz9dJw==", + "license": "MIT", + "dependencies": { + "@radix-ui/primitive": "1.1.0", + "@radix-ui/react-compose-refs": "1.1.0", + "@radix-ui/react-context": "1.1.0", + "@radix-ui/react-primitive": "2.0.0", + "@radix-ui/react-use-controllable-state": "1.1.0", + "@radix-ui/react-use-previous": "1.1.0", + "@radix-ui/react-use-size": "1.1.0" + }, + "peerDependencies": { + "@types/react": "*", + "@types/react-dom": "*", + "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc", + "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" + }, + "peerDependenciesMeta": { + "@types/react": { + "optional": true + }, + "@types/react-dom": { + "optional": true + } + } + }, "node_modules/@radix-ui/react-toast": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/@radix-ui/react-toast/-/react-toast-1.2.1.tgz", diff --git a/package.json b/package.json index 6c1508cc02..45b48bae40 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ "@radix-ui/react-select": "^2.1.1", "@radix-ui/react-separator": "^1.1.0", "@radix-ui/react-slot": "^1.1.0", + "@radix-ui/react-switch": "^1.1.0", "@radix-ui/react-toast": "^1.2.1", "@radix-ui/react-toggle": "^1.1.0", "@radix-ui/react-toggle-group": "^1.1.0", diff --git a/src/v2/components/ui/Progress.tsx b/src/v2/components/ui/Progress.tsx index 1897978e28..5904211351 100644 --- a/src/v2/components/ui/Progress.tsx +++ b/src/v2/components/ui/Progress.tsx @@ -1,13 +1,17 @@ "use client" -import * as ProgressPrimitive from "@radix-ui/react-progress" -import * as React from "react" - import { cn } from "@/lib/utils" +import * as ProgressPrimitive from "@radix-ui/react-progress" +import { + ComponentPropsWithoutRef, + ElementRef, + FunctionComponent, + forwardRef, +} from "react" -const ProgressRoot = React.forwardRef< - React.ElementRef, - React.ComponentPropsWithoutRef +const ProgressRoot = forwardRef< + ElementRef, + ComponentPropsWithoutRef >(({ className, value, ...props }, ref) => ( )) -const ProgressIndicator = React.forwardRef< - React.ElementRef< - React.FunctionComponent - >, +const ProgressIndicator = forwardRef< + ElementRef>, ProgressPrimitive.ProgressIndicatorProps & { value: number } >(({ className, value, style, ...props }, ref) => ( , - React.ComponentPropsWithoutRef +const ScrollArea = forwardRef< + ElementRef, + ComponentPropsWithoutRef >(({ className, children, ...props }, ref) => ( , - React.ComponentPropsWithoutRef +const ScrollBar = forwardRef< + ElementRef, + ComponentPropsWithoutRef >(({ className, orientation = "vertical", ...props }, ref) => ( , - React.ComponentPropsWithoutRef +const SelectTrigger = forwardRef< + ElementRef, + ComponentPropsWithoutRef >(({ className, children, ...props }, ref) => ( , - React.ComponentPropsWithoutRef +const SelectScrollUpButton = forwardRef< + ElementRef, + ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( , - React.ComponentPropsWithoutRef +const SelectScrollDownButton = forwardRef< + ElementRef, + ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( , - React.ComponentPropsWithoutRef +const SelectContent = forwardRef< + ElementRef, + ComponentPropsWithoutRef >(({ className, children, position = "popper", ...props }, ref) => ( , - React.ComponentPropsWithoutRef +const SelectLabel = forwardRef< + ElementRef, + ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( , - React.ComponentPropsWithoutRef +const SelectItem = forwardRef< + ElementRef, + ComponentPropsWithoutRef >(({ className, children, ...props }, ref) => ( , - React.ComponentPropsWithoutRef +const SelectSeparator = forwardRef< + ElementRef, + ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( ( +
+ + +
+) + +const meta: Meta = { + title: "Design system/Switch", + component: SwitchExample, +} + +export default meta + +type Story = StoryObj + +export const Default: Story = { + args: { + disabled: false, + }, + argTypes: { + disabled: { + type: "boolean", + control: "boolean", + }, + }, +} diff --git a/src/v2/components/ui/Switch.tsx b/src/v2/components/ui/Switch.tsx new file mode 100644 index 0000000000..2fd993f659 --- /dev/null +++ b/src/v2/components/ui/Switch.tsx @@ -0,0 +1,30 @@ +"use client" + +import { cn } from "@/lib/utils" +import * as SwitchPrimitives from "@radix-ui/react-switch" +import { ComponentPropsWithoutRef, ElementRef, forwardRef } from "react" + +export interface SwitchProps + extends ComponentPropsWithoutRef {} + +const Switch = forwardRef, SwitchProps>( + ({ className, ...props }, ref) => ( + + + + ) +) +Switch.displayName = SwitchPrimitives.Root.displayName + +export { Switch } diff --git a/src/v2/components/ui/Textarea.tsx b/src/v2/components/ui/Textarea.tsx index 8f29acfa9d..141ef02cdd 100644 --- a/src/v2/components/ui/Textarea.tsx +++ b/src/v2/components/ui/Textarea.tsx @@ -1,11 +1,9 @@ -import * as React from "react" - import { cn } from "@/lib/utils" +import { TextareaHTMLAttributes, forwardRef } from "react" -export interface TextareaProps - extends React.TextareaHTMLAttributes {} +export interface TextareaProps extends TextareaHTMLAttributes {} -const Textarea = React.forwardRef( +const Textarea = forwardRef( ({ className, ...props }, ref) => { return (