Skip to content

[autocomplete] Enable clearing highlight when mouse leaves popup#48354

Merged
mj12albert merged 7 commits into
mui:masterfrom
mj12albert:autocomplete-unhighlight-on-mouseleave
May 12, 2026
Merged

[autocomplete] Enable clearing highlight when mouse leaves popup#48354
mj12albert merged 7 commits into
mui:masterfrom
mj12albert:autocomplete-unhighlight-on-mouseleave

Conversation

@mj12albert

@mj12albert mj12albert commented Apr 22, 2026

Copy link
Copy Markdown
Member

Add a resetHighlightOnMouseLeave prop that clears mouse highlight when the pointer moves out of the listbox when true. (Matches Base UI combobox behavior) Prop marked @deprecated so we can make this the default behavior in the future, replaceable with disableResetHighlightOnMouseLeave if necessary.

Demo: https://stackblitz.com/edit/njytbidp?file=src%2FDemo.tsx

Manual testing steps:

  1. Click the Movie input to open the popup.
  2. Move the mouse over an option, for example The Godfather. It should become highlighted.
  3. Move the pointer outside the popup, into the page whitespace. Expected: the option highlight clears while the popup stays open.
  4. Press ArrowDown. Expected: keyboard navigation starts from the first option, The Shawshank Redemption, confirming the internal highlight was reset.

Closes #23916

Preview: https://deploy-preview-48354--material-ui.netlify.app/material-ui/react-autocomplete/

@mj12albert mj12albert added scope: autocomplete Changes related to the autocomplete. This includes ComboBox. type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature. labels Apr 22, 2026
@code-infra-dashboard

code-infra-dashboard Bot commented Apr 22, 2026

Copy link
Copy Markdown

Deploy preview

Bundle size

Bundle Parsed size Gzip size
@mui/material 🔺+301B(+0.06%) 🔺+53B(+0.04%)
@mui/lab 0B(0.00%) 0B(0.00%)
@mui/private-theming 0B(0.00%) 0B(0.00%)
@mui/system 0B(0.00%) 0B(0.00%)
@mui/utils 0B(0.00%) 0B(0.00%)

Details of bundle changes


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

@mj12albert
mj12albert marked this pull request as ready for review April 22, 2026 18:20
@mj12albert
mj12albert force-pushed the autocomplete-unhighlight-on-mouseleave branch 2 times, most recently from fb60010 to 68399e4 Compare April 27, 2026 09:41
setHighlightedIndex({
event,
index: -1,
reason: 'mouse',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
reason: 'mouse',
reason: null,

Should it be null instead to indicate reset?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

index: -1 indicates that this is a reset, reason: 'mouse' indicates that it was a mouse interaction that caused the reset

Though this is internal, reason is not nullable in the implementation, since all highlight changes must be caused by one of mouse/keyboard/touch

@siriwatknp siriwatknp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good overall, need final decision from @silviuaavram for thenull reason

Comment on lines +42 to +47
function getActiveDescendant(textbox) {
const activeDescendantId = textbox.getAttribute('aria-activedescendant');

return activeDescendantId ? document.getElementById(activeDescendantId) : null;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess this PR fixes the accessibility issue as well right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@siriwatknp Yep, it goes through the normal setHighlightedIndex which removes aria-activedescendant accordingly when the highlight is cleared (if that's what you're referring to)

The helper fn here is just to help make the actual assertions more readable

@mj12albert
mj12albert force-pushed the autocomplete-unhighlight-on-mouseleave branch from 6871ee1 to aca7926 Compare April 29, 2026 16:40
Comment thread packages/mui-material/src/Autocomplete/Autocomplete.test.js Outdated
Comment thread packages/mui-material/src/useAutocomplete/useAutocomplete.js Outdated

@ZeeshanTamboli ZeeshanTamboli left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we add this prop example in the Autocomplete docs playground: https://mui.com/material-ui/react-autocomplete/#playground or perhaps a new demo altogether?

@mj12albert
mj12albert force-pushed the autocomplete-unhighlight-on-mouseleave branch 2 times, most recently from 9e50efa to bb703f3 Compare May 5, 2026 11:39
@mj12albert

Copy link
Copy Markdown
Member Author

Should we add this prop example in the Autocomplete docs

Added it to the free solo demos where stale mouse highlights can affect the behavior of Enter which should be enough here (I'm refreshing this doc in a different PR)

Comment thread packages/mui-material/src/Autocomplete/Autocomplete.test.js Outdated
Comment thread packages/mui-material/src/Autocomplete/Autocomplete.test.js Outdated
@mj12albert
mj12albert force-pushed the autocomplete-unhighlight-on-mouseleave branch from 7b85bee to 68256d1 Compare May 8, 2026 10:19

@ZeeshanTamboli ZeeshanTamboli left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me!

@mj12albert
mj12albert enabled auto-merge (squash) May 12, 2026 18:31
@mj12albert
mj12albert merged commit d36f08f into mui:master May 12, 2026
22 checks passed
readOnly?: boolean | undefined;
/**
* If `true`, clears an option highlighted by mouse movement when the mouse leaves the listbox.
* @deprecated This behavior will be enabled by default in the next major version.

@oliviertassinari oliviertassinari May 12, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What's the logic here?

As a developer, this is how the demo looks:

Image

So I would try to remove that resetHighlightOnMouseLeave prop. So I would read the prop description to figure out how, but then the description tells me that it's already correct 🤔? So I guess we need to remove the deprecation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a good point which got unnoticed!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Opened #48536 to change it

@oliviertassinari oliviertassinari May 19, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Alright, it looks like we are good now. Thanks


While I was at it, I have created #48547 so we can keep track of all those ideas for future breaking changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: autocomplete Changes related to the autocomplete. This includes ComboBox. type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[autocomplete] Differentiate hover & keyboard states

5 participants