import React, { Component } from 'react'; import isNil from 'lodash/isNil'; import classNames from 'classnames'; import Scrollbars from 'react-custom-scrollbars'; interface Props { className?: string; autoHide?: boolean; autoHideTimeout?: number; autoHideDuration?: number; autoHeightMax?: string; hideTracksWhenNotNeeded?: boolean; renderTrackHorizontal?: React.FunctionComponent; renderTrackVertical?: React.FunctionComponent; scrollTop?: number; setScrollTop: (event: any) => void; autoHeightMin?: number | string; updateAfterMountMs?: number; } /** * Wraps component into component from `react-custom-scrollbars` */ export class CustomScrollbar extends Component { static defaultProps: Partial = { autoHide: false, autoHideTimeout: 200, autoHideDuration: 200, setScrollTop: () => {}, hideTracksWhenNotNeeded: false, autoHeightMin: '0', autoHeightMax: '100%', }; private ref: React.RefObject; constructor(props: Props) { super(props); this.ref = React.createRef(); } updateScroll() { const ref = this.ref.current; if (ref && !isNil(this.props.scrollTop)) { ref.scrollTop(this.props.scrollTop); } } componentDidMount() { this.updateScroll(); // this logic is to make scrollbar visible when content is added body after mount if (this.props.updateAfterMountMs) { setTimeout(() => this.updateAfterMount(), this.props.updateAfterMountMs); } } updateAfterMount() { if (this.ref && this.ref.current) { const scrollbar = this.ref.current as any; if (scrollbar.update) { scrollbar.update(); } } } componentDidUpdate() { this.updateScroll(); } render() { const { className, children, autoHeightMax, autoHeightMin, setScrollTop, autoHide, autoHideTimeout, hideTracksWhenNotNeeded, renderTrackHorizontal, renderTrackVertical, } = this.props; return (
)} renderTrackVertical={renderTrackVertical || (props =>
)} renderThumbHorizontal={props =>
} renderThumbVertical={props =>
} renderView={props =>
} > {children} ); } } export default CustomScrollbar;