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

Compatibility issue with react-custom-scrollbars #4227

Closed
cseas opened this issue Sep 30, 2020 · 5 comments
Closed

Compatibility issue with react-custom-scrollbars #4227

cseas opened this issue Sep 30, 2020 · 5 comments
Labels
awaiting-author-response Issues or PRs waiting for more information from the author

Comments

@cseas
Copy link

cseas commented Sep 30, 2020

Trying to use react-custom-scrollbars inside a custom MenuList component makes the keyboard navigation of react-select break.

Example:

// CustomMenuList.tsx
import React from "react";
import { components } from "react-select";
import { Scrollbars } from "react-custom-scrollbars";
import styled from "styled-components";

export const MenuList = (props: any) => {
  return (
    <components.MenuList {...props}>
      <div style={{ height: 200 }}>
        <Scrollbars>
          {props.children}
        </Scrollbars>
      </div>
    </components.MenuList>
  );
};
// App.tsx
<Select 
  components={{ MenuList }}
  captureMenuScroll={false} // custom scrollbar doesn't scroll if this is true
/>

With the above code, the dropdown list no longer scrolls to the focused option when trying to navigate using the keyboard arrow keys. However, if we remove the <Scrollbars> component, then the keyboard navigation works as expected and the menu starts scrolling as we press the down arrow key repeatedly.

@kabdelkareem
Copy link

I am facing the same issue using version in package.json
"react-select": "^3.1.1",
"react-custom-scrollbars": "^4.2.1",

@Rall3n
Copy link
Collaborator

Rall3n commented Dec 10, 2020

@cseas @kabdelkareem

The Select component requires a ref to the scrollable container of the options list to scroll to the focused option. The MenuList component receives for this the innerRef prop.

But we can´t just simply set the ref prop on the Scrollbars component, because this would be a reference to the component, not the actual DOM element.

For this we would have to do as follows:

const MenuList = ({ children, innerRef, ...props }) => {
  const intermediateRef = React.useRef();

  React.useEffect(() => {
    innerRef(intermediateRef.current ? intermediateRef.current.view : null);
  }, [innerRef, intermediateRef]);

  return (
    <components.MenuList {...props}>
      <div style={{ height: 200 }}>
        <Scrollbars ref={intermediateRef}>{children}</Scrollbars>
      </div>
    </components.MenuList>
  );
};

At first we need the reference to the Scrollbars component. It contains a reference to the needed DOM element. Then as soon as the reference exists we set the reference to the scrolling DOM element as our innerRef reference.

@ebonow ebonow added awaiting-author-response Issues or PRs waiting for more information from the author and removed issue/needs-review labels Dec 19, 2020
@ebonow
Copy link
Collaborator

ebonow commented Dec 19, 2020

Greetings @cseas @kabdelkareem,

Can you confirm that the above solution works for you and if so close the ticket?

@ebonow ebonow closed this as completed Dec 19, 2020
@ebonow ebonow reopened this Dec 19, 2020
@cseas
Copy link
Author

cseas commented Dec 20, 2020

My team figured that a custom JavaScript scrollbar isn't the best idea and we'd like to use the native one. So we just added the following styles to menuList so at least the scrollbar is styled on webkit browsers:

"::-webkit-scrollbar": {
    width: "0.6rem",
  },
  "::-webkit-scrollbar-track": {
    background: "rgba(0, 0, 0, 0)", // transparent
    borderRadius: "1rem",
  },
  "::-webkit-scrollbar-thumb": {
    background: state.theme.colors.primary25,
    borderRadius: "1rem",
    boxShadow: "inset 0 0 0.6rem rgba(0, 0, 0, 0.2)",
  },
  "::-webkit-scrollbar-thumb:hover": {
    background: Color.silver,
  }

Full example here.

Maybe @kabdelkareem can confirm whether the above solution using ref works for their use case? I'll close the Issue for now.

@cseas cseas closed this as completed Dec 20, 2020
@kabdelkareem
Copy link

@cseas I have tried it and it is working. thanks @Rall3n

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-author-response Issues or PRs waiting for more information from the author
Projects
None yet
Development

No branches or pull requests

5 participants