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 in safari scroll overflow #228

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
39 changes: 33 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,32 @@ const scrollingEffectClassNameReg = new RegExp(
'g',
);

let scrollPosition = 0;
const isIosDevice = /iP(ad|hone|od)|MacIntel/.test(window.navigator.platform);
Copy link
Member

Choose a reason for hiding this comment

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

We need support ssr, so

Suggested change
const isIosDevice = /iP(ad|hone|od)|MacIntel/.test(window.navigator.platform);
const isIosDevice = canUseDom() && window.navigator &&
window.navigator.platform &&
(/iP(ad|hone|od)/.test(window.navigator.platform) ||
(window.navigator.platform === 'MacIntel' && window.navigator.maxTouchPoints > 1))`;

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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


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

if (isIosDevice && container === document.body) {
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 +118,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 +157,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 (isIosDevice && container === document.body) {
window.scrollTo(0, scrollPosition);
}
cacheStyle.delete(container);
container.className = container.className
.replace(scrollingEffectClassNameReg, '')
Expand Down