@twistedandy/better-carousel 0.0.0-stage → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +839 -2
- package/dist/better-carousel.css +1 -0
- package/dist/better-carousel.js +3373 -0
- package/dist/better-carousel.min.js +1 -0
- package/package.json +55 -5
- package/src/index.js +49 -0
- package/src/scripts/autoplay.js +322 -0
- package/src/scripts/carousel.js +870 -0
- package/src/scripts/geometry.js +432 -0
- package/src/scripts/input.js +484 -0
- package/src/scripts/layout.js +258 -0
- package/src/scripts/loop.js +83 -0
- package/src/scripts/markup.js +288 -0
- package/src/scripts/motion.js +232 -0
- package/src/scripts/navigation.js +209 -0
- package/src/scripts/options.js +153 -0
- package/src/styles/better-carousel.scss +323 -0
- package/src/types/better-carousel.d.ts +309 -0
|
@@ -0,0 +1,870 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Carousel: its state, its API, the setup, the measure pass, the render and the motion.
|
|
3
|
+
*
|
|
4
|
+
* The features get their part of the carousel through the context object. Each feature is a function that takes the context.
|
|
5
|
+
* It returns nothing if its option is off, or an object with remove(). remove() removes all that the feature added.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { alignOffset, clamp, compute, inView, nearestSnap, physicalSnap, progress, snapIndex, snapOfSlide, snapPosition, targetLocation } from './geometry.js';
|
|
9
|
+
import { observeSize, readLayout, readRects, visibleArea, waitNear, windowSize } from './layout.js';
|
|
10
|
+
import { normalize, pathFits, placement } from './loop.js';
|
|
11
|
+
import { addClass, build, cleanStyle, copyContent, copySlides, labelSlides, readSlides, restore, setClass, setStyle } from './markup.js';
|
|
12
|
+
import { animator, EASE, snapDuration } from './motion.js';
|
|
13
|
+
import { breakpoints, classNames, merge, resolve } from './options.js';
|
|
14
|
+
|
|
15
|
+
// The instance of each root, viewport and container
|
|
16
|
+
const registry = new WeakMap();
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Get the carousel of a root, a viewport or a container.
|
|
20
|
+
*
|
|
21
|
+
* @param {Element} element
|
|
22
|
+
* @returns {object|undefined}
|
|
23
|
+
*/
|
|
24
|
+
export const find = (element) => registry.get(element);
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Make an event emitter. A handler gets (api, name), as in Embla.
|
|
28
|
+
* The handlers of the features run before the handlers of the page, and clear() removes them.
|
|
29
|
+
*
|
|
30
|
+
* @param {object} api
|
|
31
|
+
* @returns {{on: Function, off: Function, listen: Function, clear: Function, emit: Function, has: Function}}
|
|
32
|
+
*/
|
|
33
|
+
function emitter(api) {
|
|
34
|
+
const user = {},
|
|
35
|
+
own = {},
|
|
36
|
+
add = (map, name, handler) => (map[name] ||= new Set()).add(handler);
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
on(name, handler) {
|
|
40
|
+
add(user, name, handler);
|
|
41
|
+
return api;
|
|
42
|
+
},
|
|
43
|
+
off(name, handler) {
|
|
44
|
+
user[name]?.delete(handler);
|
|
45
|
+
return api;
|
|
46
|
+
},
|
|
47
|
+
listen: (name, handler) => add(own, name, handler),
|
|
48
|
+
clear: () => Object.values(own).forEach((set) => set.clear()),
|
|
49
|
+
emit(name) {
|
|
50
|
+
own[name]?.forEach((handler) => handler(api, name));
|
|
51
|
+
user[name]?.forEach((handler) => handler(api, name));
|
|
52
|
+
},
|
|
53
|
+
has: (name) => !!user[name]?.size
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Make a carousel.
|
|
59
|
+
*
|
|
60
|
+
* @param {HTMLElement} element The container of the slides, or its viewport.
|
|
61
|
+
* @param {object} userOptions
|
|
62
|
+
* @param {object} defaults The site defaults.
|
|
63
|
+
* @param {Array<(context: object) => ({remove: () => void}|undefined)>} features
|
|
64
|
+
* @returns {object} The carousel API.
|
|
65
|
+
*/
|
|
66
|
+
export default function carousel(element, userOptions, defaults, features) {
|
|
67
|
+
const found = registry.get(element);
|
|
68
|
+
|
|
69
|
+
if (found) {
|
|
70
|
+
return found;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// The options and the markup
|
|
74
|
+
let options,
|
|
75
|
+
cls,
|
|
76
|
+
root,
|
|
77
|
+
viewport,
|
|
78
|
+
container,
|
|
79
|
+
slides = [],
|
|
80
|
+
clones = [],
|
|
81
|
+
nodes = [],
|
|
82
|
+
// The features that are on
|
|
83
|
+
parts = [];
|
|
84
|
+
|
|
85
|
+
// The observers and the measure state
|
|
86
|
+
let observed = [],
|
|
87
|
+
mutationObserver,
|
|
88
|
+
// Watches the content of the slides while the carousel has clones
|
|
89
|
+
copyObserver,
|
|
90
|
+
media = null,
|
|
91
|
+
ready = false,
|
|
92
|
+
started = false,
|
|
93
|
+
changed = false,
|
|
94
|
+
signature = [],
|
|
95
|
+
viewWidth = 0,
|
|
96
|
+
pending = -1,
|
|
97
|
+
lastWindow = 0;
|
|
98
|
+
|
|
99
|
+
// The geometry, the location and the selection
|
|
100
|
+
let geo = null,
|
|
101
|
+
location = 0,
|
|
102
|
+
selected = 0,
|
|
103
|
+
previous = 0,
|
|
104
|
+
requested = -1,
|
|
105
|
+
followed = -1,
|
|
106
|
+
visible = [],
|
|
107
|
+
written = { axis: '', container: 0, slides: [] };
|
|
108
|
+
|
|
109
|
+
// The motion and the drag. A free motion, such as the auto scroll, has no snap: it selects the nearest snap when it stops.
|
|
110
|
+
let motion,
|
|
111
|
+
dragging = false,
|
|
112
|
+
free = false;
|
|
113
|
+
|
|
114
|
+
const api = {
|
|
115
|
+
goToSnap: (index, instant) => go(physical(index), instant),
|
|
116
|
+
// Before the first measure pass, keep the step in the selected snap
|
|
117
|
+
goToNext: (instant) => geo ? api.canGoToNext() && step(selected, 1, instant) : go(selected + 1, instant),
|
|
118
|
+
goToPrev: (instant) => geo ? api.canGoToPrev() && step(selected, -1, instant) : go(selected - 1, instant),
|
|
119
|
+
goToSlide: (index, instant) => go(physical(api.snapOfSlide(index)), instant, index),
|
|
120
|
+
canGoToNext: () => !!geo && !geo.fit && (!!geo.period || selected < geo.snaps.length - 1),
|
|
121
|
+
canGoToPrev: () => !!geo && !geo.fit && (!!geo.period || selected > 0),
|
|
122
|
+
selectedSnap: () => logical(selected),
|
|
123
|
+
previousSnap: () => logical(previous),
|
|
124
|
+
selectedSlide,
|
|
125
|
+
snapOfSlide: (index) => geo ? logical(snapOfSlide(geo.snaps, geo.slides, index, geo.view, geo.align, geo.period)) : index,
|
|
126
|
+
snapProgress: () => geo ? geo.logical.map((index) => progress(geo.snaps[index].location, geo)) : [],
|
|
127
|
+
progress: () => geo ? progress(current(), geo) : 0,
|
|
128
|
+
slideNodes: () => slides.slice(),
|
|
129
|
+
slidesInView: () => visible.slice(),
|
|
130
|
+
rootNode: () => root,
|
|
131
|
+
viewportNode: () => viewport,
|
|
132
|
+
containerNode: () => container,
|
|
133
|
+
isFit: () => !geo || geo.fit,
|
|
134
|
+
options: () => options,
|
|
135
|
+
reinit,
|
|
136
|
+
destroy: () => {
|
|
137
|
+
teardown(false);
|
|
138
|
+
events.emit('destroy');
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const events = emitter(api),
|
|
143
|
+
owner = { read: measure, write: measured, skip: heightOnly },
|
|
144
|
+
// The states that the "progress" and the "offset" callbacks get. Each call gets the same object, so a frame makes no garbage.
|
|
145
|
+
whole = { progress: 0, snap: 0 },
|
|
146
|
+
each = { node: null, index: 0, offset: 0, distance: 0, size: 0, visible: 0 },
|
|
147
|
+
/**
|
|
148
|
+
* What the features share: the API, the events, and the state and the actions of the carousel.
|
|
149
|
+
* setup() adds root, viewport, container, cls and options. reinit() adds playing: the play state of the autoplay and the auto scroll before a breakpoint change.
|
|
150
|
+
*/
|
|
151
|
+
context = {
|
|
152
|
+
api,
|
|
153
|
+
emit: events.emit,
|
|
154
|
+
// Listen to an event until the teardown
|
|
155
|
+
on: events.listen,
|
|
156
|
+
|
|
157
|
+
// The state
|
|
158
|
+
geo: () => geo,
|
|
159
|
+
nodes: () => nodes,
|
|
160
|
+
selected: () => selected,
|
|
161
|
+
dragging: () => dragging,
|
|
162
|
+
// The location at rest or at the end of the motion. During a free motion, the location now.
|
|
163
|
+
location: () => free ? current() : location,
|
|
164
|
+
current,
|
|
165
|
+
|
|
166
|
+
// The actions
|
|
167
|
+
hold,
|
|
168
|
+
halt,
|
|
169
|
+
step,
|
|
170
|
+
scroll,
|
|
171
|
+
// Select a snap and move to a location
|
|
172
|
+
move: (index, target, duration, curve) => {
|
|
173
|
+
requested = -1;
|
|
174
|
+
move(index, target, duration, curve);
|
|
175
|
+
},
|
|
176
|
+
// Start or end a drag, and set the "dragging" class. The start is an interaction, as goToSnap() is.
|
|
177
|
+
drag: (on) => {
|
|
178
|
+
dragging = on;
|
|
179
|
+
setClass(root, cls.dragging, on);
|
|
180
|
+
if (on) {
|
|
181
|
+
events.emit('interact');
|
|
182
|
+
events.emit('dragstart');
|
|
183
|
+
} else {
|
|
184
|
+
events.emit('dragend');
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
// Set the slide that the sync option follows
|
|
188
|
+
follow: (index) => {
|
|
189
|
+
followed = index;
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
api.on = events.on;
|
|
194
|
+
api.off = events.off;
|
|
195
|
+
|
|
196
|
+
// The options of the first setup. The lazy option: a carousel that is not near the screen waits. A carousel near the screen starts now:
|
|
197
|
+
// a start after the first paint adds the arrows and the dots in view, and this moves the page (CLS).
|
|
198
|
+
let resolved = resolve(element, defaults, userOptions),
|
|
199
|
+
waiting = resolved.lazy ? waitNear(element, resolved.lazy, reinit) : null;
|
|
200
|
+
|
|
201
|
+
if (waiting) {
|
|
202
|
+
registry.set(element, api);
|
|
203
|
+
} else {
|
|
204
|
+
setup();
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return api;
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Read the options, add the markup, start the features and the observers. The first ResizeObserver callback measures the layout.
|
|
211
|
+
*/
|
|
212
|
+
function setup() {
|
|
213
|
+
media = breakpoints(resolved || resolve(element, defaults, userOptions), () => reinit());
|
|
214
|
+
resolved = null;
|
|
215
|
+
options = media.options;
|
|
216
|
+
|
|
217
|
+
if (!options.active) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
cls = classNames(options.classes, options.state);
|
|
222
|
+
({ root, viewport, container } = build(element, options, cls));
|
|
223
|
+
|
|
224
|
+
for (const node of [root, viewport, container]) {
|
|
225
|
+
registry.set(node, api);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
slides = readSlides(container, options.slides);
|
|
229
|
+
nodes = slides;
|
|
230
|
+
selected = options.startSnap;
|
|
231
|
+
|
|
232
|
+
observe(true);
|
|
233
|
+
|
|
234
|
+
Object.assign(context, { root, viewport, container, cls, options });
|
|
235
|
+
|
|
236
|
+
parts = features.map((feature) => feature(context)).filter(Boolean);
|
|
237
|
+
motion = animator(container, css, frame, settle, (options.progress || options.offset) && ((at) => track(at, motion.shifts(geo.sign) || written.slides)));
|
|
238
|
+
|
|
239
|
+
if (options.autoHeight) {
|
|
240
|
+
addClass(root, cls.autoHeight);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
viewport.addEventListener('scroll', onScroll);
|
|
244
|
+
if (options.loop && options.clones === 'fill') {
|
|
245
|
+
addEventListener('resize', onWindow);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (options.slideChanges) {
|
|
249
|
+
mutationObserver = new MutationObserver(onMutation);
|
|
250
|
+
mutationObserver.observe(container, { childList: true });
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Remove what setup() added.
|
|
256
|
+
*
|
|
257
|
+
* @param {boolean} keep Keep the wrapper markup for a new setup().
|
|
258
|
+
*/
|
|
259
|
+
function teardown(keep) {
|
|
260
|
+
waiting?.();
|
|
261
|
+
waiting = null;
|
|
262
|
+
registry.delete(element);
|
|
263
|
+
observe(false);
|
|
264
|
+
mutationObserver?.disconnect();
|
|
265
|
+
media?.remove();
|
|
266
|
+
media = null;
|
|
267
|
+
|
|
268
|
+
if (!cls) {
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
motion.stop();
|
|
273
|
+
parts.forEach((part) => part.remove());
|
|
274
|
+
events.clear();
|
|
275
|
+
parts = [];
|
|
276
|
+
followed = -1;
|
|
277
|
+
dragging = free = false;
|
|
278
|
+
setClones(0);
|
|
279
|
+
viewport.removeEventListener('scroll', onScroll);
|
|
280
|
+
removeEventListener('resize', onWindow);
|
|
281
|
+
|
|
282
|
+
for (const node of nodes) {
|
|
283
|
+
setStyle(node, 'translate', '');
|
|
284
|
+
node.style.removeProperty('--better-offset');
|
|
285
|
+
setClass(node, cls.selected, false);
|
|
286
|
+
setClass(node, cls.inView, false);
|
|
287
|
+
cleanStyle(node);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
setStyle(container, 'translate', '');
|
|
291
|
+
setStyle(container, 'willChange', '');
|
|
292
|
+
setStyle(viewport, 'touchAction', '');
|
|
293
|
+
setStyle(viewport, 'height', '');
|
|
294
|
+
root.style.removeProperty('--better-progress');
|
|
295
|
+
|
|
296
|
+
for (const name of ['horizontal', 'vertical', 'fit', 'dragging']) {
|
|
297
|
+
setClass(root, cls[name], false);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
for (const node of [root, viewport, container]) {
|
|
301
|
+
cleanStyle(node);
|
|
302
|
+
registry.delete(node);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
restore(root, viewport, container, slides, keep);
|
|
306
|
+
|
|
307
|
+
cls = geo = null;
|
|
308
|
+
ready = false;
|
|
309
|
+
location = 0;
|
|
310
|
+
visible = [];
|
|
311
|
+
signature = [];
|
|
312
|
+
viewWidth = 0;
|
|
313
|
+
written = { axis: '', container: 0, slides: [] };
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Merge new options and set up the carousel again. The selected slide stays.
|
|
318
|
+
*
|
|
319
|
+
* @param {object} [newOptions]
|
|
320
|
+
*/
|
|
321
|
+
function reinit(newOptions) {
|
|
322
|
+
if (newOptions) {
|
|
323
|
+
userOptions = merge(userOptions, newOptions);
|
|
324
|
+
resolved = null;
|
|
325
|
+
}
|
|
326
|
+
const keep = geo ? selectedSlide() : requested;
|
|
327
|
+
// After a breakpoint change, the new autoplay and auto scroll keep the play state. New options start them again.
|
|
328
|
+
context.playing = newOptions ? null : { autoplay: api.autoplay?.isPlaying(), autoScroll: api.autoScroll?.isPlaying() };
|
|
329
|
+
teardown(true);
|
|
330
|
+
setup();
|
|
331
|
+
requested = keep;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Put the clones after the last slide.
|
|
336
|
+
*
|
|
337
|
+
* @param {number} count The number of copies of all slides.
|
|
338
|
+
*/
|
|
339
|
+
function setClones(count) {
|
|
340
|
+
if (clones.length === count * slides.length) {
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
clones.forEach((node) => node.remove());
|
|
345
|
+
clones = copySlides(slides, count, cls.clone);
|
|
346
|
+
container.append(...clones);
|
|
347
|
+
nodes = slides.concat(clones);
|
|
348
|
+
written.slides.length = slides.length;
|
|
349
|
+
mutationObserver?.takeRecords();
|
|
350
|
+
|
|
351
|
+
copyObserver?.disconnect();
|
|
352
|
+
if (clones.length) {
|
|
353
|
+
copyObserver ||= new MutationObserver(onCopy);
|
|
354
|
+
slides.forEach((node) => copyObserver.observe(node, { childList: true, characterData: true, subtree: true, attributeFilter: ['class', 'src', 'srcset', 'href', 'value'] }));
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Copy a changed slide into its clones. The carousel writes the classes of the slides, so a change of a slide's own attributes does not count.
|
|
360
|
+
*
|
|
361
|
+
* @param {MutationRecord[]} records
|
|
362
|
+
*/
|
|
363
|
+
function onCopy(records) {
|
|
364
|
+
const changed = new Set(records.filter((record) => record.type !== 'attributes' || !slides.includes(record.target)).map(({ target }) => slides.find((node) => node.contains(target))));
|
|
365
|
+
|
|
366
|
+
changed.forEach((node) => {
|
|
367
|
+
for (let index = slides.indexOf(node); node && index < clones.length; index += slides.length) {
|
|
368
|
+
copyContent(clones[index], node);
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Watch the size of the viewport, the container and the slides.
|
|
375
|
+
*
|
|
376
|
+
* @param {boolean} on false stops the watch.
|
|
377
|
+
*/
|
|
378
|
+
function observe(on) {
|
|
379
|
+
observeSize(observed);
|
|
380
|
+
observed = on ? [viewport, container, ...nodes] : [];
|
|
381
|
+
observeSize(observed, owner);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Find a resize of the viewport that changed only its height in a horizontal carousel with autoHeight.
|
|
386
|
+
* autoHeight sets this height, and its transition changes it on each frame, so it needs no measure pass.
|
|
387
|
+
*
|
|
388
|
+
* @param {ResizeObserverEntry} entry
|
|
389
|
+
* @returns {boolean}
|
|
390
|
+
*/
|
|
391
|
+
function heightOnly({ target, contentRect: { width } }) {
|
|
392
|
+
if (target !== viewport) {
|
|
393
|
+
return false;
|
|
394
|
+
}
|
|
395
|
+
const same = width === viewWidth;
|
|
396
|
+
viewWidth = width;
|
|
397
|
+
return same && options.autoHeight && geo?.axis === 'x';
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// Keep the selected slide. If the page removed it, select the slide that is now at its index, or the last slide.
|
|
401
|
+
function onMutation() {
|
|
402
|
+
const old = ready ? selectedSlide() : -1,
|
|
403
|
+
node = slides[old];
|
|
404
|
+
slides = readSlides(container, options.slides);
|
|
405
|
+
if (old > -1) {
|
|
406
|
+
const index = slides.indexOf(node);
|
|
407
|
+
requested = index > -1 ? index : Math.min(old, slides.length - 1);
|
|
408
|
+
}
|
|
409
|
+
setClones(0);
|
|
410
|
+
nodes = slides;
|
|
411
|
+
changed = true;
|
|
412
|
+
observe(true);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// The original slide index of a slide or a clone
|
|
416
|
+
function original(index) {
|
|
417
|
+
return index % slides.length;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// The logical snap of a snap. They are different only when the carousel has clones.
|
|
421
|
+
function logical(index) {
|
|
422
|
+
return geo ? geo.map[index] ?? 0 : index;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// The snap of a logical snap that is nearest to the current location
|
|
426
|
+
function physical(index) {
|
|
427
|
+
return geo ? physicalSnap(geo, index, current()) : index;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function selectedSlide() {
|
|
431
|
+
// A carousel with the sync option selects the slide of the other carousel, also after a drag
|
|
432
|
+
if (followed > -1) {
|
|
433
|
+
return followed;
|
|
434
|
+
}
|
|
435
|
+
if (!geo) {
|
|
436
|
+
return Math.max(requested, 0);
|
|
437
|
+
}
|
|
438
|
+
return requested >= 0 && api.snapOfSlide(requested) === logical(selected) ? requested : original(geo.snaps[selected].slide);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
// The location now, also during a motion
|
|
442
|
+
function current() {
|
|
443
|
+
return motion.current() ?? location;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Read the layout and compute the geometry. This function does not write to the DOM.
|
|
448
|
+
*
|
|
449
|
+
* @returns {boolean} false if nothing changed.
|
|
450
|
+
*/
|
|
451
|
+
function measure() {
|
|
452
|
+
const sign = geo?.sign || 1,
|
|
453
|
+
at = motion.current(),
|
|
454
|
+
layout = readLayout(container, options),
|
|
455
|
+
// The translate values that show now: the written values, or the values of a motion
|
|
456
|
+
rects = readRects(viewport, nodes, layout, at === null ? written : { axis: written.axis, container: sign * at, slides: motion.shifts(sign) || written.slides });
|
|
457
|
+
|
|
458
|
+
// With clones: 'fill', the loop fills the window
|
|
459
|
+
lastWindow = options.loop && options.clones === 'fill' ? windowSize(layout.axis) : 0;
|
|
460
|
+
|
|
461
|
+
const area = visibleArea(viewport, layout, rects.view, rects.pad, lastWindow),
|
|
462
|
+
values = [layout.axis, layout.rtl, typeof layout.align === 'string' && layout.align, rects.view, rects.gap, area.start, area.size, ...rects.slides.flatMap((slide) => [slide.start, slide.end, slide.cross])];
|
|
463
|
+
|
|
464
|
+
// A change of less than 0.5 px is not a change. During a motion, the rects have small errors, because the browser and bezier() round the easing differently.
|
|
465
|
+
if (ready && values.length === signature.length && values.every((value, index) => value === signature[index] || Math.abs(value - signature[index]) < 0.5)) {
|
|
466
|
+
return false;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
signature = values;
|
|
470
|
+
|
|
471
|
+
// After a change of the slides, onMutation() gives the slide to keep
|
|
472
|
+
const keep = ready && !changed ? selectedSlide() : requested,
|
|
473
|
+
next = compute(layout, rects, area, slides.length, options);
|
|
474
|
+
|
|
475
|
+
if (!next.snaps) {
|
|
476
|
+
pending = next.copies;
|
|
477
|
+
return true;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
geo = next;
|
|
481
|
+
|
|
482
|
+
const total = geo.snaps.length;
|
|
483
|
+
|
|
484
|
+
// Before the first measure pass, selected is the logical snap of the startSnap option, of goToSnap() or of goToNext().
|
|
485
|
+
// In loop mode, a step before the first snap goes to the last snap.
|
|
486
|
+
const count = geo.logical.length;
|
|
487
|
+
selected = keep >= 0 ? physical(api.snapOfSlide(keep)) : Math.min(total - 1, ready ? selected : physical(geo.period ? (selected % count + count) % count : Math.max(selected, 0)));
|
|
488
|
+
previous = Math.min(previous, total - 1);
|
|
489
|
+
requested = keep;
|
|
490
|
+
if (!dragging) {
|
|
491
|
+
location = targetLocation(geo.snaps[selected].location, location, geo.period);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
return true;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// A change of the window size can change the visible area, and the viewport size can stay the same.
|
|
498
|
+
// Mobile browsers send a resize when the address bar shows or hides. Measure only if the size on the main axis changes.
|
|
499
|
+
function onWindow() {
|
|
500
|
+
if (geo && windowSize(geo.axis) !== lastWindow && measure()) {
|
|
501
|
+
measured();
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* Write the result of a measure pass and emit the events.
|
|
507
|
+
*/
|
|
508
|
+
function measured() {
|
|
509
|
+
if (pending >= 0) {
|
|
510
|
+
setClones(pending);
|
|
511
|
+
pending = -1;
|
|
512
|
+
signature = [];
|
|
513
|
+
observe(true);
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
// A free motion stops where it is, and its feature starts it again on "resize"
|
|
518
|
+
if (free) {
|
|
519
|
+
halt();
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
// A motion goes on to the new location of its snap, for example when the selected slide changes its width. A drag keeps its location.
|
|
523
|
+
const at = motion.current();
|
|
524
|
+
if (at !== null && written.axis === geo.axis && !geo.fit && motion.retarget(location)) {
|
|
525
|
+
place(at, location);
|
|
526
|
+
render(true);
|
|
527
|
+
} else {
|
|
528
|
+
motion.stop();
|
|
529
|
+
render(dragging);
|
|
530
|
+
}
|
|
531
|
+
labelSlides(slides, options.labels.slide);
|
|
532
|
+
|
|
533
|
+
if (!options.resize) {
|
|
534
|
+
observe(false);
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
if (!ready) {
|
|
538
|
+
ready = true;
|
|
539
|
+
events.emit(started ? 'reinit' : 'init');
|
|
540
|
+
started = true;
|
|
541
|
+
} else {
|
|
542
|
+
events.emit('resize');
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
if (changed) {
|
|
546
|
+
changed = false;
|
|
547
|
+
events.emit('slideschanged');
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* Write the location, the classes, the touch action and the auto height.
|
|
553
|
+
*
|
|
554
|
+
* @param {boolean} [keep] Do not write the location, because a motion or a drag writes it.
|
|
555
|
+
*/
|
|
556
|
+
function render(keep) {
|
|
557
|
+
const x = geo.axis === 'x';
|
|
558
|
+
|
|
559
|
+
setClass(root, cls.horizontal, x);
|
|
560
|
+
setClass(root, cls.vertical, !x);
|
|
561
|
+
setClass(root, cls.fit, geo.fit);
|
|
562
|
+
setStyle(viewport, 'touchAction', geo.fit ? '' : x ? 'pan-y pinch-zoom' : 'pan-x pinch-zoom');
|
|
563
|
+
setStyle(container, 'willChange', geo.fit ? '' : 'translate');
|
|
564
|
+
|
|
565
|
+
if (!keep) {
|
|
566
|
+
write(geo.fit ? 0 : location);
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
// Auto height: the height of the tallest slide in view at the target location
|
|
570
|
+
if (options.autoHeight) {
|
|
571
|
+
setStyle(viewport, 'height', x ? Math.max(...inView(geo.slides, location, geo.view, 0, geo.period).map((index) => geo.slides[index].cross)) + 'px' : '');
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
const chosen = selectedSlide(),
|
|
575
|
+
list = [...new Set(inView(geo.slides, location, geo.view, options.inViewThreshold, geo.period).map(original))].sort((a, b) => a - b);
|
|
576
|
+
|
|
577
|
+
nodes.forEach((node, index) => setClass(node, cls.selected, original(index) === chosen));
|
|
578
|
+
|
|
579
|
+
if (list.join() !== visible.join()) {
|
|
580
|
+
visible = list;
|
|
581
|
+
nodes.forEach((node, index) => setClass(node, cls.inView, list.includes(original(index))));
|
|
582
|
+
events.emit('slidesinview');
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* Get the screen value of a location: rounded to device pixels, with the sign of the direction.
|
|
588
|
+
*
|
|
589
|
+
* @param {number} value
|
|
590
|
+
* @returns {number}
|
|
591
|
+
*/
|
|
592
|
+
function screen(value) {
|
|
593
|
+
const scale = devicePixelRatio || 1;
|
|
594
|
+
return Math.round(value * scale) / scale * geo.sign;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
* Get the CSS translate value of a screen value.
|
|
599
|
+
*
|
|
600
|
+
* @param {number} value px
|
|
601
|
+
* @returns {string}
|
|
602
|
+
*/
|
|
603
|
+
function translate(value) {
|
|
604
|
+
return value ? (geo.axis === 'x' ? value + 'px' : '0 ' + value + 'px') : '';
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
/**
|
|
608
|
+
* Write the location as the translate of the container, and give it to the "progress" and the "offset" options.
|
|
609
|
+
*
|
|
610
|
+
* @param {number} value
|
|
611
|
+
*/
|
|
612
|
+
function write(value) {
|
|
613
|
+
if (written.axis !== geo.axis) {
|
|
614
|
+
nodes.forEach((node) => setStyle(node, 'translate', ''));
|
|
615
|
+
written = { axis: geo.axis, container: 0, slides: [] };
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
written.container = screen(value);
|
|
619
|
+
setStyle(container, 'translate', translate(written.container));
|
|
620
|
+
place(value, value);
|
|
621
|
+
if (options.progress || options.offset) {
|
|
622
|
+
track(value, written.slides);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* Write the "progress" and the "offset" options for a location: the CSS variables, or the calls of the callbacks.
|
|
628
|
+
* The motion engine also calls this function on each frame of a motion on the compositor.
|
|
629
|
+
*
|
|
630
|
+
* @param {number} at The location.
|
|
631
|
+
* @param {number[]} shifts The loop shift of each slide on the screen, in px.
|
|
632
|
+
*/
|
|
633
|
+
function track(at, shifts) {
|
|
634
|
+
if (options.progress === true) {
|
|
635
|
+
root.style.setProperty('--better-progress', progress(at, geo));
|
|
636
|
+
} else if (options.progress) {
|
|
637
|
+
const value = snapPosition(geo.snaps, at, geo.period),
|
|
638
|
+
snap = Math.floor(value);
|
|
639
|
+
whole.progress = progress(at, geo);
|
|
640
|
+
whole.snap = logical(snap) + value - snap;
|
|
641
|
+
options.progress(whole, api);
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
if (options.offset) {
|
|
645
|
+
nodes.forEach((node, index) => {
|
|
646
|
+
const box = geo.slides[index],
|
|
647
|
+
size = box.end - box.start,
|
|
648
|
+
// The slide start on the screen, and its distance from its snap position
|
|
649
|
+
start = box.start + geo.sign * (shifts[index] || 0) + at,
|
|
650
|
+
delta = start - alignOffset(geo.align, geo.view, size, index),
|
|
651
|
+
offset = delta / (size + geo.gap);
|
|
652
|
+
|
|
653
|
+
if (options.offset === true) {
|
|
654
|
+
node.style.setProperty('--better-offset', offset);
|
|
655
|
+
} else {
|
|
656
|
+
each.node = node;
|
|
657
|
+
each.index = original(index);
|
|
658
|
+
each.offset = offset;
|
|
659
|
+
each.distance = geo.sign * delta;
|
|
660
|
+
each.size = size;
|
|
661
|
+
each.visible = clamp((Math.min(start + size, geo.view) - Math.max(start, 0)) / size, 0, 1) || 0;
|
|
662
|
+
options.offset(each, api);
|
|
663
|
+
}
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
/**
|
|
669
|
+
* Write the loop shift of each slide for a motion from one location to another.
|
|
670
|
+
*
|
|
671
|
+
* @param {number} from
|
|
672
|
+
* @param {number} to
|
|
673
|
+
*/
|
|
674
|
+
function place(from, to) {
|
|
675
|
+
const list = geo.period ? placement(geo.slides, from, to, geo.area.size, geo.period, geo.area.start) : [];
|
|
676
|
+
|
|
677
|
+
nodes.forEach((node, index) => {
|
|
678
|
+
const value = list.length ? screen(list[index] * geo.period) : 0;
|
|
679
|
+
if ((written.slides[index] || 0) !== value) {
|
|
680
|
+
written.slides[index] = value;
|
|
681
|
+
setStyle(node, 'translate', translate(value));
|
|
682
|
+
}
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
// The CSS translate value of a location
|
|
687
|
+
function css(value) {
|
|
688
|
+
return translate(screen(value)) || '0px';
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
/**
|
|
692
|
+
* Go to the snap after a snap in a direction. In loop mode, the first snap follows the last snap.
|
|
693
|
+
*
|
|
694
|
+
* @param {number} index The snap.
|
|
695
|
+
* @param {number} direction 1, -1, or 0 for the snap itself.
|
|
696
|
+
* @param {boolean} [instant]
|
|
697
|
+
*/
|
|
698
|
+
function step(index, direction, instant) {
|
|
699
|
+
go(snapIndex(index + direction, geo.snaps.length, geo.period), instant, -1, direction);
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
/**
|
|
703
|
+
* Go to a snap. This is an interaction: it stops the autoplay and the auto scroll, or makes them wait.
|
|
704
|
+
*
|
|
705
|
+
* @param {number} index The snap index. physical() and step() give a valid index.
|
|
706
|
+
* @param {boolean} [instant]
|
|
707
|
+
* @param {number} [slide] The slide index, for goToSlide().
|
|
708
|
+
* @param {number} [direction] 1 to go forward, -1 to go back, 0 for the shortest path in a loop.
|
|
709
|
+
*/
|
|
710
|
+
function go(index, instant, slide = -1, direction = 0) {
|
|
711
|
+
if (waiting) {
|
|
712
|
+
reinit();
|
|
713
|
+
}
|
|
714
|
+
events.emit('interact');
|
|
715
|
+
requested = slide;
|
|
716
|
+
if (!geo) {
|
|
717
|
+
selected = index;
|
|
718
|
+
return;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
const target = targetLocation(geo.snaps[index].location, current(), geo.period, direction),
|
|
722
|
+
span = Math.abs(index - selected),
|
|
723
|
+
count = geo.period ? Math.min(span, geo.snaps.length - span) : span;
|
|
724
|
+
|
|
725
|
+
move(index, target, instant ? 0 : snapDuration(options.duration, count), EASE);
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
* Select a snap and move to a location.
|
|
730
|
+
*
|
|
731
|
+
* @param {number} index The snap index.
|
|
732
|
+
* @param {number} target The location.
|
|
733
|
+
* @param {number} duration ms. With 0, the carousel jumps.
|
|
734
|
+
* @param {number[]} curve
|
|
735
|
+
*/
|
|
736
|
+
function move(index, target, duration, curve) {
|
|
737
|
+
const from = halt(),
|
|
738
|
+
picked = select(index),
|
|
739
|
+
jump = !duration || geo.fit || Math.abs(target - from) < 0.5;
|
|
740
|
+
|
|
741
|
+
location = target;
|
|
742
|
+
|
|
743
|
+
if (jump) {
|
|
744
|
+
render();
|
|
745
|
+
} else {
|
|
746
|
+
const raf = events.has('scroll') || !!geo.period && !pathFits(geo.slides, from, target, geo.area.size, geo.period);
|
|
747
|
+
render(true);
|
|
748
|
+
if (!raf) {
|
|
749
|
+
place(from, target);
|
|
750
|
+
}
|
|
751
|
+
motion.run(from, target, duration, curve, raf);
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
if (picked) {
|
|
755
|
+
events.emit('select');
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
if (jump) {
|
|
759
|
+
settle();
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
/**
|
|
764
|
+
* Move at a constant speed, as a free motion. With the loop, the motion goes on until it stops. Without the loop, it goes to the end.
|
|
765
|
+
* A free motion changes the classes only when it stops.
|
|
766
|
+
*
|
|
767
|
+
* @param {number} direction 1 for forward, -1 for back.
|
|
768
|
+
* @param {number} speed px/ms
|
|
769
|
+
*/
|
|
770
|
+
function scroll(direction, speed) {
|
|
771
|
+
const from = halt(),
|
|
772
|
+
to = direction > 0 ? geo.min : geo.max;
|
|
773
|
+
|
|
774
|
+
if (geo.period) {
|
|
775
|
+
free = true;
|
|
776
|
+
// Start in the first period, so that the location does not grow after each pause
|
|
777
|
+
location = normalize(from, geo.period);
|
|
778
|
+
motion.loop(location, direction, geo.period / speed, geo, nodes);
|
|
779
|
+
} else if (Math.abs(to - from) > 1) {
|
|
780
|
+
free = true;
|
|
781
|
+
location = to;
|
|
782
|
+
motion.run(from, to, Math.abs(to - from) / speed, [0, 0, 1, 1], events.has('scroll'));
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
/**
|
|
787
|
+
* Change the selected snap.
|
|
788
|
+
*
|
|
789
|
+
* @param {number} index
|
|
790
|
+
* @returns {boolean} true if the snap changed.
|
|
791
|
+
*/
|
|
792
|
+
function select(index) {
|
|
793
|
+
if (index === selected) {
|
|
794
|
+
return false;
|
|
795
|
+
}
|
|
796
|
+
previous = selected;
|
|
797
|
+
selected = index;
|
|
798
|
+
return true;
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
// Select the snap that is nearest to the location
|
|
802
|
+
function pick() {
|
|
803
|
+
if (select(nearestSnap(geo.snaps, location, geo.period))) {
|
|
804
|
+
render(true);
|
|
805
|
+
events.emit('select');
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
/**
|
|
810
|
+
* Write a location during a motion or a drag.
|
|
811
|
+
*
|
|
812
|
+
* @param {number} value
|
|
813
|
+
*/
|
|
814
|
+
function frame(value) {
|
|
815
|
+
write(value);
|
|
816
|
+
events.emit('scroll');
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
/**
|
|
820
|
+
* End a motion.
|
|
821
|
+
*/
|
|
822
|
+
function settle() {
|
|
823
|
+
if (free) {
|
|
824
|
+
free = false;
|
|
825
|
+
pick();
|
|
826
|
+
}
|
|
827
|
+
if (geo.period) {
|
|
828
|
+
location = normalize(location, geo.period);
|
|
829
|
+
}
|
|
830
|
+
render();
|
|
831
|
+
events.emit('settle');
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
/**
|
|
835
|
+
* Stop the motion where it is, and write that location. A free motion also selects the nearest snap.
|
|
836
|
+
*
|
|
837
|
+
* @returns {number} The location.
|
|
838
|
+
*/
|
|
839
|
+
function halt() {
|
|
840
|
+
const at = motion.stop();
|
|
841
|
+
if (at !== null) {
|
|
842
|
+
location = at;
|
|
843
|
+
write(at);
|
|
844
|
+
if (free) {
|
|
845
|
+
pick();
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
free = false;
|
|
849
|
+
return location;
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
/**
|
|
853
|
+
* Write a location without a motion, and select the snap that is nearest to it.
|
|
854
|
+
*
|
|
855
|
+
* @param {number} value
|
|
856
|
+
* @param {boolean} [moving] Emit "scroll", as during a motion.
|
|
857
|
+
*/
|
|
858
|
+
function hold(value, moving) {
|
|
859
|
+
location = value;
|
|
860
|
+
(moving ? frame : write)(value);
|
|
861
|
+
pick();
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
/**
|
|
865
|
+
* The browser scrolls the viewport to a focused element, although its overflow is hidden. Scroll it back.
|
|
866
|
+
*/
|
|
867
|
+
function onScroll() {
|
|
868
|
+
viewport.scrollLeft = viewport.scrollTop = 0;
|
|
869
|
+
}
|
|
870
|
+
}
|