eui
eui copied to clipboard
[EuiSelectable] Scrolls page when `autoFocus` is set to `true`
Describe the bug
When using EuiSelectable within an EuiPopover in an EuiFlyout in push mode when the body is set to scroll, enabling autoFocus on the searchProps results in a jump in the parent scroll.
Impact and severity I would say this is a fairly common use case in kibana given the prevalent use of flyouts.
Related to https://github.com/elastic/kibana/issues/201544
Environment and versions
- EUI version:
latest
To Reproduce
- Go to a kibana sample data dashboard
- Enable edit mode
- Scroll down near the bottom, not all the way
- Click on the edit icon to edit a vis near the bottom
- Click on the data view picker in the flyout
- Notice the body element scrolls to the top when the input is focused
Screenshots
https://github.com/user-attachments/assets/9f44ceb6-f371-4113-a5b5-a9c5836c0580
Additional context
I was able to repro the issue in codesandbox here. Note you must have the preview environment width enough the enable the push mode of the flyout.
A workaround to this is to manually focus the inputRef like eui is doing internally. But set the preventScroll option to true.
const Example = () => {
function setInitialFocus(ref: HTMLInputElement | null) {
ref?.focus({ preventScroll: true });
}
return (
<EuiSelectable
// other props...
searchProps={{
inputRef: setInitialFocus,
autoFocus: false,
}}
/>
);
};
The strange thing is that the element is inside a nested scroll container (to flyout options) but is setting the scroll on the parent (body) element. I don't think it should care at all about the body scroll and maybe there is a way to prevent that while scrolling the closest scroll container. Maybe @elastic/eui-team knows more about why this is the case and has a better solution.
If no better ideas are voiced I think a we could simply allow autoFocus as a boolean or an object of focus options where specifying options is the same as true but with modifications. So something like this...
const Example = () => {
return (
<EuiSelectable
// other props...
searchProps={{
autoFocus: { preventScroll: true },
}}
/>
);
};
@nickofthyme Sorry Nick, I didn't see this one earlier. For some reason it didn't get added to our team board automatically so it went off our radar.