fix: perfect scroll not working

This commit is contained in:
Roman Hotsiy 2018-01-23 14:58:30 +02:00
parent 2bae43fd06
commit 199f240e7c
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
6 changed files with 65 additions and 33 deletions

View File

@ -1,4 +1,4 @@
import styled from 'styled-components'; import styled from '../styled-components';
export const MiddlePanel = styled.div` export const MiddlePanel = styled.div`
width: ${props => 100 - props.theme.rightPanel.width}%; width: ${props => 100 - props.theme.rightPanel.width}%;

View File

@ -1,10 +0,0 @@
import psStyles from 'perfect-scrollbar/css/perfect-scrollbar.css';
import styled, { injectGlobal } from '../styled-components';
import PerfectScrollbarOriginal from 'react-perfect-scrollbar';
injectGlobal`${psStyles}`;
export const PerfectScrollbar = styled(PerfectScrollbarOriginal)`
position: relative;
`;

View File

@ -0,0 +1,48 @@
import * as React from 'react';
import { default as PerfectScrollbarConstructor } from 'perfect-scrollbar';
import psStyles from 'perfect-scrollbar/css/perfect-scrollbar.css';
import styled, { injectGlobal } from '../styled-components';
injectGlobal`${psStyles.toString()}`;
const StyledScrollWrapper = styled.div`
position: relative;
`;
export class PerfectScrollbar extends React.Component<{
options?: PerfectScrollbarConstructor.Options;
className?: string;
updateFn: (fn) => void;
}> {
private _container: HTMLElement;
private inst: PerfectScrollbarConstructor;
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>
);
}
}

View File

@ -9,6 +9,8 @@ import { PerfectScrollbar } from '../../common-elements/perfect-scrollbar';
@observer @observer
export class SideMenu extends ComponentWithOptions<{ menu: MenuStore }> { export class SideMenu extends ComponentWithOptions<{ menu: MenuStore }> {
private _updateScroll?: () => void;
render() { render() {
const store = this.props.menu; const store = this.props.menu;
const nativeScrollbars = this.options.nativeScrollbars; const nativeScrollbars = this.options.nativeScrollbars;
@ -22,7 +24,7 @@ export class SideMenu extends ComponentWithOptions<{ menu: MenuStore }> {
onActivate={this.activate} onActivate={this.activate}
/> />
) : ( ) : (
<PerfectScrollbar> <PerfectScrollbar updateFn={this.saveScrollUpdate}>
<MenuItems items={store.items} onActivate={this.activate} /> <MenuItems items={store.items} onActivate={this.activate} />
</PerfectScrollbar> </PerfectScrollbar>
); );
@ -30,5 +32,14 @@ export class SideMenu extends ComponentWithOptions<{ menu: MenuStore }> {
activate = (item: IMenuItem) => { activate = (item: IMenuItem) => {
this.props.menu.activateAndScroll(item, true); this.props.menu.activateAndScroll(item, true);
setTimeout(() => {
if (this._updateScroll) {
this._updateScroll();
}
});
};
private saveScrollUpdate = upd => {
this._updateScroll = upd;
}; };
} }

View File

@ -221,11 +221,7 @@ export class MenuStore {
* @see MenuStore.activate * @see MenuStore.activate
*/ */
@action @action
activateAndScroll( activateAndScroll(item: IMenuItem | undefined, updateHash: boolean, rewriteHistory?: boolean) {
item: IMenuItem | undefined,
updateHash: boolean,
rewriteHistory?: boolean,
) {
this.activate(item, updateHash, rewriteHistory); this.activate(item, updateHash, rewriteHistory);
this.scrollToActive(); this.scrollToActive();
} }

View File

@ -5218,10 +5218,6 @@ pend@~1.2.0:
version "1.2.0" version "1.2.0"
resolved "https://registry.yarnpkg.com/pend/-/pend-1.2.0.tgz#7a57eb550a6783f9115331fcf4663d5c8e007a50" resolved "https://registry.yarnpkg.com/pend/-/pend-1.2.0.tgz#7a57eb550a6783f9115331fcf4663d5c8e007a50"
perfect-scrollbar@^0.7.1:
version "0.7.1"
resolved "https://registry.yarnpkg.com/perfect-scrollbar/-/perfect-scrollbar-0.7.1.tgz#0c256fb9c5cee401d60a299687a3f9a61487e0d5"
perfect-scrollbar@^1.3.0: perfect-scrollbar@^1.3.0:
version "1.3.0" version "1.3.0"
resolved "https://registry.yarnpkg.com/perfect-scrollbar/-/perfect-scrollbar-1.3.0.tgz#61da56f94b58870d8e0a617bce649cee17d1e3b2" resolved "https://registry.yarnpkg.com/perfect-scrollbar/-/perfect-scrollbar-1.3.0.tgz#61da56f94b58870d8e0a617bce649cee17d1e3b2"
@ -5609,7 +5605,7 @@ promise@^7.1.1:
dependencies: dependencies:
asap "~2.0.3" asap "~2.0.3"
prop-types@*, prop-types@^15.5.0, prop-types@^15.5.4, prop-types@^15.6.0: prop-types@^15.5.0, prop-types@^15.5.4, prop-types@^15.6.0:
version "15.6.0" version "15.6.0"
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.6.0.tgz#ceaf083022fc46b4a35f69e13ef75aed0d639856" resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.6.0.tgz#ceaf083022fc46b4a35f69e13ef75aed0d639856"
dependencies: dependencies:
@ -5792,7 +5788,7 @@ react-dev-utils@^4.1.0:
strip-ansi "3.0.1" strip-ansi "3.0.1"
text-table "0.2.0" text-table "0.2.0"
react-dom@>=0.14.0, react-dom@^16.2.0: react-dom@^16.2.0:
version "16.2.0" version "16.2.0"
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.2.0.tgz#69003178601c0ca19b709b33a83369fe6124c044" resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.2.0.tgz#69003178601c0ca19b709b33a83369fe6124c044"
dependencies: dependencies:
@ -5822,15 +5818,6 @@ react-hot-loader@3.0.0-beta.6:
redbox-react "^1.2.5" redbox-react "^1.2.5"
source-map "^0.4.4" source-map "^0.4.4"
react-perfect-scrollbar@^0.2.2:
version "0.2.3"
resolved "https://registry.yarnpkg.com/react-perfect-scrollbar/-/react-perfect-scrollbar-0.2.3.tgz#abbe13a70348fdb18318152030c77c440b82ad44"
dependencies:
perfect-scrollbar "^0.7.1"
prop-types "*"
react ">=0.14.0"
react-dom ">=0.14.0"
react-proxy@^3.0.0-alpha.0: react-proxy@^3.0.0-alpha.0:
version "3.0.0-alpha.1" version "3.0.0-alpha.1"
resolved "https://registry.yarnpkg.com/react-proxy/-/react-proxy-3.0.0-alpha.1.tgz#4400426bcfa80caa6724c7755695315209fa4b07" resolved "https://registry.yarnpkg.com/react-proxy/-/react-proxy-3.0.0-alpha.1.tgz#4400426bcfa80caa6724c7755695315209fa4b07"
@ -5861,7 +5848,7 @@ react-test-renderer@^16.0.0-0:
object-assign "^4.1.1" object-assign "^4.1.1"
prop-types "^15.6.0" prop-types "^15.6.0"
react@>=0.14.0, react@^16.2.0: react@^16.2.0:
version "16.2.0" version "16.2.0"
resolved "https://registry.yarnpkg.com/react/-/react-16.2.0.tgz#a31bd2dab89bff65d42134fa187f24d054c273ba" resolved "https://registry.yarnpkg.com/react/-/react-16.2.0.tgz#a31bd2dab89bff65d42134fa187f24d054c273ba"
dependencies: dependencies: