-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathscrollLocker.ts
More file actions
168 lines (137 loc) · 4.12 KB
/
scrollLocker.ts
File metadata and controls
168 lines (137 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import getScrollBarSize from '../getScrollBarSize';
import setStyle from '../setStyle';
import type React from 'react';
export interface scrollLockOptions {
container: HTMLElement;
}
interface Ilocks {
target: typeof uuid;
options: scrollLockOptions;
}
let locks: Ilocks[] = [];
const scrollingEffectClassName = 'ant-scrolling-effect';
const scrollingEffectClassNameReg = new RegExp(
`${scrollingEffectClassName}`,
'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) {
scrollPosition = window.pageYOffset;
return {
...defaultStyle,
position: 'fixed',
top: `-${scrollPosition}px`,
};
}
return defaultStyle;
};
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 {
private lockTarget: typeof uuid;
private options: scrollLockOptions;
constructor(options?: scrollLockOptions) {
// eslint-disable-next-line no-plusplus
this.lockTarget = uuid++;
this.options = options;
}
getContainer = (): HTMLElement | undefined => {
return this.options?.container;
};
// if options change...
reLock = (options?: scrollLockOptions) => {
const findLock = locks.find(({ target }) => target === this.lockTarget);
if (findLock) {
this.unLock();
}
this.options = options;
if (findLock) {
findLock.options = options;
this.lock();
}
};
lock = () => {
// If lockTarget exist return
if (locks.some(({ target }) => target === this.lockTarget)) {
return;
}
// If same container effect, return
if (
locks.some(
({ options }) => options?.container === this.options?.container,
)
) {
locks = [...locks, { target: this.lockTarget, options: this.options }];
return;
}
let scrollBarSize = 0;
const container = this.options?.container || document.body;
if (
(container === document.body &&
window.innerWidth - document.documentElement.clientWidth > 0) ||
container.scrollHeight > container.clientHeight
) {
scrollBarSize = getScrollBarSize();
}
const containerClassName = container.className;
if (
locks.filter(
({ options }) => options?.container === this.options?.container,
).length === 0
) {
cacheStyle.set(
container,
setStyle(
// https://github.com/ant-design/ant-design/issues/23202
setContainerStyle(container, scrollBarSize),
{
element: container,
},
),
);
}
// https://github.com/ant-design/ant-design/issues/19729
if (!scrollingEffectClassNameReg.test(containerClassName)) {
const addClassName = `${containerClassName} ${scrollingEffectClassName}`;
container.className = addClassName.trim();
}
locks = [...locks, { target: this.lockTarget, options: this.options }];
};
unLock = () => {
const findLock = locks.find(({ target }) => target === this.lockTarget);
locks = locks.filter(({ target }) => target !== this.lockTarget);
if (
!findLock ||
locks.some(
({ options }) => options?.container === findLock.options?.container,
)
) {
return;
}
// Remove Effect
const container = this.options?.container || document.body;
const containerClassName = container.className;
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, '')
.trim();
};
}