Skip to content
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

[Autocomplete] Fix keyboard navigation when using custom popover #35160

Merged
merged 8 commits into from Nov 21, 2022

Conversation

sai6855
Copy link
Contributor

@sai6855 sai6855 commented Nov 15, 2022

fixes: #34998

working proof: https://www.loom.com/share/8641a1e8a33844b0a48bff6e0d392289

Issue: element.offsetTop from below code returns incorrect value if parent element doesn't have relative position . attached some references below on this

if (listboxNode.scrollHeight > listboxNode.clientHeight && reason !== 'mouse') {
const element = option;
const scrollBottom = listboxNode.clientHeight + listboxNode.scrollTop;
const elementBottom = element.offsetTop + element.offsetHeight;
if (elementBottom > scrollBottom) {
listboxNode.scrollTop = elementBottom - listboxNode.clientHeight;
} else if (
element.offsetTop - element.offsetHeight * (groupBy ? 1.3 : 0) <
listboxNode.scrollTop
) {
listboxNode.scrollTop = element.offsetTop - element.offsetHeight * (groupBy ? 1.3 : 0);
}
}

https://stackoverflow.com/questions/29277608/jquery-offset-top-returns-wrong-value-error-with-margin#:~:text=if%20you%20try%20to%20get,will%20get%20a%20wrong%20value.&text=Try%20removing%20the%20margin%2Dtop,will%20see%20it%20will%20work.

https://medium.com/@alexcambose/js-offsettop-property-is-not-great-and-here-is-why-b79842ef7582.

So i've added position relative to parent element and added test.

@mui-bot
Copy link

mui-bot commented Nov 15, 2022

Messages
📖 Netlify deploy preview: https://deploy-preview-35160--material-ui.netlify.app/

Details of bundle changes

Generated by 🚫 dangerJS against d851146

@sai6855 sai6855 changed the title added position relative to AutocompleteListbox Autocomplete navigation fix when using custom popover Nov 15, 2022
@sai6855 sai6855 changed the title Autocomplete navigation fix when using custom popover Autocomplete keyboard navigation fix when using custom popover Nov 15, 2022
@michaldudak michaldudak changed the title Autocomplete keyboard navigation fix when using custom popover [Autocomplete] Keyboard navigation fix when using custom popover Nov 15, 2022
@michaldudak michaldudak added the component: autocomplete This is the name of the generic UI component, not the React module! label Nov 15, 2022
@michaldudak
Copy link
Member

Thanks, at first glance it seems to be working well.

I've got one remark on the test, however. Instead of verifying if a certain CSS property is set, it would be better to test if the selected item is visible (the outcome that's visible to the end-user). You could verify if the scrollTop of the listbox is equal to 0 after performing the steps described in the issue.

@sai6855
Copy link
Contributor Author

sai6855 commented Nov 15, 2022

thanks for quick review @michaldudak, updated test case as described

@sai6855
Copy link
Contributor Author

sai6855 commented Nov 18, 2022

hi @michaldudak i've updated test as described, also created another PR (#35196) which fixes similar navigation bug. can you review that when you get time

Copy link
Member

@michaldudak michaldudak left a comment

Choose a reason for hiding this comment

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

All good now. Thanks for your work!

@michaldudak michaldudak changed the title [Autocomplete] Keyboard navigation fix when using custom popover [Autocomplete] Fix keyboard navigation when using custom popover Nov 21, 2022
@michaldudak michaldudak merged commit ab9ef05 into mui:master Nov 21, 2022
@oliviertassinari oliviertassinari added the bug 🐛 Something doesn't work label Nov 21, 2022
@oliviertassinari
Copy link
Member

oliviertassinari commented Nov 21, 2022

Oh, nice detail. I have learned something today: the parent offset !== scroll container. I see Joy UI already has this style applied so is not impacted. We could have solved this without CSS: by using .offsetParent and comparing it with the scroll container but it might have been overkill 😁. To consider if this bug gets raised again when using MUI Base directly.

alexfauquette pushed a commit to alexfauquette/material-ui that referenced this pull request Nov 22, 2022
daniel-rabe pushed a commit to daniel-rabe/material-ui that referenced this pull request Nov 29, 2022
feliperli pushed a commit to jesrodri/material-ui that referenced this pull request Dec 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: autocomplete This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Autocomplete] Keyboard arrow key option selection broken with custom PopperComponent and renderInput
4 participants