Skip to content
Open
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
19 changes: 14 additions & 5 deletions src/components/Collapsible.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ type CollapsibleProps = {
className?: string
}

type CollapsibleTriggerProps = {
type CollapsibleTriggerProps = React.ButtonHTMLAttributes<HTMLButtonElement> & {
children: React.ReactNode
className?: string
}

type CollapsibleContentProps = {
Expand Down Expand Up @@ -76,16 +75,26 @@ export function Collapsible({
export function CollapsibleTrigger({
children,
className,
onClick,
onMouseDown,
type = 'button',
...props
}: CollapsibleTriggerProps) {
const { toggle } = useCollapsible()
const { open, toggle } = useCollapsible()

return (
<button
type="button"
onMouseDown={(e) => e.stopPropagation()}
{...props}
type={type}
aria-expanded={open}
onMouseDown={(e) => {
e.stopPropagation()
onMouseDown?.(e)
}}
onClick={(e) => {
e.stopPropagation()
toggle()
onClick?.(e)
}}
Comment on lines 94 to 98

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Honor consumer click cancellation before toggling.

On Line 96, toggle() runs before the consumer onClick (Line 97). With the new button-props API, consumers cannot prevent toggling via event.preventDefault(), which makes the trigger hard to compose in controlled flows.

Suggested fix
       onClick={(e) => {
         e.stopPropagation()
-        toggle()
-        onClick?.(e)
+        onClick?.(e)
+        if (!e.defaultPrevented) {
+          toggle()
+        }
       }}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
onClick={(e) => {
e.stopPropagation()
toggle()
onClick?.(e)
}}
onClick={(e) => {
e.stopPropagation()
onClick?.(e)
if (!e.defaultPrevented) {
toggle()
}
}}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/components/Collapsible.tsx` around lines 94 - 98, The onClick handler in
the Collapsible component calls toggle() before the consumer's onClick callback,
preventing consumers from canceling the toggle via event.preventDefault().
Reorder the logic in the onClick handler to invoke the consumer's onClick
callback first, then check if event.defaultPrevented is true before calling
toggle(). This allows consumers to prevent toggling when needed by calling
preventDefault() in their onClick handler.

className={twMerge('cursor-pointer select-none', className)}
data-collapsible-trigger
Expand Down
Loading
Loading