Skip to content

[select] Fire onClick during drag-to-select - #3969

Merged
atomiks merged 11 commits into
mui:masterfrom
obeattie:fix/select-drag-to-select-onclick
Feb 20, 2026
Merged

atomiks merged 11 commits into
mui:masterfrom
obeattie:fix/select-drag-to-select-onclick

Conversation

@obeattie

@obeattie obeattie commented Feb 4, 2026

Copy link
Copy Markdown
Contributor

The drag-to-select behavior which was originally introduced in #541 (mousedown on trigger → drag to item → mouseup to select) bypasses the item's onClick handler since the browser doesn't fire a click event when mousedown and mouseup occur on different elements. This is inconsistent with the other ways to select an item: both regular clicks and keyboard selection both invoke onClick already.

This PR calls onClick from onMouseUp before committing the selection, allowing preventBaseUIHandler() to cancel the selection.

Includes tests. (Sorry about #3968 - I closed it by accident.)

@pkg-pr-new

pkg-pr-new Bot commented Feb 4, 2026 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/@base-ui/react@3969
    
    pnpm add https://pkg.pr.new/@base-ui/utils@3969
    

commit: c7eb728

@netlify

netlify Bot commented Feb 4, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit c7eb728
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6997f1170b9e6b00082f682c
😎 Deploy Preview https://deploy-preview-3969--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@greptile-apps

greptile-apps Bot commented Feb 4, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Overview

Greptile Summary

This PR updates Select drag-to-select so the item’s onClick runs when the mouse is released over an option (mousedown on trigger → drag → mouseup on item), enabling cancellation via Base UI’s handler-prevention mechanism. It also refactors useDismiss intentional outside-press suppression to better handle drag sequences (including nested/portaled floating trees) and adds targeted regression tests.

Main integration points:

  • SelectItem changes how the drag-to-select mouseup path commits selection.
  • useDismiss changes how outside-press is suppressed after inside→outside drag, which affects Popover/Select compositions.
  • Tests cover both the Select behavior and the updated dismissal sequencing.

Confidence Score: 2/5

  • This PR has a correctness risk in the drag-to-select onClick/cancellation path that should be resolved before merging.
  • Most changes are well-tested, but SelectItem now uses a programmatic .click() to simulate selection. In this codebase, Base UI’s preventBaseUIHandler() relies on event augmentation; native events created by element.click() don’t carry that augmentation, so the PR’s stated guarantee (allow canceling selection via preventBaseUIHandler) may not hold in real usage. The behavior could also differ between nativeButton and non-native render modes.
  • packages/react/src/select/item/SelectItem.tsx

Important Files Changed

Filename Overview
packages/react/src/floating-ui-react/hooks/useDismiss.ts Refactors intentional outside-press suppression by tracking presses that start inside and end outside; adds pointer/mouse up/cancel listeners and new suppression logic. Changes are localized but subtle event-ordering behavior needs careful validation.
packages/react/src/floating-ui-react/hooks/useDismiss.test.tsx Adds regression tests covering intentional outside-press suppression across inside clicks, drags, and outsidePress-ignored targets.
packages/react/src/select/item/SelectItem.tsx Changes drag-to-select mouseup behavior to call element.click() instead of committing selection directly, aiming to fire onClick and allow preventBaseUIHandler. This introduces correctness risk because native click events bypass BaseUI preventable event augmentation.
packages/react/src/select/item/SelectItem.test.tsx Adds tests ensuring onClick fires once on normal click and during drag-to-select, plus that preventBaseUIHandler cancels selection in drag-to-select. Coverage is good, but implementation may not satisfy preventBaseUIHandler semantics.
packages/react/src/select/root/SelectRoot.test.tsx Adds non-JSDOM test ensuring a Select inside a Popover stays open when selecting via drag-to-select, exercising the updated dismiss suppression behavior.

@atomiks atomiks added the component: select Changes related to the select component. label Feb 5, 2026
Comment on lines +232 to +244
commitSelection(event.nativeEvent);
makeEventPreventable(event);
componentProps.onClick?.(event);

if (!event.baseUIHandlerPrevented) {
commitSelection(event.nativeEvent);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In MenuItem, we call itemRef.current.click() instead, I wonder if that's better for consistency

@obeattie

obeattie commented Feb 5, 2026

Copy link
Copy Markdown
Contributor Author

@atomiks Thanks for taking a look. Agree that doing itemRef.current.click() is the right move here. I'd actually tried that initially, but it exposed another bug that I've now gone ahead and fixed here.

Specifically, when a Select is nested inside a Popover, drag-to-select when implemented like this was unexpectedly dismissing the popover. What was happening is that the “started inside” drag state could be consumed too early. The fix is to keep that state through click capture, check whether the click target is inside first, and only use the one-shot suppression on the first real outside click.

This also shows up outside Select. For example, see this (ugly) Stackblitz that exhibits it with a Menu inside a Popover: https://stackblitz.com/edit/gzrjdbd3?file=src%2FApp.tsx

@obeattie
obeattie requested a review from atomiks February 5, 2026 15:06
@atomiks

atomiks commented Feb 5, 2026

Copy link
Copy Markdown
Contributor

but it exposed another bug that I've now gone ahead and fixed here.

Oh nice, thanks for the fix & explanation

@obeattie
obeattie force-pushed the fix/select-drag-to-select-onclick branch from 7c02ba6 to b44c932 Compare February 5, 2026 22:08
@atomiks

atomiks commented Feb 5, 2026

Copy link
Copy Markdown
Contributor

Couple lint issues:

ci/circleci: Linting — Your tests failed on CircleCI

@mui-bot

mui-bot commented Feb 5, 2026 •

Copy link
Copy Markdown

Bundle size report

Bundle Parsed size Gzip size
@base-ui/react 🔺+353B(+0.08%) 🔺+103B(+0.07%)

Details of bundle changes


Check out the code infra dashboard for more information about this PR.

expect(screen.queryByRole('tooltip')).not.toBeInTheDocument();
});

test('inside click does not suppress the next outside click when no drag occurred', async () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Codex review found a logic regression here:

a normal inside click in intentional mode can leave endedOrStartedInsideRef set,
so the next outside click is incorrectly ignored once (even without a drag).

I can't reproduce it this on the Popover demo, though. The tests are only failing in JSDOM but not Chromium.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@obeattie this seems like an edge case, but theoretically possible to trigger an issue

In-browser popover usage closes because real browser clicks fire the other mouse events (mousedown -> mouseup -> click). And in useDismiss, non-click events clear endedOrStartedInsideRef, so by the time outside click runs, it closes normally.

So it's an issue only if .click() runs without the mouse events (tests, assistive tech, programmatic clicks?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review and good catch. The only sane way I could think of to resolve this was to change the suppression logic to better tie the suppression to actual drag sequences.

Now, two flags are used for this:

  • pressStartedInsideRef: Tracks whether there's an active press that began inside. Always cleared in pointerup/mouseup
  • suppressNextOutsideClickRef: Tracks whether we want to ignore an outside click event – set on pointerup/mouseup (which fire before click and won't fire in the synthetic cases like the one we're now firing for drag-to-select).

Hopefully this works better!

@obeattie
obeattie force-pushed the fix/select-drag-to-select-onclick branch from 73353df to 7d23e1a Compare February 5, 2026 22:42
obeattie and others added 6 commits February 10, 2026 15:13
Split endedOrStartedInsideRef into pressStartedInsideRef (tracks active
press that began inside) and suppressNextOutsideClickRef (one-shot flag
for when press starts inside and ends outside). Suppression now only
activates after a real drag sequence, not any inside click, which fixes
programmatic/AT clicks that don't fire intermediate mouse events.
@atomiks
atomiks force-pushed the fix/select-drag-to-select-onclick branch from 3789c70 to 0fad799 Compare February 10, 2026 04:14
@atomiks

atomiks commented Feb 10, 2026

Copy link
Copy Markdown
Contributor

Simplified the useDismiss logic further to avoid a (minzip) bundle size increase, some existing logic wasn't necessary

@atomiks
atomiks force-pushed the fix/select-drag-to-select-onclick branch from 0fad799 to 8fb120c Compare February 10, 2026 04:26
@atomiks

atomiks commented Feb 10, 2026

Copy link
Copy Markdown
Contributor

@greptileai

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

5 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

@atomiks
atomiks force-pushed the fix/select-drag-to-select-onclick branch 9 times, most recently from 9f34f5e to 55c6612 Compare February 10, 2026 07:16
@atomiks
atomiks force-pushed the fix/select-drag-to-select-onclick branch from 55c6612 to 9333be5 Compare February 10, 2026 07:35
@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Feb 16, 2026
@obeattie

Copy link
Copy Markdown
Contributor Author

Anything more I can do to help here?

@atomiks

atomiks commented Feb 18, 2026 •

Copy link
Copy Markdown
Contributor

@obeattie nope, sorry, I edited your PR and then found various issues caused by my edits, and spent awhile refining it the other day. I think its current state is good but I left it off the last release because it changes useDismiss significantly and wanted to manually test it more thoroughly

@obeattie

Copy link
Copy Markdown
Contributor Author

No worries - thank you for your effort!

@github-actions github-actions Bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Feb 19, 2026
@atomiks atomiks added the type: bug It doesn't behave as expected. label Feb 20, 2026
@atomiks
atomiks merged commit b612a3e into mui:master Feb 20, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: select Changes related to the select component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants