@cntrl-site/sdk-nextjs 1.11.16 → 1.11.18
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/cntrl-site-sdk-nextjs-1.9.125.tgz +0 -0
- package/lib/components/ImageLightbox/ImageLightbox.js +599 -0
- package/lib/components/StructuredBlockItem/StructuredImage/StructuredImage.js +61 -8
- package/lib/components/StructuredBlockItem/StructuredTable/StructuredTable.js +19 -6
- package/package.json +2 -2
- package/src/components/ImageLightbox/ImageLightbox.test.tsx +203 -0
- package/src/components/ImageLightbox/ImageLightbox.tsx +731 -0
- package/src/components/StructuredBlockItem/StructuredImage/StructuredImage.tsx +90 -18
- package/src/components/StructuredBlockItem/StructuredTable/StructuredTable.tsx +31 -16
- package/src/components/StructuredContent/StructuredContent.test.tsx +96 -7
- package/.idea/codeStyles/codeStyleConfig.xml +0 -5
- package/.idea/modules.xml +0 -8
- package/.idea/sdk-nextjs.iml +0 -12
- package/.idea/vcs.xml +0 -6
|
Binary file
|
|
@@ -0,0 +1,599 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ImageLightbox = void 0;
|
|
7
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
8
|
+
const sdk_1 = require("@cntrl-site/sdk");
|
|
9
|
+
const react_1 = require("react");
|
|
10
|
+
const react_dom_1 = require("react-dom");
|
|
11
|
+
const style_1 = __importDefault(require("styled-jsx/style"));
|
|
12
|
+
const useCntrlContext_1 = require("../../provider/useCntrlContext");
|
|
13
|
+
const vw_1 = require("../StructuredBlockItem/vw");
|
|
14
|
+
const PORTAL_ID = 'component-portal';
|
|
15
|
+
const ANIM_MS = 500;
|
|
16
|
+
const EASING = 'cubic-bezier(0.22, 1, 0.36, 1)';
|
|
17
|
+
const SWIPE_CLOSE_THRESHOLD = 72;
|
|
18
|
+
const SWIPE_NAV_THRESHOLD = 50;
|
|
19
|
+
const BACKDROP = 'rgba(28,31,34,0.9)';
|
|
20
|
+
// the close icon's size and its margins as shares of the layout width, on the layouts a project
|
|
21
|
+
// starts with: a narrower layout takes larger ones, to keep the icon a size to tap. Any other
|
|
22
|
+
// layout takes the desktop's.
|
|
23
|
+
const CLOSE_ICON = {
|
|
24
|
+
m: { size: 0.081, marginTop: 0.081, marginRight: 0.05405 },
|
|
25
|
+
t: { size: 0.03906, marginTop: 0.026, marginRight: 0.026 },
|
|
26
|
+
d: { size: 0.0125, marginTop: 0.01, marginRight: 0.01 }
|
|
27
|
+
};
|
|
28
|
+
function rectOf(element) {
|
|
29
|
+
const { top, left, width, height } = element.getBoundingClientRect();
|
|
30
|
+
return { top, left, width, height };
|
|
31
|
+
}
|
|
32
|
+
/** Where an image starts from or returns to: the place of its element on the page, if it has one and takes up room there. */
|
|
33
|
+
function originRect(origin) {
|
|
34
|
+
if (!origin)
|
|
35
|
+
return null;
|
|
36
|
+
const rect = rectOf(origin);
|
|
37
|
+
return rect.width && rect.height ? rect : null;
|
|
38
|
+
}
|
|
39
|
+
/** The transform that draws an element laid out at `from` over `to`, its top left corner the one held in place. */
|
|
40
|
+
function moveTo(from, to) {
|
|
41
|
+
return `translate(${to.left - from.left}px, ${to.top - from.top}px) scale(${to.width / from.width}, ${to.height / from.height})`;
|
|
42
|
+
}
|
|
43
|
+
function prefersReducedMotion() {
|
|
44
|
+
var _a;
|
|
45
|
+
return typeof window !== 'undefined' && Boolean((_a = window.matchMedia) === null || _a === void 0 ? void 0 : _a.call(window, '(prefers-reduced-motion: reduce)').matches);
|
|
46
|
+
}
|
|
47
|
+
function containIn(box, natural) {
|
|
48
|
+
if (!natural || !natural.width || !natural.height || !box.width || !box.height)
|
|
49
|
+
return box;
|
|
50
|
+
const imageRatio = natural.width / natural.height;
|
|
51
|
+
if (imageRatio > box.width / box.height) {
|
|
52
|
+
const height = box.width / imageRatio;
|
|
53
|
+
return { width: box.width, height, left: box.left, top: box.top + (box.height - height) / 2 };
|
|
54
|
+
}
|
|
55
|
+
const width = box.height * imageRatio;
|
|
56
|
+
return { width, height: box.height, left: box.left + (box.width - width) / 2, top: box.top };
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* A full-screen viewer of images, one at a time, each fitted within the screen: stepped through by
|
|
60
|
+
* the halves of the image or a sideways swipe, around from the last to the first, and closed by
|
|
61
|
+
* Escape, the backdrop, the close icon or a swipe down. Told where its images are on the page, it
|
|
62
|
+
* opens the first out of its place there and closes the one shown back into its own. It holds the
|
|
63
|
+
* page still while it is up. Mount it to open it, and unmount it when it calls `onClose`.
|
|
64
|
+
*/
|
|
65
|
+
const ImageLightbox = ({ images, initialIndex, onClose, originOf }) => {
|
|
66
|
+
var _a;
|
|
67
|
+
const { layouts } = (0, useCntrlContext_1.useCntrlContext)();
|
|
68
|
+
const [index, setIndex] = (0, react_1.useState)(initialIndex);
|
|
69
|
+
const count = images.length;
|
|
70
|
+
const showPrev = (0, react_1.useCallback)(() => setIndex(prev => (prev - 1 + count) % count), [count]);
|
|
71
|
+
const showNext = (0, react_1.useCallback)(() => setIndex(prev => (prev + 1) % count), [count]);
|
|
72
|
+
(0, react_1.useEffect)(() => {
|
|
73
|
+
const prevOverflow = document.body.style.overflow;
|
|
74
|
+
document.body.style.overflow = 'hidden';
|
|
75
|
+
return () => {
|
|
76
|
+
document.body.style.overflow = prevOverflow;
|
|
77
|
+
};
|
|
78
|
+
}, []);
|
|
79
|
+
if (typeof document === 'undefined')
|
|
80
|
+
return null;
|
|
81
|
+
return (0, react_dom_1.createPortal)((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(Overlay, { images: images, index: index, onClose: onClose, onPrev: showPrev, onNext: showNext, originOf: originOf }), (0, jsx_runtime_1.jsx)(style_1.default, { id: "image-lightbox", children: `
|
|
82
|
+
.image-lightbox {
|
|
83
|
+
position: fixed;
|
|
84
|
+
inset: 0;
|
|
85
|
+
z-index: 9997;
|
|
86
|
+
overscroll-behavior: none;
|
|
87
|
+
overflow: hidden;
|
|
88
|
+
}
|
|
89
|
+
.image-lightbox-close {
|
|
90
|
+
all: unset;
|
|
91
|
+
display: block;
|
|
92
|
+
flex-shrink: 0;
|
|
93
|
+
cursor: pointer;
|
|
94
|
+
color: #FFFFFF;
|
|
95
|
+
transition: color 0.2s;
|
|
96
|
+
}
|
|
97
|
+
.image-lightbox-close:hover {
|
|
98
|
+
color: #999999;
|
|
99
|
+
}
|
|
100
|
+
.image-lightbox-close:focus-visible {
|
|
101
|
+
outline: 1px solid currentColor;
|
|
102
|
+
outline-offset: 4px;
|
|
103
|
+
}
|
|
104
|
+
.image-lightbox-close > svg {
|
|
105
|
+
display: block;
|
|
106
|
+
width: 100%;
|
|
107
|
+
height: 100%;
|
|
108
|
+
}
|
|
109
|
+
${(0, sdk_1.getLayoutStyles)(layouts, [CLOSE_ICON], ([icon = CLOSE_ICON.d]) => (`
|
|
110
|
+
.image-lightbox-close-row {
|
|
111
|
+
height: ${(0, vw_1.vw)(icon.marginTop)};
|
|
112
|
+
}
|
|
113
|
+
.image-lightbox-close {
|
|
114
|
+
width: ${(0, vw_1.vw)(icon.size)};
|
|
115
|
+
height: ${(0, vw_1.vw)(icon.size)};
|
|
116
|
+
margin-right: ${(0, vw_1.vw)(icon.marginRight)};
|
|
117
|
+
}
|
|
118
|
+
`))}
|
|
119
|
+
` })] }), (_a = document.getElementById(PORTAL_ID)) !== null && _a !== void 0 ? _a : document.body);
|
|
120
|
+
};
|
|
121
|
+
exports.ImageLightbox = ImageLightbox;
|
|
122
|
+
const Overlay = ({ images, index, onClose, onPrev, onNext, originOf }) => {
|
|
123
|
+
var _a, _b;
|
|
124
|
+
const containerRef = (0, react_1.useRef)(null);
|
|
125
|
+
const ghostRef = (0, react_1.useRef)(null);
|
|
126
|
+
const touchStartRef = (0, react_1.useRef)(null);
|
|
127
|
+
const swipeOffsetRef = (0, react_1.useRef)(0);
|
|
128
|
+
const swipeDeltaXRef = (0, react_1.useRef)(0);
|
|
129
|
+
const swipeAxisRef = (0, react_1.useRef)('none');
|
|
130
|
+
const isSwipingRef = (0, react_1.useRef)(false);
|
|
131
|
+
const touchInteractionRef = (0, react_1.useRef)(false);
|
|
132
|
+
const viewportRectRef = (0, react_1.useRef)(null);
|
|
133
|
+
const slideOffsetRef = (0, react_1.useRef)(0);
|
|
134
|
+
const slideDirRef = (0, react_1.useRef)(null);
|
|
135
|
+
const slideAnimatingRef = (0, react_1.useRef)(false);
|
|
136
|
+
const slideCommitTimerRef = (0, react_1.useRef)(null);
|
|
137
|
+
const slideCommitDirectionRef = (0, react_1.useRef)(null);
|
|
138
|
+
const rapidNavDirectionRef = (0, react_1.useRef)(null);
|
|
139
|
+
const startProgrammaticSlideRef = (0, react_1.useRef)(() => { });
|
|
140
|
+
const [finalRect, setFinalRect] = (0, react_1.useState)(null);
|
|
141
|
+
const [viewportRect, setViewportRect] = (0, react_1.useState)(null);
|
|
142
|
+
const [phase, setPhase] = (0, react_1.useState)('opening');
|
|
143
|
+
const [swipeOffset, setSwipeOffset] = (0, react_1.useState)(0);
|
|
144
|
+
const [slideOffset, setSlideOffset] = (0, react_1.useState)(0);
|
|
145
|
+
const [slideDir, setSlideDir] = (0, react_1.useState)(null);
|
|
146
|
+
const [slideAnimating, setSlideAnimating] = (0, react_1.useState)(false);
|
|
147
|
+
const [isSwiping, setIsSwiping] = (0, react_1.useState)(false);
|
|
148
|
+
const [swipeDismiss, setSwipeDismiss] = (0, react_1.useState)(false);
|
|
149
|
+
const current = images[index];
|
|
150
|
+
// whether images move between their places on the page and the lightbox, settled as it opens
|
|
151
|
+
const [moves] = (0, react_1.useState)(() => Boolean(originOf) && !prefersReducedMotion());
|
|
152
|
+
// the place the first image opens out of, with the size its file is known to have there, which
|
|
153
|
+
// fits it in the lightbox before its own copy has loaded
|
|
154
|
+
const [opening] = (0, react_1.useState)(() => {
|
|
155
|
+
const origin = moves ? originOf === null || originOf === void 0 ? void 0 : originOf(index) : null;
|
|
156
|
+
const rect = originRect(origin);
|
|
157
|
+
const picture = origin instanceof HTMLImageElement ? origin : origin === null || origin === void 0 ? void 0 : origin.querySelector('img');
|
|
158
|
+
return rect ? { rect, natural: (picture === null || picture === void 0 ? void 0 : picture.naturalWidth) && picture.naturalHeight ? { width: picture.naturalWidth, height: picture.naturalHeight } : null } : null;
|
|
159
|
+
});
|
|
160
|
+
// the place the image shown closes back into, measured when it starts to; none while it is open, or where it has none
|
|
161
|
+
const [closingRect, setClosingRect] = (0, react_1.useState)(null);
|
|
162
|
+
const [naturalSize, setNaturalSize] = (0, react_1.useState)((_a = opening === null || opening === void 0 ? void 0 : opening.natural) !== null && _a !== void 0 ? _a : null);
|
|
163
|
+
const prevIndexRef = (0, react_1.useRef)(index);
|
|
164
|
+
const prevSrcRef = (0, react_1.useRef)(current === null || current === void 0 ? void 0 : current.src);
|
|
165
|
+
const handleImageLoad = (0, react_1.useCallback)((event) => {
|
|
166
|
+
const { naturalWidth, naturalHeight } = event.currentTarget;
|
|
167
|
+
if (!naturalWidth || !naturalHeight)
|
|
168
|
+
return;
|
|
169
|
+
setNaturalSize({ width: naturalWidth, height: naturalHeight });
|
|
170
|
+
}, []);
|
|
171
|
+
(0, react_1.useEffect)(() => {
|
|
172
|
+
viewportRectRef.current = viewportRect;
|
|
173
|
+
}, [viewportRect]);
|
|
174
|
+
(0, react_1.useEffect)(() => {
|
|
175
|
+
const container = containerRef.current;
|
|
176
|
+
if (!container)
|
|
177
|
+
return;
|
|
178
|
+
const update = () => setViewportRect(rectOf(container));
|
|
179
|
+
update();
|
|
180
|
+
const observer = new ResizeObserver(update);
|
|
181
|
+
observer.observe(container);
|
|
182
|
+
return () => observer.disconnect();
|
|
183
|
+
}, []);
|
|
184
|
+
const clearSlideCommitTimer = (0, react_1.useCallback)(() => {
|
|
185
|
+
if (slideCommitTimerRef.current !== null) {
|
|
186
|
+
window.clearTimeout(slideCommitTimerRef.current);
|
|
187
|
+
slideCommitTimerRef.current = null;
|
|
188
|
+
}
|
|
189
|
+
}, []);
|
|
190
|
+
const commitSlideNavigation = (0, react_1.useCallback)(() => {
|
|
191
|
+
const dir = slideCommitDirectionRef.current;
|
|
192
|
+
slideCommitDirectionRef.current = null;
|
|
193
|
+
clearSlideCommitTimer();
|
|
194
|
+
slideDirRef.current = null;
|
|
195
|
+
slideAnimatingRef.current = false;
|
|
196
|
+
setSlideDir(null);
|
|
197
|
+
setSlideAnimating(false);
|
|
198
|
+
setSlideOffset(0);
|
|
199
|
+
slideOffsetRef.current = 0;
|
|
200
|
+
setIsSwiping(false);
|
|
201
|
+
isSwipingRef.current = false;
|
|
202
|
+
swipeDeltaXRef.current = 0;
|
|
203
|
+
swipeAxisRef.current = 'none';
|
|
204
|
+
if (dir === 1)
|
|
205
|
+
onNext();
|
|
206
|
+
else if (dir === -1)
|
|
207
|
+
onPrev();
|
|
208
|
+
}, [clearSlideCommitTimer, onNext, onPrev]);
|
|
209
|
+
const cancelSlideAnimation = (0, react_1.useCallback)(() => {
|
|
210
|
+
clearSlideCommitTimer();
|
|
211
|
+
slideCommitDirectionRef.current = null;
|
|
212
|
+
slideDirRef.current = null;
|
|
213
|
+
slideAnimatingRef.current = false;
|
|
214
|
+
setSlideDir(null);
|
|
215
|
+
setSlideAnimating(false);
|
|
216
|
+
setSlideOffset(0);
|
|
217
|
+
slideOffsetRef.current = 0;
|
|
218
|
+
}, [clearSlideCommitTimer]);
|
|
219
|
+
// the transition's own end may never be told, in a tab left in the background say: the timer commits the slide then
|
|
220
|
+
const scheduleSlideCommit = (0, react_1.useCallback)(() => {
|
|
221
|
+
clearSlideCommitTimer();
|
|
222
|
+
slideCommitTimerRef.current = window.setTimeout(() => {
|
|
223
|
+
if (slideCommitDirectionRef.current === null)
|
|
224
|
+
return;
|
|
225
|
+
commitSlideNavigation();
|
|
226
|
+
}, ANIM_MS + 32);
|
|
227
|
+
}, [clearSlideCommitTimer, commitSlideNavigation]);
|
|
228
|
+
const finishSlide = (0, react_1.useCallback)((dir, commit) => {
|
|
229
|
+
var _a, _b;
|
|
230
|
+
const width = (_b = (_a = viewportRectRef.current) === null || _a === void 0 ? void 0 : _a.width) !== null && _b !== void 0 ? _b : 0;
|
|
231
|
+
clearSlideCommitTimer();
|
|
232
|
+
slideDirRef.current = dir;
|
|
233
|
+
setSlideDir(dir);
|
|
234
|
+
slideAnimatingRef.current = true;
|
|
235
|
+
setSlideAnimating(true);
|
|
236
|
+
if (commit) {
|
|
237
|
+
slideCommitDirectionRef.current = dir;
|
|
238
|
+
const target = -dir * width;
|
|
239
|
+
slideOffsetRef.current = target;
|
|
240
|
+
setSlideOffset(target);
|
|
241
|
+
scheduleSlideCommit();
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
slideOffsetRef.current = 0;
|
|
245
|
+
setSlideOffset(0);
|
|
246
|
+
slideCommitTimerRef.current = window.setTimeout(() => {
|
|
247
|
+
slideCommitTimerRef.current = null;
|
|
248
|
+
cancelSlideAnimation();
|
|
249
|
+
}, ANIM_MS + 32);
|
|
250
|
+
}, [clearSlideCommitTimer, cancelSlideAnimation, scheduleSlideCommit]);
|
|
251
|
+
const startProgrammaticSlide = (0, react_1.useCallback)((dir) => {
|
|
252
|
+
if (images.length <= 1)
|
|
253
|
+
return;
|
|
254
|
+
if (slideAnimatingRef.current) {
|
|
255
|
+
// a second step the same way while the first still slides: the first lands at once and the second starts from there
|
|
256
|
+
if (slideCommitDirectionRef.current === dir) {
|
|
257
|
+
rapidNavDirectionRef.current = dir;
|
|
258
|
+
commitSlideNavigation();
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
cancelSlideAnimation();
|
|
262
|
+
}
|
|
263
|
+
clearSlideCommitTimer();
|
|
264
|
+
slideDirRef.current = dir;
|
|
265
|
+
setSlideDir(dir);
|
|
266
|
+
slideAnimatingRef.current = false;
|
|
267
|
+
setSlideAnimating(false);
|
|
268
|
+
slideOffsetRef.current = 0;
|
|
269
|
+
setSlideOffset(0);
|
|
270
|
+
// the neighbour is drawn at rest beside the image first, so that the slide has somewhere to start from
|
|
271
|
+
requestAnimationFrame(() => {
|
|
272
|
+
requestAnimationFrame(() => {
|
|
273
|
+
finishSlide(dir, true);
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
}, [images.length, finishSlide, commitSlideNavigation, cancelSlideAnimation, clearSlideCommitTimer]);
|
|
277
|
+
startProgrammaticSlideRef.current = startProgrammaticSlide;
|
|
278
|
+
const handleSlideTransitionEnd = (0, react_1.useCallback)((event) => {
|
|
279
|
+
if (event.propertyName !== 'transform' || !slideAnimatingRef.current)
|
|
280
|
+
return;
|
|
281
|
+
if (slideCommitDirectionRef.current === null)
|
|
282
|
+
return;
|
|
283
|
+
commitSlideNavigation();
|
|
284
|
+
}, [commitSlideNavigation]);
|
|
285
|
+
const computeFinalRect = (0, react_1.useCallback)(() => {
|
|
286
|
+
const ghost = ghostRef.current;
|
|
287
|
+
if (!ghost)
|
|
288
|
+
return;
|
|
289
|
+
const rect = rectOf(ghost);
|
|
290
|
+
if (!rect.width || !rect.height)
|
|
291
|
+
return;
|
|
292
|
+
setFinalRect(rect);
|
|
293
|
+
}, []);
|
|
294
|
+
(0, react_1.useEffect)(() => {
|
|
295
|
+
computeFinalRect();
|
|
296
|
+
const ghost = ghostRef.current;
|
|
297
|
+
if (!ghost)
|
|
298
|
+
return;
|
|
299
|
+
const observer = new ResizeObserver(() => computeFinalRect());
|
|
300
|
+
observer.observe(ghost);
|
|
301
|
+
return () => observer.disconnect();
|
|
302
|
+
}, [computeFinalRect]);
|
|
303
|
+
(0, react_1.useEffect)(() => {
|
|
304
|
+
if (phase !== 'opening' || !finalRect)
|
|
305
|
+
return;
|
|
306
|
+
if (!opening) {
|
|
307
|
+
setPhase('open');
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
// the image is drawn over its place on the page first, so that it has somewhere to move from
|
|
311
|
+
let frame = requestAnimationFrame(() => {
|
|
312
|
+
frame = requestAnimationFrame(() => setPhase('open'));
|
|
313
|
+
});
|
|
314
|
+
return () => cancelAnimationFrame(frame);
|
|
315
|
+
}, [phase, finalRect, opening]);
|
|
316
|
+
const close = (0, react_1.useCallback)(() => {
|
|
317
|
+
const rect = moves ? originRect(originOf === null || originOf === void 0 ? void 0 : originOf(index)) : null;
|
|
318
|
+
setClosingRect(rect);
|
|
319
|
+
setPhase('closing');
|
|
320
|
+
return rect !== null;
|
|
321
|
+
}, [moves, originOf, index]);
|
|
322
|
+
const closeRef = (0, react_1.useRef)(close);
|
|
323
|
+
closeRef.current = close;
|
|
324
|
+
(0, react_1.useEffect)(() => {
|
|
325
|
+
if (phase !== 'closing')
|
|
326
|
+
return;
|
|
327
|
+
const timer = setTimeout(() => onClose(), ANIM_MS);
|
|
328
|
+
return () => clearTimeout(timer);
|
|
329
|
+
}, [phase, onClose]);
|
|
330
|
+
(0, react_1.useEffect)(() => {
|
|
331
|
+
return () => {
|
|
332
|
+
clearSlideCommitTimer();
|
|
333
|
+
};
|
|
334
|
+
}, [clearSlideCommitTimer]);
|
|
335
|
+
(0, react_1.useLayoutEffect)(() => {
|
|
336
|
+
if (prevIndexRef.current === index && prevSrcRef.current === (current === null || current === void 0 ? void 0 : current.src))
|
|
337
|
+
return;
|
|
338
|
+
clearSlideCommitTimer();
|
|
339
|
+
slideCommitDirectionRef.current = null;
|
|
340
|
+
setSlideDir(null);
|
|
341
|
+
setSwipeOffset(0);
|
|
342
|
+
setSlideOffset(0);
|
|
343
|
+
setSlideAnimating(false);
|
|
344
|
+
setSwipeDismiss(false);
|
|
345
|
+
setIsSwiping(false);
|
|
346
|
+
setNaturalSize(null);
|
|
347
|
+
swipeOffsetRef.current = 0;
|
|
348
|
+
swipeDeltaXRef.current = 0;
|
|
349
|
+
swipeAxisRef.current = 'none';
|
|
350
|
+
isSwipingRef.current = false;
|
|
351
|
+
slideAnimatingRef.current = false;
|
|
352
|
+
slideOffsetRef.current = 0;
|
|
353
|
+
slideDirRef.current = null;
|
|
354
|
+
touchStartRef.current = null;
|
|
355
|
+
touchInteractionRef.current = false;
|
|
356
|
+
prevIndexRef.current = index;
|
|
357
|
+
prevSrcRef.current = current === null || current === void 0 ? void 0 : current.src;
|
|
358
|
+
}, [index, current === null || current === void 0 ? void 0 : current.src, clearSlideCommitTimer]);
|
|
359
|
+
(0, react_1.useLayoutEffect)(() => {
|
|
360
|
+
const rapidDirection = rapidNavDirectionRef.current;
|
|
361
|
+
if (rapidDirection === null)
|
|
362
|
+
return;
|
|
363
|
+
rapidNavDirectionRef.current = null;
|
|
364
|
+
requestAnimationFrame(() => {
|
|
365
|
+
startProgrammaticSlideRef.current(rapidDirection);
|
|
366
|
+
});
|
|
367
|
+
}, [index, current === null || current === void 0 ? void 0 : current.src]);
|
|
368
|
+
(0, react_1.useEffect)(() => {
|
|
369
|
+
if (phase !== 'open')
|
|
370
|
+
return;
|
|
371
|
+
let blockClick = null;
|
|
372
|
+
const clearBlockSwipeClick = () => {
|
|
373
|
+
if (!blockClick)
|
|
374
|
+
return;
|
|
375
|
+
document.removeEventListener('click', blockClick, true);
|
|
376
|
+
blockClick = null;
|
|
377
|
+
};
|
|
378
|
+
// a swipe ends in a click on whatever is under the finger, which would step or close once more
|
|
379
|
+
const blockSwipeClick = () => {
|
|
380
|
+
clearBlockSwipeClick();
|
|
381
|
+
blockClick = (clickEvent) => {
|
|
382
|
+
clickEvent.stopPropagation();
|
|
383
|
+
clickEvent.preventDefault();
|
|
384
|
+
clearBlockSwipeClick();
|
|
385
|
+
};
|
|
386
|
+
document.addEventListener('click', blockClick, true);
|
|
387
|
+
};
|
|
388
|
+
const startSlideRelease = (deltaX) => {
|
|
389
|
+
const dir = deltaX < 0 ? 1 : -1;
|
|
390
|
+
finishSlide(dir, Math.abs(deltaX) > SWIPE_NAV_THRESHOLD && images.length > 1);
|
|
391
|
+
};
|
|
392
|
+
const handleTouchStart = (event) => {
|
|
393
|
+
if (event.touches.length !== 1 || slideAnimatingRef.current)
|
|
394
|
+
return;
|
|
395
|
+
const touch = event.touches[0];
|
|
396
|
+
touchInteractionRef.current = true;
|
|
397
|
+
touchStartRef.current = { x: touch.clientX, y: touch.clientY };
|
|
398
|
+
swipeAxisRef.current = 'none';
|
|
399
|
+
swipeDeltaXRef.current = 0;
|
|
400
|
+
isSwipingRef.current = false;
|
|
401
|
+
};
|
|
402
|
+
const handleTouchMove = (event) => {
|
|
403
|
+
if (!touchStartRef.current || event.touches.length !== 1 || slideAnimatingRef.current)
|
|
404
|
+
return;
|
|
405
|
+
const touch = event.touches[0];
|
|
406
|
+
const deltaX = touch.clientX - touchStartRef.current.x;
|
|
407
|
+
const deltaY = touch.clientY - touchStartRef.current.y;
|
|
408
|
+
if (swipeAxisRef.current === 'none') {
|
|
409
|
+
if (Math.abs(deltaX) < 10 && Math.abs(deltaY) < 10)
|
|
410
|
+
return;
|
|
411
|
+
if (Math.abs(deltaX) > Math.abs(deltaY)) {
|
|
412
|
+
if (images.length <= 1)
|
|
413
|
+
return;
|
|
414
|
+
swipeAxisRef.current = 'horizontal';
|
|
415
|
+
}
|
|
416
|
+
else if (deltaY > 0) {
|
|
417
|
+
swipeAxisRef.current = 'vertical';
|
|
418
|
+
}
|
|
419
|
+
else {
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
if (swipeAxisRef.current === 'horizontal') {
|
|
424
|
+
isSwipingRef.current = true;
|
|
425
|
+
setIsSwiping(true);
|
|
426
|
+
swipeDeltaXRef.current = deltaX;
|
|
427
|
+
const dir = deltaX < 0 ? 1 : -1;
|
|
428
|
+
if (slideDirRef.current !== dir) {
|
|
429
|
+
slideDirRef.current = dir;
|
|
430
|
+
setSlideDir(dir);
|
|
431
|
+
}
|
|
432
|
+
slideOffsetRef.current = deltaX;
|
|
433
|
+
setSlideOffset(deltaX);
|
|
434
|
+
event.preventDefault();
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
if (swipeAxisRef.current === 'vertical') {
|
|
438
|
+
isSwipingRef.current = true;
|
|
439
|
+
setIsSwiping(true);
|
|
440
|
+
swipeOffsetRef.current = deltaY;
|
|
441
|
+
setSwipeOffset(deltaY);
|
|
442
|
+
event.preventDefault();
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
const handleTouchEnd = () => {
|
|
446
|
+
if (!touchStartRef.current)
|
|
447
|
+
return;
|
|
448
|
+
const axis = swipeAxisRef.current;
|
|
449
|
+
const wasSwiping = isSwipingRef.current;
|
|
450
|
+
if (wasSwiping && axis === 'horizontal') {
|
|
451
|
+
startSlideRelease(swipeDeltaXRef.current);
|
|
452
|
+
blockSwipeClick();
|
|
453
|
+
}
|
|
454
|
+
else if (wasSwiping && axis === 'vertical') {
|
|
455
|
+
if (swipeOffsetRef.current > SWIPE_CLOSE_THRESHOLD) {
|
|
456
|
+
// let go of, the image returns to its place on the page from where it was dragged to, or is
|
|
457
|
+
// carried on down off the screen where it has no such place
|
|
458
|
+
const returns = closeRef.current();
|
|
459
|
+
const dismissOffset = returns ? 0 : window.innerHeight;
|
|
460
|
+
swipeOffsetRef.current = dismissOffset;
|
|
461
|
+
setSwipeDismiss(!returns);
|
|
462
|
+
setSwipeOffset(dismissOffset);
|
|
463
|
+
}
|
|
464
|
+
else {
|
|
465
|
+
swipeOffsetRef.current = 0;
|
|
466
|
+
setSwipeOffset(0);
|
|
467
|
+
blockSwipeClick();
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
else if (axis === 'horizontal' && Math.abs(swipeDeltaXRef.current) > 0) {
|
|
471
|
+
startSlideRelease(swipeDeltaXRef.current);
|
|
472
|
+
blockSwipeClick();
|
|
473
|
+
}
|
|
474
|
+
touchStartRef.current = null;
|
|
475
|
+
swipeAxisRef.current = 'none';
|
|
476
|
+
swipeDeltaXRef.current = 0;
|
|
477
|
+
isSwipingRef.current = false;
|
|
478
|
+
setIsSwiping(false);
|
|
479
|
+
};
|
|
480
|
+
document.addEventListener('touchstart', handleTouchStart, { passive: true });
|
|
481
|
+
document.addEventListener('touchmove', handleTouchMove, { passive: false });
|
|
482
|
+
document.addEventListener('touchend', handleTouchEnd);
|
|
483
|
+
document.addEventListener('touchcancel', handleTouchEnd);
|
|
484
|
+
return () => {
|
|
485
|
+
document.removeEventListener('touchstart', handleTouchStart);
|
|
486
|
+
document.removeEventListener('touchmove', handleTouchMove);
|
|
487
|
+
document.removeEventListener('touchend', handleTouchEnd);
|
|
488
|
+
document.removeEventListener('touchcancel', handleTouchEnd);
|
|
489
|
+
clearBlockSwipeClick();
|
|
490
|
+
};
|
|
491
|
+
}, [phase, images.length, finishSlide]);
|
|
492
|
+
(0, react_1.useEffect)(() => {
|
|
493
|
+
const onKeyDown = (event) => {
|
|
494
|
+
if (event.key !== 'Escape')
|
|
495
|
+
return;
|
|
496
|
+
event.preventDefault();
|
|
497
|
+
event.stopPropagation();
|
|
498
|
+
closeRef.current();
|
|
499
|
+
};
|
|
500
|
+
window.addEventListener('keydown', onKeyDown, true);
|
|
501
|
+
return () => window.removeEventListener('keydown', onKeyDown, true);
|
|
502
|
+
}, []);
|
|
503
|
+
const isOpen = phase === 'open';
|
|
504
|
+
const isClosing = phase === 'closing';
|
|
505
|
+
const swipeBackdropOpacity = swipeOffset > 0 ? Math.max(0, 1 - swipeOffset / 500) : 1;
|
|
506
|
+
const swipeImageOpacity = swipeOffset > 0 ? Math.max(0.35, 1 - swipeOffset / 500) : 1;
|
|
507
|
+
const contentRect = finalRect ? containIn(finalRect, naturalSize) : null;
|
|
508
|
+
const backingBaseStyle = {
|
|
509
|
+
position: 'fixed',
|
|
510
|
+
top: viewportRect === null || viewportRect === void 0 ? void 0 : viewportRect.top,
|
|
511
|
+
left: viewportRect === null || viewportRect === void 0 ? void 0 : viewportRect.left,
|
|
512
|
+
width: viewportRect === null || viewportRect === void 0 ? void 0 : viewportRect.width,
|
|
513
|
+
height: viewportRect === null || viewportRect === void 0 ? void 0 : viewportRect.height,
|
|
514
|
+
zIndex: 9998,
|
|
515
|
+
pointerEvents: 'none'
|
|
516
|
+
};
|
|
517
|
+
// moved between its place on the page and the lightbox, an image is seen all the way; otherwise it fades
|
|
518
|
+
const fadesIn = phase === 'opening' && !opening;
|
|
519
|
+
const fadesOut = isClosing && !closingRect;
|
|
520
|
+
const currentTransition = (isSwiping && !slideAnimating)
|
|
521
|
+
? 'none'
|
|
522
|
+
: slideAnimating
|
|
523
|
+
? `transform ${ANIM_MS}ms ${EASING}`
|
|
524
|
+
: swipeDismiss || isClosing
|
|
525
|
+
? `transform ${ANIM_MS}ms ${EASING}, opacity ${ANIM_MS}ms ${EASING}`
|
|
526
|
+
: phase === 'opening'
|
|
527
|
+
? `opacity ${ANIM_MS}ms ${EASING}`
|
|
528
|
+
: 'none';
|
|
529
|
+
const currentBackingStyle = Object.assign(Object.assign({}, backingBaseStyle), { transform: (slideOffset !== 0 || swipeOffset > 0) ? `translate(${slideOffset}px, ${swipeOffset}px)` : undefined, opacity: swipeOffset > 0
|
|
530
|
+
? swipeImageOpacity
|
|
531
|
+
: fadesIn || fadesOut
|
|
532
|
+
? 0
|
|
533
|
+
: 1, transition: currentTransition });
|
|
534
|
+
const imageStyle = (rect) => {
|
|
535
|
+
var _a, _b, _c, _d;
|
|
536
|
+
return ({
|
|
537
|
+
position: 'absolute',
|
|
538
|
+
top: ((_a = rect === null || rect === void 0 ? void 0 : rect.top) !== null && _a !== void 0 ? _a : 0) - ((_b = viewportRect === null || viewportRect === void 0 ? void 0 : viewportRect.top) !== null && _b !== void 0 ? _b : 0),
|
|
539
|
+
left: ((_c = rect === null || rect === void 0 ? void 0 : rect.left) !== null && _c !== void 0 ? _c : 0) - ((_d = viewportRect === null || viewportRect === void 0 ? void 0 : viewportRect.left) !== null && _d !== void 0 ? _d : 0),
|
|
540
|
+
width: rect === null || rect === void 0 ? void 0 : rect.width,
|
|
541
|
+
height: rect === null || rect === void 0 ? void 0 : rect.height,
|
|
542
|
+
objectFit: 'contain',
|
|
543
|
+
pointerEvents: 'none'
|
|
544
|
+
});
|
|
545
|
+
};
|
|
546
|
+
// where the image shown is drawn instead of in the middle of the screen: its place on the page, as it opens out of it and closes back into it
|
|
547
|
+
const pageRect = phase === 'opening' ? opening === null || opening === void 0 ? void 0 : opening.rect : closingRect;
|
|
548
|
+
const currentImageStyle = (rect) => (Object.assign(Object.assign({}, imageStyle(rect)), { transformOrigin: 'top left', transform: rect && pageRect ? moveTo(rect, pageRect) : undefined,
|
|
549
|
+
// set down over its place on the page at once, moved from and to it in time
|
|
550
|
+
transition: phase === 'opening' ? 'none' : `transform ${ANIM_MS}ms ${EASING}` }));
|
|
551
|
+
const slideWidth = (_b = viewportRect === null || viewportRect === void 0 ? void 0 : viewportRect.width) !== null && _b !== void 0 ? _b : 0;
|
|
552
|
+
const neighborIndex = slideDir !== null ? (index + slideDir + images.length) % images.length : null;
|
|
553
|
+
const neighbor = neighborIndex !== null ? images[neighborIndex] : null;
|
|
554
|
+
const neighborBackingStyle = Object.assign(Object.assign({}, backingBaseStyle), { transform: slideDir !== null ? `translateX(${slideDir * slideWidth + slideOffset}px)` : undefined, transition: slideAnimating ? `transform ${ANIM_MS}ms ${EASING}` : 'none' });
|
|
555
|
+
const stepZoneStyle = (side) => contentRect ? {
|
|
556
|
+
position: 'fixed',
|
|
557
|
+
top: contentRect.top,
|
|
558
|
+
left: side === 'prev' ? contentRect.left : contentRect.left + contentRect.width / 2,
|
|
559
|
+
width: contentRect.width / 2,
|
|
560
|
+
height: contentRect.height,
|
|
561
|
+
zIndex: 9999,
|
|
562
|
+
cursor: side === 'prev' ? 'w-resize' : 'e-resize',
|
|
563
|
+
pointerEvents: isSwiping ? 'none' : 'auto'
|
|
564
|
+
} : undefined;
|
|
565
|
+
const step = (dir) => (event) => {
|
|
566
|
+
// the tap that ends a touch has done its work as a swipe or is left to close the lightbox
|
|
567
|
+
if (touchInteractionRef.current) {
|
|
568
|
+
touchInteractionRef.current = false;
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
event.stopPropagation();
|
|
572
|
+
startProgrammaticSlide(dir);
|
|
573
|
+
};
|
|
574
|
+
return ((0, jsx_runtime_1.jsxs)("div", { ref: containerRef, className: "image-lightbox", role: "dialog", "aria-modal": "true", style: { touchAction: isOpen ? 'none' : undefined }, onClick: () => {
|
|
575
|
+
close();
|
|
576
|
+
}, children: [(0, jsx_runtime_1.jsx)("div", { style: {
|
|
577
|
+
position: 'absolute',
|
|
578
|
+
inset: 0,
|
|
579
|
+
background: BACKDROP,
|
|
580
|
+
opacity: isOpen ? swipeBackdropOpacity : 0,
|
|
581
|
+
transition: isSwiping ? 'none' : `opacity ${ANIM_MS}ms ${EASING}`,
|
|
582
|
+
pointerEvents: 'none'
|
|
583
|
+
} }), (0, jsx_runtime_1.jsx)("div", { style: { position: 'absolute', top: '10%', left: '15%', width: '70%', height: '80%' }, children: (0, jsx_runtime_1.jsx)("div", { ref: ghostRef, style: { width: '100%', height: '100%' } }) }), finalRect && viewportRect && neighbor && ((0, jsx_runtime_1.jsx)("div", { style: neighborBackingStyle, onTransitionEnd: handleSlideTransitionEnd, children: (0, jsx_runtime_1.jsx)("img", { src: neighbor.src, alt: neighbor.alt, style: imageStyle(finalRect) }, `neighbor-${neighborIndex}-${neighbor.src}`) })), finalRect && viewportRect && current && ((0, jsx_runtime_1.jsx)("div", { style: currentBackingStyle, onTransitionEnd: handleSlideTransitionEnd, children: (0, jsx_runtime_1.jsx)("img", { src: current.src, alt: current.alt, onLoad: handleImageLoad, style: currentImageStyle(contentRect) }, `${index}-${current.src}`) })), isOpen && contentRect && images.length > 1 && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { "data-step": "prev", style: stepZoneStyle('prev'), onClick: step(-1) }), (0, jsx_runtime_1.jsx)("div", { "data-step": "next", style: stepZoneStyle('next'), onClick: step(1) })] })), (0, jsx_runtime_1.jsxs)("div", { style: {
|
|
584
|
+
position: 'absolute',
|
|
585
|
+
top: 0,
|
|
586
|
+
left: 0,
|
|
587
|
+
right: 0,
|
|
588
|
+
zIndex: 10000,
|
|
589
|
+
display: 'flex',
|
|
590
|
+
flexDirection: 'column',
|
|
591
|
+
alignItems: 'flex-end',
|
|
592
|
+
pointerEvents: 'none',
|
|
593
|
+
opacity: isOpen ? 1 : 0,
|
|
594
|
+
transition: `opacity ${ANIM_MS}ms ${EASING}`
|
|
595
|
+
}, children: [(0, jsx_runtime_1.jsx)("div", { className: "image-lightbox-close-row" }), (0, jsx_runtime_1.jsx)("button", { type: "button", className: "image-lightbox-close", "aria-label": "Close", style: { pointerEvents: isOpen ? 'auto' : 'none' }, onClick: (event) => {
|
|
596
|
+
event.stopPropagation();
|
|
597
|
+
close();
|
|
598
|
+
}, children: (0, jsx_runtime_1.jsxs)("svg", { viewBox: "0 0 17 17", fill: "none", "aria-hidden": "true", children: [(0, jsx_runtime_1.jsx)("path", { d: "M15.959 0.40332L0.402635 15.9597", stroke: "currentColor", strokeWidth: "1.14" }), (0, jsx_runtime_1.jsx)("path", { d: "M15.959 15.9594L0.402635 0.403002", stroke: "currentColor", strokeWidth: "1.14" })] }) })] })] }));
|
|
599
|
+
};
|