-
Notifications
You must be signed in to change notification settings - Fork 914
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d38f133
feat: add checkbox component
jaaydenh c64f5c3
Merge branch 'main' into jaaydenh/checkbox-component
jaaydenh 99f2964
Merge branch 'main' into jaaydenh/checkbox-component
jaaydenh 890c683
Merge branch 'main' into jaaydenh/checkbox-component
jaaydenh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)} | ||
/> | ||
); | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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