eui icon indicating copy to clipboard operation
eui copied to clipboard

[EuiSelectable] Scrolls page when `autoFocus` is set to `true`

Open nickofthyme opened this issue 10 months ago • 1 comments

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

  1. Go to a kibana sample data dashboard
  2. Enable edit mode
  3. Scroll down near the bottom, not all the way
  4. Click on the edit icon to edit a vis near the bottom
  5. Click on the data view picker in the flyout
  6. 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 avatar Jan 21 '25 19:01 nickofthyme

@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.

JasonStoltz avatar May 09 '25 13:05 JasonStoltz