Skip to content

Commit c82bdfa

Browse files
authored
refactor: use ScrollLocker (#220)
* refactor: use ScrollLocker * upgrade rc-util * add package-lock.json ignore * fix lint error * improve consistent-return * add semi * fix useEffect return
1 parent 23865bc commit c82bdfa

File tree

4 files changed

+44
-41
lines changed

4 files changed

+44
-41
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ coverage
3232
/ios/
3333
/android/
3434
yarn.lock
35+
package-lock.json
3536
.storybook
3637
.doc
3738

docs/examples/ant-design.tsx

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -201,41 +201,43 @@ const MyControl = () => {
201201
);
202202

203203
return (
204-
<div style={{ width: '90%', margin: '0 auto', height: '150vh' }}>
205-
<style>
206-
{`
204+
<React.StrictMode>
205+
<div style={{ width: '90%', margin: '0 auto', height: '150vh' }}>
206+
<style>
207+
{`
207208
.center {
208209
display: flex;
209210
align-items: center;
210211
justify-content: center;
211212
}
212213
`}
213-
</style>
214-
<p>
215-
<button type="button" className="btn btn-primary" onClick={onClick}>
216-
show dialog
217-
</button>
218-
&nbsp;
219-
<label>
220-
destroy on close:
221-
<input type="checkbox" checked={destroyOnClose} onChange={onDestroyOnCloseChange} />
222-
</label>
223-
&nbsp;
224-
<label>
225-
center
226-
<input type="checkbox" checked={center} onChange={centerEvent} />
227-
</label>
228-
&nbsp;
229-
<label>
230-
force render
231-
<input type="checkbox" checked={forceRender} onChange={onForceRenderChange} />
232-
</label>
233-
<input placeholder="Useless Input" onClick={onClick} />
234-
</p>
235-
{dialog}
236-
{dialog2}
237-
{dialog3}
238-
</div>
214+
</style>
215+
<p>
216+
<button type="button" className="btn btn-primary" onClick={onClick}>
217+
show dialog
218+
</button>
219+
&nbsp;
220+
<label>
221+
destroy on close:
222+
<input type="checkbox" checked={destroyOnClose} onChange={onDestroyOnCloseChange} />
223+
</label>
224+
&nbsp;
225+
<label>
226+
center
227+
<input type="checkbox" checked={center} onChange={centerEvent} />
228+
</label>
229+
&nbsp;
230+
<label>
231+
force render
232+
<input type="checkbox" checked={forceRender} onChange={onForceRenderChange} />
233+
</label>
234+
<input placeholder="Useless Input" onClick={onClick} />
235+
</p>
236+
{dialog}
237+
{dialog2}
238+
{dialog3}
239+
</div>
240+
</React.StrictMode>
239241
);
240242
};
241243

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"@babel/runtime": "^7.10.1",
5151
"classnames": "^2.2.6",
5252
"rc-motion": "^2.3.0",
53-
"rc-util": "^5.0.1"
53+
"rc-util": "^5.6.1"
5454
},
5555
"devDependencies": {
5656
"@types/enzyme": "^3.10.7",

src/Dialog/index.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useRef, useEffect } from 'react';
33
import classNames from 'classnames';
44
import KeyCode from 'rc-util/lib/KeyCode';
55
import contains from 'rc-util/lib/Dom/contains';
6+
import ScollLocker from 'rc-util/lib/Dom/scrollLocker';
67
import { IDialogPropTypes } from '../IDialogPropTypes';
78
import Mask from './Mask';
89
import { getMotionName, getUUID } from '../util';
@@ -12,7 +13,7 @@ export interface IDialogChildProps extends IDialogPropTypes {
1213
// zombieJ: This should be handle on top instead of each Dialog.
1314
// TODO: refactor to remove this.
1415
getOpenCount: () => number;
15-
switchScrollingEffect?: () => void;
16+
scrollLocker?: ScollLocker;
1617
}
1718

1819
export default function Dialog(props: IDialogChildProps) {
@@ -22,7 +23,7 @@ export default function Dialog(props: IDialogChildProps) {
2223
visible = false,
2324
keyboard = true,
2425
focusTriggerAfterClose = true,
25-
switchScrollingEffect = () => {},
26+
scrollLocker,
2627

2728
// Wrapper
2829
title,
@@ -69,7 +70,6 @@ export default function Dialog(props: IDialogChildProps) {
6970
} else {
7071
// Clean up scroll bar & focus back
7172
setAnimatedVisible(false);
72-
switchScrollingEffect();
7373

7474
if (mask && lastOutSideActiveElementRef.current && focusTriggerAfterClose) {
7575
try {
@@ -96,24 +96,22 @@ export default function Dialog(props: IDialogChildProps) {
9696
const onContentMouseDown: React.MouseEventHandler = () => {
9797
clearTimeout(contentTimeoutRef.current);
9898
contentClickRef.current = true;
99-
}
99+
};
100100

101101
const onContentMouseUp: React.MouseEventHandler = () => {
102102
contentTimeoutRef.current = setTimeout(() => {
103103
contentClickRef.current = false;
104104
});
105-
}
105+
};
106106

107107
// >>> Wrapper
108108
// Close only when element not on dialog
109109
let onWrapperClick: (e: React.SyntheticEvent) => void = null;
110110
if (maskClosable) {
111111
onWrapperClick = (e) => {
112-
if(contentClickRef.current) {
112+
if (contentClickRef.current) {
113113
contentClickRef.current = false;
114-
} else if (
115-
wrapperRef.current === e.target
116-
) {
114+
} else if (wrapperRef.current === e.target) {
117115
onInternalClose(e);
118116
}
119117
};
@@ -138,14 +136,16 @@ export default function Dialog(props: IDialogChildProps) {
138136
useEffect(() => {
139137
if (visible) {
140138
setAnimatedVisible(true);
141-
switchScrollingEffect();
139+
scrollLocker?.lock();
140+
141+
return scrollLocker?.unLock;
142142
}
143+
return () => {};
143144
}, [visible]);
144145

145146
// Remove direct should also check the scroll bar update
146147
useEffect(
147148
() => () => {
148-
switchScrollingEffect();
149149
clearTimeout(contentTimeoutRef.current);
150150
},
151151
[],

0 commit comments

Comments
 (0)