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