Skip to content

Commit 78614e4

Browse files
authored
Merge pull request #13593 from TylerAPfledderer/feat/shadcn-switch
[ShadCN] feat: create switch component with ShadCN
2 parents e7ca8c6 + 3c341e0 commit 78614e4

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"@radix-ui/react-popover": "^1.1.1",
4343
"@radix-ui/react-radio-group": "^1.2.0",
4444
"@radix-ui/react-slot": "^1.1.0",
45+
"@radix-ui/react-switch": "^1.1.0",
4546
"@radix-ui/react-visually-hidden": "^1.1.0",
4647
"@sentry/nextjs": "^8.19.0",
4748
"@socialgouv/matomo-next": "^1.8.0",

tailwind/ui/Switch.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
import { commonControlClasses } from "./Checkbox"
7+
8+
const Switch = React.forwardRef<
9+
React.ElementRef<typeof SwitchPrimitives.Root>,
10+
React.ComponentPropsWithoutRef<typeof SwitchPrimitives.Root>
11+
>(({ className, ...props }, ref) => (
12+
<SwitchPrimitives.Root
13+
className={cn(
14+
commonControlClasses,
15+
"w-[26px] rounded-full bg-body-medium p-px hover:border-primary-hover hover:bg-primary-hover data-[state=unchecked]:disabled:bg-transparent data-[state=checked]:hover:not-disabled:border-primary-hover",
16+
className
17+
)}
18+
{...props}
19+
ref={ref}
20+
>
21+
<SwitchPrimitives.Thumb
22+
className={cn(
23+
"pointer-events-none block size-3 rounded-full bg-background transition-transform data-[state=checked]:translate-x-2.5 data-[state=unchecked]:translate-x-0 data-[state=unchecked]:data-[disabled]:border data-[state=unchecked]:data-[disabled]:border-disabled"
24+
)}
25+
/>
26+
</SwitchPrimitives.Root>
27+
))
28+
Switch.displayName = SwitchPrimitives.Root.displayName
29+
30+
export default Switch
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as React from "react"
2+
import { Meta, type StoryObj } from "@storybook/react"
3+
4+
import SwitchComponent from "../Switch"
5+
6+
const meta = {
7+
title: "Atoms / Form / ShadCN Switch",
8+
component: SwitchComponent,
9+
} satisfies Meta<typeof SwitchComponent>
10+
11+
export default meta
12+
13+
export const Switch: StoryObj = {
14+
render: () => (
15+
<div className="grid grid-cols-2 items-center gap-x-1 lg:grid-cols-4">
16+
<span>default:</span>
17+
<SwitchComponent id="default" />
18+
19+
<span>checked:</span>
20+
<SwitchComponent id="checked" checked />
21+
22+
<span>disabled:</span>
23+
<SwitchComponent id="disabled" disabled />
24+
25+
<span>disabled and checked:</span>
26+
<SwitchComponent id="disabled-checked" disabled defaultChecked />
27+
</div>
28+
),
29+
}

yarn.lock

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4480,6 +4480,19 @@
44804480
dependencies:
44814481
"@radix-ui/react-compose-refs" "1.1.0"
44824482

4483+
"@radix-ui/react-switch@^1.1.0":
4484+
version "1.1.0"
4485+
resolved "https://registry.yarnpkg.com/@radix-ui/react-switch/-/react-switch-1.1.0.tgz#fcf8e778500f1d60d4b2bec2fc3fad77a7c118e3"
4486+
integrity sha512-OBzy5WAj641k0AOSpKQtreDMe+isX0MQJ1IVyF03ucdF3DunOnROVrjWs8zsXUxC3zfZ6JL9HFVCUlMghz9dJw==
4487+
dependencies:
4488+
"@radix-ui/primitive" "1.1.0"
4489+
"@radix-ui/react-compose-refs" "1.1.0"
4490+
"@radix-ui/react-context" "1.1.0"
4491+
"@radix-ui/react-primitive" "2.0.0"
4492+
"@radix-ui/react-use-controllable-state" "1.1.0"
4493+
"@radix-ui/react-use-previous" "1.1.0"
4494+
"@radix-ui/react-use-size" "1.1.0"
4495+
44834496
"@radix-ui/react-use-callback-ref@1.0.1":
44844497
version "1.0.1"
44854498
resolved "https://registry.yarnpkg.com/@radix-ui/react-use-callback-ref/-/react-use-callback-ref-1.0.1.tgz#f4bb1f27f2023c984e6534317ebc411fc181107a"
@@ -13055,8 +13068,7 @@ prelude-ls@^1.2.1:
1305513068
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396"
1305613069
integrity sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==
1305713070

13058-
"prettier-fallback@npm:prettier@^3", prettier@^3.1.1:
13059-
name prettier-fallback
13071+
"prettier-fallback@npm:prettier@^3":
1306013072
version "3.3.2"
1306113073
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.3.2.tgz#03ff86dc7c835f2d2559ee76876a3914cec4a90a"
1306213074
integrity sha512-rAVeHYMcv8ATV5d508CFdn+8/pHPpXeIid1DdrPwXnaAdH7cqjVbpJaT5eq4yRAFU/lsbwYwSF/n5iNrdJHPQA==
@@ -13071,6 +13083,11 @@ prettier@^2.0.5, prettier@^2.8.8:
1307113083
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.8.tgz#e8c5d7e98a4305ffe3de2e1fc4aca1a71c28b1da"
1307213084
integrity sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==
1307313085

13086+
prettier@^3.1.1:
13087+
version "3.3.2"
13088+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.3.2.tgz#03ff86dc7c835f2d2559ee76876a3914cec4a90a"
13089+
integrity sha512-rAVeHYMcv8ATV5d508CFdn+8/pHPpXeIid1DdrPwXnaAdH7cqjVbpJaT5eq4yRAFU/lsbwYwSF/n5iNrdJHPQA==
13090+
1307413091
prettier@^3.3.3:
1307513092
version "3.3.3"
1307613093
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.3.3.tgz#30c54fe0be0d8d12e6ae61dbb10109ea00d53105"

0 commit comments

Comments
 (0)