2018-01-23 15:58:30 +03:00
|
|
|
import * as React from 'react';
|
|
|
|
|
2018-03-26 21:56:39 +03:00
|
|
|
import PerfectScrollbarType, * as PerfectScrollbarNamespace from 'perfect-scrollbar';
|
2018-01-23 15:58:30 +03:00
|
|
|
import psStyles from 'perfect-scrollbar/css/perfect-scrollbar.css';
|
|
|
|
import styled, { injectGlobal } from '../styled-components';
|
|
|
|
|
2018-03-26 21:56:39 +03:00
|
|
|
/*
|
|
|
|
* perfect scrollbar umd bundle uses exports assignment while module uses default export
|
|
|
|
* so when bundled with webpack default export works but with jest it crashes
|
|
|
|
* That's why the following ugly fix is required
|
|
|
|
*/
|
|
|
|
const PerfectScrollbarConstructor =
|
|
|
|
PerfectScrollbarNamespace.default || ((PerfectScrollbarNamespace as any) as PerfectScrollbarType);
|
|
|
|
|
2018-01-23 20:09:23 +03:00
|
|
|
injectGlobal`${psStyles && psStyles.toString()}`;
|
2018-01-23 15:58:30 +03:00
|
|
|
|
|
|
|
const StyledScrollWrapper = styled.div`
|
|
|
|
position: relative;
|
|
|
|
`;
|
|
|
|
|
|
|
|
export class PerfectScrollbar extends React.Component<{
|
2018-03-26 21:56:39 +03:00
|
|
|
options?: PerfectScrollbarType.Options;
|
2018-01-23 15:58:30 +03:00
|
|
|
className?: string;
|
|
|
|
updateFn: (fn) => void;
|
|
|
|
}> {
|
|
|
|
private _container: HTMLElement;
|
2018-03-26 21:56:39 +03:00
|
|
|
private inst: PerfectScrollbarType;
|
2018-01-23 15:58:30 +03:00
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.inst = new PerfectScrollbarConstructor(this._container, this.props.options || {});
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
this.inst.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.inst.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleRef = ref => {
|
|
|
|
this._container = ref;
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { children, className, updateFn } = this.props;
|
|
|
|
|
|
|
|
updateFn(this.componentDidUpdate.bind(this));
|
|
|
|
|
|
|
|
return (
|
|
|
|
<StyledScrollWrapper className={`scrollbar-container ${className}`} innerRef={this.handleRef}>
|
|
|
|
{children}
|
|
|
|
</StyledScrollWrapper>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|