Skip to content

fix in safari scroll overflow #228

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
38 changes: 32 additions & 6 deletions src/Dom/scrollLocker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import getScrollBarSize from '../getScrollBarSize';
import setStyle from '../setStyle';
import type React from 'react';

export interface scrollLockOptions {
container: HTMLElement;
Expand All @@ -17,6 +18,31 @@ const scrollingEffectClassNameReg = new RegExp(
'g',
);

let scrollPosition = 0;

const setContainerStyle = (
container: HTMLElement,
scrollBarSize: number,
): React.CSSProperties => {
const defaultStyle: React.CSSProperties = {
width: `calc(100% - ${scrollBarSize}px)`,
overflow: 'hidden',
overflowX: 'hidden',
overflowY: 'hidden',
};

if (container === document.body) {
Copy link
Member

Choose a reason for hiding this comment

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

Can we judge mobile?

Copy link
Author

Choose a reason for hiding this comment

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

@shaodahong , what do you mean? I don't understand, in a mobile view, body is also present in the DOM

Copy link
Member

Choose a reason for hiding this comment

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

Maybe should if (isMobile && container === document.body).

Copy link
Author

@PavelRazumov PavelRazumov May 11, 2021

Choose a reason for hiding this comment

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

@shaodahong , It's bug actually for safari browser, for example, on IPhone (mobile device ) and IPad (tablet device) this defect is present, and i checked work of these devices, but don't know about Mac

Copy link
Member

Choose a reason for hiding this comment

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

In my desktop browser, everything is normal, so my suggestion is to add this fix to the mobile browser.

Copy link
Author

Choose a reason for hiding this comment

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

incorrect-ipad-body

Copy link
Author

Choose a reason for hiding this comment

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

@shaodahong , I checked on IPad with isMobile() flag and work incorrect

Copy link
Author

@PavelRazumov PavelRazumov May 12, 2021

Choose a reason for hiding this comment

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

@shaodahong on Tablet devices isMobile() -> return false

Copy link
Member

Choose a reason for hiding this comment

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

I checked on IPad with isMobile() flag and work incorrect

Yep, so we need judge like this

const isIosDevice = (/iP(ad|hone|od)/.test(window.navigator.platform)

if (isIosDevice) {
//...
}

Copy link
Author

Choose a reason for hiding this comment

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

@shaodahong, on IPad devices now platform return MacIntel
image

scrollPosition = window.pageYOffset;
return {
...defaultStyle,
position: 'fixed',
top: `-${scrollPosition}px`,
};
}

return defaultStyle;
};

let uuid = 0;

// https://github.com/ant-design/ant-design/issues/19340
Expand Down Expand Up @@ -91,12 +117,8 @@ export default class ScrollLocker {
cacheStyle.set(
container,
setStyle(
{
width: `calc(100% - ${scrollBarSize}px)`,
overflow: 'hidden',
overflowX: 'hidden',
overflowY: 'hidden',
},
// https://github.com/ant-design/ant-design/issues/23202
setContainerStyle(container, scrollBarSize),
{
element: container,
},
Expand Down Expand Up @@ -134,6 +156,10 @@ export default class ScrollLocker {
if (!scrollingEffectClassNameReg.test(containerClassName)) return;

setStyle(cacheStyle.get(container), { element: container });
// https://github.com/ant-design/ant-design/issues/23202
if (container === document.body) {
window.scrollTo(0, scrollPosition);
}
cacheStyle.delete(container);
container.className = container.className
.replace(scrollingEffectClassNameReg, '')
Expand Down
19 changes: 11 additions & 8 deletions tests/scrollLocker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ describe('ScrollLocker', () => {
const effectStyle =
'overflow: hidden; overflow-x: hidden; overflow-y: hidden;';

const bodyEffectStyle =
'overflow: hidden; overflow-x: hidden; overflow-y: hidden; position: fixed; top: -0px;';

// jsdom can not capture calc
const initialStyle = '';

Expand All @@ -29,7 +32,7 @@ describe('ScrollLocker', () => {
scrollLocker.lock();

expect(document.body.className).toBe(effectClassname);
expect(document.body.getAttribute('style')).toBe(effectStyle);
expect(document.body.getAttribute('style')).toBe(bodyEffectStyle);

scrollLocker.unLock();

Expand All @@ -44,7 +47,7 @@ describe('ScrollLocker', () => {
scrollLocker.lock();

expect(document.body.className).toBe(effectClassname);
expect(document.body.getAttribute('style')).toBe(effectStyle);
expect(document.body.getAttribute('style')).toBe(bodyEffectStyle);

scrollLocker.unLock();

Expand All @@ -66,17 +69,17 @@ describe('ScrollLocker', () => {
locker2.lock();

expect(document.body.className).toBe(effectClassname);
expect(document.body.getAttribute('style')).toBe(effectStyle);
expect(document.body.getAttribute('style')).toBe(bodyEffectStyle);

locker2.unLock();

expect(document.body.className).toBe(effectClassname);
expect(document.body.getAttribute('style')).toBe(effectStyle);
expect(document.body.getAttribute('style')).toBe(bodyEffectStyle);

locker1.unLock();

expect(document.body.className).toBe(effectClassname);
expect(document.body.getAttribute('style')).toBe(effectStyle);
expect(document.body.getAttribute('style')).toBe(bodyEffectStyle);

scrollLocker.unLock();

Expand Down Expand Up @@ -108,14 +111,14 @@ describe('ScrollLocker', () => {
locker2.lock();

expect(document.body.className).toBe(effectClassname);
expect(document.body.getAttribute('style')).toBe(effectStyle);
expect(document.body.getAttribute('style')).toBe(bodyEffectStyle);
expect(testContainer.className).toBe(effectClassname);
expect(testContainer.getAttribute('style')).toBe(effectStyle);

locker1.unLock();

expect(document.body.className).toBe(effectClassname);
expect(document.body.getAttribute('style')).toBe(effectStyle);
expect(document.body.getAttribute('style')).toBe(bodyEffectStyle);
expect(testContainer.className).toBe(effectClassname);
expect(testContainer.getAttribute('style')).toBe(effectStyle);

Expand Down Expand Up @@ -151,7 +154,7 @@ describe('ScrollLocker', () => {
});

expect(document.body.className).toBe(effectClassname);
expect(document.body.getAttribute('style')).toBe(effectStyle);
expect(document.body.getAttribute('style')).toBe(bodyEffectStyle);

scrollLocker.reLock({
container: testContainer,
Expand Down