Skip to content

feat: add shadcn checkbox component #17248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@mui/utils": "5.16.14",
"@mui/x-tree-view": "7.25.0",
"@radix-ui/react-avatar": "1.1.2",
"@radix-ui/react-checkbox": "1.1.4",
"@radix-ui/react-collapsible": "1.1.2",
"@radix-ui/react-dialog": "1.1.4",
"@radix-ui/react-dropdown-menu": "2.1.4",
Expand Down
32 changes: 32 additions & 0 deletions site/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 89 additions & 0 deletions site/src/components/Checkbox/Checkbox.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import type { Meta, StoryObj } from "@storybook/react";
import React from "react";
import { Checkbox } from "./Checkbox";

const meta: Meta<typeof Checkbox> = {
title: "components/Checkbox",
component: Checkbox,
args: {},
argTypes: {
checked: {
control: "boolean",
description: "The controlled checked state of the checkbox",
},
defaultChecked: {
control: "boolean",
description: "The default checked state when initially rendered",
},
disabled: {
control: "boolean",
description:
"When true, prevents the user from interacting with the checkbox",
},
},
};

export default meta;
type Story = StoryObj<typeof Checkbox>;

export const Unchecked: Story = {};

export const Checked: Story = {
args: {
defaultChecked: true,
checked: true,
},
};

export const Disabled: Story = {
args: {
disabled: true,
},
};

export const DisabledChecked: Story = {
args: {
disabled: true,
defaultChecked: true,
checked: true,
},
};

export const CustomStyling: Story = {
args: {
className: "h-6 w-6 rounded-full",
},
};

export const WithLabel: Story = {
render: () => (
<div className="flex gap-3">
<Checkbox id="terms" />
<div className="grid">
<label
htmlFor="terms"
className="text-sm font-medium peer-disabled:cursor-not-allowed peer-disabled:text-content-disabled"
>
Accept terms and conditions
</label>
<p className="text-sm text-content-secondary mt-1">
You agree to our Terms of Service and Privacy Policy.
</p>
</div>
</div>
),
};

export const Indeterminate: Story = {
render: () => {
const [checked, setChecked] = React.useState<boolean | "indeterminate">(
"indeterminate",
);
return (
<Checkbox
checked={checked}
onCheckedChange={(value) => setChecked(value)}
/>
);
},
};
42 changes: 42 additions & 0 deletions site/src/components/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copied from shadc/ui on 04/03/2025
* @see {@link https://ui.shadcn.com/docs/components/checkbox}
*/
import * as CheckboxPrimitive from "@radix-ui/react-checkbox";
import { Check, Minus } from "lucide-react";
import * as React from "react";

import { cn } from "utils/cn";

export const Checkbox = React.forwardRef<
React.ElementRef<typeof CheckboxPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof CheckboxPrimitive.Root>
>(({ className, ...props }, ref) => (
<CheckboxPrimitive.Root
ref={ref}
className={cn(
`peer h-6 w-6 shrink-0 rounded-sm border border-border border-solid
focus-visible:outline-none focus-visible:ring-2
focus-visible:ring-content-link focus-visible:ring-offset-4 focus-visible:ring-offset-surface-primary
disabled:cursor-not-allowed disabled:bg-surface-primary disabled:data-[state=checked]:bg-surface-tertiary
data-[state=unchecked]:bg-surface-primary
data-[state=checked]:bg-surface-invert-primary data-[state=checked]:text-content-invert
hover:border-border-hover hover:data-[state=checked]:bg-surface-invert-secondary`,
className,
)}
{...props}
>
<CheckboxPrimitive.Indicator
className={cn("flex items-center justify-center text-current relative")}
>
<div className="flex">
{(props.checked === true || props.defaultChecked === true) && (
<Check className="w-5 h-5" strokeWidth={2.5} />
)}
{props.checked === "indeterminate" && (
<Minus className="w-5 h-5" strokeWidth={2.5} />
)}
</div>
</CheckboxPrimitive.Indicator>
</CheckboxPrimitive.Root>
));
2 changes: 2 additions & 0 deletions site/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
--border-default: 240 6% 90%;
--border-success: 142 76% 36%;
--border-destructive: 0 84% 60%;
--border-hover: 240, 5%, 34%;
--overlay-default: 240 5% 84% / 80%;
--radius: 0.5rem;
--highlight-purple: 262 83% 58%;
Expand Down Expand Up @@ -67,6 +68,7 @@
--border-default: 240 4% 16%;
--border-success: 142 76% 36%;
--border-destructive: 0 91% 71%;
--border-hover: 240, 5%, 34%;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it something we should apply to the input as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe checkbox and radio are the only components I saw in Figma Coder Kit that have a hover effect on a border

--overlay-default: 240 10% 4% / 80%;
--highlight-purple: 252 95% 85%;
--highlight-green: 141 79% 85%;
Expand Down
2 changes: 2 additions & 0 deletions site/tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ module.exports = {
border: {
DEFAULT: "hsl(var(--border-default))",
destructive: "hsl(var(--border-destructive))",
success: "hsl(var(--border-success))",
hover: "hsl(var(--border-hover))",
},
overlay: "hsla(var(--overlay-default))",
input: "hsl(var(--input))",
Expand Down
Loading