This repository has been archived by the owner on Feb 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
scroll.ts
217 lines (196 loc) · 6.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
type EasingOptions = 'linear' | 'ease-in' | 'ease-out' | 'ease-in-out';
export interface ScrollToCustomOptions extends ScrollToOptions {
duration?: number;
easing?: EasingOptions;
}
export const DEFAULT_DURATION = 300;
export async function scrollTo(
el: Element | Window,
options: ScrollToCustomOptions = {}
) {
if (!(el instanceof Element) && !(el instanceof Window)) {
throw new Error(
`element passed to scrollTo() must be either the window or a DOM element, you passed ${el}!`
);
}
options = sanitizeScrollOptions(options);
const scroll = (
from: number,
to: number,
prop: string,
startTime: number,
duration: number | undefined = DEFAULT_DURATION,
easeFunc: EasingFunction,
callback: Function
) => {
window.requestAnimationFrame(() => {
const currentTime = Date.now();
const time = Math.min(1, (currentTime - startTime) / duration);
if (from === to) {
return callback ? callback() : null;
}
setScrollPosition(el, easeFunc(time) * (to - from) + from);
/* prevent scrolling, if already there, or at end */
if (time < 1) {
scroll(from, to, prop, startTime, duration, easeFunc, callback);
} else if (callback) {
callback();
}
});
};
const currentScrollPosition = getScrollPosition(el);
const scrollProperty = getScrollPropertyByElement(el);
return new Promise((resolve) => {
scroll(
currentScrollPosition,
typeof options.top === 'number'
? options.top
: currentScrollPosition,
scrollProperty,
Date.now(),
options.duration,
getEasing(options.easing),
resolve
);
});
}
export function scrollIntoView(
element: HTMLElement,
scroller?: Element | ScrollIntoViewOptions,
options?: ScrollIntoViewOptions
) {
validateElement(element);
if (scroller && !(scroller instanceof Element)) {
options = scroller;
scroller = undefined;
}
const { duration, easing } = sanitizeScrollOptions(options);
scroller = scroller || utils.getDocument().body;
let currentContainerScrollYPos = 0;
let elementScrollYPos = element ? element.offsetTop : 0;
const document = utils.getDocument();
// if the container is the document body or document itself, we'll
// need a different set of coordinates for accuracy
if (scroller === document.body || scroller === document.documentElement) {
// using pageYOffset for cross-browser compatibility
currentContainerScrollYPos = window.pageYOffset;
// must add containers scroll y position to ensure an absolute value that does not change
elementScrollYPos =
element.getBoundingClientRect().top + currentContainerScrollYPos;
}
return scrollTo(scroller as Element, {
top: elementScrollYPos,
left: 0,
duration,
easing,
});
}
function validateElement(element?: HTMLElement) {
if (element === undefined) {
const errorMsg =
'The element passed to scrollIntoView() was undefined.';
throw new Error(errorMsg);
}
if (!(element instanceof HTMLElement)) {
throw new Error(
`The element passed to scrollIntoView() must be a valid element. You passed ${element}.`
);
}
}
function getScrollPropertyByElement(
el: Element | Window
): 'scrollY' | 'scrollX' | 'scrollTop' | 'scrollLeft' {
const props: any = {
window: {
y: 'scrollY',
x: 'scrollX',
},
element: {
y: 'scrollTop',
x: 'scrollLeft',
},
};
const axis = 'y';
if (el instanceof Window) {
return props.window[axis];
} else {
return props.element[axis];
}
}
function sanitizeScrollOptions(
options: ScrollToCustomOptions = {}
): ScrollToCustomOptions {
if (options.behavior === 'smooth') {
options.easing = 'ease-in-out';
options.duration = DEFAULT_DURATION;
}
if (options.behavior === 'auto') {
options.duration = 0;
options.easing = 'linear';
}
return options;
}
function getScrollPosition(el: Element | Window): number {
const document = utils.getDocument();
if (
el === document.body ||
el === document.documentElement ||
el instanceof Window
) {
return document.body.scrollTop || document.documentElement.scrollTop;
} else {
return el.scrollTop;
}
}
function setScrollPosition(el: Element | Window, value: number) {
const document = utils.getDocument();
if (
el === document.body ||
el === document.documentElement ||
el instanceof Window
) {
document.body.scrollTop = value;
document.documentElement.scrollTop = value;
} else {
el.scrollTop = value;
}
}
export const utils = {
// we're really just exporting this so that tests can mock the document.documentElement
getDocument(): HTMLDocument {
return document;
},
};
// eslint-disable-next-line no-unused-vars
type EasingFunction = (t: number) => number;
interface EasingFunctions {
linear: EasingFunction;
'ease-in': EasingFunction;
'ease-out': EasingFunction;
'ease-in-out': EasingFunction;
}
export const easingMap: EasingFunctions = {
linear(t: number) {
return t;
},
'ease-in'(t: number) {
return t * t;
},
'ease-out'(t: number) {
return t * (2 - t);
},
'ease-in-out'(t: number) {
return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
},
};
const getEasing = (easing?: EasingOptions): EasingFunction => {
const defaultEasing = 'linear';
const easeFunc = easingMap[easing || defaultEasing];
if (!easeFunc) {
const options = Object.keys(easingMap).join(',');
throw new Error(
`Scroll error: scroller does not support an easing option of "${easing}". Supported options are ${options}`
);
}
return easeFunc;
};