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

fix: ScrollLocker close at the same time cacheStyle queue #181

Merged
merged 2 commits into from
Dec 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 29 additions & 30 deletions src/Dom/scrollLocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ const preventDefault = (event: React.TouchEvent | TouchEvent): boolean => {
return false;
};

let uuid = 0;

interface Ilocks {
target: typeof uuid;
cacheStyle?: React.CSSProperties;
options: scrollLockOptions;
}

Expand All @@ -45,6 +42,12 @@ const scrollingEffectClassNameReg = new RegExp(
'g',
);

let uuid = 0;

// https://github.com/ant-design/ant-design/issues/19340
// https://github.com/ant-design/ant-design/issues/19332
const cacheStyle = new Map<Element, React.CSSProperties>();

export default class ScrollLocker {
lockTarget: typeof uuid;

Expand Down Expand Up @@ -81,19 +84,26 @@ export default class ScrollLocker {
const container = this.options?.container || document.body;
const containerClassName = container.className;

// https://github.com/ant-design/ant-design/issues/19340
// https://github.com/ant-design/ant-design/issues/19332
const cacheStyle = setStyle(
{
paddingRight: `${scrollBarSize}px`,
overflow: 'hidden',
overflowX: 'hidden',
overflowY: 'hidden',
},
{
element: container,
},
);
if (
locks.filter(
({ options }) => options?.container === this.options?.container,
).length === 0
) {
cacheStyle.set(
container,
setStyle(
{
paddingRight: `${scrollBarSize}px`,
overflow: 'hidden',
overflowX: 'hidden',
overflowY: 'hidden',
},
{
element: container,
},
),
);
}

// https://github.com/ant-design/ant-design/issues/19729
if (!scrollingEffectClassNameReg.test(containerClassName)) {
Expand All @@ -107,10 +117,7 @@ export default class ScrollLocker {
);
}

locks = [
...locks,
{ target: this.lockTarget, options: this.options, cacheStyle },
];
locks = [...locks, { target: this.lockTarget, options: this.options }];
};

unLock = () => {
Expand All @@ -133,16 +140,8 @@ export default class ScrollLocker {

if (!scrollingEffectClassNameReg.test(containerClassName)) return;

setStyle(
// @ts-ignore position should be empty string
findLock.cacheStyle || {
paddingRight: '',
overflow: '',
overflowX: '',
overflowY: '',
},
{ element: container },
);
setStyle(cacheStyle.get(container), { element: container });
cacheStyle.delete(container);
container.className = container.className
.replace(scrollingEffectClassNameReg, '')
.trim();
Expand Down