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.

Solution: Compability with react-custom-scrollbars

See original GitHub issue

After a long discussion in #143 I also took a shot at this problem and arrived to the following solution:

import React, { Component } from 'react';
import { Scrollbars } from 'react-custom-scrollbars';
import { List } from 'react-virtualized';

const SCROLL_DIRECTION_BACKWARD = -1;
const SCROLL_DIRECTION_FORWARD = 1;
const SCROLL_POSITION_CHANGE_REASON_OBSERVED = 'observed';

const listStyle = {
  overflowX: false,
  overflowY: false,
};

export default class SmartList extends Component {
  _getScrollDirection(scrollTop) {
    const {
      scrollTop: oldScrollTop,
      scrollDirectionVertical,
    } = this.List.Grid.state;

    if (scrollTop !== oldScrollTop) {
      return scrollTop > oldScrollTop ?
        SCROLL_DIRECTION_FORWARD :
        SCROLL_DIRECTION_BACKWARD;
    }

    return scrollDirectionVertical;
  }

  handleScroll = ({ target }) => {
    const {
      scrollTop,
      scrollLeft,
    } = target;

    const { Grid: grid } = this.List;

    grid._debounceScrollEnded();

    const scrollDirectionVertical = this._getScrollDirection(scrollTop);
    const totalColumnsWidth = grid._columnSizeAndPositionManager.getTotalSize();
    const totalRowsHeight = grid._rowSizeAndPositionManager.getTotalSize();

    grid.setState({
      isScrolling: true,
      scrollDirectionVertical,
      scrollTop,
      scrollPositionChangeReason: SCROLL_POSITION_CHANGE_REASON_OBSERVED,
    });

    grid._invokeOnScrollMemoizer({ scrollLeft, scrollTop, totalColumnsWidth, totalRowsHeight });
  };

  List = null;

  render() {
    const { width, height } = this.props;

    return (
      <Scrollbars
        autoHide
        style={{ width, height }}
        onScroll={this.handleScroll}
      >
        <List
          {...this.props}
          ref={instance => (this.List = instance)}
          style={listStyle}
        />
      </Scrollbars>
    );
  }
}

You can see that I’ve pretty much rewritten the _onScroll method in https://github.com/bvaughn/react-virtualized/blob/master/source/Grid/Grid.js#L1110

The simplest way to make this work will be to add a way to bypass the container node check in _onScroll.

@bvaughn what do you think?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:16 (10 by maintainers)

github_iconTop GitHub Comments

53reactions
alexpyzhianovcommented, Oct 25, 2017

For anyone who struggles with handleScrollEvent and doesn’t understand how it is related to the code snippet above, here how it all looks with the new method. @5angel tons of 👍 to you

import React, { Component } from 'react';
import { Scrollbars } from 'react-custom-scrollbars';
import { List } from 'react-virtualized';

const listStyle = {
  overflowX: false,
  overflowY: false,
};

export default class SmartList extends Component {
  handleScroll = ({ target }) => {
    const { scrollTop, scrollLeft } = target;

    const { Grid: grid } = this.List;

    grid.handleScrollEvent({ scrollTop, scrollLeft });
  }

  List = null;

  render() {
    const { width, height } = this.props;

    return (
      <Scrollbars
        autoHide
        style={{ width, height }}
        onScroll={this.handleScroll}
      >
        <List
          {...this.props}
          ref={instance => (this.List = instance)}
          style={listStyle}
        />
      </Scrollbars>
    );
  }
}
6reactions
serlecommented, Aug 6, 2018

Can this technique be used with the table and would it let the headers be fixed and only scroll the data?

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-custom-scrollbars can't get styling to work - Stack Overflow
For the one that are struggling to figure out how to make the customization work. Know as it's mentioned in this question, you...
Read more >
react-custom-scrollbars-2 - npm
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 >
Create an advanced scroll lock React Hook - LogRocket Blog
Learn how to create and troubleshoot a custom scroll lock React Hook on web and mobile with this comprehensive tutorial.
Read more >
How To Implement Smooth Scrolling in React - DigitalOcean
Smooth scrolling is when instead of clicking on a button and being instantly taken to a different part of the same page, the...
Read more >
How to Change the Position of Scrollbar using CSS
< title >Customize the scroll-bar</ title >. < style > ... GeeksforGeeks is a Computer Science ... explained computer science and.
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