Skip to content

Commit dd49640

Browse files
authored
Merge pull request #13441 from TylerAPfledderer/feat/shadcn-migrate-checkbox
feat: set Checkbox component for ShadCN
2 parents 0c8f831 + ee7fed0 commit dd49640

File tree

6 files changed

+124
-2
lines changed

6 files changed

+124
-2
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-popover": "^1.1.1",
4041
"@radix-ui/react-slot": "^1.1.0",

src/layouts/BaseLayout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export const BaseLayout = ({
5252
* layout on initial load.
5353
*/}
5454
<SkipLink />
55-
<div className="max-w-screen-2xl mx-auto">
55+
<div className="mx-auto max-w-screen-2xl">
5656
<Nav />
5757

5858
{/* TODO: FIX TRANSLATION BANNER LOGIC FOR https://github.com/ethereum/ethereum-org-website/issues/11305 */}

tailwind.config.ts

Lines changed: 19 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"],
@@ -134,7 +135,24 @@ const config = {
134135
},
135136
},
136137
},
137-
plugins: [require("tailwindcss-animate")],
138+
plugins: [
139+
require("tailwindcss-animate"),
140+
plugin(function ({ matchVariant }) {
141+
// The :not() pseudo-class. `i.e. not-[:checked]`
142+
matchVariant(
143+
"not",
144+
(value) => {
145+
return `&:not(${value})`
146+
},
147+
{
148+
values: {
149+
// not-disabled => ":not(:disabled)"
150+
disabled: ":disabled",
151+
},
152+
}
153+
)
154+
}),
155+
],
138156
} satisfies Config
139157

140158
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 aria-[invalid]:border-error aria-[invalid]:bg-error-light 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 className="flex items-center justify-center">
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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
{
39+
id: "invalid",
40+
value: "invalid",
41+
label: "invalid",
42+
"aria-invalid": true,
43+
},
44+
]
45+
46+
export const Checkbox: StoryObj<typeof meta> = {
47+
render: () => (
48+
<VStack className="items-start gap-4">
49+
{checkboxDataSet.map(({ label, ...props }) => (
50+
<HStack key={props.id} asChild>
51+
<label>
52+
<CheckboxComponent {...props} />
53+
{label}
54+
</label>
55+
</HStack>
56+
))}
57+
</VStack>
58+
),
59+
}

yarn.lock

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3789,6 +3789,20 @@
37893789
dependencies:
37903790
"@radix-ui/react-primitive" "2.0.0"
37913791

3792+
"@radix-ui/react-checkbox@^1.1.1":
3793+
version "1.1.1"
3794+
resolved "https://registry.yarnpkg.com/@radix-ui/react-checkbox/-/react-checkbox-1.1.1.tgz#a559c4303957d797acee99914480b755aa1f27d6"
3795+
integrity sha512-0i/EKJ222Afa1FE0C6pNJxDq1itzcl3HChE9DwskA4th4KRse8ojx8a1nVcOjwJdbpDLcz7uol77yYnQNMHdKw==
3796+
dependencies:
3797+
"@radix-ui/primitive" "1.1.0"
3798+
"@radix-ui/react-compose-refs" "1.1.0"
3799+
"@radix-ui/react-context" "1.1.0"
3800+
"@radix-ui/react-presence" "1.1.0"
3801+
"@radix-ui/react-primitive" "2.0.0"
3802+
"@radix-ui/react-use-controllable-state" "1.1.0"
3803+
"@radix-ui/react-use-previous" "1.1.0"
3804+
"@radix-ui/react-use-size" "1.1.0"
3805+
37923806
"@radix-ui/react-collapsible@1.1.0":
37933807
version "1.1.0"
37943808
resolved "https://registry.yarnpkg.com/@radix-ui/react-collapsible/-/react-collapsible-1.1.0.tgz#4d49ddcc7b7d38f6c82f1fd29674f6fab5353e77"

0 commit comments

Comments
 (0)