@twistedandy/better-carousel 1.0.0 → 1.1.1
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/README.md +12 -11
- package/dist/better-carousel.js +569 -363
- package/dist/better-carousel.min.js +1 -1
- package/package.json +1 -1
- package/src/scripts/autoplay.js +40 -12
- package/src/scripts/carousel.js +76 -18
- package/src/scripts/geometry.js +42 -44
- package/src/scripts/input.js +91 -53
- package/src/scripts/layout.js +76 -29
- package/src/scripts/markup.js +55 -7
- package/src/scripts/motion.js +3 -2
- package/src/scripts/navigation.js +200 -209
- package/src/types/better-carousel.d.ts +232 -232
package/src/scripts/geometry.js
CHANGED
|
@@ -8,6 +8,9 @@
|
|
|
8
8
|
|
|
9
9
|
import { cloneCount } from './loop.js';
|
|
10
10
|
|
|
11
|
+
// Short names of the Math functions, which the minified file can make shorter
|
|
12
|
+
export const { abs, floor, round } = Math;
|
|
13
|
+
|
|
11
14
|
/**
|
|
12
15
|
* Keep a value between two limits.
|
|
13
16
|
*
|
|
@@ -60,6 +63,9 @@ export function bounds(slides) {
|
|
|
60
63
|
max = 0;
|
|
61
64
|
|
|
62
65
|
for (const slide of slides) {
|
|
66
|
+
if (slide.hidden) {
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
63
69
|
start = Math.min(start, slide.start);
|
|
64
70
|
end = Math.max(end, slide.end);
|
|
65
71
|
max = Math.max(max, slide.end - slide.start);
|
|
@@ -69,14 +75,14 @@ export function bounds(slides) {
|
|
|
69
75
|
}
|
|
70
76
|
|
|
71
77
|
/**
|
|
72
|
-
* Put the slides that start at the same position (1 px or less) into one column.
|
|
78
|
+
* Put the slides that start at the same position (1 px or less) into one column. A hidden slide is in no column.
|
|
73
79
|
* In a flex row, one column has one slide. In a grid, a column can have more slides.
|
|
74
80
|
*
|
|
75
81
|
* @param {Array<{start: number, end: number}>} slides
|
|
76
82
|
* @returns {Array<{start: number, end: number}>} The columns, sorted by start.
|
|
77
83
|
*/
|
|
78
84
|
export function columns(slides) {
|
|
79
|
-
const order = slides.map((slide, index) => index).sort((a, b) => slides[a].start - slides[b].start || a - b),
|
|
85
|
+
const order = slides.map((slide, index) => index).filter((index) => !slides[index].hidden).sort((a, b) => slides[a].start - slides[b].start || a - b),
|
|
80
86
|
list = [];
|
|
81
87
|
|
|
82
88
|
let column;
|
|
@@ -123,6 +129,29 @@ export function groups(list, view, count) {
|
|
|
123
129
|
return result;
|
|
124
130
|
}
|
|
125
131
|
|
|
132
|
+
/**
|
|
133
|
+
* Find the item with the lowest score.
|
|
134
|
+
*
|
|
135
|
+
* @param {Array} list
|
|
136
|
+
* @param {(item: *, index: number) => number} score Infinity excludes an item.
|
|
137
|
+
* @param {number} [margin] A score must be lower than the best score by more than this value. Then the lower index wins a tie.
|
|
138
|
+
* @returns {number} The index, or -1 if all items are excluded.
|
|
139
|
+
*/
|
|
140
|
+
function lowest(list, score, margin = 0) {
|
|
141
|
+
let best = -1,
|
|
142
|
+
gap = Infinity;
|
|
143
|
+
|
|
144
|
+
list.forEach((item, index) => {
|
|
145
|
+
const value = score(item, index);
|
|
146
|
+
if (value < gap - margin) {
|
|
147
|
+
gap = value;
|
|
148
|
+
best = index;
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
return best;
|
|
153
|
+
}
|
|
154
|
+
|
|
126
155
|
/**
|
|
127
156
|
* Get the distance between two locations. In loop mode, use the shortest distance modulo the period.
|
|
128
157
|
*
|
|
@@ -133,7 +162,7 @@ export function groups(list, view, count) {
|
|
|
133
162
|
*/
|
|
134
163
|
export function distance(a, b, period) {
|
|
135
164
|
const value = a - b;
|
|
136
|
-
return period ? value -
|
|
165
|
+
return period ? value - round(value / period) * period : value;
|
|
137
166
|
}
|
|
138
167
|
|
|
139
168
|
/**
|
|
@@ -167,8 +196,8 @@ export function snaps({ slides, view, align, slidesPerSnap, containScroll, perio
|
|
|
167
196
|
}
|
|
168
197
|
|
|
169
198
|
if (period || containScroll !== 'keepSnaps') {
|
|
170
|
-
list = list.filter((location, index) => !index ||
|
|
171
|
-
if (period && list.length > 1 &&
|
|
199
|
+
list = list.filter((location, index) => !index || abs(distance(location, list[index - 1], period)) > 1);
|
|
200
|
+
if (period && list.length > 1 && abs(distance(list[0], list[list.length - 1], period)) <= 1) {
|
|
172
201
|
list.pop();
|
|
173
202
|
}
|
|
174
203
|
}
|
|
@@ -202,7 +231,7 @@ export function snaps({ slides, view, align, slidesPerSnap, containScroll, perio
|
|
|
202
231
|
/**
|
|
203
232
|
* Find the slide whose anchor is nearest to the align point at a location.
|
|
204
233
|
* The anchor is the start for 'start', the center for 'center' and the end for 'end'.
|
|
205
|
-
* If two slides have the same distance, the lower index wins.
|
|
234
|
+
* If two slides have the same distance, the lower index wins. A hidden slide is never the active slide.
|
|
206
235
|
*
|
|
207
236
|
* @param {Array<{start: number, end: number}>} slides
|
|
208
237
|
* @param {number} location
|
|
@@ -214,18 +243,7 @@ export function snaps({ slides, view, align, slidesPerSnap, containScroll, perio
|
|
|
214
243
|
* @returns {number} The slide index, or -1 if the range is empty.
|
|
215
244
|
*/
|
|
216
245
|
export function activeSlide(slides, location, view, align, period, from = 0, to = slides.length) {
|
|
217
|
-
|
|
218
|
-
gap = Infinity;
|
|
219
|
-
|
|
220
|
-
slides.forEach((slide, index) => {
|
|
221
|
-
const value = Math.abs(distance(slide.start + location, alignOffset(align, view, slide.end - slide.start, index), period));
|
|
222
|
-
if (index >= from && index < to && value < gap - 0.5) {
|
|
223
|
-
gap = value;
|
|
224
|
-
best = index;
|
|
225
|
-
}
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
return best;
|
|
246
|
+
return lowest(slides, (slide, index) => index >= from && index < to && !slide.hidden ? abs(distance(slide.start + location, alignOffset(align, view, slide.end - slide.start, index), period)) : Infinity, 0.5);
|
|
229
247
|
}
|
|
230
248
|
|
|
231
249
|
/**
|
|
@@ -259,18 +277,7 @@ export function snapOfSlide(list, slides, index, view, align, period) {
|
|
|
259
277
|
* @returns {number} The snap index.
|
|
260
278
|
*/
|
|
261
279
|
export function nearestSnap(list, location, period) {
|
|
262
|
-
|
|
263
|
-
gap = Infinity;
|
|
264
|
-
|
|
265
|
-
list.forEach((snap, index) => {
|
|
266
|
-
const value = Math.abs(distance(snap.location, location, period));
|
|
267
|
-
if (value < gap) {
|
|
268
|
-
gap = value;
|
|
269
|
-
best = index;
|
|
270
|
-
}
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
return best;
|
|
280
|
+
return Math.max(0, lowest(list, (snap) => abs(distance(snap.location, location, period))));
|
|
274
281
|
}
|
|
275
282
|
|
|
276
283
|
/**
|
|
@@ -292,7 +299,7 @@ export function targetLocation(target, location, period, direction = 0) {
|
|
|
292
299
|
}
|
|
293
300
|
|
|
294
301
|
if (direction < 0) {
|
|
295
|
-
return target + (
|
|
302
|
+
return target + (floor((location + 0.5 - target) / period) + 1) * period;
|
|
296
303
|
}
|
|
297
304
|
|
|
298
305
|
return location + distance(target, location, period);
|
|
@@ -308,7 +315,7 @@ export function targetLocation(target, location, period, direction = 0) {
|
|
|
308
315
|
export function progress(location, { min, max, period }) {
|
|
309
316
|
if (period) {
|
|
310
317
|
const value = (max - location) / period;
|
|
311
|
-
return value -
|
|
318
|
+
return value - floor(value);
|
|
312
319
|
}
|
|
313
320
|
return max > min ? clamp((max - location) / (max - min), 0, 1) : 0;
|
|
314
321
|
}
|
|
@@ -331,7 +338,7 @@ export function inView(slides, location, view, threshold, period) {
|
|
|
331
338
|
start = period ? distance(slide.start + location + size / 2, view / 2, period) + view / 2 - size / 2 : slide.start + location,
|
|
332
339
|
visible = Math.min(start + size, view) - Math.max(start, 0);
|
|
333
340
|
|
|
334
|
-
if (visible > 0 && visible >= threshold * size - 0.5) {
|
|
341
|
+
if (!slide.hidden && visible > 0 && visible >= threshold * size - 0.5) {
|
|
335
342
|
result.push(index);
|
|
336
343
|
}
|
|
337
344
|
});
|
|
@@ -392,7 +399,7 @@ export function compute(layout, rects, area, count, options) {
|
|
|
392
399
|
const geo = { ...layout, ...rects, area, fit, count, period, min: 0, max: 0 };
|
|
393
400
|
|
|
394
401
|
if (fit) {
|
|
395
|
-
geo.snaps = [{ location: 0, slide: activeSlide(rects.slides, 0, rects.view, layout.align) }];
|
|
402
|
+
geo.snaps = [{ location: 0, slide: Math.max(0, activeSlide(rects.slides, 0, rects.view, layout.align)) }];
|
|
396
403
|
} else {
|
|
397
404
|
Object.assign(geo, snaps({ ...rects, align: layout.align, slidesPerSnap: options.slidesPerSnap, containScroll: options.containScroll, period }));
|
|
398
405
|
}
|
|
@@ -414,16 +421,7 @@ export function compute(layout, rects, area, count, options) {
|
|
|
414
421
|
* @returns {number} The snap index.
|
|
415
422
|
*/
|
|
416
423
|
export function physicalSnap(geo, index, location) {
|
|
417
|
-
|
|
418
|
-
gap = Infinity;
|
|
419
|
-
|
|
420
|
-
geo.snaps.forEach((snap, item) => {
|
|
421
|
-
const value = Math.abs(distance(snap.location, location, geo.period));
|
|
422
|
-
if (geo.map[item] === index && value < gap) {
|
|
423
|
-
gap = value;
|
|
424
|
-
best = item;
|
|
425
|
-
}
|
|
426
|
-
});
|
|
424
|
+
const best = lowest(geo.snaps, (snap, item) => geo.map[item] === index ? abs(distance(snap.location, location, geo.period)) : Infinity);
|
|
427
425
|
|
|
428
426
|
return best < 0 ? clamp(index, 0, geo.snaps.length - 1) : best;
|
|
429
427
|
}
|
package/src/scripts/input.js
CHANGED
|
@@ -4,12 +4,16 @@
|
|
|
4
4
|
* Each feature takes the context of a carousel (see carousel.js). It returns nothing if its option is off, or an object with remove().
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import { clamp, inView, nearestSnap, snapIndex, targetLocation } from './geometry.js';
|
|
7
|
+
import { abs, clamp, inView, nearestSnap, snapIndex, targetLocation } from './geometry.js';
|
|
8
|
+
import { bind } from './markup.js';
|
|
8
9
|
import { EASE, GLIDE } from './motion.js';
|
|
9
10
|
|
|
10
11
|
// The targets that keep their own pointer behavior
|
|
11
12
|
const fields = 'input,textarea,select,[contenteditable]';
|
|
12
13
|
|
|
14
|
+
// The pointerdown events that a carousel took for a drag
|
|
15
|
+
const claimed = new WeakSet();
|
|
16
|
+
|
|
13
17
|
// The duration of the motion for one wheel event, in ms
|
|
14
18
|
const WHEEL = 180;
|
|
15
19
|
|
|
@@ -26,10 +30,10 @@ const MOMENTUM = 325;
|
|
|
26
30
|
* @returns {number}
|
|
27
31
|
*/
|
|
28
32
|
export function releaseDuration(path, velocity, curve, fallback) {
|
|
29
|
-
if (Math.sign(path) !== Math.sign(velocity) ||
|
|
33
|
+
if (Math.sign(path) !== Math.sign(velocity) || abs(velocity) < 0.05) {
|
|
30
34
|
return fallback;
|
|
31
35
|
}
|
|
32
|
-
return clamp(
|
|
36
|
+
return clamp(abs(path) * curve[1] / curve[0] / abs(velocity), 200, 1200);
|
|
33
37
|
}
|
|
34
38
|
|
|
35
39
|
/**
|
|
@@ -94,9 +98,9 @@ export function releaseTarget({ location, from, velocity, current, snaps, min, m
|
|
|
94
98
|
|
|
95
99
|
let direction = 0;
|
|
96
100
|
|
|
97
|
-
if (
|
|
101
|
+
if (abs(velocity) > 0.3) {
|
|
98
102
|
direction = velocity < 0 ? 1 : -1;
|
|
99
|
-
} else if (
|
|
103
|
+
} else if (abs(moved) > 20) {
|
|
100
104
|
direction = moved < 0 ? 1 : -1;
|
|
101
105
|
}
|
|
102
106
|
|
|
@@ -131,27 +135,31 @@ export function drag(context) {
|
|
|
131
135
|
// The selected snap at the drag start
|
|
132
136
|
snap = 0;
|
|
133
137
|
|
|
134
|
-
const listen = (
|
|
138
|
+
const listen = bind(window, [['pointermove', handle], ['pointerup', handle], ['pointercancel', handle]]);
|
|
135
139
|
|
|
136
140
|
function down(event) {
|
|
137
141
|
block = false;
|
|
138
142
|
|
|
139
143
|
const geo = context.geo();
|
|
140
144
|
|
|
141
|
-
|
|
145
|
+
// In nested carousels, the inner carousel gets the press first and claims it
|
|
146
|
+
if (pointer !== null || event.button || !geo || geo.fit || claimed.has(event) || event.target.closest(fields)) {
|
|
142
147
|
return;
|
|
143
148
|
}
|
|
144
149
|
|
|
150
|
+
claimed.add(event);
|
|
151
|
+
|
|
145
152
|
// Stop the text selection and the native image drag
|
|
146
153
|
if (event.pointerType === 'mouse') {
|
|
147
154
|
event.preventDefault();
|
|
148
155
|
}
|
|
149
156
|
|
|
150
157
|
pointer = event.pointerId;
|
|
151
|
-
|
|
158
|
+
// In a parent with a CSS scale, one px of the pointer is not one px of the carousel
|
|
159
|
+
axis = { x: geo.axis === 'x', sign: geo.sign, scale: geo.scale };
|
|
152
160
|
start = [event.clientX, event.clientY];
|
|
153
161
|
samples = [];
|
|
154
|
-
listen(
|
|
162
|
+
listen(true);
|
|
155
163
|
}
|
|
156
164
|
|
|
157
165
|
function handle(event) {
|
|
@@ -167,17 +175,17 @@ export function drag(context) {
|
|
|
167
175
|
return end(event.type === 'pointerup');
|
|
168
176
|
}
|
|
169
177
|
|
|
170
|
-
const dx = event.clientX - start[0],
|
|
171
|
-
dy = event.clientY - start[1],
|
|
178
|
+
const dx = (event.clientX - start[0]) / axis.scale,
|
|
179
|
+
dy = (event.clientY - start[1]) / axis.scale,
|
|
172
180
|
main = axis.x ? dx * axis.sign : dy,
|
|
173
181
|
cross = axis.x ? dy : dx;
|
|
174
182
|
|
|
175
183
|
if (!context.dragging()) {
|
|
176
184
|
// A touch move across the axis scrolls the page
|
|
177
|
-
if (event.pointerType !== 'mouse' &&
|
|
185
|
+
if (event.pointerType !== 'mouse' && abs(cross) > abs(main)) {
|
|
178
186
|
return end();
|
|
179
187
|
}
|
|
180
|
-
if (
|
|
188
|
+
if (abs(main) < options.dragThreshold) {
|
|
181
189
|
return;
|
|
182
190
|
}
|
|
183
191
|
// Stop the motion, and start the drag at the location where the carousel is now
|
|
@@ -207,7 +215,7 @@ export function drag(context) {
|
|
|
207
215
|
* @param {boolean} [up] true for a pointerup. A pointercancel releases with no velocity.
|
|
208
216
|
*/
|
|
209
217
|
function end(up) {
|
|
210
|
-
listen(
|
|
218
|
+
listen();
|
|
211
219
|
pointer = null;
|
|
212
220
|
|
|
213
221
|
if (!context.dragging()) {
|
|
@@ -238,22 +246,43 @@ export function drag(context) {
|
|
|
238
246
|
// Stop a text selection during a drag. A "user-select" rule on the dragging class does the same, but its change paints all layers of the carousel.
|
|
239
247
|
const select = (event) => context.dragging() && event.preventDefault();
|
|
240
248
|
|
|
241
|
-
viewport
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
viewport.addEventListener('selectstart', select);
|
|
249
|
+
const own = bind(viewport, [['pointerdown', down], ['click', click, true], ['dragstart', prevent], ['selectstart', select]]);
|
|
250
|
+
|
|
251
|
+
own(true);
|
|
245
252
|
|
|
246
253
|
return {
|
|
247
254
|
remove() {
|
|
248
|
-
listen(
|
|
249
|
-
|
|
250
|
-
viewport.removeEventListener('click', click, true);
|
|
251
|
-
viewport.removeEventListener('dragstart', prevent);
|
|
252
|
-
viewport.removeEventListener('selectstart', select);
|
|
255
|
+
listen();
|
|
256
|
+
own();
|
|
253
257
|
}
|
|
254
258
|
};
|
|
255
259
|
}
|
|
256
260
|
|
|
261
|
+
/**
|
|
262
|
+
* Find an element between a node and the viewport that can scroll in a direction.
|
|
263
|
+
*
|
|
264
|
+
* @param {Element} node The target of the wheel event.
|
|
265
|
+
* @param {Element} viewport
|
|
266
|
+
* @param {string} axis 'x' or 'y'.
|
|
267
|
+
* @param {number} delta The wheel delta on the axis.
|
|
268
|
+
* @returns {boolean}
|
|
269
|
+
*/
|
|
270
|
+
function scrolls(node, viewport, axis, delta) {
|
|
271
|
+
const [size, client, position, overflow] = axis === 'x' ? ['scrollWidth', 'clientWidth', 'scrollLeft', 'overflowX'] : ['scrollHeight', 'clientHeight', 'scrollTop', 'overflowY'];
|
|
272
|
+
|
|
273
|
+
for (; node && node !== viewport; node = node.parentElement) {
|
|
274
|
+
// Read the style only for an element with more content than space
|
|
275
|
+
if (node[size] > node[client] && /auto|scroll/.test(getComputedStyle(node)[overflow])) {
|
|
276
|
+
const at = abs(node[position]);
|
|
277
|
+
if (delta > 0 ? at + node[client] < node[size] - 1 : at > 0) {
|
|
278
|
+
return true;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return false;
|
|
284
|
+
}
|
|
285
|
+
|
|
257
286
|
/**
|
|
258
287
|
* Make the wheel input of a viewport: in a vertical carousel, and with wheel: 'both' also in a horizontal carousel. The carousel follows
|
|
259
288
|
* the wheel distance with a short motion on the compositor. When the wheel stops, the carousel goes to a snap. At the first and the last
|
|
@@ -271,9 +300,12 @@ export function wheel(context) {
|
|
|
271
300
|
return;
|
|
272
301
|
}
|
|
273
302
|
|
|
274
|
-
// The time of the last wheel event, and
|
|
303
|
+
// The time and the delta size of the last wheel event, and true if the gesture moved the carousel
|
|
275
304
|
let time = 0,
|
|
276
|
-
|
|
305
|
+
last = 0,
|
|
306
|
+
held = false,
|
|
307
|
+
// true while a gesture scrolls an element in a slide
|
|
308
|
+
inner = false,
|
|
277
309
|
// The timer that finds the end of a gesture
|
|
278
310
|
timer = 0,
|
|
279
311
|
// The selected snap at the gesture start, and the direction of the last step
|
|
@@ -282,7 +314,7 @@ export function wheel(context) {
|
|
|
282
314
|
|
|
283
315
|
function onWheel(event) {
|
|
284
316
|
const geo = context.geo(),
|
|
285
|
-
side = geo.axis === 'x' &&
|
|
317
|
+
side = geo.axis === 'x' && abs(event.deltaX) > abs(event.deltaY),
|
|
286
318
|
delta = side ? event.deltaX * geo.sign : event.deltaY;
|
|
287
319
|
|
|
288
320
|
if (event.ctrlKey || !delta || context.dragging()) {
|
|
@@ -291,7 +323,21 @@ export function wheel(context) {
|
|
|
291
323
|
|
|
292
324
|
const now = event.timeStamp,
|
|
293
325
|
gesture = now - time < 200,
|
|
294
|
-
|
|
326
|
+
size = abs(delta),
|
|
327
|
+
// A delta that does not grow is the inertia of the gesture, not a new push
|
|
328
|
+
inertia = gesture && size <= last;
|
|
329
|
+
|
|
330
|
+
time = now;
|
|
331
|
+
last = size;
|
|
332
|
+
held &&= gesture;
|
|
333
|
+
|
|
334
|
+
// An element in a slide that can scroll in the wheel direction scrolls, also for the rest of its gesture
|
|
335
|
+
inner = (gesture && inner) || scrolls(event.target, viewport, side ? 'x' : 'y', side ? event.deltaX : delta);
|
|
336
|
+
if (inner) {
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const base = context.location(),
|
|
295
341
|
path = delta * (event.deltaMode === 1 ? 40 : event.deltaMode ? geo.view : 1);
|
|
296
342
|
|
|
297
343
|
let target = base - path;
|
|
@@ -299,11 +345,9 @@ export function wheel(context) {
|
|
|
299
345
|
target = clamp(target, geo.min, geo.max);
|
|
300
346
|
}
|
|
301
347
|
|
|
302
|
-
time = now;
|
|
303
|
-
|
|
304
348
|
// At the end, the page scrolls. The inertia of a gesture that moved the carousel does not scroll the page.
|
|
305
|
-
if (
|
|
306
|
-
if (
|
|
349
|
+
if (abs(target - base) < 0.5) {
|
|
350
|
+
if (held && inertia) {
|
|
307
351
|
event.preventDefault();
|
|
308
352
|
}
|
|
309
353
|
return;
|
|
@@ -315,7 +359,7 @@ export function wheel(context) {
|
|
|
315
359
|
if (!timer) {
|
|
316
360
|
snap = context.selected();
|
|
317
361
|
}
|
|
318
|
-
|
|
362
|
+
held = true;
|
|
319
363
|
direction = path > 0 ? 1 : -1;
|
|
320
364
|
context.move(nearestSnap(geo.snaps, target, geo.period), target, WHEEL, EASE);
|
|
321
365
|
|
|
@@ -331,23 +375,23 @@ export function wheel(context) {
|
|
|
331
375
|
const geo = context.geo(),
|
|
332
376
|
at = context.current(),
|
|
333
377
|
index = nearestSnap(geo.snaps, at, geo.period),
|
|
334
|
-
away =
|
|
378
|
+
away = abs(targetLocation(geo.snaps[index].location, at, geo.period) - at) > 10;
|
|
335
379
|
|
|
336
380
|
context.step(index, index === snap && away ? direction : 0);
|
|
337
381
|
}
|
|
338
382
|
|
|
339
383
|
// A wheel listener that is not passive makes the browser wait for the main thread before it scrolls the page.
|
|
340
384
|
// Add it only where the wheel moves the carousel.
|
|
341
|
-
|
|
342
|
-
const geo = context.geo();
|
|
343
|
-
viewport[!geo.fit && (geo.axis === 'y' || options.wheel === 'both') ? 'addEventListener' : 'removeEventListener']('wheel', onWheel, { passive: false });
|
|
344
|
-
}
|
|
385
|
+
const listen = bind(viewport, [['wheel', onWheel, { passive: false }]]);
|
|
345
386
|
|
|
346
|
-
['init', 'reinit', 'resize'].forEach((name) => context.on(name,
|
|
387
|
+
['init', 'reinit', 'resize'].forEach((name) => context.on(name, () => {
|
|
388
|
+
const geo = context.geo();
|
|
389
|
+
listen(!geo.fit && (geo.axis === 'y' || options.wheel === 'both'));
|
|
390
|
+
}));
|
|
347
391
|
|
|
348
392
|
return {
|
|
349
393
|
remove() {
|
|
350
|
-
|
|
394
|
+
listen();
|
|
351
395
|
clearTimeout(timer);
|
|
352
396
|
}
|
|
353
397
|
};
|
|
@@ -380,13 +424,11 @@ export function keys(context) {
|
|
|
380
424
|
}
|
|
381
425
|
}
|
|
382
426
|
|
|
383
|
-
root
|
|
427
|
+
const listen = bind(root, [['keydown', onKey]]);
|
|
384
428
|
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
}
|
|
389
|
-
};
|
|
429
|
+
listen(true);
|
|
430
|
+
|
|
431
|
+
return { remove: listen };
|
|
390
432
|
}
|
|
391
433
|
|
|
392
434
|
/**
|
|
@@ -420,13 +462,9 @@ export function focus(context) {
|
|
|
420
462
|
context.emit('slidefocus');
|
|
421
463
|
}
|
|
422
464
|
|
|
423
|
-
container
|
|
424
|
-
container.addEventListener('focusin', onFocus);
|
|
465
|
+
const listen = bind(container, [['pointerdown', press], ['focusin', onFocus]]);
|
|
425
466
|
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
container.removeEventListener('focusin', onFocus);
|
|
430
|
-
}
|
|
431
|
-
};
|
|
467
|
+
listen(true);
|
|
468
|
+
|
|
469
|
+
return { remove: listen };
|
|
432
470
|
}
|
package/src/scripts/layout.js
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
* The reads run in the ResizeObserver callback, when the layout is clean.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
import { abs, round } from './geometry.js';
|
|
8
|
+
|
|
7
9
|
/**
|
|
8
10
|
* Read the axis, the alignment, the direction and the gap from the computed style of the container.
|
|
9
11
|
*
|
|
@@ -45,54 +47,75 @@ export function readLayout(container, options) {
|
|
|
45
47
|
}
|
|
46
48
|
|
|
47
49
|
/**
|
|
48
|
-
* Read the slide boxes on the main axis, relative to the viewport
|
|
50
|
+
* Read the slide boxes on the main axis, relative to the inner edge of the viewport padding at location 0. The snaps align to this edge.
|
|
51
|
+
* The values are in the px of the carousel: in a parent with a CSS scale, such as a modal window that opens with a scale animation, the
|
|
52
|
+
* values do not change with a scale or a rotation of a parent.
|
|
53
|
+
* A slide with no box, such as a slide with display: none, is hidden. It gets no snap, and it stays at the end of the slide before it.
|
|
49
54
|
*
|
|
50
55
|
* @param {HTMLElement} viewport
|
|
51
56
|
* @param {HTMLElement[]} nodes All slides, clones included.
|
|
52
57
|
* @param {object} layout The result of readLayout().
|
|
53
58
|
* @param {{axis: string, container: number, slides: number[]}} written The translate values that the library wrote, in px on the screen.
|
|
54
|
-
* @returns {{view: number, slides: Array<{start: number, end: number, cross: number}>, gap: number}}
|
|
55
|
-
* gap is the loop gap: the space between the last slide and the next copy of the first slide.
|
|
59
|
+
* @returns {{view: number, slides: Array<{start: number, end: number, cross: number, hidden: boolean}>, gap: number, scale: number, pad: {start: number, end: number}}}
|
|
60
|
+
* view is the viewport size without the padding. gap is the loop gap: the space between the last slide and the next copy of the first slide.
|
|
61
|
+
* scale is the screen px of one px of the carousel. pad is the viewport padding at the start and at the end.
|
|
56
62
|
*/
|
|
57
63
|
export function readRects(viewport, nodes, layout, written) {
|
|
58
64
|
const box = viewport.getBoundingClientRect(),
|
|
59
65
|
x = layout.axis === 'x',
|
|
60
66
|
sign = layout.sign,
|
|
61
67
|
known = written.axis === layout.axis,
|
|
62
|
-
shift = known ? written.container : 0
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
68
|
+
shift = known ? written.container : 0,
|
|
69
|
+
width = viewport.offsetWidth,
|
|
70
|
+
height = viewport.offsetHeight,
|
|
71
|
+
// offsetWidth is a whole number: a difference of less than 1 px is not a transform
|
|
72
|
+
transformed = width && height && (abs(box.width - width) >= 1 || abs(box.height - height) >= 1),
|
|
73
|
+
// A scale s and a rotation by an angle a give a box of s(w cos a + h sin a) by s(w sin a + h cos a). The main axis gets s cos a.
|
|
74
|
+
scale = !transformed ? 1 : abs(width - height) > 1 ? (width * box.width - height * box.height) / (width * width - height * height) : box.width / width,
|
|
75
|
+
view = !transformed ? (x ? box.width : box.height) : x ? width : height,
|
|
76
|
+
style = getComputedStyle(viewport),
|
|
77
|
+
side = x ? (layout.rtl ? ['Right', 'Left'] : ['Left', 'Right']) : ['Top', 'Bottom'],
|
|
78
|
+
pad = { start: parseFloat(style['padding' + side[0]]) || 0, end: parseFloat(style['padding' + side[1]]) || 0 };
|
|
66
79
|
|
|
67
80
|
const slides = nodes.map((node, index) => {
|
|
68
81
|
const rect = node.getBoundingClientRect(),
|
|
69
82
|
offset = sign * (shift + (known && written.slides[index] || 0)),
|
|
70
|
-
start = (x ? (layout.rtl ? box.right - rect.right : rect.left - box.left) : rect.top - box.top) - offset,
|
|
71
|
-
size = x ? rect.width : rect.height;
|
|
83
|
+
start = (x ? (layout.rtl ? box.right - rect.right : rect.left - box.left) : rect.top - box.top) / scale - pad.start - offset,
|
|
84
|
+
size = (x ? rect.width : rect.height) / scale;
|
|
72
85
|
|
|
73
|
-
return { start, end: start + size, cross: x ? rect.height : rect.width };
|
|
86
|
+
return { start, end: start + size, cross: (x ? rect.height : rect.width) / scale, hidden: !rect.width && !rect.height };
|
|
74
87
|
});
|
|
75
88
|
|
|
89
|
+
// A hidden slide goes to the end of the visible slide before it. The first and the last visible slides give the margins of the loop gap.
|
|
90
|
+
let edge = slides.find((slide) => !slide.hidden)?.start || 0,
|
|
91
|
+
first = -1,
|
|
92
|
+
last = -1,
|
|
93
|
+
gap = layout.gap;
|
|
94
|
+
|
|
76
95
|
slides.forEach((slide, index) => {
|
|
77
|
-
if (slide.
|
|
96
|
+
if (slide.hidden) {
|
|
97
|
+
slide.start = slide.end = edge;
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
edge = slide.end;
|
|
101
|
+
if (first < 0 || slide.start < slides[first].start) {
|
|
78
102
|
first = index;
|
|
79
103
|
}
|
|
80
|
-
if (slide.end > slides[last].end) {
|
|
104
|
+
if (last < 0 || slide.end > slides[last].end) {
|
|
81
105
|
last = index;
|
|
82
106
|
}
|
|
83
107
|
});
|
|
84
108
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
if (slides.length) {
|
|
88
|
-
const side = x ? (layout.rtl ? ['Right', 'Left'] : ['Left', 'Right']) : ['Top', 'Bottom'];
|
|
109
|
+
if (first > -1) {
|
|
89
110
|
gap += (parseFloat(getComputedStyle(nodes[first])['margin' + side[0]]) || 0) + (parseFloat(getComputedStyle(nodes[last])['margin' + side[1]]) || 0);
|
|
90
111
|
}
|
|
91
112
|
|
|
92
113
|
return {
|
|
93
|
-
view:
|
|
114
|
+
view: view - pad.start - pad.end,
|
|
94
115
|
slides,
|
|
95
|
-
gap
|
|
116
|
+
gap,
|
|
117
|
+
scale,
|
|
118
|
+
pad
|
|
96
119
|
};
|
|
97
120
|
}
|
|
98
121
|
|
|
@@ -107,46 +130,70 @@ export function windowSize(axis) {
|
|
|
107
130
|
}
|
|
108
131
|
|
|
109
132
|
/**
|
|
110
|
-
* Get the part of the main axis where the loop must show slides, relative to the viewport
|
|
111
|
-
* It is the viewport. With clones: 'fill', it is the window, so that the slides fill it when the viewport overflow shows.
|
|
133
|
+
* Get the part of the main axis where the loop must show slides, relative to the inner edge of the viewport padding.
|
|
134
|
+
* It is the viewport with its padding. With clones: 'fill', it is the window, so that the slides fill it when the viewport overflow shows.
|
|
112
135
|
*
|
|
113
136
|
* @param {HTMLElement} viewport
|
|
114
137
|
* @param {{axis: string, rtl: boolean}} layout
|
|
115
|
-
* @param {number} view The viewport size.
|
|
138
|
+
* @param {number} view The viewport size without the padding.
|
|
139
|
+
* @param {{start: number, end: number}} pad The viewport padding.
|
|
116
140
|
* @param {number} [size] The window size on the main axis, for clones: 'fill'. Without it, the area is the viewport.
|
|
117
141
|
* @returns {{start: number, size: number}}
|
|
118
142
|
*/
|
|
119
|
-
export function visibleArea(viewport, layout, view, size) {
|
|
143
|
+
export function visibleArea(viewport, layout, view, pad, size) {
|
|
120
144
|
if (!size) {
|
|
121
|
-
return { start:
|
|
145
|
+
return { start: -pad.start, size: pad.start + view + pad.end };
|
|
122
146
|
}
|
|
123
147
|
|
|
124
148
|
const box = viewport.getBoundingClientRect(),
|
|
125
|
-
before = Math.max(0,
|
|
149
|
+
before = Math.max(0, round(layout.axis === 'x' ? (layout.rtl ? size - box.right : box.left) : box.top)) + pad.start;
|
|
126
150
|
|
|
127
|
-
return { start: -before, size: before + Math.max(view,
|
|
151
|
+
return { start: -before, size: before + Math.max(view + pad.end, round(size - before)) };
|
|
128
152
|
}
|
|
129
153
|
|
|
130
154
|
// One ResizeObserver for all carousels. Each callback first reads the layout of all carousels, then writes. In this way, the reads of one carousel do not
|
|
131
155
|
// come after the writes of another carousel, and the browser does one layout for all of them.
|
|
132
|
-
const owners = new WeakMap()
|
|
156
|
+
const owners = new WeakMap(),
|
|
157
|
+
// The elements of each owner, and the last size of each element
|
|
158
|
+
watched = new WeakMap(),
|
|
159
|
+
sizes = new WeakMap();
|
|
133
160
|
|
|
134
161
|
let resizer;
|
|
135
162
|
|
|
136
163
|
/**
|
|
137
164
|
* Watch the size of elements. The ResizeObserver callback calls owner.read() for each owner, and then owner.write() for each owner whose read() returned true.
|
|
138
|
-
* An entry for which owner.skip() returns true does not count.
|
|
165
|
+
* An entry for which owner.skip() returns true does not count. An entry with the same size as the last entry of its element does not count:
|
|
166
|
+
* a new watch of an element after a write sends such an entry. observeSize() clears the last size, so its first entry always counts.
|
|
139
167
|
*
|
|
140
168
|
* @param {Element[]} nodes
|
|
141
169
|
* @param {{read: () => boolean, write: () => void, skip: (entry: ResizeObserverEntry) => boolean}} [owner] Without an owner, stop the watch.
|
|
142
170
|
*/
|
|
143
171
|
export function observeSize(nodes, owner) {
|
|
144
172
|
resizer ||= new ResizeObserver((entries) => {
|
|
145
|
-
const list = new Set(entries.filter((entry) =>
|
|
146
|
-
|
|
173
|
+
const list = new Set(entries.filter((entry) => {
|
|
174
|
+
const { inlineSize, blockSize } = entry.borderBoxSize[0],
|
|
175
|
+
size = inlineSize + ',' + blockSize,
|
|
176
|
+
same = sizes.get(entry.target) === size;
|
|
177
|
+
sizes.set(entry.target, size);
|
|
178
|
+
return !same && !owners.get(entry.target)?.skip(entry);
|
|
179
|
+
}).map((entry) => owners.get(entry.target)));
|
|
180
|
+
const changed = [...list].filter((item) => item && item.read());
|
|
181
|
+
changed.forEach((item) => item.write());
|
|
182
|
+
// A write can change the size of an element that the observer watches, for example the auto height or the clones. If the observer has
|
|
183
|
+
// a change to report after its callback, the browser sends the "ResizeObserver loop" error to window.onerror. Stop the watch of these
|
|
184
|
+
// elements, and watch them again in the next frame. Then the first callback measures again and finds no change.
|
|
185
|
+
if (changed.length) {
|
|
186
|
+
changed.forEach((item) => watched.get(item)?.forEach((node) => resizer.unobserve(node)));
|
|
187
|
+
requestAnimationFrame(() => changed.forEach((item) => watched.get(item)?.forEach((node) => owners.get(node) === item && resizer.observe(node))));
|
|
188
|
+
}
|
|
147
189
|
});
|
|
148
190
|
|
|
191
|
+
if (owner) {
|
|
192
|
+
watched.set(owner, nodes);
|
|
193
|
+
}
|
|
194
|
+
|
|
149
195
|
for (const node of nodes) {
|
|
196
|
+
sizes.delete(node);
|
|
150
197
|
if (owner) {
|
|
151
198
|
owners.set(node, owner);
|
|
152
199
|
resizer.observe(node);
|