question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Compatibility issue with react-custom-scrollbars

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

2reactions
Rall3ncommented, 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.

1reaction
kabdelkareemcommented, Dec 20, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-custom-scrollbars-2
Start using react-custom-scrollbars-2 in your project by running `npm i react-custom-scrollbars-2`. There are 54 other projects in the npm ...
Read more >
IE compatability issues in react app. scroll bar problems
i am building a react app for my client and i am having some compatibility issues with IE. My side bar works fine...
Read more >
react-custom-scrollbars
Is there any way of disabling the custom scrollbar when the div doesn't need a scrollbar? Or at least any workaround for this...
Read more >
react-custom-scrollbars versions and peer dependencies
Online Peer dependency version tool to search for compatible versions of related NPM packages.
Read more >
react-scrollbars-custom | Yarn - Package Manager
owner xobotyi157.4kMIT4.1.1TS vulns 0 vulnerabilities. The best React custom scrollbars component. customizable, scrollbars, scroll, scrollbar ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found