-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
scroll.ts
188 lines (154 loc) · 5.13 KB
/
scroll.ts
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { getDocument } from './dom/getDocument';
import { mergeStyles } from '@uifabric/merge-styles';
import { EventGroup } from './EventGroup';
let _scrollbarWidth: number;
let _bodyScrollDisabledCount = 0;
const DisabledScrollClassName = mergeStyles({
overflow: 'hidden !important' as 'hidden'
});
/**
* Placing this attribute on scrollable divs optimizes detection to know
* if the div is scrollable or not (given we can avoid expensive operations
* like getComputedStyle.)
*
* @public
*/
export const DATA_IS_SCROLLABLE_ATTRIBUTE = 'data-is-scrollable';
const _makeElementScrollAllower = () => {
let _previousClientY = 0;
let _element: Element | null = null;
// remember the clientY for future calls of _preventOverscrolling
const _saveClientY = (event: TouchEvent): void => {
if (event.targetTouches.length === 1) {
_previousClientY = event.targetTouches[0].clientY;
}
};
// prevent the body from scrolling when the user attempts
// to scroll past the top or bottom of the element
const _preventOverscrolling = (event: TouchEvent): void => {
// only respond to a single-finger touch
if (event.targetTouches.length !== 1) {
return;
}
// prevent the body touchmove handler from firing
// so that scrolling is allowed within the element
event.stopPropagation();
if (!_element) {
return;
}
const clientY = event.targetTouches[0].clientY - _previousClientY;
const scrollableParent = findScrollableParent(event.target as HTMLElement);
if (scrollableParent) {
_element = scrollableParent;
}
// if the element is scrolled to the top,
// prevent the user from scrolling up
if (_element.scrollTop === 0 && clientY > 0) {
event.preventDefault();
}
// if the element is scrolled to the bottom,
// prevent the user from scrolling down
if (_element.scrollHeight - _element.scrollTop <= _element.clientHeight && clientY < 0) {
event.preventDefault();
}
};
return (element: HTMLElement | null, events: EventGroup): void => {
if (!element) {
return;
}
events.on(element, 'touchstart', _saveClientY, { passive: false });
events.on(element, 'touchmove', _preventOverscrolling, { passive: false });
_element = element;
};
};
/**
* Allows the user to scroll within a element,
* while preventing the user from scrolling the body
*/
export const allowScrollOnElement = _makeElementScrollAllower();
const _disableIosBodyScroll = (event: TouchEvent) => {
event.preventDefault();
};
/**
* Disables the body scrolling.
*
* @public
*/
export function disableBodyScroll(): void {
let doc = getDocument();
if (doc && doc.body && !_bodyScrollDisabledCount) {
doc.body.classList.add(DisabledScrollClassName);
doc.body.addEventListener('touchmove', _disableIosBodyScroll, { passive: false, capture: false });
}
_bodyScrollDisabledCount++;
}
/**
* Enables the body scrolling.
*
* @public
*/
export function enableBodyScroll(): void {
if (_bodyScrollDisabledCount > 0) {
let doc = getDocument();
if (doc && doc.body && _bodyScrollDisabledCount === 1) {
doc.body.classList.remove(DisabledScrollClassName);
doc.body.removeEventListener('touchmove', _disableIosBodyScroll);
}
_bodyScrollDisabledCount--;
}
}
/**
* Calculates the width of a scrollbar for the browser/os.
*
* @public
*/
export function getScrollbarWidth(): number {
if (_scrollbarWidth === undefined) {
let scrollDiv: HTMLElement = document.createElement('div');
scrollDiv.style.setProperty('width', '100px');
scrollDiv.style.setProperty('height', '100px');
scrollDiv.style.setProperty('overflow', 'scroll');
scrollDiv.style.setProperty('position', 'absolute');
scrollDiv.style.setProperty('top', '-9999px');
document.body.appendChild(scrollDiv);
// Get the scrollbar width
_scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
// Delete the DIV
document.body.removeChild(scrollDiv);
}
return _scrollbarWidth;
}
/**
* Traverses up the DOM for the element with the data-is-scrollable=true attribute, or returns
* document.body.
*
* @public
*/
export function findScrollableParent(startingElement: HTMLElement | null): HTMLElement | null {
let el: HTMLElement | null = startingElement;
// First do a quick scan for the scrollable attribute.
while (el && el !== document.body) {
if (el.getAttribute(DATA_IS_SCROLLABLE_ATTRIBUTE) === 'true') {
return el;
}
el = el.parentElement;
}
// If we haven't found it, the use the slower method: compute styles to evaluate if overflow is set.
el = startingElement;
while (el && el !== document.body) {
if (el.getAttribute(DATA_IS_SCROLLABLE_ATTRIBUTE) !== 'false') {
const computedStyles = getComputedStyle(el);
let overflowY = computedStyles ? computedStyles.getPropertyValue('overflow-y') : '';
if (overflowY && (overflowY === 'scroll' || overflowY === 'auto')) {
return el;
}
}
el = el.parentElement;
}
// Fall back to window scroll.
if (!el || el === document.body) {
// tslint:disable-next-line:no-any
el = window as any;
}
return el;
}