Skip to content

Commit 199f240

Browse files
committed
fix: perfect scroll not working
1 parent 2bae43f commit 199f240

File tree

6 files changed

+65
-33
lines changed

6 files changed

+65
-33
lines changed

src/common-elements/panels.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import styled from 'styled-components';
1+
import styled from '../styled-components';
22

33
export const MiddlePanel = styled.div`
44
width: ${props => 100 - props.theme.rightPanel.width}%;

src/common-elements/perfect-scrollbar.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import * as React from 'react';
2+
3+
import { default as PerfectScrollbarConstructor } from 'perfect-scrollbar';
4+
import psStyles from 'perfect-scrollbar/css/perfect-scrollbar.css';
5+
import styled, { injectGlobal } from '../styled-components';
6+
7+
injectGlobal`${psStyles.toString()}`;
8+
9+
const StyledScrollWrapper = styled.div`
10+
position: relative;
11+
`;
12+
13+
export class PerfectScrollbar extends React.Component<{
14+
options?: PerfectScrollbarConstructor.Options;
15+
className?: string;
16+
updateFn: (fn) => void;
17+
}> {
18+
private _container: HTMLElement;
19+
private inst: PerfectScrollbarConstructor;
20+
21+
componentDidMount() {
22+
this.inst = new PerfectScrollbarConstructor(this._container, this.props.options || {});
23+
}
24+
25+
componentDidUpdate() {
26+
this.inst.update();
27+
}
28+
29+
componentWillUnmount() {
30+
this.inst.destroy();
31+
}
32+
33+
handleRef = ref => {
34+
this._container = ref;
35+
};
36+
37+
render() {
38+
const { children, className, updateFn } = this.props;
39+
40+
updateFn(this.componentDidUpdate.bind(this));
41+
42+
return (
43+
<StyledScrollWrapper className={`scrollbar-container ${className}`} innerRef={this.handleRef}>
44+
{children}
45+
</StyledScrollWrapper>
46+
);
47+
}
48+
}

src/components/SideMenu/SideMenu.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { PerfectScrollbar } from '../../common-elements/perfect-scrollbar';
99

1010
@observer
1111
export class SideMenu extends ComponentWithOptions<{ menu: MenuStore }> {
12+
private _updateScroll?: () => void;
13+
1214
render() {
1315
const store = this.props.menu;
1416
const nativeScrollbars = this.options.nativeScrollbars;
@@ -22,13 +24,22 @@ export class SideMenu extends ComponentWithOptions<{ menu: MenuStore }> {
2224
onActivate={this.activate}
2325
/>
2426
) : (
25-
<PerfectScrollbar>
27+
<PerfectScrollbar updateFn={this.saveScrollUpdate}>
2628
<MenuItems items={store.items} onActivate={this.activate} />
2729
</PerfectScrollbar>
2830
);
2931
}
3032

3133
activate = (item: IMenuItem) => {
3234
this.props.menu.activateAndScroll(item, true);
35+
setTimeout(() => {
36+
if (this._updateScroll) {
37+
this._updateScroll();
38+
}
39+
});
40+
};
41+
42+
private saveScrollUpdate = upd => {
43+
this._updateScroll = upd;
3344
};
3445
}

src/services/MenuStore.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,7 @@ export class MenuStore {
221221
* @see MenuStore.activate
222222
*/
223223
@action
224-
activateAndScroll(
225-
item: IMenuItem | undefined,
226-
updateHash: boolean,
227-
rewriteHistory?: boolean,
228-
) {
224+
activateAndScroll(item: IMenuItem | undefined, updateHash: boolean, rewriteHistory?: boolean) {
229225
this.activate(item, updateHash, rewriteHistory);
230226
this.scrollToActive();
231227
}

yarn.lock

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5218,10 +5218,6 @@ pend@~1.2.0:
52185218
version "1.2.0"
52195219
resolved "https://registry.yarnpkg.com/pend/-/pend-1.2.0.tgz#7a57eb550a6783f9115331fcf4663d5c8e007a50"
52205220

5221-
perfect-scrollbar@^0.7.1:
5222-
version "0.7.1"
5223-
resolved "https://registry.yarnpkg.com/perfect-scrollbar/-/perfect-scrollbar-0.7.1.tgz#0c256fb9c5cee401d60a299687a3f9a61487e0d5"
5224-
52255221
perfect-scrollbar@^1.3.0:
52265222
version "1.3.0"
52275223
resolved "https://registry.yarnpkg.com/perfect-scrollbar/-/perfect-scrollbar-1.3.0.tgz#61da56f94b58870d8e0a617bce649cee17d1e3b2"
@@ -5609,7 +5605,7 @@ promise@^7.1.1:
56095605
dependencies:
56105606
asap "~2.0.3"
56115607

5612-
prop-types@*, prop-types@^15.5.0, prop-types@^15.5.4, prop-types@^15.6.0:
5608+
prop-types@^15.5.0, prop-types@^15.5.4, prop-types@^15.6.0:
56135609
version "15.6.0"
56145610
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.6.0.tgz#ceaf083022fc46b4a35f69e13ef75aed0d639856"
56155611
dependencies:
@@ -5792,7 +5788,7 @@ react-dev-utils@^4.1.0:
57925788
strip-ansi "3.0.1"
57935789
text-table "0.2.0"
57945790

5795-
react-dom@>=0.14.0, react-dom@^16.2.0:
5791+
react-dom@^16.2.0:
57965792
version "16.2.0"
57975793
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.2.0.tgz#69003178601c0ca19b709b33a83369fe6124c044"
57985794
dependencies:
@@ -5822,15 +5818,6 @@ react-hot-loader@3.0.0-beta.6:
58225818
redbox-react "^1.2.5"
58235819
source-map "^0.4.4"
58245820

5825-
react-perfect-scrollbar@^0.2.2:
5826-
version "0.2.3"
5827-
resolved "https://registry.yarnpkg.com/react-perfect-scrollbar/-/react-perfect-scrollbar-0.2.3.tgz#abbe13a70348fdb18318152030c77c440b82ad44"
5828-
dependencies:
5829-
perfect-scrollbar "^0.7.1"
5830-
prop-types "*"
5831-
react ">=0.14.0"
5832-
react-dom ">=0.14.0"
5833-
58345821
react-proxy@^3.0.0-alpha.0:
58355822
version "3.0.0-alpha.1"
58365823
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:
58615848
object-assign "^4.1.1"
58625849
prop-types "^15.6.0"
58635850

5864-
react@>=0.14.0, react@^16.2.0:
5851+
react@^16.2.0:
58655852
version "16.2.0"
58665853
resolved "https://registry.yarnpkg.com/react/-/react-16.2.0.tgz#a31bd2dab89bff65d42134fa187f24d054c273ba"
58675854
dependencies:

0 commit comments

Comments
 (0)