react-select icon indicating copy to clipboard operation
react-select copied to clipboard

react-select does not scroll into view selected option when `menuPortalTarget={document.body}`

Open mrudowski opened this issue 2 years ago • 2 comments

react-select does not scroll into view selected option when we use menuPortalTarget={document.body}

In docs example: https://react-select.com/advanced#portaling

Or please check this codesandbox: https://codesandbox.io/s/codesandboxer-example-forked-c5xp8p?file=/example.tsx

mrudowski avatar Oct 13 '23 12:10 mrudowski

When will this change be live?

tdambra avatar May 09 '24 16:05 tdambra

still no solution?

jgwiazdowski avatar Jun 18 '24 09:06 jgwiazdowski

A workaround: find the focused option HTML element in the document tree and scrollIntoView on it after the render.

Little bit more precisely: use setTimeout to getElementById or first from getElementsByClassName (whichever suits you better) and apply scrollIntoView on it, with e.g. { behavior: "smooth", block: "center", inline: "start" }.

konradekk avatar Apr 01 '25 10:04 konradekk

I tried @konradekk's workaround and even when it works it also adds some unexpected behavior when the menu is hovered (the scroll jumps one option). Since I was using custom options I decided to fix it inspired by #2112 Using the innerRef prop of the Option component and a scrollIntoView inside a useEffect hook when option is selected.

Cosmitar avatar Oct 22 '25 08:10 Cosmitar