ublo-lib 1.2.0 → 1.2.2
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.
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
|
-
import { forwardRef, memo, useEffect, useState, useRef, useCallback } from "react";
|
|
3
2
|
import classnames from "classnames";
|
|
4
3
|
import { useUbloContext } from "ublo/with-ublo";
|
|
5
4
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
6
5
|
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
7
6
|
const UNDRAGGABLE_TAGS = "img, a, button";
|
|
8
7
|
|
|
9
|
-
const isPlaceholder = path => path.indexOf("https://placehold.it/") !== -1;
|
|
10
|
-
|
|
11
8
|
const euclideanModulo = (a, b) => {
|
|
12
9
|
const m = a % b;
|
|
13
10
|
return m < 0 ? b < 0 ? m - b : m + b : m;
|
|
@@ -23,11 +20,11 @@ const show = section => {
|
|
|
23
20
|
};
|
|
24
21
|
|
|
25
22
|
function useInterval(callback, delay, current, dragging) {
|
|
26
|
-
const savedCallback = useRef();
|
|
27
|
-
useEffect(() => {
|
|
23
|
+
const savedCallback = React.useRef();
|
|
24
|
+
React.useEffect(() => {
|
|
28
25
|
savedCallback.current = callback;
|
|
29
26
|
}, [callback]);
|
|
30
|
-
useEffect(() => {
|
|
27
|
+
React.useEffect(() => {
|
|
31
28
|
function tick() {
|
|
32
29
|
savedCallback.current();
|
|
33
30
|
}
|
|
@@ -39,14 +36,6 @@ function useInterval(callback, delay, current, dragging) {
|
|
|
39
36
|
}, [current, delay, dragging]);
|
|
40
37
|
}
|
|
41
38
|
|
|
42
|
-
const Thumbnails = forwardRef((_, ref) => _jsx("div", {
|
|
43
|
-
className: "carousel--navigation",
|
|
44
|
-
children: _jsx("div", {
|
|
45
|
-
className: "carousel--thumbnails",
|
|
46
|
-
ref: ref
|
|
47
|
-
})
|
|
48
|
-
}));
|
|
49
|
-
|
|
50
39
|
const Dots = ({
|
|
51
40
|
count,
|
|
52
41
|
current,
|
|
@@ -77,45 +66,33 @@ const Dots = ({
|
|
|
77
66
|
const Carousel = ({
|
|
78
67
|
delay = 4000,
|
|
79
68
|
fade = false,
|
|
80
|
-
thumbnails,
|
|
81
69
|
controls,
|
|
82
70
|
dots,
|
|
83
71
|
alwaysShowControls,
|
|
84
72
|
allowDragOnDesktop,
|
|
85
73
|
children
|
|
86
74
|
}) => {
|
|
87
|
-
const carouselRef = useRef();
|
|
88
|
-
const thumbnailsRef = useRef();
|
|
75
|
+
const carouselRef = React.useRef();
|
|
89
76
|
const {
|
|
90
77
|
cmsMode
|
|
91
78
|
} = useUbloContext();
|
|
92
|
-
const [count, setCount] = useState(0);
|
|
93
|
-
const [current, setCurrent] = useState(0);
|
|
94
|
-
const [
|
|
95
|
-
const [
|
|
96
|
-
const [dragging, setDragging] = useState(0);
|
|
79
|
+
const [count, setCount] = React.useState(0);
|
|
80
|
+
const [current, setCurrent] = React.useState(0);
|
|
81
|
+
const [touchStartPosition, setTouchStartPosition] = React.useState();
|
|
82
|
+
const [dragging, setDragging] = React.useState(0);
|
|
97
83
|
const editing = cmsMode === "editing";
|
|
98
84
|
const draggingAllowed = !editing && allowDragOnDesktop;
|
|
99
85
|
const showNextArrow = current < count - 1;
|
|
100
86
|
const showPrevArrow = current !== 0;
|
|
101
|
-
const next = useCallback(e => {
|
|
87
|
+
const next = React.useCallback(e => {
|
|
102
88
|
e?.stopPropagation();
|
|
103
89
|
setCurrent(euclideanModulo(current + 1, count));
|
|
104
90
|
}, [current, count]);
|
|
105
|
-
const prev = useCallback(e => {
|
|
91
|
+
const prev = React.useCallback(e => {
|
|
106
92
|
e?.stopPropagation();
|
|
107
93
|
setCurrent(euclideanModulo(current - 1, count));
|
|
108
94
|
}, [current, count]);
|
|
109
95
|
|
|
110
|
-
const jump = e => {
|
|
111
|
-
e?.stopPropagation();
|
|
112
|
-
const {
|
|
113
|
-
target
|
|
114
|
-
} = e;
|
|
115
|
-
const index = target.closest(".carousel--thumbnail").dataset.index;
|
|
116
|
-
setCurrent(index);
|
|
117
|
-
};
|
|
118
|
-
|
|
119
96
|
const onTouchStart = e => {
|
|
120
97
|
if (fade) return;
|
|
121
98
|
const {
|
|
@@ -218,12 +195,12 @@ const Carousel = ({
|
|
|
218
195
|
setTouchStartPosition(undefined);
|
|
219
196
|
};
|
|
220
197
|
|
|
221
|
-
const onSectionCreated = useCallback(() => {
|
|
198
|
+
const onSectionCreated = React.useCallback(() => {
|
|
222
199
|
const carousel = carouselRef.current;
|
|
223
200
|
const sections = Array.from(carousel.querySelectorAll("section"));
|
|
224
201
|
setCount(sections.length);
|
|
225
202
|
}, []);
|
|
226
|
-
useEffect(() => {
|
|
203
|
+
React.useEffect(() => {
|
|
227
204
|
const zone = carouselRef.current?.querySelector(".cms");
|
|
228
205
|
|
|
229
206
|
if (zone) {
|
|
@@ -231,12 +208,11 @@ const Carousel = ({
|
|
|
231
208
|
return () => zone.removeEventListener("ubloSectionCreated", onSectionCreated);
|
|
232
209
|
}
|
|
233
210
|
}, [onSectionCreated]);
|
|
234
|
-
useEffect(() => {
|
|
211
|
+
React.useEffect(() => {
|
|
235
212
|
const carousel = carouselRef.current;
|
|
236
213
|
const inner = carousel.firstElementChild;
|
|
237
214
|
const sections = Array.from(carousel.querySelectorAll("section"));
|
|
238
215
|
const undraggableElements = Array.from(carousel.querySelectorAll(UNDRAGGABLE_TAGS));
|
|
239
|
-
const container = thumbnailsRef.current;
|
|
240
216
|
setCount(sections.length);
|
|
241
217
|
|
|
242
218
|
if (!editing) {
|
|
@@ -278,26 +254,12 @@ const Carousel = ({
|
|
|
278
254
|
});
|
|
279
255
|
if (fade) inner.style.removeProperty("transform");
|
|
280
256
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
sections.forEach((section, index) => {
|
|
284
|
-
const thumbnail = section.querySelector(".carousel--thumbnail").cloneNode(true);
|
|
285
|
-
|
|
286
|
-
if (thumbnail && !isPlaceholder(thumbnail.querySelector("img").src)) {
|
|
287
|
-
thumbnail.setAttribute("data-index", index);
|
|
288
|
-
thumbnail.addEventListener("click", jump);
|
|
289
|
-
container.appendChild(thumbnail);
|
|
290
|
-
}
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
setThumbnailsLoaded(true);
|
|
295
|
-
}, [children, current, editing, fade, count, thumbnails, thumbnailsLoaded]);
|
|
296
|
-
const onUndraggableElementClick = useCallback(e => {
|
|
257
|
+
}, [children, current, editing, fade, count]);
|
|
258
|
+
const onUndraggableElementClick = React.useCallback(e => {
|
|
297
259
|
e.preventDefault();
|
|
298
260
|
e.stopPropagation();
|
|
299
261
|
}, []);
|
|
300
|
-
useEffect(() => {
|
|
262
|
+
React.useEffect(() => {
|
|
301
263
|
const carousel = carouselRef.current;
|
|
302
264
|
const inner = carousel.firstElementChild;
|
|
303
265
|
const undraggableElements = Array.from(inner.querySelectorAll(UNDRAGGABLE_TAGS));
|
|
@@ -330,7 +292,6 @@ const Carousel = ({
|
|
|
330
292
|
...eventHandlers,
|
|
331
293
|
children: [children, (editing || controls && showPrevArrow || alwaysShowControls) && _jsx("button", {
|
|
332
294
|
className: "carousel--prev",
|
|
333
|
-
"aria-label": "Previous",
|
|
334
295
|
onClick: prev,
|
|
335
296
|
disabled: alwaysShowControls && !showPrevArrow,
|
|
336
297
|
children: _jsx("svg", {
|
|
@@ -342,7 +303,6 @@ const Carousel = ({
|
|
|
342
303
|
})
|
|
343
304
|
}), (editing || controls && showNextArrow || alwaysShowControls) && _jsx("button", {
|
|
344
305
|
className: "carousel--next",
|
|
345
|
-
"aria-label": "Next",
|
|
346
306
|
onClick: next,
|
|
347
307
|
disabled: alwaysShowControls && !showNextArrow,
|
|
348
308
|
children: _jsx("svg", {
|
|
@@ -352,8 +312,6 @@ const Carousel = ({
|
|
|
352
312
|
d: "M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6z"
|
|
353
313
|
})
|
|
354
314
|
})
|
|
355
|
-
}), thumbnails && _jsx(Thumbnails, {
|
|
356
|
-
ref: thumbnailsRef
|
|
357
315
|
}), dots && _jsx(Dots, {
|
|
358
316
|
count: count,
|
|
359
317
|
current: current,
|
|
@@ -362,4 +320,4 @@ const Carousel = ({
|
|
|
362
320
|
});
|
|
363
321
|
};
|
|
364
322
|
|
|
365
|
-
export default memo(Carousel);
|
|
323
|
+
export default React.memo(Carousel);
|