-
Notifications
You must be signed in to change notification settings - Fork 2
add checkbox component #4
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
2 commits
Select commit
Hold shift + click to select a range
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
Empty file.
Empty file.
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,87 @@ | ||
| .Checkbox { | ||
| box-sizing: border-box; | ||
| display: flex; | ||
| width: 1.25rem; | ||
| height: 1.25rem; | ||
| align-items: center; | ||
| justify-content: center; | ||
| border-radius: 0.25rem; | ||
| outline: 0; | ||
| padding: 0; | ||
| margin: 0; | ||
| border: none; | ||
|
|
||
| &[data-unchecked] { | ||
| border: 1px solid var(--color-filled-bg-neutral); | ||
| background-color: transparent; | ||
|
|
||
| &[data-disabled] { | ||
| border: 1px solid var(--color-filled-fg-neutral-disabled); | ||
| } | ||
| } | ||
|
|
||
| &[data-checked] { | ||
| background-color: var(--color-gray-900); | ||
|
|
||
| &[data-disabled] { | ||
| background-color: var(--color-filled-fg-neutral-disabled); | ||
| } | ||
| } | ||
|
|
||
| &:focus-visible { | ||
| outline: 2px solid var(--color-blue); | ||
| outline-offset: 2px; | ||
| } | ||
| } | ||
|
|
||
| .Neutral { | ||
| &[data-checked] { | ||
| background-color: var(--color-filled-bg-neutral); | ||
| } | ||
| } | ||
|
|
||
| .Brand { | ||
| &[data-checked] { | ||
| background-color: var(--color-bg-brand); | ||
| } | ||
| } | ||
|
|
||
| .Indicator { | ||
| display: flex; | ||
| color: var(--color-gray-50); | ||
|
|
||
| &[data-unchecked] { | ||
| display: none; | ||
| } | ||
| } | ||
|
|
||
| .Icon { | ||
| width: 0.75rem; | ||
| height: 0.75rem; | ||
| } | ||
|
|
||
| .Circle { | ||
| display: flex; | ||
| justify-content: center; | ||
| align-items: center; | ||
| width: 2.5rem; | ||
| height: 2.5rem; | ||
| border-radius: 50%; | ||
| background-color: transparent; | ||
| } | ||
|
|
||
| .Circle:hover { | ||
| background-color: var(--color-filled-bg-neutral-disabled); | ||
|
|
||
| &[data-disabled] { | ||
| background-color: transparent; | ||
| } | ||
| } | ||
|
|
||
| .Circle:active { | ||
| background-color: var(--color-tonal-bg-neutral-hover); | ||
|
|
||
| &[data-disabled] { | ||
| background-color: transparent; | ||
| } | ||
| } |
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,57 @@ | ||
| import { Checkbox as BaseUICheckbox } from "@base-ui-components/react/checkbox"; | ||
| import { cva, type VariantProps } from "cva"; | ||
| import { cn } from "../../utils"; | ||
| import styles from "./index.module.css"; | ||
|
|
||
| const CheckboxRootStyles = cva({ | ||
| base: styles.Checkbox, | ||
| variants: { | ||
| disabled: { | ||
| true: "cursor-not-allowed", | ||
| false: "", | ||
| }, | ||
| palette: { | ||
| neutral: styles.Neutral, | ||
| brand: styles.Brand, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| function CheckIcon(props: React.ComponentProps<"svg">) { | ||
| return ( | ||
| <svg | ||
| role="img" | ||
| aria-labelledby="title-id" | ||
| fill="currentcolor" | ||
| width="10" | ||
| height="10" | ||
| viewBox="0 0 10 10" | ||
| {...props} | ||
| > | ||
| <title id="title-id">A checkmark icon</title> | ||
| <path d="M9.1603 1.12218C9.50684 1.34873 9.60427 1.81354 9.37792 2.16038L5.13603 8.66012C5.01614 8.8438 4.82192 8.96576 4.60451 8.99384C4.3871 9.02194 4.1683 8.95335 4.00574 8.80615L1.24664 6.30769C0.939709 6.02975 0.916013 5.55541 1.19372 5.24822C1.47142 4.94102 1.94536 4.91731 2.2523 5.19524L4.36085 7.10461L8.12299 1.33999C8.34934 0.993152 8.81376 0.895638 9.1603 1.12218Z" /> | ||
| </svg> | ||
| ); | ||
| } | ||
|
|
||
| type CheckboxProps = React.ComponentProps<typeof BaseUICheckbox.Root> & | ||
| VariantProps<typeof CheckboxRootStyles>; | ||
|
|
||
| export const Checkbox = ({ | ||
| palette = "neutral", | ||
| disabled, | ||
| ...props | ||
| }: CheckboxProps) => ( | ||
| <div className={styles.Circle}> | ||
| <BaseUICheckbox.Root | ||
| defaultChecked | ||
| disabled={disabled} | ||
| className={cn(CheckboxRootStyles({ palette, disabled: disabled }))} | ||
| {...props} | ||
| > | ||
| <BaseUICheckbox.Indicator className={styles.Indicator}> | ||
| <CheckIcon className={styles.Icon} /> | ||
| </BaseUICheckbox.Indicator> | ||
| </BaseUICheckbox.Root> | ||
| </div> | ||
| ); | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import type { Meta, StoryObj } from "@storybook/react-vite"; | ||
| import { Checkbox } from "../../lib/components/Checkbox"; | ||
|
|
||
| const meta = { | ||
| component: Checkbox, | ||
| argTypes: { | ||
| palette: { | ||
| table: { type: { summary: "neutral | brand" } }, | ||
| options: ["neutral", "brand"], | ||
| control: "select", | ||
| }, | ||
| disabled: { | ||
| table: { type: { summary: "boolean" } }, | ||
| control: "boolean", | ||
| }, | ||
| }, | ||
| tags: ["autodocs"], | ||
| } satisfies Meta<typeof Checkbox>; | ||
| export default meta; | ||
|
|
||
| type Story = StoryObj<typeof meta>; | ||
| export const Neutral: Story = { | ||
| args: { | ||
| palette: "neutral", | ||
| disabled: false, | ||
| }, | ||
| }; | ||
|
|
||
| export const Brand: Story = { | ||
| args: { | ||
| palette: "brand", | ||
| disabled: false, | ||
| }, | ||
| }; | ||
|
|
||
| export const DefaultChecked: Story = { | ||
| args: { | ||
| defaultChecked: true, | ||
| }, | ||
| }; |
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.
Bug: The
Checkboxcomponent hardcodes thedefaultCheckedprop, causing all instances to be checked by default instead of inheriting the value from props.Severity: HIGH
🔍 Detailed Analysis
The
Checkboxcomponent atlib/components/Checkbox/index.tsxhardcodes thedefaultCheckedprop. This causes all instances of the checkbox to be checked by default, which is contrary to standard UI behavior and the Base UI library's own default (false). The component's Storybook stories forNeutralandBranddo not passdefaultChecked, implying they should be unchecked, but they will render as checked. This forces consumers to explicitly and unintuitively passdefaultChecked={false}to achieve the standard unchecked state.💡 Suggested Fix
Remove the hardcoded
defaultCheckedprop from the<BaseUICheckbox.Root>component inlib/components/Checkbox/index.tsx. Allow the checked state to be controlled by spreading thepropsobject, which will correctly handle thedefaultCheckedvalue if provided by the consumer.🤖 Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID:
8372246