-
-
Notifications
You must be signed in to change notification settings - Fork 140
/
panelSnap.js
235 lines (191 loc) · 6.68 KB
/
panelSnap.js
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import Tweezer from 'tweezer.js';
import {
getScrollingElement,
getScrollEventContainer,
getTargetScrollOffset,
getElementsInContainerViewport,
elementFillsContainer,
passiveIsSupported,
} from './utilities';
let INSTANCE_COUNTER = 0;
const TWEEN_MAX_VALUE = 10000;
const defaultOptions = {
container: document.body,
panelSelector: '> section',
directionThreshold: 50,
delay: 0,
duration: 300,
easing: t => t,
};
export default class PanelSnap {
constructor(options) {
this.options = {
...defaultOptions,
...options,
};
if (this.options.container.dataset.panelsnapId) {
throw new Error('PanelSnap is already initialised on this container, aborting.');
}
this.container = this.options.container;
this.scrollContainer = getScrollingElement(this.container);
this.scrollEventContainer = getScrollEventContainer(this.container);
INSTANCE_COUNTER += 1;
this.instanceIndex = INSTANCE_COUNTER;
this.container.dataset.panelsnapId = this.instanceIndex;
const panelQuery = `[data-panelsnap-id="${this.instanceIndex}"] ${this.options.panelSelector}`;
this.panelList = Array.from(document.querySelectorAll(panelQuery));
this.events = [];
this.isEnabled = true;
this.isInteracting = false;
this.scrollTimeout = null;
this.animation = null;
this.currentScrollOffset = {
top: this.scrollContainer.scrollTop,
left: this.scrollContainer.scrollLeft,
};
this.targetScrollOffset = {
top: 0,
left: 0,
};
this.scrollEventContainer.addEventListener('keydown', this.onInteractStart.bind(this), passiveIsSupported && { passive: true });
this.scrollEventContainer.addEventListener('keyup', this.onInteractStop.bind(this), passiveIsSupported && { passive: true });
this.scrollEventContainer.addEventListener('mousedown', this.onInteractStart.bind(this), passiveIsSupported && { passive: true });
this.scrollEventContainer.addEventListener('mouseup', this.onInteractStop.bind(this), passiveIsSupported && { passive: true });
this.scrollEventContainer.addEventListener('touchstart', this.onInteractStart.bind(this), passiveIsSupported && { passive: true });
this.scrollEventContainer.addEventListener('touchend', this.onInteractStop.bind(this), passiveIsSupported && { passive: true });
this.scrollEventContainer.addEventListener('scroll', this.onScroll.bind(this), passiveIsSupported && { passive: true });
}
enable() {
this.isEnabled = true;
}
disable() {
this.isEnabled = false;
}
on(name, handler) {
const currentHandlers = this.events[name] || [];
this.events[name] = [...currentHandlers, handler];
}
off(name, handler) {
const currentHandlers = this.events[name] || [];
this.events[name] = currentHandlers.filter(h => h !== handler);
}
emit(name, value) {
const currentHandlers = this.events[name] || [];
currentHandlers.forEach(h => h.call(this, value));
}
onInteractStart() {
this.stopAnimation();
this.isInteracting = true;
}
onInteractStop() {
this.isInteracting = false;
this.onScroll();
}
onInteract() {
clearTimeout(this.scrollTimeout);
this.stopAnimation();
}
onScroll() {
clearTimeout(this.scrollTimeout);
if (this.isInteracting || this.animation) {
return;
}
if (
this.currentScrollOffset.top === this.scrollContainer.scrollTop
&& this.currentScrollOffset.left === this.scrollContainer.scrollLeft
) {
return;
}
this.stopAnimation();
this.scrollTimeout = setTimeout(this.findSnapTarget.bind(this), 50 + this.options.delay);
}
findSnapTarget() {
const deltaY = this.scrollContainer.scrollTop - this.currentScrollOffset.top;
const deltaX = this.scrollContainer.scrollLeft - this.currentScrollOffset.left;
this.currentScrollOffset = {
top: this.scrollContainer.scrollTop,
left: this.scrollContainer.scrollLeft,
};
const panelsInViewport = getElementsInContainerViewport(this.container, this.panelList);
if (panelsInViewport.length === 0) {
throw new Error('PanelSnap could not find a snappable panel, aborting.');
}
if (panelsInViewport.length > 1) {
if (
Math.abs(deltaY) < this.options.directionThreshold
&& Math.abs(deltaX) < this.options.directionThreshold
&& this.activePanel
) {
this.snapToPanel(this.activePanel, deltaY > 0, deltaX > 0);
return;
}
const targetIndex = deltaY > 0 || deltaX > 0 ? 1 : panelsInViewport.length - 2;
this.snapToPanel(panelsInViewport[targetIndex], deltaY < 0, deltaX < 0);
return;
}
const visiblePanel = panelsInViewport[0];
if (elementFillsContainer(this.container, visiblePanel)) {
this.activatePanel(visiblePanel);
return;
}
// TODO: Only one partial panel in viewport, add support for space between panels?
// eslint-disable-next-line no-console
console.error('PanelSnap does not support space between panels, snapping back.');
this.snapToPanel(visiblePanel, deltaY > 0, deltaX > 0);
}
snapToPanel(panel, toBottom = false, toRight = false) {
this.activatePanel(panel);
if (!this.isEnabled) {
return;
}
if (this.animation) {
this.animation.stop();
}
this.targetScrollOffset = getTargetScrollOffset(this.container, panel, toBottom, toRight);
this.animation = new Tweezer({
start: 0,
end: TWEEN_MAX_VALUE,
duration: this.options.duration,
});
this.animation.on('tick', this.animationTick.bind(this));
this.animation.on('done', () => {
this.emit('snapStop', panel);
this.clearAnimation();
});
this.emit('snapStart', panel);
this.animation.begin();
}
animationTick(value) {
const scrollTopDelta = this.targetScrollOffset.top - this.currentScrollOffset.top;
const scrollTop = this.currentScrollOffset.top + (scrollTopDelta * value / TWEEN_MAX_VALUE);
this.scrollContainer.scrollTop = scrollTop;
const scrollLeftDelta = this.targetScrollOffset.left - this.currentScrollOffset.left;
const scrollLeft = this.currentScrollOffset.left + (scrollLeftDelta * value / TWEEN_MAX_VALUE);
this.scrollContainer.scrollLeft = scrollLeft;
}
stopAnimation() {
if (!this.animation) {
return;
}
this.animation.stop();
this.clearAnimation();
}
clearAnimation() {
this.currentScrollOffset = {
top: this.scrollContainer.scrollTop,
left: this.scrollContainer.scrollLeft,
};
this.targetScrollOffset = {
top: 0,
left: 0,
};
this.animation = null;
}
activatePanel(panel) {
if (this.activePanel === panel) {
return;
}
this.emit('activatePanel', panel);
this.activePanel = panel;
}
}