Skip to content

Commit f04ab33

Browse files
authored
refactor: use ScrollLocker (#157)
* refactor: use ScrollLocker * fix useless * upgrade rc-util * fix prettier
1 parent cae4ba6 commit f04ab33

File tree

5 files changed

+21
-41
lines changed

5 files changed

+21
-41
lines changed

.prettierrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"semi": true,
44
"singleQuote": true,
55
"tabWidth": 2,
6-
"trailingComma": "all"
6+
"trailingComma": "all",
7+
"arrowParens": "avoid"
78
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,6 @@
7171
"dependencies": {
7272
"@babel/runtime": "^7.10.1",
7373
"classnames": "^2.2.6",
74-
"rc-util": "^5.0.1"
74+
"rc-util": "^5.6.2"
7575
}
7676
}

src/DrawerChild.tsx

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ interface IState {
2727
}
2828

2929
class DrawerChild extends React.Component<IDrawerChildProps, IState> {
30-
static defaultProps = {
31-
switchScrollingEffect: () => {},
32-
};
33-
3430
public static getDerivedStateFromProps(
3531
props: IDrawerChildProps,
3632
{ prevProps, _self }: { prevProps: IDrawerChildProps; _self: DrawerChild },
@@ -115,35 +111,35 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
115111
this.forceUpdate(() => {
116112
this.domFocus();
117113
});
114+
this.props.scrollLocker?.lock();
118115
}
119116
}
120117

121118
public componentDidUpdate(prevProps: IDrawerChildProps) {
122-
const { open, getContainer } = this.props;
119+
const { open, getContainer, scrollLocker } = this.props;
123120
const container = getContainer && getContainer();
124121
if (open !== prevProps.open) {
125-
if (open) {
126-
this.domFocus();
127-
}
128122
if (container && container.parentNode === document.body) {
129123
currentDrawer[this.drawerId] = !!open;
130124
}
131125
this.openLevelTransition();
126+
if (open) {
127+
this.domFocus();
128+
scrollLocker?.lock();
129+
} else {
130+
scrollLocker?.unLock();
131+
}
132132
}
133133
}
134134

135135
public componentWillUnmount() {
136-
const { getOpenCount, open, switchScrollingEffect } = this.props;
137-
const openCount = typeof getOpenCount === 'function' && getOpenCount();
136+
const { open, scrollLocker } = this.props;
138137
delete currentDrawer[this.drawerId];
139138
if (open) {
140139
this.setLevelTransform(false);
141140
document.body.style.touchAction = '';
142141
}
143-
if (!openCount) {
144-
document.body.style.overflow = '';
145-
switchScrollingEffect(true);
146-
}
142+
scrollLocker?.unLock();
147143
}
148144

149145
private domFocus = () => {
@@ -346,17 +342,7 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
346342
};
347343

348344
private addScrollingEffect = (right: number) => {
349-
const {
350-
placement,
351-
duration,
352-
ease,
353-
getOpenCount,
354-
switchScrollingEffect,
355-
} = this.props;
356-
const openCount = getOpenCount && getOpenCount();
357-
if (openCount === 1) {
358-
switchScrollingEffect();
359-
}
345+
const { placement, duration, ease } = this.props;
360346
const widthTransition = `width ${duration} ${ease}`;
361347
const transformTransition = `transform ${duration} ${ease}`;
362348
this.dom.style.transition = 'none';
@@ -383,17 +369,8 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
383369
};
384370

385371
private remScrollingEffect = (right: number) => {
386-
const {
387-
placement,
388-
duration,
389-
ease,
390-
getOpenCount,
391-
switchScrollingEffect,
392-
} = this.props;
393-
const openCount = getOpenCount && getOpenCount();
394-
if (!openCount) {
395-
switchScrollingEffect(true);
396-
}
372+
const { placement, duration, ease } = this.props;
373+
397374
if (transitionStr) {
398375
document.body.style.overflowX = 'hidden';
399376
}
@@ -511,7 +488,7 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
511488
onHandleClick,
512489
keyboard,
513490
getOpenCount,
514-
switchScrollingEffect,
491+
scrollLocker,
515492
...props
516493
} = this.props;
517494
// 首次渲染都将是关闭状态。

src/IDrawerPropTypes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Definitions: https://github.com/react-component/drawer
55
import { GetContainer } from 'rc-util/lib/PortalWrapper';
66
import * as React from 'react';
7+
import ScrollLocker from 'rc-util/lib/Dom/scrollLocker';
78

89
export type IPlacement = 'left' | 'top' | 'right' | 'bottom';
910

@@ -44,5 +45,5 @@ export interface IDrawerProps extends IProps {
4445
export interface IDrawerChildProps extends IProps {
4546
getContainer?: () => HTMLElement;
4647
getOpenCount?: () => number;
47-
switchScrollingEffect?: (close?: boolean) => void;
48+
scrollLocker?: ScrollLocker;
4849
}

tests/__snapshots__/drawer.spec.tsx.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ exports[`Drawer render boolean 1`] = `
4141
exports[`Drawer render dom 1`] = `
4242
<div>
4343
<div
44-
class="main"
44+
class="main ant-scrolling-effect"
45+
style="padding-right: 0px; overflow: hidden; overflow-x: hidden; overflow-y: hidden;"
4546
>
4647
<div>
4748
<div

0 commit comments

Comments
 (0)