@donotdev/components 0.2.1 → 0.2.3
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/dist/{CarouselContent-F4JSFIQD.js → CarouselContent-MQPZBYOS.js} +1 -1
- package/dist/CodeContent-64D4U3NM.js +2 -0
- package/dist/{CrawlContent-6LR66N57.js → CrawlContent-5Z6AVDDW.js} +1 -1
- package/dist/{MarqueeContent-NBXB2YLR.js → MarqueeContent-KAYCOUNM.js} +1 -1
- package/dist/advanced/Bento/Bento.d.ts.map +1 -1
- package/dist/advanced/Carousel/CarouselContent.d.ts.map +1 -1
- package/dist/advanced/Carousel/CarouselContent.js +7 -4
- package/dist/advanced/Code/CodeContent.js +3 -5
- package/dist/advanced/Crawl/utils/parseHtml.d.ts.map +1 -1
- package/dist/advanced/Crawl/utils/parseHtml.js +9 -2
- package/dist/advanced/ImageGallery/GalleryArrows.d.ts +24 -0
- package/dist/advanced/ImageGallery/GalleryArrows.d.ts.map +1 -0
- package/dist/advanced/ImageGallery/GalleryArrows.js +13 -0
- package/dist/advanced/ImageGallery/ImageGallery.d.ts +6 -4
- package/dist/advanced/ImageGallery/ImageGallery.d.ts.map +1 -1
- package/dist/advanced/ImageGallery/ImageGallery.js +116 -147
- package/dist/advanced/ImageGallery/LightboxViewer.d.ts +39 -0
- package/dist/advanced/ImageGallery/LightboxViewer.d.ts.map +1 -0
- package/dist/advanced/ImageGallery/LightboxViewer.js +138 -0
- package/dist/advanced/ImageGallery/lightboxGestures.d.ts +28 -0
- package/dist/advanced/ImageGallery/lightboxGestures.d.ts.map +1 -0
- package/dist/advanced/ImageGallery/lightboxGestures.js +425 -0
- package/dist/advanced/ImageGallery/protectImages.d.ts +19 -0
- package/dist/advanced/ImageGallery/protectImages.d.ts.map +1 -0
- package/dist/advanced/ImageGallery/protectImages.js +10 -0
- package/dist/advanced/ImageGallery/snapStrip.d.ts +29 -0
- package/dist/advanced/ImageGallery/snapStrip.d.ts.map +1 -0
- package/dist/advanced/ImageGallery/snapStrip.js +74 -0
- package/dist/advanced/Marquee/MarqueeContent.d.ts.map +1 -1
- package/dist/advanced/Marquee/MarqueeContent.js +7 -6
- package/dist/advanced/Reveal/Reveal.d.ts.map +1 -1
- package/dist/advanced/Reveal/Reveal.js +3 -2
- package/dist/advanced/Roadmap/RoadmapContent.d.ts.map +1 -1
- package/dist/advanced/Roadmap/RoadmapContent.js +2 -1
- package/dist/atomic/ActionButton/index.d.ts +7 -3
- package/dist/atomic/ActionButton/index.d.ts.map +1 -1
- package/dist/atomic/ActionButton/index.js +2 -2
- package/dist/atomic/Button/index.d.ts +1 -1
- package/dist/atomic/Calendar/index.d.ts.map +1 -1
- package/dist/atomic/Calendar/index.js +1 -0
- package/dist/atomic/Card/cover.d.ts +13 -8
- package/dist/atomic/Card/cover.d.ts.map +1 -1
- package/dist/atomic/Card/cover.js +13 -6
- package/dist/atomic/Card/index.d.ts +17 -8
- package/dist/atomic/Card/index.d.ts.map +1 -1
- package/dist/atomic/Card/index.js +8 -4
- package/dist/atomic/ColorPicker/index.d.ts.map +1 -1
- package/dist/atomic/ColorPicker/index.js +3 -2
- package/dist/atomic/Combobox/index.d.ts.map +1 -1
- package/dist/atomic/Combobox/index.js +3 -2
- package/dist/atomic/Grid/index.d.ts.map +1 -1
- package/dist/atomic/Grid/index.js +11 -6
- package/dist/atomic/Icons/Partner/InstagramIcon.d.ts +13 -0
- package/dist/atomic/Icons/Partner/InstagramIcon.d.ts.map +1 -0
- package/dist/atomic/Icons/Partner/InstagramIcon.js +29 -0
- package/dist/atomic/Icons/Partner/index.d.ts +1 -0
- package/dist/atomic/Icons/Partner/index.d.ts.map +1 -1
- package/dist/atomic/Icons/Partner/index.js +1 -0
- package/dist/atomic/Icons/Partner/providerIcons.d.ts.map +1 -1
- package/dist/atomic/Icons/Partner/providerIcons.js +6 -1
- package/dist/atomic/NavigationMenu/index.d.ts.map +1 -1
- package/dist/atomic/NavigationMenu/index.js +2 -2
- package/dist/atomic/PasswordInput/index.d.ts +1 -1
- package/dist/atomic/PasswordInput/index.d.ts.map +1 -1
- package/dist/atomic/PasswordInput/index.js +4 -2
- package/dist/atomic/Switch/index.d.ts +14 -11
- package/dist/atomic/Switch/index.d.ts.map +1 -1
- package/dist/atomic/Switch/index.js +21 -10
- package/dist/atomic/Table/editing.d.ts +109 -0
- package/dist/atomic/Table/editing.d.ts.map +1 -0
- package/dist/atomic/Table/editing.js +170 -0
- package/dist/atomic/Table/index.d.ts +42 -1
- package/dist/atomic/Table/index.d.ts.map +1 -1
- package/dist/atomic/Table/index.js +159 -54
- package/dist/atomic/Text/index.d.ts +0 -9
- package/dist/atomic/Text/index.d.ts.map +1 -1
- package/dist/atomic/Text/index.js +14 -7
- package/dist/atomic/index.d.ts +3 -2
- package/dist/atomic/index.d.ts.map +1 -1
- package/dist/atomic/index.js +1 -0
- package/dist/chunk-3WHSDDVJ.js +1 -0
- package/dist/chunk-CIBFOOWV.js +1 -0
- package/dist/chunk-JAV36ZQC.js +3 -0
- package/dist/hooks/useSwipeToClose.d.ts.map +1 -1
- package/dist/hooks/useSwipeToClose.js +9 -2
- package/dist/index.js +9 -6
- package/dist/styles/index.css +431 -119
- package/dist/symbol-index.json +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/cssVars.d.ts +22 -0
- package/dist/utils/cssVars.d.ts.map +1 -0
- package/dist/utils/cssVars.js +10 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +2 -0
- package/dist/utils/intersectionObserver.d.ts.map +1 -1
- package/dist/utils/intersectionObserver.js +4 -1
- package/dist/utils/linkComponent.d.ts +26 -0
- package/dist/utils/linkComponent.d.ts.map +1 -0
- package/dist/utils/linkComponent.js +37 -0
- package/dist/utils/responsiveImage.d.ts +55 -0
- package/dist/utils/responsiveImage.d.ts.map +1 -0
- package/dist/utils/responsiveImage.js +45 -0
- package/guidelines/COMPONENTS.md +2 -1
- package/package.json +1 -1
- package/dist/CarouselContent-OQEW3MTR.css +0 -1
- package/dist/CarouselContent-UJARFDSE.js +0 -1
- package/dist/CodeContent-7KN6DQT2.js +0 -2
- package/dist/CodeContent-ENVN76NP.css +0 -1
- package/dist/CodeContent-JLL6BLG2.js +0 -2
- package/dist/CodeContent-S3BDX46F.js +0 -2
- package/dist/MarqueeContent-AL2RBZAP.css +0 -1
- package/dist/MarqueeContent-EKASHCRG.js +0 -1
- package/dist/chunk-2CNNVCFI.js +0 -1
- package/dist/chunk-6UB4NXNR.js +0 -3
- package/dist/chunk-E7SJFHOC.js +0 -3
- package/dist/chunk-EOCKFSHC.js +0 -1
- package/dist/chunk-FA4BDD6N.js +0 -1
- package/dist/chunk-RIB7YURO.js +0 -3
|
@@ -1,18 +1,35 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
import { jsx as _jsx,
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
3
|
// packages/components/src/advanced/ImageGallery/ImageGallery.tsx
|
|
4
4
|
/**
|
|
5
5
|
* @fileoverview ImageGallery component
|
|
6
|
-
* @description
|
|
6
|
+
* @description Image gallery: swipeable main view, arrows, keyboard navigation, wrapping
|
|
7
|
+
* thumbnails and a full-screen zoomable lightbox.
|
|
7
8
|
*
|
|
8
|
-
* @version 0.
|
|
9
|
+
* @version 0.2.0
|
|
9
10
|
* @since 0.0.1
|
|
10
11
|
* @author AMBROISE PARK Consulting
|
|
11
12
|
*/
|
|
12
|
-
import {
|
|
13
|
+
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
14
|
+
import { flushSync } from 'react-dom';
|
|
13
15
|
import Dialog from '../../atomic/Dialog';
|
|
14
16
|
import { handleImageError } from '../../utils/imageRetry';
|
|
17
|
+
import { ResponsiveImg } from '../../utils/responsiveImage';
|
|
18
|
+
import { GalleryArrows } from './GalleryArrows';
|
|
19
|
+
import { LightboxViewer, prepareFrame } from './LightboxViewer';
|
|
20
|
+
import { blockContextMenu } from './protectImages';
|
|
21
|
+
import { arrowStep, isNearSlide, scrollToSlide, useCurrentSlide, } from './snapStrip';
|
|
15
22
|
import './ImageGallery.css';
|
|
23
|
+
/** `view-transition-name` of the image morphing between the gallery and the lightbox. */
|
|
24
|
+
const MORPH_NAME = 'image-gallery-image';
|
|
25
|
+
/**
|
|
26
|
+
* Whether open / close can morph: view transitions supported and motion allowed.
|
|
27
|
+
* Same conditions as the CSS that turns the dialog's own fade off.
|
|
28
|
+
*/
|
|
29
|
+
function canMorph() {
|
|
30
|
+
return (typeof document.startViewTransition === 'function' &&
|
|
31
|
+
!window.matchMedia('(prefers-reduced-motion: reduce)').matches);
|
|
32
|
+
}
|
|
16
33
|
/**
|
|
17
34
|
* Normalize image data to consistent format
|
|
18
35
|
*/
|
|
@@ -30,11 +47,13 @@ function normalizeImage(img, fit) {
|
|
|
30
47
|
* ImageGallery - Full-featured image gallery component
|
|
31
48
|
*
|
|
32
49
|
* Features:
|
|
33
|
-
* -
|
|
34
|
-
* - Previous/Next buttons
|
|
50
|
+
* - Main view swipes natively (CSS scroll-snap, browser-driven scrolling)
|
|
51
|
+
* - Previous/Next buttons, keyboard navigation (← → Home End)
|
|
35
52
|
* - Wrapping thumbnail grid
|
|
36
|
-
* -
|
|
37
|
-
*
|
|
53
|
+
* - Lightbox on main image click (opt out with `nobox`): the image morphs into it and
|
|
54
|
+
* back (view transition); swipe to page, swipe up/down to close, pinch / double-tap /
|
|
55
|
+
* ctrl+wheel to zoom, drag to pan with momentum and elastic edges
|
|
56
|
+
* - Current and neighbouring images load eagerly, the rest lazily
|
|
38
57
|
*
|
|
39
58
|
* @example
|
|
40
59
|
* ```tsx
|
|
@@ -50,163 +69,113 @@ function normalizeImage(img, fit) {
|
|
|
50
69
|
export function ImageGallery({ images, aspectRatio = '16/9', altPrefix = 'Image', onImageChange, nobox, fit = 'cover', }) {
|
|
51
70
|
const [selectedIndex, setSelectedIndex] = useState(0);
|
|
52
71
|
const [lightboxOpen, setLightboxOpen] = useState(false);
|
|
53
|
-
const
|
|
54
|
-
const
|
|
55
|
-
const
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
const
|
|
60
|
-
const hasMultiple = normalizedImages.length > 1;
|
|
61
|
-
// Trim stale refs when the image list shrinks
|
|
62
|
-
thumbnailRefs.current = thumbnailRefs.current.slice(0, normalizedImages.length);
|
|
72
|
+
const stripRef = useRef(null);
|
|
73
|
+
const thumbnailsRef = useRef(null);
|
|
74
|
+
const lightboxFrameRef = useRef(null);
|
|
75
|
+
const normalizedImages = useMemo(() => images.map((img) => normalizeImage(img, fit)), [images, fit]);
|
|
76
|
+
const count = normalizedImages.length;
|
|
77
|
+
const hasMultiple = count > 1;
|
|
78
|
+
const clampedIndex = Math.min(selectedIndex, Math.max(count - 1, 0));
|
|
63
79
|
// Clamp selectedIndex when images array shrinks
|
|
64
80
|
useEffect(() => {
|
|
65
|
-
if (
|
|
66
|
-
|
|
67
|
-
setSelectedIndex(normalizedImages.length - 1);
|
|
81
|
+
if (count > 0 && selectedIndex >= count) {
|
|
82
|
+
setSelectedIndex(count - 1);
|
|
68
83
|
}
|
|
69
|
-
}, [
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
84
|
+
}, [count, selectedIndex]);
|
|
85
|
+
/** Updates the current index; reports it when it changed. */
|
|
86
|
+
const commitIndex = (index) => {
|
|
87
|
+
if (index === clampedIndex)
|
|
88
|
+
return;
|
|
89
|
+
setSelectedIndex(index);
|
|
90
|
+
onImageChange?.(index);
|
|
91
|
+
};
|
|
92
|
+
/** Navigation from buttons, keys, thumbnails and the lightbox. Wraps around. */
|
|
93
|
+
const goToImage = (raw, instant = false) => {
|
|
94
|
+
const index = (raw + count) % count;
|
|
95
|
+
commitIndex(index);
|
|
96
|
+
if (stripRef.current)
|
|
97
|
+
scrollToSlide(stripRef.current, index, instant);
|
|
98
|
+
return index;
|
|
99
|
+
};
|
|
100
|
+
// Swipes / trackpad scrolls are native: the slide that is mostly visible is current.
|
|
101
|
+
useCurrentSlide(stripRef, count, commitIndex);
|
|
102
|
+
/**
|
|
103
|
+
* Opens / closes the lightbox. With view transitions, the current image morphs:
|
|
104
|
+
* gallery slide -> lightbox frame on open, back on close (whatever closes it: swipe,
|
|
105
|
+
* background tap, close button, Esc). One element carries the name at a time.
|
|
106
|
+
*/
|
|
107
|
+
const setLightbox = (open) => {
|
|
108
|
+
if (open === lightboxOpen)
|
|
109
|
+
return;
|
|
110
|
+
const slide = stripRef.current?.children[clampedIndex];
|
|
111
|
+
const from = open ? slide : lightboxFrameRef.current;
|
|
112
|
+
if (!canMorph() || !(from instanceof HTMLElement)) {
|
|
113
|
+
setLightboxOpen(open);
|
|
114
|
+
return;
|
|
89
115
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
116
|
+
from.style.viewTransitionName = MORPH_NAME;
|
|
117
|
+
let to = null;
|
|
118
|
+
const transition = document.startViewTransition(async () => {
|
|
119
|
+
from.style.viewTransitionName = '';
|
|
120
|
+
flushSync(() => setLightboxOpen(open));
|
|
121
|
+
const target = open ? lightboxFrameRef.current : slide;
|
|
122
|
+
if (!(target instanceof HTMLElement))
|
|
123
|
+
return;
|
|
124
|
+
if (open)
|
|
125
|
+
await prepareFrame(target);
|
|
126
|
+
target.style.viewTransitionName = MORPH_NAME;
|
|
127
|
+
to = target;
|
|
128
|
+
});
|
|
129
|
+
void transition.finished.then(() => {
|
|
130
|
+
if (to)
|
|
131
|
+
to.style.viewTransitionName = '';
|
|
132
|
+
});
|
|
133
|
+
};
|
|
134
|
+
const handleMainAreaClick = (e) => {
|
|
94
135
|
if (nobox)
|
|
95
136
|
return;
|
|
96
137
|
if (e.target.closest('.image-gallery__nav'))
|
|
97
138
|
return;
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
const handleLightboxKeyDown = useCallback((e) => {
|
|
101
|
-
if (!hasMultiple)
|
|
102
|
-
return;
|
|
103
|
-
switch (e.key) {
|
|
104
|
-
case 'ArrowLeft':
|
|
105
|
-
e.preventDefault();
|
|
106
|
-
goPrev();
|
|
107
|
-
break;
|
|
108
|
-
case 'ArrowRight':
|
|
109
|
-
e.preventDefault();
|
|
110
|
-
goNext();
|
|
111
|
-
break;
|
|
112
|
-
default:
|
|
113
|
-
break;
|
|
114
|
-
}
|
|
115
|
-
}, [hasMultiple, goPrev, goNext]);
|
|
139
|
+
setLightbox(true);
|
|
140
|
+
};
|
|
116
141
|
useEffect(() => {
|
|
117
|
-
if (
|
|
142
|
+
if (count === 0)
|
|
118
143
|
setLightboxOpen(false);
|
|
119
|
-
}, [
|
|
144
|
+
}, [count]);
|
|
120
145
|
useEffect(() => {
|
|
121
146
|
if (nobox)
|
|
122
147
|
setLightboxOpen(false);
|
|
123
148
|
}, [nobox]);
|
|
124
|
-
|
|
125
|
-
|
|
149
|
+
// Keyboard navigation; focus follows the selected thumbnail (tablist pattern).
|
|
150
|
+
const handleKeyDown = (e) => {
|
|
151
|
+
if (!hasMultiple || lightboxOpen)
|
|
126
152
|
return;
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
153
|
+
const step = arrowStep(e);
|
|
154
|
+
let target;
|
|
155
|
+
if (step !== 0)
|
|
156
|
+
target = clampedIndex + step;
|
|
157
|
+
else if (e.key === 'Home')
|
|
158
|
+
target = 0;
|
|
159
|
+
else if (e.key === 'End')
|
|
160
|
+
target = count - 1;
|
|
161
|
+
else
|
|
135
162
|
return;
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
e.preventDefault();
|
|
143
|
-
goNext();
|
|
144
|
-
break;
|
|
145
|
-
case 'Home':
|
|
146
|
-
e.preventDefault();
|
|
147
|
-
goToImage(0);
|
|
148
|
-
break;
|
|
149
|
-
case 'End':
|
|
150
|
-
e.preventDefault();
|
|
151
|
-
goToImage(normalizedImages.length - 1);
|
|
152
|
-
break;
|
|
153
|
-
}
|
|
154
|
-
}, [hasMultiple, goPrev, goNext, goToImage, normalizedImages.length]);
|
|
155
|
-
// Focus thumbnail only on user-driven navigation, not on initial mount (WCAG)
|
|
156
|
-
useEffect(() => {
|
|
157
|
-
if (!isUserNavigationRef.current)
|
|
158
|
-
return;
|
|
159
|
-
isUserNavigationRef.current = false;
|
|
160
|
-
thumbnailRefs.current[selectedIndex]?.focus();
|
|
161
|
-
}, [selectedIndex]);
|
|
162
|
-
if (normalizedImages.length === 0) {
|
|
163
|
+
e.preventDefault();
|
|
164
|
+
const thumbnail = thumbnailsRef.current?.children[goToImage(target)];
|
|
165
|
+
if (thumbnail instanceof HTMLElement)
|
|
166
|
+
thumbnail.focus();
|
|
167
|
+
};
|
|
168
|
+
if (count === 0) {
|
|
163
169
|
return (_jsx("div", { className: "image-gallery image-gallery--empty", style: { aspectRatio }, children: _jsx("span", { className: "image-gallery__placeholder", children: "No images" }) }));
|
|
164
170
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
position: 'absolute',
|
|
173
|
-
inset: 0,
|
|
174
|
-
width: '100%',
|
|
175
|
-
height: '100%',
|
|
176
|
-
objectFit: currentImage.fit,
|
|
177
|
-
...containInset,
|
|
178
|
-
opacity: loadedFullUrls.has(clampedIndex) ? 0 : 1,
|
|
179
|
-
transition: 'opacity 0.3s ease',
|
|
180
|
-
zIndex: 1,
|
|
181
|
-
} })), _jsx("img", { src: currentImage.full, alt: `${altPrefix} ${clampedIndex + 1} of ${normalizedImages.length}`, className: "image-gallery__main-image", style: {
|
|
182
|
-
position: currentImage.full !== currentImage.thumb
|
|
183
|
-
? 'absolute'
|
|
184
|
-
: 'relative',
|
|
185
|
-
inset: 0,
|
|
186
|
-
width: '100%',
|
|
187
|
-
height: '100%',
|
|
188
|
-
objectFit: currentImage.fit,
|
|
189
|
-
...containInset,
|
|
190
|
-
opacity: loadedFullUrls.has(clampedIndex) ||
|
|
191
|
-
currentImage.full === currentImage.thumb
|
|
192
|
-
? 1
|
|
193
|
-
: 0,
|
|
194
|
-
transition: 'opacity 0.3s ease',
|
|
195
|
-
zIndex: 2,
|
|
196
|
-
}, onError: handleImageError, onLoad: () => {
|
|
197
|
-
if (currentImage.full !== currentImage.thumb) {
|
|
198
|
-
setLoadedFullUrls((prev) => new Set(prev).add(clampedIndex));
|
|
199
|
-
}
|
|
200
|
-
} }), hasMultiple && (_jsxs(_Fragment, { children: [_jsx("button", { type: "button", className: "image-gallery__nav image-gallery__nav--prev", onClick: goPrev, "aria-label": "Previous image", children: _jsx("svg", { width: "24", height: "24", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", children: _jsx("polyline", { points: "15 18 9 12 15 6" }) }) }), _jsx("button", { type: "button", className: "image-gallery__nav image-gallery__nav--next", onClick: goNext, "aria-label": "Next image", children: _jsx("svg", { width: "24", height: "24", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", children: _jsx("polyline", { points: "9 18 15 12 9 6" }) }) })] }))] }), hasMultiple && (_jsx("div", { className: "image-gallery__thumbnails", role: "tablist", "aria-label": "Image thumbnails", children: normalizedImages.map((img, index) => (_jsx("button", { ref: (el) => {
|
|
201
|
-
thumbnailRefs.current[index] = el;
|
|
202
|
-
}, type: "button", role: "tab", "aria-selected": index === clampedIndex, "aria-label": `View image ${index + 1}`, className: `image-gallery__thumbnail ${index === clampedIndex
|
|
171
|
+
return (_jsxs("div", { className: "image-gallery", onKeyDown: handleKeyDown, onContextMenu: blockContextMenu, tabIndex: 0, role: "region", "aria-label": "Image gallery", "aria-roledescription": "carousel", children: [_jsxs("div", { className: `image-gallery__main${nobox ? '' : ' image-gallery__main--lightbox'}`, style: { aspectRatio }, onClick: handleMainAreaClick, children: [_jsx("div", { ref: stripRef, className: "image-gallery__strip", children: normalizedImages.map((img, index) => {
|
|
172
|
+
// Current and neighbours load now, so a swipe never reveals an empty slide.
|
|
173
|
+
const loading = isNearSlide(index, clampedIndex, count)
|
|
174
|
+
? 'eager'
|
|
175
|
+
: 'lazy';
|
|
176
|
+
return (_jsxs("div", { className: "image-gallery__slide", "aria-hidden": index === clampedIndex ? undefined : true, children: [img.fit === 'cover' && img.full !== img.thumb && (_jsx("img", { src: img.thumb, alt: "", className: "image-gallery__slide-img image-gallery__slide-placeholder", loading: loading, decoding: "async", draggable: false, onError: handleImageError })), _jsx(ResponsiveImg, { image: { fullUrl: img.full, thumbUrl: img.thumb }, alt: `${altPrefix} ${index + 1} of ${count}`, className: "image-gallery__slide-img", "data-fit": img.fit, loading: loading, decoding: "async", draggable: false, onError: handleImageError })] }, index));
|
|
177
|
+
}) }), hasMultiple && (_jsx(GalleryArrows, { onPrev: () => goToImage(clampedIndex - 1), onNext: () => goToImage(clampedIndex + 1) }))] }), hasMultiple && (_jsx("div", { ref: thumbnailsRef, className: "image-gallery__thumbnails", role: "tablist", "aria-label": "Image thumbnails", children: normalizedImages.map((img, index) => (_jsx("button", { type: "button", role: "tab", "aria-selected": index === clampedIndex, "aria-label": `View image ${index + 1}`, className: `image-gallery__thumbnail ${index === clampedIndex
|
|
203
178
|
? 'image-gallery__thumbnail--selected'
|
|
204
|
-
: ''}`, onClick: () => goToImage(index), children: _jsx("img", { src: img.thumb, alt: "", className: "image-gallery__thumbnail-image", "data-fit": img.fit, onError: handleImageError }) }, index))) })), !nobox && (_jsx(Dialog, { open: lightboxOpen, onOpenChange:
|
|
205
|
-
e.stopPropagation();
|
|
206
|
-
goPrev();
|
|
207
|
-
}, "aria-label": "Previous image", children: _jsx("svg", { width: "24", height: "24", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", children: _jsx("polyline", { points: "15 18 9 12 15 6" }) }) }), _jsx("button", { type: "button", className: "image-gallery__nav image-gallery__nav--next image-gallery__lightbox-nav", onClick: (e) => {
|
|
208
|
-
e.stopPropagation();
|
|
209
|
-
goNext();
|
|
210
|
-
}, "aria-label": "Next image", children: _jsx("svg", { width: "24", height: "24", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", children: _jsx("polyline", { points: "9 18 15 12 9 6" }) }) })] }))] }) }))] }));
|
|
179
|
+
: ''}`, onClick: () => goToImage(index), children: _jsx("img", { src: img.thumb, alt: "", className: "image-gallery__thumbnail-image", "data-fit": img.fit, draggable: false, onError: handleImageError }) }, index))) })), !nobox && (_jsx(Dialog, { open: lightboxOpen, onOpenChange: setLightbox, showClose: true, noSwipe: true, className: "image-gallery__lightbox", children: _jsx(LightboxViewer, { images: normalizedImages, index: clampedIndex, onIndexChange: (index) => goToImage(index, true), onClose: () => setLightbox(false), altPrefix: altPrefix, frameRef: lightboxFrameRef }) }))] }));
|
|
211
180
|
}
|
|
212
181
|
export default ImageGallery;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { ReactElement, RefObject } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Waits until a frame shows its image (the cached thumbnail decodes at once) and sizes
|
|
4
|
+
* it. For the open morph: the new state must be captured with the image painted.
|
|
5
|
+
* A decode failure (broken image) does not block: the frame is shown as it is.
|
|
6
|
+
*
|
|
7
|
+
* @param frame - The current frame (`frameRef`)
|
|
8
|
+
*/
|
|
9
|
+
export declare function prepareFrame(frame: HTMLElement): Promise<void>;
|
|
10
|
+
/** Props for {@link LightboxViewer}. */
|
|
11
|
+
export interface LightboxViewerProps {
|
|
12
|
+
/** Images: full-size and thumbnail URLs */
|
|
13
|
+
images: ReadonlyArray<{
|
|
14
|
+
full: string;
|
|
15
|
+
thumb: string;
|
|
16
|
+
}>;
|
|
17
|
+
/** Index of the image shown */
|
|
18
|
+
index: number;
|
|
19
|
+
/** Called with the new index after a page turn */
|
|
20
|
+
onIndexChange: (index: number) => void;
|
|
21
|
+
/** Called when a gesture (swipe up/down, background tap) closes the viewer */
|
|
22
|
+
onClose: () => void;
|
|
23
|
+
/** Alt text prefix for images */
|
|
24
|
+
altPrefix: string;
|
|
25
|
+
/** Set to the current image's frame (the open / close morph target) */
|
|
26
|
+
frameRef: RefObject<HTMLDivElement | null>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Full-screen image viewer. Render it inside a dialog; it fills its container and
|
|
30
|
+
* opens on `index`.
|
|
31
|
+
*
|
|
32
|
+
* Known limitation: a pinch that starts once a one-finger horizontal swipe is under way
|
|
33
|
+
* is ignored (the browser owns that touch for scrolling); lift and pinch again.
|
|
34
|
+
*
|
|
35
|
+
* @param props - LightboxViewerProps
|
|
36
|
+
* @returns The viewer
|
|
37
|
+
*/
|
|
38
|
+
export declare function LightboxViewer({ images, index, onIndexChange, onClose, altPrefix, frameRef, }: LightboxViewerProps): ReactElement;
|
|
39
|
+
//# sourceMappingURL=LightboxViewer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LightboxViewer.d.ts","sourceRoot":"","sources":["../../../src/advanced/ImageGallery/LightboxViewer.tsx"],"names":[],"mappings":"AAyBA,OAAO,KAAK,EAAiB,YAAY,EAAE,SAAS,EAAkB,MAAM,OAAO,CAAC;AAqCpF;;;;;;GAMG;AACH,wBAAsB,YAAY,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CASpE;AAED,wCAAwC;AACxC,MAAM,WAAW,mBAAmB;IAClC,2CAA2C;IAC3C,MAAM,EAAE,aAAa,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACvD,+BAA+B;IAC/B,KAAK,EAAE,MAAM,CAAC;IACd,kDAAkD;IAClD,aAAa,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACvC,8EAA8E;IAC9E,OAAO,EAAE,MAAM,IAAI,CAAC;IACpB,iCAAiC;IACjC,SAAS,EAAE,MAAM,CAAC;IAClB,uEAAuE;IACvE,QAAQ,EAAE,SAAS,CAAC,cAAc,GAAG,IAAI,CAAC,CAAC;CAC5C;AAED;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,EAC7B,MAAM,EACN,KAAK,EACL,aAAa,EACb,OAAO,EACP,SAAS,EACT,QAAQ,GACT,EAAE,mBAAmB,GAAG,YAAY,CA+HpC"}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
3
|
+
// packages/components/src/advanced/ImageGallery/LightboxViewer.tsx
|
|
4
|
+
/**
|
|
5
|
+
* @fileoverview Lightbox viewer
|
|
6
|
+
* @description Full-screen image viewer for ImageGallery.
|
|
7
|
+
*
|
|
8
|
+
* - Paging: a CSS scroll-snap strip (native swipe, momentum, snapping; no JS per frame).
|
|
9
|
+
* - Swipe up/down to close, pinch / double-tap / click / ctrl+wheel to zoom, drag to
|
|
10
|
+
* pan: `lightboxGestures.ts`, attached to the current slide.
|
|
11
|
+
* - React renders only when the index changes. Only the current image and its two
|
|
12
|
+
* neighbours are mounted, so the next image is already loaded when a swipe starts
|
|
13
|
+
* and decoded full-size images stay bounded at three. The current one is fetched at
|
|
14
|
+
* high priority, the neighbours at low.
|
|
15
|
+
* - Each image sits in a frame over its cached thumbnail, so the viewer never shows an
|
|
16
|
+
* empty slide while a full-size image downloads. The frame takes the image's aspect
|
|
17
|
+
* ratio as soon as either rendition knows it (CSS sizes it), so it is exactly the
|
|
18
|
+
* image: the zoom / pan / hit-test target and the open / close morph target.
|
|
19
|
+
*
|
|
20
|
+
* @version 0.2.0
|
|
21
|
+
* @since 0.2.2
|
|
22
|
+
* @author AMBROISE PARK Consulting
|
|
23
|
+
*/
|
|
24
|
+
import { useEffect, useEffectEvent, useLayoutEffect, useRef } from 'react';
|
|
25
|
+
import { handleImageError } from '../../utils/imageRetry';
|
|
26
|
+
import { GalleryArrows } from './GalleryArrows';
|
|
27
|
+
import { attachLightboxGestures } from './lightboxGestures';
|
|
28
|
+
import { blockContextMenu } from './protectImages';
|
|
29
|
+
import { arrowStep, isNearSlide, scrollToSlide, useCurrentSlide, } from './snapStrip';
|
|
30
|
+
/**
|
|
31
|
+
* Gives a frame the aspect ratio of `img` (any rendition), once. CSS then sizes the frame
|
|
32
|
+
* to the image fitted in the slide.
|
|
33
|
+
*/
|
|
34
|
+
function sizeFrame(frame, img) {
|
|
35
|
+
if (frame.hasAttribute('data-sized') || img.naturalWidth === 0)
|
|
36
|
+
return;
|
|
37
|
+
frame.style.setProperty('--image-ratio', String(img.naturalWidth / img.naturalHeight));
|
|
38
|
+
frame.setAttribute('data-sized', '');
|
|
39
|
+
}
|
|
40
|
+
/** Load handler of the frame images: size the frame; the full image hides the placeholder. */
|
|
41
|
+
function onFrameImageLoad(e) {
|
|
42
|
+
const img = e.currentTarget;
|
|
43
|
+
const frame = img.parentElement;
|
|
44
|
+
if (!frame)
|
|
45
|
+
return;
|
|
46
|
+
sizeFrame(frame, img);
|
|
47
|
+
if (img.classList.contains('image-gallery__lightbox-img')) {
|
|
48
|
+
frame.setAttribute('data-loaded', '');
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Waits until a frame shows its image (the cached thumbnail decodes at once) and sizes
|
|
53
|
+
* it. For the open morph: the new state must be captured with the image painted.
|
|
54
|
+
* A decode failure (broken image) does not block: the frame is shown as it is.
|
|
55
|
+
*
|
|
56
|
+
* @param frame - The current frame (`frameRef`)
|
|
57
|
+
*/
|
|
58
|
+
export async function prepareFrame(frame) {
|
|
59
|
+
const img = frame.querySelector('img');
|
|
60
|
+
if (!img)
|
|
61
|
+
return;
|
|
62
|
+
try {
|
|
63
|
+
await img.decode();
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
sizeFrame(frame, img);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Full-screen image viewer. Render it inside a dialog; it fills its container and
|
|
72
|
+
* opens on `index`.
|
|
73
|
+
*
|
|
74
|
+
* Known limitation: a pinch that starts once a one-finger horizontal swipe is under way
|
|
75
|
+
* is ignored (the browser owns that touch for scrolling); lift and pinch again.
|
|
76
|
+
*
|
|
77
|
+
* @param props - LightboxViewerProps
|
|
78
|
+
* @returns The viewer
|
|
79
|
+
*/
|
|
80
|
+
export function LightboxViewer({ images, index, onIndexChange, onClose, altPrefix, frameRef, }) {
|
|
81
|
+
const count = images.length;
|
|
82
|
+
const rootRef = useRef(null);
|
|
83
|
+
const backdropRef = useRef(null);
|
|
84
|
+
const stripRef = useRef(null);
|
|
85
|
+
const openAtIndex = useEffectEvent(() => {
|
|
86
|
+
if (stripRef.current)
|
|
87
|
+
scrollToSlide(stripRef.current, index, true);
|
|
88
|
+
});
|
|
89
|
+
const close = useEffectEvent(onClose);
|
|
90
|
+
// Open on the gallery's image, before paint, without animation. Focus the viewer so
|
|
91
|
+
// the arrow keys work at once.
|
|
92
|
+
useLayoutEffect(() => {
|
|
93
|
+
openAtIndex();
|
|
94
|
+
const id = requestAnimationFrame(() => rootRef.current?.focus());
|
|
95
|
+
return () => cancelAnimationFrame(id);
|
|
96
|
+
}, []);
|
|
97
|
+
// Native swipes: the slide that is mostly visible becomes the index.
|
|
98
|
+
useCurrentSlide(stripRef, count, onIndexChange);
|
|
99
|
+
// Close / zoom / pan gestures act on the current slide. Re-attaching on a page turn
|
|
100
|
+
// is what resets the zoom.
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
const root = rootRef.current;
|
|
103
|
+
const backdrop = backdropRef.current;
|
|
104
|
+
const slide = stripRef.current?.children[index];
|
|
105
|
+
const frame = frameRef.current;
|
|
106
|
+
const img = frame?.querySelector('.image-gallery__lightbox-img');
|
|
107
|
+
if (!root ||
|
|
108
|
+
!backdrop ||
|
|
109
|
+
!frame ||
|
|
110
|
+
!(slide instanceof HTMLElement) ||
|
|
111
|
+
!(img instanceof HTMLImageElement)) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
return attachLightboxGestures({
|
|
115
|
+
root,
|
|
116
|
+
backdrop,
|
|
117
|
+
slide,
|
|
118
|
+
frame,
|
|
119
|
+
img,
|
|
120
|
+
onClose: () => close(),
|
|
121
|
+
});
|
|
122
|
+
}, [index]);
|
|
123
|
+
/** Page turn from a button or key. Wraps around. */
|
|
124
|
+
const go = (step) => {
|
|
125
|
+
const target = (index + step + count) % count;
|
|
126
|
+
onIndexChange(target);
|
|
127
|
+
if (stripRef.current)
|
|
128
|
+
scrollToSlide(stripRef.current, target);
|
|
129
|
+
};
|
|
130
|
+
const handleKeyDown = (e) => {
|
|
131
|
+
const step = arrowStep(e);
|
|
132
|
+
if (step === 0 || count < 2)
|
|
133
|
+
return;
|
|
134
|
+
e.preventDefault();
|
|
135
|
+
go(step);
|
|
136
|
+
};
|
|
137
|
+
return (_jsxs("div", { ref: rootRef, className: "image-gallery__lightbox-stage", tabIndex: -1, onKeyDown: handleKeyDown, onContextMenu: blockContextMenu, children: [_jsx("div", { ref: backdropRef, className: "image-gallery__lightbox-backdrop" }), _jsx("div", { ref: stripRef, className: "image-gallery__strip image-gallery__lightbox-strip", children: images.map((image, i) => (_jsx("div", { className: "image-gallery__slide image-gallery__lightbox-slide", "aria-hidden": i === index ? undefined : true, children: isNearSlide(i, index, count) && (_jsxs("div", { ref: i === index ? frameRef : undefined, className: "image-gallery__lightbox-frame", children: [image.thumb !== image.full && (_jsx("img", { src: image.thumb, alt: "", className: "image-gallery__lightbox-placeholder", draggable: false, onLoad: onFrameImageLoad, onError: handleImageError })), _jsx("img", { src: image.full, alt: `${altPrefix} ${i + 1} of ${count}`, className: "image-gallery__lightbox-img", decoding: "async", fetchPriority: i === index ? 'high' : 'low', draggable: false, onLoad: onFrameImageLoad, onError: handleImageError })] })) }, i))) }), count > 1 && (_jsxs(_Fragment, { children: [_jsxs("span", { className: "image-gallery__lightbox-counter", "aria-live": "polite", children: [index + 1, " / ", count] }), _jsx(GalleryArrows, { className: "image-gallery__lightbox-nav", onPrev: () => go(-1), onNext: () => go(1) })] }))] }));
|
|
138
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** Elements and callback of {@link attachLightboxGestures}. */
|
|
2
|
+
export interface LightboxGestureTargets {
|
|
3
|
+
/** The viewer: receives the pointer and wheel events and the state attributes */
|
|
4
|
+
root: HTMLElement;
|
|
5
|
+
/** Opaque layer behind the slides, faded by the close drag */
|
|
6
|
+
backdrop: HTMLElement;
|
|
7
|
+
/** The current slide, moved by the close drag */
|
|
8
|
+
slide: HTMLElement;
|
|
9
|
+
/** Box of the current image (exactly the image once sized): zoomed, panned, hit-tested */
|
|
10
|
+
frame: HTMLElement;
|
|
11
|
+
/** The current full-size image: its resolution caps the zoom */
|
|
12
|
+
img: HTMLImageElement;
|
|
13
|
+
/** Called when a gesture closes the viewer (swipe up/down, background tap) */
|
|
14
|
+
onClose: () => void;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Attaches the close / zoom / pan gestures of the lightbox to its current slide.
|
|
18
|
+
* Call it again with the new slide after a page turn.
|
|
19
|
+
*
|
|
20
|
+
* Known limitation: a pinch that starts once a one-finger horizontal scroll is under way
|
|
21
|
+
* is not seen (the browser took the touch for scrolling and cancelled its pointer).
|
|
22
|
+
* Lifting the finger and pinching again works.
|
|
23
|
+
*
|
|
24
|
+
* @param targets - The viewer elements and the close callback
|
|
25
|
+
* @returns Detach function: removes the listeners and resets zoom and drag
|
|
26
|
+
*/
|
|
27
|
+
export declare function attachLightboxGestures({ root, backdrop, slide, frame, img, onClose, }: LightboxGestureTargets): () => void;
|
|
28
|
+
//# sourceMappingURL=lightboxGestures.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lightboxGestures.d.ts","sourceRoot":"","sources":["../../../src/advanced/ImageGallery/lightboxGestures.ts"],"names":[],"mappings":"AAwEA,+DAA+D;AAC/D,MAAM,WAAW,sBAAsB;IACrC,iFAAiF;IACjF,IAAI,EAAE,WAAW,CAAC;IAClB,8DAA8D;IAC9D,QAAQ,EAAE,WAAW,CAAC;IACtB,iDAAiD;IACjD,KAAK,EAAE,WAAW,CAAC;IACnB,0FAA0F;IAC1F,KAAK,EAAE,WAAW,CAAC;IACnB,gEAAgE;IAChE,GAAG,EAAE,gBAAgB,CAAC;IACtB,8EAA8E;IAC9E,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB;AAaD;;;;;;;;;;GAUG;AACH,wBAAgB,sBAAsB,CAAC,EACrC,IAAI,EACJ,QAAQ,EACR,KAAK,EACL,KAAK,EACL,GAAG,EACH,OAAO,GACR,EAAE,sBAAsB,GAAG,MAAM,IAAI,CAiYrC"}
|