@internetarchive/elements 0.3.0 → 0.4.0

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.
Files changed (29) hide show
  1. package/dist/src/elements/ia-image-viewer/ia-image-viewer.d.ts +147 -0
  2. package/dist/src/elements/ia-image-viewer/ia-image-viewer.d.ts.map +1 -0
  3. package/dist/src/elements/ia-image-viewer/ia-image-viewer.js +533 -0
  4. package/dist/src/elements/ia-image-viewer/ia-image-viewer.js.map +1 -0
  5. package/dist/src/elements/ia-image-viewer/ia-imgview-controls.d.ts +22 -0
  6. package/dist/src/elements/ia-image-viewer/ia-imgview-controls.d.ts.map +1 -0
  7. package/dist/src/elements/ia-image-viewer/ia-imgview-controls.js +145 -0
  8. package/dist/src/elements/ia-image-viewer/ia-imgview-controls.js.map +1 -0
  9. package/dist/src/elements/ia-image-viewer/ia-imgview-slide.d.ts +44 -0
  10. package/dist/src/elements/ia-image-viewer/ia-imgview-slide.d.ts.map +1 -0
  11. package/dist/src/elements/ia-image-viewer/ia-imgview-slide.js +213 -0
  12. package/dist/src/elements/ia-image-viewer/ia-imgview-slide.js.map +1 -0
  13. package/dist/src/elements/ia-image-viewer/image-viewer-gestures.d.ts +79 -0
  14. package/dist/src/elements/ia-image-viewer/image-viewer-gestures.d.ts.map +1 -0
  15. package/dist/src/elements/ia-image-viewer/image-viewer-gestures.js +254 -0
  16. package/dist/src/elements/ia-image-viewer/image-viewer-gestures.js.map +1 -0
  17. package/dist/src/elements/ia-image-viewer/models.d.ts +20 -0
  18. package/dist/src/elements/ia-image-viewer/models.d.ts.map +1 -0
  19. package/dist/src/elements/ia-image-viewer/models.js +2 -0
  20. package/dist/src/elements/ia-image-viewer/models.js.map +1 -0
  21. package/dist/src/elements/index.d.ts +1 -0
  22. package/dist/src/elements/index.d.ts.map +1 -1
  23. package/dist/src/elements/index.js +1 -0
  24. package/dist/src/elements/index.js.map +1 -1
  25. package/dist/src/util/prefers-reduced-motion.d.ts +10 -0
  26. package/dist/src/util/prefers-reduced-motion.d.ts.map +1 -0
  27. package/dist/src/util/prefers-reduced-motion.js +12 -0
  28. package/dist/src/util/prefers-reduced-motion.js.map +1 -0
  29. package/package.json +1 -1
@@ -0,0 +1,147 @@
1
+ import { type CSSResultGroup, LitElement, nothing, type PropertyValues, type TemplateResult } from 'lit';
2
+ import type { ImageViewerImage, SlideDirection } from './models.js';
3
+ import './ia-imgview-slide.js';
4
+ import './ia-imgview-controls.js';
5
+ /**
6
+ * A swipeable image carousel.
7
+ *
8
+ * Shows one image at a time from a list, with previous/next buttons, a
9
+ * counter, keyboard arrows, horizontal wheel scrolling and touch swipes.
10
+ * Clicking an image opens it in a new tab.
11
+ *
12
+ * The viewer holds no opinion about where the images came from: it takes a
13
+ * plain list of names and URLs, and reports the image it lands on through
14
+ * `imageChanged` so a host can mirror that into its own URL or analytics.
15
+ *
16
+ * @fires imageChanged - The viewer settled on a new image.
17
+ * @fires imageLoaded - An image the viewer showed finished loading.
18
+ * @fires imageLoadFailed - An image the viewer showed could not be loaded.
19
+ * Fires once per image until it loads successfully or the set changes.
20
+ * @fires imageActivated - An image was clicked. Cancelable: preventing it
21
+ * stops the default of opening the image in a new tab.
22
+ */
23
+ export declare class IAImageViewer extends LitElement {
24
+ images: ImageViewerImage[];
25
+ /**
26
+ * Name of the image to show. Changing it jumps straight to that image
27
+ * without animating. An unknown name shows the first image.
28
+ */
29
+ currentImageName?: string;
30
+ /** Read by the gesture controller, which tracks touches against it. */
31
+ imageArea?: HTMLElement;
32
+ /** Read by the gesture controller, which translates it during a drag. */
33
+ slideTrack?: HTMLElement;
34
+ /** Read by the gesture controller to navigate relative to the current image. */
35
+ currentIndex: number;
36
+ private animating;
37
+ /**
38
+ * Incremented when an animation is interrupted to force the track element to
39
+ * be recreated via the `keyed()` directive, which guarantees the CSS
40
+ * animation restarts even if the direction is the same.
41
+ */
42
+ private slideGeneration;
43
+ /** Which edge just wrapped, if any. Drives the glow indicator. */
44
+ private wrapEdge;
45
+ /**
46
+ * Images already reported as failed, so a broken image isn't counted again
47
+ * every time the viewer passes over it. Navigating back re-fires `error` on
48
+ * the recreated element, and the neighbouring slides staged during an
49
+ * animation or a drag load the same image too, so without this a single
50
+ * broken derivative reports several times a session.
51
+ *
52
+ * Cleared when the image set changes, which is the point a name stops
53
+ * standing for the same file.
54
+ */
55
+ private reportedFailures;
56
+ private slideTarget;
57
+ private slideDirection;
58
+ private gestures;
59
+ get imageCount(): number;
60
+ /** The image currently on screen, if there is one. */
61
+ get currentImage(): ImageViewerImage | undefined;
62
+ protected willUpdate(changed: PropertyValues): void;
63
+ /**
64
+ * Where to land when the images or the requested name change.
65
+ *
66
+ * A new `currentImageName` is the host asking for a particular image, so it
67
+ * wins. A new `images` array on its own isn't: a host that builds its list
68
+ * in a getter hands over a fresh array on every render, which says nothing
69
+ * about the images having changed. So the viewer holds its place by name and
70
+ * only goes back to the start when the image it was showing is really gone.
71
+ */
72
+ private indexToShow;
73
+ render(): TemplateResult | typeof nothing;
74
+ private slideTemplate;
75
+ /**
76
+ * Announces the image to a screen reader, which otherwise gets nothing when
77
+ * the counter and the picture change under it.
78
+ */
79
+ private get announcementTemplate();
80
+ /**
81
+ * Passes a slide's load result on to the host, which is where reporting
82
+ * belongs. The slide's own events stop at this shadow root, so they're
83
+ * re-dispatched rather than allowed to escape.
84
+ */
85
+ private onImageLoaded;
86
+ private onImageFailed;
87
+ /**
88
+ * Decides whether a click on a slide opens the image.
89
+ *
90
+ * Two things can stop it: the click is the tail end of a swipe, or the host
91
+ * cancels the event to show the image its own way. The slide's event doesn't
92
+ * cross this shadow root, so it's re-raised here for the host to see, and a
93
+ * veto is passed back down to cancel the click.
94
+ */
95
+ private onImageActivated;
96
+ /**
97
+ * Inserts the neighbouring slides around the current one so the touch target
98
+ * element stays in the DOM (avoiding touchcancel). These aren't rendered
99
+ * from the template, so they're marked for the gesture controller to clean
100
+ * up. The dragging class shifts the track to centre on the middle slide.
101
+ */
102
+ insertDragSlides(): void;
103
+ removeDragSlides(): void;
104
+ private createDragSlide;
105
+ showNext(): void;
106
+ showPrevious(): void;
107
+ /**
108
+ * Starts the move to another image. With reduced motion there's no animation
109
+ * to wait on, so the move lands immediately: nothing would fire the
110
+ * `animationend` that otherwise commits it.
111
+ */
112
+ private moveTo;
113
+ /**
114
+ * Moves to the image a completed drag landed on. The drag has already
115
+ * animated the track there, so this only updates state.
116
+ */
117
+ commitDrag(direction: SlideDirection): void;
118
+ /**
119
+ * If an animation is in progress, immediately commit its target so the next
120
+ * animation starts from the right position. Bumps the generation key to
121
+ * force the track element to be recreated, which restarts the CSS animation.
122
+ */
123
+ settleIfAnimating(): void;
124
+ private onWrapGlowEnd;
125
+ private onSlideEnd;
126
+ /** Finishes a move, however it was driven. */
127
+ private settleAtCurrentIndex;
128
+ private emitImageChanged;
129
+ /**
130
+ * Preloads the next and previous images around `currentIndex` so the first
131
+ * navigation feels instant.
132
+ */
133
+ private preloadAdjacentImages;
134
+ /**
135
+ * Preloads images adjacent to the given index. Called both at rest (around
136
+ * `currentIndex`) and at the start of a move (around the target) so rapid
137
+ * navigation stays ahead of the browser cache.
138
+ *
139
+ * A preload that fails is deliberately not reported. These requests are
140
+ * speculative and the viewer may never show them, so counting them would
141
+ * inflate the failure rate with images nobody looked at. Navigating to a
142
+ * broken image renders a real slide, whose `error` reports it then.
143
+ */
144
+ preloadAroundIndex(index: number): void;
145
+ static get styles(): CSSResultGroup;
146
+ }
147
+ //# sourceMappingURL=ia-image-viewer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ia-image-viewer.d.ts","sourceRoot":"","sources":["../../../../src/elements/ia-image-viewer/ia-image-viewer.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,cAAc,EAEnB,UAAU,EACV,OAAO,EACP,KAAK,cAAc,EACnB,KAAK,cAAc,EACpB,MAAM,KAAK,CAAC;AASb,OAAO,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAY,MAAM,UAAU,CAAC;AAE3E,OAAO,oBAAoB,CAAC;AAC5B,OAAO,uBAAuB,CAAC;AAK/B;;;;;;;;;;;;;;;;;GAiBG;AACH,qBACa,aAAc,SAAQ,UAAU;IAChB,MAAM,EAAE,gBAAgB,EAAE,CAAM;IAE3D;;;OAGG;IACyB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAEtD,uEAAuE;IACjD,SAAS,CAAC,EAAE,WAAW,CAAC;IAE9C,yEAAyE;IAClD,UAAU,CAAC,EAAE,WAAW,CAAC;IAEhD,gFAAgF;IACvE,YAAY,SAAK;IAEjB,OAAO,CAAC,SAAS,CAAS;IAEnC;;;;OAIG;IACM,OAAO,CAAC,eAAe,CAAK;IAErC,kEAAkE;IACzD,OAAO,CAAC,QAAQ,CAAyB;IAElD;;;;;;;;;OASG;IACH,OAAO,CAAC,gBAAgB,CAAqB;IAE7C,OAAO,CAAC,WAAW,CAAuB;IAE1C,OAAO,CAAC,cAAc,CAA+B;IAErD,OAAO,CAAC,QAAQ,CAAiC;IAEjD,IAAI,UAAU,IAAI,MAAM,CAEvB;IAED,sDAAsD;IACtD,IAAI,YAAY,IAAI,gBAAgB,GAAG,SAAS,CAE/C;IAED,SAAS,CAAC,UAAU,CAAC,OAAO,EAAE,cAAc,GAAG,IAAI;IAenD;;;;;;;;OAQG;IACH,OAAO,CAAC,WAAW;IAyBnB,MAAM,IAAI,cAAc,GAAG,OAAO,OAAO;IA6EzC,OAAO,CAAC,aAAa;IAIrB;;;OAGG;IACH,OAAO,KAAK,oBAAoB,GAW/B;IAED;;;;OAIG;IACH,OAAO,CAAC,aAAa;IASrB,OAAO,CAAC,aAAa;IASrB;;;;;;;OAOG;IACH,OAAO,CAAC,gBAAgB;IAcxB;;;;;OAKG;IACH,gBAAgB,IAAI,IAAI;IAgBxB,gBAAgB,IAAI,IAAI;IAOxB,OAAO,CAAC,eAAe;IASvB,QAAQ,IAAI,IAAI;IAKhB,YAAY,IAAI,IAAI;IAQpB;;;;OAIG;IACH,OAAO,CAAC,MAAM;IAmBd;;;OAGG;IACH,UAAU,CAAC,SAAS,EAAE,cAAc,GAAG,IAAI;IAc3C;;;;OAIG;IACH,iBAAiB,IAAI,IAAI;IAMzB,OAAO,CAAC,aAAa;IAIrB,OAAO,CAAC,UAAU;IAMlB,8CAA8C;IAC9C,OAAO,CAAC,oBAAoB;IAS5B,OAAO,CAAC,gBAAgB;IAUxB;;;OAGG;IACH,OAAO,CAAC,qBAAqB;IAI7B;;;;;;;;;OASG;IACH,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAUvC,MAAM,KAAK,MAAM,IAAI,cAAc,CAgJlC;CACF"}
@@ -0,0 +1,533 @@
1
+ import { __decorate } from "tslib";
2
+ import { css, html, LitElement, nothing, } from 'lit';
3
+ import { customElement, property, query, state } from 'lit/decorators.js';
4
+ import { classMap } from 'lit/directives/class-map.js';
5
+ import { keyed } from 'lit/directives/keyed.js';
6
+ import { msg, str } from '@lit/localize';
7
+ import themeStyles from '../../themes/theme-styles.js';
8
+ import { prefersReducedMotion } from '../../util/prefers-reduced-motion.js';
9
+ import { ImageViewerGestures } from './image-viewer-gestures.js';
10
+ import './ia-imgview-slide.js';
11
+ import './ia-imgview-controls.js';
12
+ /**
13
+ * A swipeable image carousel.
14
+ *
15
+ * Shows one image at a time from a list, with previous/next buttons, a
16
+ * counter, keyboard arrows, horizontal wheel scrolling and touch swipes.
17
+ * Clicking an image opens it in a new tab.
18
+ *
19
+ * The viewer holds no opinion about where the images came from: it takes a
20
+ * plain list of names and URLs, and reports the image it lands on through
21
+ * `imageChanged` so a host can mirror that into its own URL or analytics.
22
+ *
23
+ * @fires imageChanged - The viewer settled on a new image.
24
+ * @fires imageLoaded - An image the viewer showed finished loading.
25
+ * @fires imageLoadFailed - An image the viewer showed could not be loaded.
26
+ * Fires once per image until it loads successfully or the set changes.
27
+ * @fires imageActivated - An image was clicked. Cancelable: preventing it
28
+ * stops the default of opening the image in a new tab.
29
+ */
30
+ let IAImageViewer = class IAImageViewer extends LitElement {
31
+ constructor() {
32
+ super(...arguments);
33
+ this.images = [];
34
+ /** Read by the gesture controller to navigate relative to the current image. */
35
+ this.currentIndex = 0;
36
+ this.animating = false;
37
+ /**
38
+ * Incremented when an animation is interrupted to force the track element to
39
+ * be recreated via the `keyed()` directive, which guarantees the CSS
40
+ * animation restarts even if the direction is the same.
41
+ */
42
+ this.slideGeneration = 0;
43
+ /** Which edge just wrapped, if any. Drives the glow indicator. */
44
+ this.wrapEdge = null;
45
+ /**
46
+ * Images already reported as failed, so a broken image isn't counted again
47
+ * every time the viewer passes over it. Navigating back re-fires `error` on
48
+ * the recreated element, and the neighbouring slides staged during an
49
+ * animation or a drag load the same image too, so without this a single
50
+ * broken derivative reports several times a session.
51
+ *
52
+ * Cleared when the image set changes, which is the point a name stops
53
+ * standing for the same file.
54
+ */
55
+ this.reportedFailures = new Set();
56
+ this.slideTarget = null;
57
+ this.slideDirection = null;
58
+ this.gestures = new ImageViewerGestures(this);
59
+ }
60
+ get imageCount() {
61
+ return this.images.length;
62
+ }
63
+ /** The image currently on screen, if there is one. */
64
+ get currentImage() {
65
+ return this.images[this.currentIndex];
66
+ }
67
+ willUpdate(changed) {
68
+ if (changed.has('images') || changed.has('currentImageName')) {
69
+ const previous = changed.get('images');
70
+ this.currentIndex = this.indexToShow(changed, previous);
71
+ this.animating = false;
72
+ this.slideGeneration = 0;
73
+ this.slideTarget = null;
74
+ this.slideDirection = null;
75
+ this.wrapEdge = null;
76
+ this.reportedFailures.clear();
77
+ this.preloadAdjacentImages();
78
+ }
79
+ }
80
+ /**
81
+ * Where to land when the images or the requested name change.
82
+ *
83
+ * A new `currentImageName` is the host asking for a particular image, so it
84
+ * wins. A new `images` array on its own isn't: a host that builds its list
85
+ * in a getter hands over a fresh array on every render, which says nothing
86
+ * about the images having changed. So the viewer holds its place by name and
87
+ * only goes back to the start when the image it was showing is really gone.
88
+ */
89
+ indexToShow(changed, previousImages) {
90
+ if (changed.has('currentImageName') && this.currentImageName) {
91
+ const requested = this.images.findIndex((image) => image.name === this.currentImageName);
92
+ if (requested >= 0)
93
+ return requested;
94
+ }
95
+ const showing = previousImages?.[this.currentIndex]?.name;
96
+ if (showing) {
97
+ const stillThere = this.images.findIndex((image) => image.name === showing);
98
+ if (stillThere >= 0)
99
+ return stillThere;
100
+ }
101
+ const named = this.currentImageName
102
+ ? this.images.findIndex((image) => image.name === this.currentImageName)
103
+ : -1;
104
+ return named >= 0 ? named : 0;
105
+ }
106
+ render() {
107
+ if (this.images.length === 0)
108
+ return nothing;
109
+ const { dragging } = this.gestures;
110
+ const current = this.images[this.currentIndex];
111
+ const showControls = this.images.length > 1;
112
+ const trackClasses = {
113
+ 'slide-track': true,
114
+ 'sliding-next': this.animating && !dragging && this.slideDirection === 'next',
115
+ 'sliding-prev': this.animating && !dragging && this.slideDirection === 'prev',
116
+ dragging,
117
+ };
118
+ const prevIndex = (this.currentIndex - 1 + this.images.length) % this.images.length;
119
+ const nextIndex = (this.currentIndex + 1) % this.images.length;
120
+ const slideTarget = this.animating && !dragging && this.slideTarget !== null
121
+ ? this.images[this.slideTarget]
122
+ : null;
123
+ const prevImage = dragging ? this.images[prevIndex] : slideTarget;
124
+ const nextImage = dragging ? this.images[nextIndex] : slideTarget;
125
+ const showPrev = dragging || (slideTarget !== null && this.slideDirection === 'prev');
126
+ const showNext = dragging || (slideTarget !== null && this.slideDirection === 'next');
127
+ const displayIndex = this.animating && this.slideTarget !== null
128
+ ? this.slideTarget
129
+ : this.currentIndex;
130
+ return html `
131
+ <div class="viewer">
132
+ <div class="image-area">
133
+ ${this.wrapEdge
134
+ ? html `<div
135
+ class="wrap-glow ${this.wrapEdge === 'start'
136
+ ? 'glow-start'
137
+ : 'glow-end'}"
138
+ @animationend=${this.onWrapGlowEnd}
139
+ ></div>`
140
+ : nothing}
141
+ ${keyed(this.slideGeneration, html `<div
142
+ class=${classMap(trackClasses)}
143
+ @animationend=${this.onSlideEnd}
144
+ @imageLoaded=${this.onImageLoaded}
145
+ @imageFailed=${this.onImageFailed}
146
+ @imageActivated=${this.onImageActivated}
147
+ >
148
+ ${showPrev && prevImage ? this.slideTemplate(prevImage) : nothing}
149
+ ${this.slideTemplate(current)}
150
+ ${showNext && nextImage ? this.slideTemplate(nextImage) : nothing}
151
+ </div>`)}
152
+ </div>
153
+ ${showControls
154
+ ? html `
155
+ <ia-imgview-controls
156
+ .currentIndex=${displayIndex}
157
+ .totalImages=${this.images.length}
158
+ @showPrevious=${this.showPrevious}
159
+ @showNext=${this.showNext}
160
+ ></ia-imgview-controls>
161
+ `
162
+ : nothing}
163
+ ${this.announcementTemplate}
164
+ </div>
165
+ `;
166
+ }
167
+ slideTemplate(image) {
168
+ return html `<ia-imgview-slide .image=${image}></ia-imgview-slide>`;
169
+ }
170
+ /**
171
+ * Announces the image to a screen reader, which otherwise gets nothing when
172
+ * the counter and the picture change under it.
173
+ */
174
+ get announcementTemplate() {
175
+ const current = this.images[this.currentIndex];
176
+ const position = msg(str `Image ${this.currentIndex + 1} of ${this.images.length}`);
177
+ const label = current?.title ?? current?.name;
178
+ return html `
179
+ <div class="sr-only" role="status" aria-live="polite" aria-atomic="true">
180
+ ${label ? `${position}: ${label}` : position}
181
+ </div>
182
+ `;
183
+ }
184
+ /**
185
+ * Passes a slide's load result on to the host, which is where reporting
186
+ * belongs. The slide's own events stop at this shadow root, so they're
187
+ * re-dispatched rather than allowed to escape.
188
+ */
189
+ onImageLoaded(e) {
190
+ // A load that succeeds clears the failure, so an image that breaks again
191
+ // later is reported again rather than swallowed as a repeat.
192
+ this.reportedFailures.delete(e.detail.image.name);
193
+ this.dispatchEvent(new CustomEvent('imageLoaded', { detail: { image: e.detail.image } }));
194
+ }
195
+ onImageFailed(e) {
196
+ const { image } = e.detail;
197
+ if (this.reportedFailures.has(image.name))
198
+ return;
199
+ this.reportedFailures.add(image.name);
200
+ this.dispatchEvent(new CustomEvent('imageLoadFailed', { detail: { image } }));
201
+ }
202
+ /**
203
+ * Decides whether a click on a slide opens the image.
204
+ *
205
+ * Two things can stop it: the click is the tail end of a swipe, or the host
206
+ * cancels the event to show the image its own way. The slide's event doesn't
207
+ * cross this shadow root, so it's re-raised here for the host to see, and a
208
+ * veto is passed back down to cancel the click.
209
+ */
210
+ onImageActivated(e) {
211
+ if (this.gestures.consumeSwipeTap()) {
212
+ e.preventDefault();
213
+ return;
214
+ }
215
+ const allowed = this.dispatchEvent(new CustomEvent('imageActivated', {
216
+ detail: { image: e.detail.image },
217
+ cancelable: true,
218
+ }));
219
+ if (!allowed)
220
+ e.preventDefault();
221
+ }
222
+ /**
223
+ * Inserts the neighbouring slides around the current one so the touch target
224
+ * element stays in the DOM (avoiding touchcancel). These aren't rendered
225
+ * from the template, so they're marked for the gesture controller to clean
226
+ * up. The dragging class shifts the track to centre on the middle slide.
227
+ */
228
+ insertDragSlides() {
229
+ const track = this.slideTrack;
230
+ if (!track)
231
+ return;
232
+ const prevIdx = (this.currentIndex - 1 + this.images.length) % this.images.length;
233
+ const nextIdx = (this.currentIndex + 1) % this.images.length;
234
+ track.insertBefore(this.createDragSlide(this.images[prevIdx]), track.firstChild);
235
+ track.appendChild(this.createDragSlide(this.images[nextIdx]));
236
+ track.classList.add('dragging');
237
+ }
238
+ removeDragSlides() {
239
+ const track = this.slideTrack;
240
+ if (!track)
241
+ return;
242
+ track.querySelectorAll('[data-drag-slide]').forEach((el) => el.remove());
243
+ track.classList.remove('dragging');
244
+ }
245
+ createDragSlide(image) {
246
+ const slide = document.createElement('ia-imgview-slide');
247
+ slide.image = image;
248
+ slide.dataset.dragSlide = '';
249
+ return slide;
250
+ }
251
+ showNext() {
252
+ this.settleIfAnimating();
253
+ this.moveTo((this.currentIndex + 1) % this.images.length, 'next');
254
+ }
255
+ showPrevious() {
256
+ this.settleIfAnimating();
257
+ this.moveTo((this.currentIndex - 1 + this.images.length) % this.images.length, 'prev');
258
+ }
259
+ /**
260
+ * Starts the move to another image. With reduced motion there's no animation
261
+ * to wait on, so the move lands immediately: nothing would fire the
262
+ * `animationend` that otherwise commits it.
263
+ */
264
+ moveTo(target, direction) {
265
+ const wraps = direction === 'next'
266
+ ? this.currentIndex === this.images.length - 1
267
+ : this.currentIndex === 0;
268
+ this.preloadAroundIndex(target);
269
+ if (prefersReducedMotion()) {
270
+ this.currentIndex = target;
271
+ this.settleAtCurrentIndex();
272
+ return;
273
+ }
274
+ this.slideTarget = target;
275
+ this.slideDirection = direction;
276
+ this.animating = true;
277
+ this.wrapEdge = wraps ? (direction === 'next' ? 'end' : 'start') : null;
278
+ }
279
+ /**
280
+ * Moves to the image a completed drag landed on. The drag has already
281
+ * animated the track there, so this only updates state.
282
+ */
283
+ commitDrag(direction) {
284
+ const wraps = direction === 'next'
285
+ ? this.currentIndex === this.images.length - 1
286
+ : this.currentIndex === 0;
287
+ const step = direction === 'next' ? 1 : -1;
288
+ this.currentIndex =
289
+ (this.currentIndex + step + this.images.length) % this.images.length;
290
+ if (wraps && !prefersReducedMotion()) {
291
+ this.wrapEdge = direction === 'next' ? 'end' : 'start';
292
+ }
293
+ this.emitImageChanged();
294
+ }
295
+ /**
296
+ * If an animation is in progress, immediately commit its target so the next
297
+ * animation starts from the right position. Bumps the generation key to
298
+ * force the track element to be recreated, which restarts the CSS animation.
299
+ */
300
+ settleIfAnimating() {
301
+ if (!this.animating || this.slideTarget === null)
302
+ return;
303
+ this.currentIndex = this.slideTarget;
304
+ this.slideGeneration++;
305
+ }
306
+ onWrapGlowEnd() {
307
+ this.wrapEdge = null;
308
+ }
309
+ onSlideEnd() {
310
+ if (this.slideTarget === null)
311
+ return;
312
+ this.currentIndex = this.slideTarget;
313
+ this.settleAtCurrentIndex();
314
+ }
315
+ /** Finishes a move, however it was driven. */
316
+ settleAtCurrentIndex() {
317
+ this.slideTarget = null;
318
+ this.slideDirection = null;
319
+ this.animating = false;
320
+ this.gestures.animationSettled();
321
+ this.emitImageChanged();
322
+ this.preloadAdjacentImages();
323
+ }
324
+ emitImageChanged() {
325
+ const image = this.images[this.currentIndex];
326
+ if (!image)
327
+ return;
328
+ this.dispatchEvent(new CustomEvent('imageChanged', {
329
+ detail: { image, index: this.currentIndex },
330
+ }));
331
+ }
332
+ /**
333
+ * Preloads the next and previous images around `currentIndex` so the first
334
+ * navigation feels instant.
335
+ */
336
+ preloadAdjacentImages() {
337
+ this.preloadAroundIndex(this.currentIndex);
338
+ }
339
+ /**
340
+ * Preloads images adjacent to the given index. Called both at rest (around
341
+ * `currentIndex`) and at the start of a move (around the target) so rapid
342
+ * navigation stays ahead of the browser cache.
343
+ *
344
+ * A preload that fails is deliberately not reported. These requests are
345
+ * speculative and the viewer may never show them, so counting them would
346
+ * inflate the failure rate with images nobody looked at. Navigating to a
347
+ * broken image renders a real slide, whose `error` reports it then.
348
+ */
349
+ preloadAroundIndex(index) {
350
+ if (this.images.length <= 1)
351
+ return;
352
+ const next = (index + 1) % this.images.length;
353
+ const prev = (index - 1 + this.images.length) % this.images.length;
354
+ for (const idx of [next, prev]) {
355
+ const img = new Image();
356
+ img.src = this.images[idx].url;
357
+ }
358
+ }
359
+ static get styles() {
360
+ return [
361
+ themeStyles,
362
+ css `
363
+ :host {
364
+ --image-viewer-slide-duration--: var(
365
+ --image-viewer-slide-duration,
366
+ 500ms
367
+ );
368
+ --image-viewer-glow-color--: var(
369
+ --image-viewer-glow-color,
370
+ rgba(255, 255, 255, 0.25)
371
+ );
372
+ /*
373
+ * The breakpoint the controls query against. Named so the query in
374
+ * ia-imgview-controls resolves to this host rather than to whatever
375
+ * container the consumer happens to have further up.
376
+ */
377
+ container: image-viewer / inline-size;
378
+
379
+ /*
380
+ * Size and placement are the host's business, set with ordinary CSS
381
+ * on the element. These are only defaults for a host that sets
382
+ * neither; a rule in the outer tree beats them.
383
+ */
384
+ display: block;
385
+ height: 100%;
386
+ margin: 0 auto;
387
+ }
388
+
389
+ .viewer {
390
+ position: relative;
391
+ width: 100%;
392
+ height: 100%;
393
+ display: flex;
394
+ flex-direction: column;
395
+ align-items: center;
396
+ justify-content: center;
397
+ }
398
+
399
+ .image-area {
400
+ position: relative;
401
+ flex: 1;
402
+ min-height: 0;
403
+ width: 100%;
404
+ display: flex;
405
+ align-items: center;
406
+ justify-content: center;
407
+ overflow: hidden;
408
+ touch-action: pan-y pinch-zoom;
409
+ }
410
+
411
+ .wrap-glow {
412
+ position: absolute;
413
+ top: 0;
414
+ bottom: 0;
415
+ width: 100px;
416
+ pointer-events: none;
417
+ z-index: 2;
418
+ animation: glow-fade 700ms ease-out both;
419
+ }
420
+
421
+ .glow-start {
422
+ left: 0;
423
+ background: linear-gradient(
424
+ to right,
425
+ var(--image-viewer-glow-color--),
426
+ transparent
427
+ );
428
+ }
429
+
430
+ .glow-end {
431
+ right: 0;
432
+ background: linear-gradient(
433
+ to left,
434
+ var(--image-viewer-glow-color--),
435
+ transparent
436
+ );
437
+ }
438
+
439
+ @keyframes glow-fade {
440
+ 0% {
441
+ opacity: 0;
442
+ }
443
+ 20% {
444
+ opacity: 1;
445
+ }
446
+ 100% {
447
+ opacity: 0;
448
+ }
449
+ }
450
+
451
+ .slide-track {
452
+ display: flex;
453
+ width: 100%;
454
+ height: 100%;
455
+ flex-shrink: 0;
456
+ }
457
+
458
+ @keyframes slide-next {
459
+ from {
460
+ transform: translateX(0);
461
+ }
462
+ to {
463
+ transform: translateX(-100%);
464
+ }
465
+ }
466
+
467
+ @keyframes slide-prev {
468
+ from {
469
+ transform: translateX(-100%);
470
+ }
471
+ to {
472
+ transform: translateX(0);
473
+ }
474
+ }
475
+
476
+ .slide-track.dragging {
477
+ transform: translateX(-100%);
478
+ }
479
+
480
+ .slide-track.sliding-next {
481
+ animation: slide-next var(--image-viewer-slide-duration--) ease-in-out
482
+ both;
483
+ }
484
+
485
+ .slide-track.sliding-prev {
486
+ animation: slide-prev var(--image-viewer-slide-duration--) ease-in-out
487
+ both;
488
+ }
489
+
490
+ .sr-only {
491
+ position: absolute;
492
+ width: 1px;
493
+ height: 1px;
494
+ margin: -1px;
495
+ padding: 0;
496
+ border: 0;
497
+ overflow: hidden;
498
+ white-space: nowrap;
499
+ clip-path: inset(50%);
500
+ }
501
+ `,
502
+ ];
503
+ }
504
+ };
505
+ __decorate([
506
+ property({ type: Array })
507
+ ], IAImageViewer.prototype, "images", void 0);
508
+ __decorate([
509
+ property({ type: String })
510
+ ], IAImageViewer.prototype, "currentImageName", void 0);
511
+ __decorate([
512
+ query('.image-area')
513
+ ], IAImageViewer.prototype, "imageArea", void 0);
514
+ __decorate([
515
+ query('.slide-track')
516
+ ], IAImageViewer.prototype, "slideTrack", void 0);
517
+ __decorate([
518
+ state()
519
+ ], IAImageViewer.prototype, "currentIndex", void 0);
520
+ __decorate([
521
+ state()
522
+ ], IAImageViewer.prototype, "animating", void 0);
523
+ __decorate([
524
+ state()
525
+ ], IAImageViewer.prototype, "slideGeneration", void 0);
526
+ __decorate([
527
+ state()
528
+ ], IAImageViewer.prototype, "wrapEdge", void 0);
529
+ IAImageViewer = __decorate([
530
+ customElement('ia-image-viewer')
531
+ ], IAImageViewer);
532
+ export { IAImageViewer };
533
+ //# sourceMappingURL=ia-image-viewer.js.map