Skip to content

Commit 4003a1e

Browse files
feat: set Checkbox component for ShadCN
1 parent 885edaa commit 4003a1e

File tree

5 files changed

+136
-3
lines changed

5 files changed

+136
-3
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"@emotion/styled": "^11.11.0",
3636
"@hookform/resolvers": "^3.8.0",
3737
"@radix-ui/react-accordion": "^1.2.0",
38+
"@radix-ui/react-checkbox": "^1.1.1",
3839
"@radix-ui/react-navigation-menu": "^1.2.0",
3940
"@radix-ui/react-slot": "^1.1.0",
4041
"@socialgouv/matomo-next": "^1.8.0",

tailwind.config.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Config } from "tailwindcss"
2+
import plugin from "tailwindcss/plugin"
23

34
const config = {
45
darkMode: ["class"],
@@ -106,7 +107,23 @@ const config = {
106107
},
107108
},
108109
},
109-
plugins: [require("tailwindcss-animate")],
110+
plugins: [
111+
require("tailwindcss-animate"),
112+
plugin(function ({ matchVariant }) {
113+
// The :not() pseudo-class. `i.e. not-[&:disabled]`
114+
matchVariant(
115+
"not",
116+
(value) => {
117+
return `&:not(${value})`
118+
},
119+
{
120+
values: {
121+
disabled: ":disabled",
122+
},
123+
}
124+
)
125+
}),
126+
],
110127
} satisfies Config
111128

112129
export default config

tailwind/ui/Checkbox.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 { RxCheck } from "react-icons/rx"
3+
import * as CheckboxPrimitive from "@radix-ui/react-checkbox"
4+
5+
import { cn } from "@/lib/utils/cn"
6+
7+
export type CheckboxProps = React.ComponentPropsWithoutRef<
8+
typeof CheckboxPrimitive.Root
9+
>
10+
11+
const Checkbox = React.forwardRef<
12+
React.ElementRef<typeof CheckboxPrimitive.Root>,
13+
CheckboxProps
14+
>(({ className, ...props }, ref) => (
15+
<CheckboxPrimitive.Root
16+
ref={ref}
17+
className={cn(
18+
"size-4 rounded-sm border border-body-medium text-background hover:border-primary-high-contrast hover:bg-body-light focus-visible:outline-offset-4 focus-visible:outline-primary-hover disabled:cursor-not-allowed disabled:border-disabled disabled:bg-disabled data-[state='checked']:not-disabled:border-primary data-[state='checked']:not-disabled:bg-primary data-[state='checked']:hover:not-disabled:bg-primary-hover",
19+
className
20+
)}
21+
{...props}
22+
>
23+
<CheckboxPrimitive.Indicator>
24+
<RxCheck className="text-sm" />
25+
</CheckboxPrimitive.Indicator>
26+
</CheckboxPrimitive.Root>
27+
))
28+
Checkbox.displayName = CheckboxPrimitive.Root.displayName
29+
30+
export default Checkbox
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import type { Meta, StoryObj } from "@storybook/react/*"
2+
3+
import { HStack, VStack } from "../../../src/components/ui/flex"
4+
import CheckboxComponent, { type CheckboxProps } from "../Checkbox"
5+
6+
const meta = {
7+
title: "Atoms / Form / ShadCN Checkbox",
8+
component: CheckboxComponent,
9+
} satisfies Meta<typeof CheckboxComponent>
10+
11+
export default meta
12+
13+
const checkboxDataSet: (CheckboxProps & { label: string })[] = [
14+
{
15+
id: "default",
16+
value: "default",
17+
label: "default",
18+
},
19+
{
20+
id: "checked",
21+
value: "checked",
22+
label: "checked",
23+
checked: true,
24+
},
25+
{
26+
id: "disabled",
27+
value: "disabled",
28+
label: "disabled",
29+
disabled: true,
30+
},
31+
{
32+
id: "disabled-checked",
33+
value: "disabled-checked",
34+
label: "disabled-checked",
35+
disabled: true,
36+
checked: true,
37+
},
38+
// TODO: Waiting for PR #13434 to be merged
39+
// {
40+
// id: "invalid",
41+
// value: "invalid",
42+
// label: "invalid",
43+
// "aria-invalid": true,
44+
// },
45+
]
46+
47+
export const Checkbox: StoryObj<typeof meta> = {
48+
render: () => (
49+
<VStack className="items-start gap-4">
50+
{checkboxDataSet.map(({ label, ...props }) => (
51+
<HStack key={props.id} asChild>
52+
<label>
53+
<CheckboxComponent {...props} />
54+
{label}
55+
</label>
56+
</HStack>
57+
))}
58+
</VStack>
59+
),
60+
}

yarn.lock

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3762,6 +3762,20 @@
37623762
"@radix-ui/react-primitive" "2.0.0"
37633763
"@radix-ui/react-use-controllable-state" "1.1.0"
37643764

3765+
"@radix-ui/react-checkbox@^1.1.1":
3766+
version "1.1.1"
3767+
resolved "https://registry.yarnpkg.com/@radix-ui/react-checkbox/-/react-checkbox-1.1.1.tgz#a559c4303957d797acee99914480b755aa1f27d6"
3768+
integrity sha512-0i/EKJ222Afa1FE0C6pNJxDq1itzcl3HChE9DwskA4th4KRse8ojx8a1nVcOjwJdbpDLcz7uol77yYnQNMHdKw==
3769+
dependencies:
3770+
"@radix-ui/primitive" "1.1.0"
3771+
"@radix-ui/react-compose-refs" "1.1.0"
3772+
"@radix-ui/react-context" "1.1.0"
3773+
"@radix-ui/react-presence" "1.1.0"
3774+
"@radix-ui/react-primitive" "2.0.0"
3775+
"@radix-ui/react-use-controllable-state" "1.1.0"
3776+
"@radix-ui/react-use-previous" "1.1.0"
3777+
"@radix-ui/react-use-size" "1.1.0"
3778+
37653779
"@radix-ui/[email protected]":
37663780
version "1.1.0"
37673781
resolved "https://registry.yarnpkg.com/@radix-ui/react-collapsible/-/react-collapsible-1.1.0.tgz#4d49ddcc7b7d38f6c82f1fd29674f6fab5353e77"
@@ -4025,6 +4039,13 @@
40254039
resolved "https://registry.yarnpkg.com/@radix-ui/react-use-previous/-/react-use-previous-1.1.0.tgz#d4dd37b05520f1d996a384eb469320c2ada8377c"
40264040
integrity sha512-Z/e78qg2YFnnXcW88A4JmTtm4ADckLno6F7OXotmkQfeuCVaKuYzqAATPhVzl3delXE7CxIV8shofPn3jPc5Og==
40274041

4042+
"@radix-ui/[email protected]":
4043+
version "1.1.0"
4044+
resolved "https://registry.yarnpkg.com/@radix-ui/react-use-size/-/react-use-size-1.1.0.tgz#b4dba7fbd3882ee09e8d2a44a3eed3a7e555246b"
4045+
integrity sha512-XW3/vWuIXHa+2Uwcc2ABSfcCledmXhhQPlGbfcRXbiUQI5Icjcg19BGCZVKKInYbvUCut/ufbbLLPFC5cbb1hw==
4046+
dependencies:
4047+
"@radix-ui/react-use-layout-effect" "1.1.0"
4048+
40284049
"@radix-ui/[email protected]":
40294050
version "1.1.0"
40304051
resolved "https://registry.yarnpkg.com/@radix-ui/react-visually-hidden/-/react-visually-hidden-1.1.0.tgz#ad47a8572580f7034b3807c8e6740cd41038a5a2"
@@ -11948,8 +11969,7 @@ prelude-ls@^1.2.1:
1194811969
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396"
1194911970
integrity sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==
1195011971

11951-
"prettier-fallback@npm:prettier@^3", prettier@^3.1.1:
11952-
name prettier-fallback
11972+
"prettier-fallback@npm:prettier@^3":
1195311973
version "3.3.2"
1195411974
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.3.2.tgz#03ff86dc7c835f2d2559ee76876a3914cec4a90a"
1195511975
integrity sha512-rAVeHYMcv8ATV5d508CFdn+8/pHPpXeIid1DdrPwXnaAdH7cqjVbpJaT5eq4yRAFU/lsbwYwSF/n5iNrdJHPQA==
@@ -11964,6 +11984,11 @@ prettier@^2.0.5, prettier@^2.8.8:
1196411984
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.8.tgz#e8c5d7e98a4305ffe3de2e1fc4aca1a71c28b1da"
1196511985
integrity sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==
1196611986

11987+
prettier@^3.1.1:
11988+
version "3.3.2"
11989+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.3.2.tgz#03ff86dc7c835f2d2559ee76876a3914cec4a90a"
11990+
integrity sha512-rAVeHYMcv8ATV5d508CFdn+8/pHPpXeIid1DdrPwXnaAdH7cqjVbpJaT5eq4yRAFU/lsbwYwSF/n5iNrdJHPQA==
11991+
1196711992
prettier@^3.3.3:
1196811993
version "3.3.3"
1196911994
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.3.3.tgz#30c54fe0be0d8d12e6ae61dbb10109ea00d53105"

0 commit comments

Comments
 (0)