Skip to content

Commit 7b907a0

Browse files
authored
refactor: Use rc-portal instead (#322)
* refactor: Use Portal instead * chore: update Portal ver * fix: forceRender logic * rm getContainer * chore: fix open logic
1 parent 3fb3004 commit 7b907a0

File tree

6 files changed

+29
-43
lines changed

6 files changed

+29
-43
lines changed

docs/examples/multiple.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import motionProps from './motion';
1313

1414
class Demo extends React.Component {
1515
public state = {
16-
open: false,
17-
openChild: false,
18-
openChildren: false,
16+
open: true,
17+
openChild: true,
18+
openChildren: true,
1919
};
2020
public onClick = () => {
2121
this.setState({

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
},
4747
"dependencies": {
4848
"@babel/runtime": "^7.10.1",
49+
"@rc-component/portal": "^1.0.0-6",
4950
"classnames": "^2.2.6",
5051
"rc-motion": "^2.6.1",
5152
"rc-util": "^5.21.2"

src/Drawer.tsx

+13-19
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as React from 'react';
2-
import type { GetContainer } from 'rc-util/lib/PortalWrapper';
3-
import Portal from 'rc-util/lib/PortalWrapper';
2+
import Portal from '@rc-component/portal';
3+
import type { PortalProps } from '@rc-component/portal';
44
import DrawerPopup from './DrawerPopup';
55
import type { DrawerPopupProps } from './DrawerPopup';
6+
import { warnCheck } from './util';
67

78
export type Placement = 'left' | 'top' | 'right' | 'bottom';
89

@@ -12,27 +13,27 @@ export interface DrawerProps
1213

1314
open?: boolean;
1415
onClose?: (e: React.MouseEvent | React.KeyboardEvent) => void;
15-
/** @deprecated Only work on Portal mode. You can replace with rootClassName instead */
16-
wrapperClassName?: string;
1716
destroyOnClose?: boolean;
18-
getContainer?: GetContainer | false;
17+
getContainer?: PortalProps['getContainer'];
1918
}
2019

21-
const defaultGetContainer = () => document.body;
22-
2320
const Drawer: React.FC<DrawerProps> = props => {
2421
const {
2522
open,
2623
getContainer,
2724
forceRender,
28-
wrapperClassName,
2925
prefixCls,
3026
afterOpenChange,
3127
destroyOnClose,
3228
} = props;
3329

3430
const [animatedVisible, setAnimatedVisible] = React.useState(false);
3531

32+
// ============================= Warn =============================
33+
if (process.env.NODE_ENV !== 'production') {
34+
warnCheck(props);
35+
}
36+
3637
// ============================= Open =============================
3738
const internalAfterOpenChange: DrawerProps['afterOpenChange'] =
3839
nextVisible => {
@@ -51,20 +52,14 @@ const Drawer: React.FC<DrawerProps> = props => {
5152
afterOpenChange: internalAfterOpenChange,
5253
};
5354

54-
if (getContainer === false) {
55-
return <DrawerPopup {...sharedDrawerProps} inline />;
56-
}
57-
5855
return (
5956
<Portal
60-
visible={open}
61-
forceRender={forceRender}
57+
open={open || forceRender || animatedVisible}
58+
autoDestroy={false}
6259
getContainer={getContainer}
63-
wrapperClassName={wrapperClassName}
60+
autoLock={open || animatedVisible}
6461
>
65-
{({ scrollLocker }) => (
66-
<DrawerPopup {...sharedDrawerProps} scrollLocker={scrollLocker} />
67-
)}
62+
<DrawerPopup {...sharedDrawerProps} inline={getContainer === false} />
6863
</Portal>
6964
);
7065
};
@@ -74,7 +69,6 @@ const Drawer: React.FC<DrawerProps> = props => {
7469
// Let's maintain this in one place.
7570
Drawer.defaultProps = {
7671
open: false,
77-
getContainer: defaultGetContainer,
7872
prefixCls: 'rc-drawer',
7973
placement: 'right',
8074
autoFocus: true,

src/DrawerPopup.tsx

+2-19
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import classNames from 'classnames';
33
import CSSMotion from 'rc-motion';
44
import type { CSSMotionProps } from 'rc-motion';
55
import DrawerPanel from './DrawerPanel';
6-
import type ScrollLocker from 'rc-util/lib/Dom/scrollLocker';
6+
// import type ScrollLocker from 'rc-util/lib/Dom/scrollLocker';
77
import DrawerContext from './context';
88
import type { DrawerContextProps } from './context';
99
import KeyCode from 'rc-util/lib/KeyCode';
@@ -32,9 +32,6 @@ export interface DrawerPopupProps {
3232
autoFocus?: boolean;
3333
keyboard?: boolean;
3434

35-
// MISC
36-
scrollLocker?: ScrollLocker;
37-
3835
// Root
3936
rootClassName?: string;
4037
rootStyle?: React.CSSProperties;
@@ -52,7 +49,7 @@ export interface DrawerPopupProps {
5249
// Mask
5350
mask?: boolean;
5451
maskClosable?: boolean;
55-
maskClassName?: React.CSSProperties;
52+
maskClassName?: string;
5653
maskStyle?: React.CSSProperties;
5754

5855
// Motion
@@ -77,9 +74,6 @@ export default function DrawerPopup(props: DrawerPopupProps) {
7774
autoFocus,
7875
keyboard,
7976

80-
// MISC
81-
scrollLocker,
82-
8377
// Root
8478
rootClassName,
8579
rootStyle,
@@ -190,17 +184,9 @@ export default function DrawerPopup(props: DrawerPopupProps) {
190184
}
191185
}, [open]);
192186

193-
// Lock window scroll
194-
React.useEffect(() => {
195-
if (open && mask) {
196-
scrollLocker?.lock();
197-
}
198-
}, [open, mask]);
199-
200187
// Clean up
201188
React.useEffect(
202189
() => () => {
203-
scrollLocker?.unLock();
204190
parentContext?.pull?.();
205191
},
206192
[],
@@ -269,9 +255,6 @@ export default function DrawerPopup(props: DrawerPopupProps) {
269255
forceRender={forceRender}
270256
onVisibleChanged={nextVisible => {
271257
afterOpenChange?.(nextVisible);
272-
if (!nextVisible) {
273-
scrollLocker?.unLock();
274-
}
275258
}}
276259
removeOnLeave={false}
277260
leavedClassName={`${prefixCls}-content-wrapper-hidden`}

src/util.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import warning from 'rc-util/lib/warning';
2+
import type { DrawerProps } from './Drawer';
23

34
export function parseWidthHeight(value?: number | string) {
45
if (typeof value === 'string' && String(Number(value)) === value) {
@@ -11,3 +12,10 @@ export function parseWidthHeight(value?: number | string) {
1112

1213
return value;
1314
}
15+
16+
export function warnCheck(props: DrawerProps) {
17+
warning(
18+
!('wrapperClassName' in props),
19+
`'wrapperClassName' is removed. Please use 'rootClassName' instead.`,
20+
);
21+
}

tests/index.spec.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ describe('rc-drawer-menu', () => {
8585
});
8686

8787
expect(document.body).toHaveStyle({
88-
overflow: 'hidden',
88+
overflowY: 'hidden',
8989
});
9090

9191
unmount();
9292

9393
expect(document.body).not.toHaveStyle({
94-
overflow: 'hidden',
94+
overflowY: 'hidden',
9595
});
9696
});
9797
});

0 commit comments

Comments
 (0)