Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use ScrollLocker #220

Merged
merged 7 commits into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ coverage
/ios/
/android/
yarn.lock
package-lock.json
.storybook
.doc

Expand Down
60 changes: 31 additions & 29 deletions docs/examples/ant-design.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,41 +201,43 @@ const MyControl = () => {
);

return (
<div style={{ width: '90%', margin: '0 auto', height: '150vh' }}>
<style>
{`
<React.StrictMode>
<div style={{ width: '90%', margin: '0 auto', height: '150vh' }}>
<style>
{`
.center {
display: flex;
align-items: center;
justify-content: center;
}
`}
</style>
<p>
<button type="button" className="btn btn-primary" onClick={onClick}>
show dialog
</button>
&nbsp;
<label>
destroy on close:
<input type="checkbox" checked={destroyOnClose} onChange={onDestroyOnCloseChange} />
</label>
&nbsp;
<label>
center
<input type="checkbox" checked={center} onChange={centerEvent} />
</label>
&nbsp;
<label>
force render
<input type="checkbox" checked={forceRender} onChange={onForceRenderChange} />
</label>
<input placeholder="Useless Input" onClick={onClick} />
</p>
{dialog}
{dialog2}
{dialog3}
</div>
</style>
<p>
<button type="button" className="btn btn-primary" onClick={onClick}>
show dialog
</button>
&nbsp;
<label>
destroy on close:
<input type="checkbox" checked={destroyOnClose} onChange={onDestroyOnCloseChange} />
</label>
&nbsp;
<label>
center
<input type="checkbox" checked={center} onChange={centerEvent} />
</label>
&nbsp;
<label>
force render
<input type="checkbox" checked={forceRender} onChange={onForceRenderChange} />
</label>
<input placeholder="Useless Input" onClick={onClick} />
</p>
{dialog}
{dialog2}
{dialog3}
</div>
</React.StrictMode>
);
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@babel/runtime": "^7.10.1",
"classnames": "^2.2.6",
"rc-motion": "^2.3.0",
"rc-util": "^5.0.1"
"rc-util": "^5.6.1"
},
"devDependencies": {
"@types/enzyme": "^3.10.7",
Expand Down
22 changes: 11 additions & 11 deletions src/Dialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useRef, useEffect } from 'react';
import classNames from 'classnames';
import KeyCode from 'rc-util/lib/KeyCode';
import contains from 'rc-util/lib/Dom/contains';
import ScollLocker from 'rc-util/lib/Dom/scrollLocker';
import { IDialogPropTypes } from '../IDialogPropTypes';
import Mask from './Mask';
import { getMotionName, getUUID } from '../util';
Expand All @@ -12,7 +13,7 @@ export interface IDialogChildProps extends IDialogPropTypes {
// zombieJ: This should be handle on top instead of each Dialog.
// TODO: refactor to remove this.
getOpenCount: () => number;
switchScrollingEffect?: () => void;
scrollLocker?: ScollLocker;
}

export default function Dialog(props: IDialogChildProps) {
Expand All @@ -22,7 +23,7 @@ export default function Dialog(props: IDialogChildProps) {
visible = false,
keyboard = true,
focusTriggerAfterClose = true,
switchScrollingEffect = () => {},
scrollLocker,

// Wrapper
title,
Expand Down Expand Up @@ -69,7 +70,6 @@ export default function Dialog(props: IDialogChildProps) {
} else {
// Clean up scroll bar & focus back
setAnimatedVisible(false);
switchScrollingEffect();

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

const onContentMouseUp: React.MouseEventHandler = () => {
contentTimeoutRef.current = setTimeout(() => {
contentClickRef.current = false;
});
}
};

// >>> Wrapper
// Close only when element not on dialog
let onWrapperClick: (e: React.SyntheticEvent) => void = null;
if (maskClosable) {
onWrapperClick = (e) => {
if(contentClickRef.current) {
if (contentClickRef.current) {
contentClickRef.current = false;
} else if (
wrapperRef.current === e.target
) {
} else if (wrapperRef.current === e.target) {
onInternalClose(e);
}
};
Expand All @@ -138,14 +136,16 @@ export default function Dialog(props: IDialogChildProps) {
useEffect(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

末尾加return null 或者 空函数可以消灭这个lint 的disable

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

……不能 return null

if (visible) {
setAnimatedVisible(true);
switchScrollingEffect();
scrollLocker?.lock();

return scrollLocker?.unLock;
}
return () => {};
}, [visible]);

// Remove direct should also check the scroll bar update
useEffect(
() => () => {
switchScrollingEffect();
clearTimeout(contentTimeoutRef.current);
},
[],
Expand Down