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

Remember scrollbar position after view rendered #4318

Closed
lengockyquang opened this issue Dec 7, 2020 · 1 comment
Closed

Remember scrollbar position after view rendered #4318

lengockyquang opened this issue Dec 7, 2020 · 1 comment

Comments

@lengockyquang
Copy link

lengockyquang commented Dec 7, 2020

Hi, I am using react-select version 2.4.4.
Here's the position of scrollbar when select value "2005"
screenshot_1607331474

But after reselect the input, position of scrollbar changed
screenshot_1607331513

Are there any option to remember position of scrollbar? Thanks

@ebonow
Copy link
Collaborator

ebonow commented Dec 7, 2020

Greetings @lengockyquang ,

The default support is to automatically focus the selected option in the Menu as the bottom most option in the menu. There are other issues related to this asking for more support about where the selected option displays in the menu...
#3822
#4305

In regards to why it simply doesn't remember scroll position, I can hypothesize that one reason is likely because the options list can change making the scroll position irrelevant to the selected option.

If you wanted to remember the scroll position, you could add a scroll listener to the MenuList and onMenuOpen, scroll to that position.

const MenuList = (props) => {
  const { children, className, cx, getStyles, isMulti, innerRef, selectProps } = props;
  return (
    <div
      css={getStyles('menuList', props)}
      className={cx(
        {
          'menu-list': true,
          'menu-list--is-multi': isMulti,
        },
        className
      )}
      ref={innerRef}
      onScroll={selectProps.onMenuScroll}
    >
      {children}
    </div>
  );
};

const MySelect = props => {
  const selectRef = useRef();
  const [scrollPosition, setScrollPosition ] = useState();
  const onMenuScroll = e => // setScrollPosition here. Probably want to use debounce...

  onMenuOpen = () => {
    // scroll `selectRef.current.select.menuListRef` to scrollPosition
    // this will need to happen on setTimeout as its a known issues as of now 
    // that menuListRef is null at time of this evocation 
    // https://github.com/JedWatson/react-select/issues/4243
  }

  return <Select ref={selectRef} components={{MenuList}} onMenuOpen={onMenuOpen} onMenuScroll={onMenuScroll} />

This should be enough to get you started. I can reopen this if necessary, and feel free to reply if you have any questions.

@ebonow ebonow closed this as completed Dec 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants