@wpmoo/ui 2.1.0 → 2.2.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 (40) hide show
  1. package/README.md +27 -2
  2. package/THIRD_PARTY_NOTICES.md +1 -1
  3. package/certification.json +2 -1
  4. package/dist/assets/css/moo-ui.css +429 -1
  5. package/dist/assets/css/moo-ui.min.css +2 -2
  6. package/dist/assets/css/moo.css +349 -1
  7. package/dist/assets/css/moo.min.css +2 -2
  8. package/dist/js/chart.js +9 -1
  9. package/dist/js/chart.min.js +2 -2
  10. package/dist/js/combobox.js +17 -6
  11. package/dist/js/context-menu.js +8 -3
  12. package/dist/js/datatable.js +41 -19
  13. package/dist/js/datepicker.js +23 -9
  14. package/dist/js/datepicker.min.js +2 -2
  15. package/dist/js/floating-panel.js +1 -1
  16. package/dist/js/image-gallery.js +247 -0
  17. package/dist/js/moo-ui.js +360 -34
  18. package/dist/js/moo-ui.min.js +2 -2
  19. package/dist/js/overlay-scrollbar.js +1 -1
  20. package/dist/js/password-requirements.js +1 -1
  21. package/dist/js/sheet.js +1 -1
  22. package/dist/js/sidebar.js +42 -1
  23. package/dist/js/slider.js +1 -1
  24. package/dist/js/state.js +1 -1
  25. package/dist/js/table-of-contents.js +1 -1
  26. package/dist/js/toc.js +1 -1
  27. package/dist/release-manifest.json +4 -4
  28. package/package.json +3 -1
  29. package/scss/_components.scss +2 -0
  30. package/scss/components/_gallery.scss +73 -0
  31. package/scss/components/_item.scss +96 -0
  32. package/scss/foundations/_globals.scss +1 -0
  33. package/scss/foundations/_scope.scss +2 -0
  34. package/scss/mixins/_banner.scss +1 -1
  35. package/scss/moo-core.scss +1 -0
  36. package/scss/moo-ui.scss +2 -0
  37. package/scss/settings/_component_variables.scss +8 -0
  38. package/scss/utilities/_gallery.scss +25 -0
  39. package/scss/utilities/_motion.scss +5 -0
  40. package/scss/utilities/_motion_primitives.scss +10 -0
@@ -0,0 +1,247 @@
1
+ /*!
2
+ * Moo UI image-gallery.js v2.2.0 (https://wpmoo.org/)
3
+ * Copyright 2026 WPMoo Authors
4
+ * Licensed under MIT (https://github.com/wpmoo-org/ui/blob/main/LICENSE)
5
+ */
6
+
7
+ const instances = /* @__PURE__ */ new WeakMap();
8
+ const SLIDESHOW_INTERVAL = 5e3;
9
+ const RESIZE_DURATION = 250;
10
+ class ImageGallery {
11
+ static getInstance(element) {
12
+ return element?.nodeType === 1 ? instances.get(element) || null : null;
13
+ }
14
+ static getOrCreateInstance(element) {
15
+ return ImageGallery.getInstance(element) || new ImageGallery(element);
16
+ }
17
+ constructor(element) {
18
+ if (element?.nodeType !== 1 || !element.matches("[data-image-gallery]")) {
19
+ throw new TypeError("ImageGallery requires a [data-image-gallery] root element.");
20
+ }
21
+ const existing = instances.get(element);
22
+ if (existing) return existing;
23
+ this._element = element;
24
+ this._window = element.ownerDocument.defaultView;
25
+ this._dialog = element.querySelector(".modal");
26
+ this._carousel = this._dialog?.querySelector(".carousel");
27
+ if (!this._dialog || !this._carousel) {
28
+ throw new TypeError("ImageGallery requires a lightbox Dialog with a Carousel.");
29
+ }
30
+ this._slides = Array.from(this._carousel.querySelectorAll(".carousel-item"));
31
+ this._counter = this._dialog.querySelector("[data-gallery-counter]");
32
+ this._caption = this._dialog.querySelector("[data-gallery-caption]");
33
+ this._credit = this._dialog.querySelector("[data-gallery-credit]");
34
+ this._stage = this._dialog.querySelector("[data-gallery-stage]");
35
+ this._frame = this._dialog.querySelector(".modal-dialog");
36
+ this._progress = this._dialog.querySelector(".progress");
37
+ this._progressBar = this._progress?.querySelector(".progress-bar");
38
+ this._progress?.setAttribute("aria-hidden", "true");
39
+ const controls = (suffix) => this._dialog.id ? element.ownerDocument.getElementById(`${this._dialog.id}-${suffix}`) : null;
40
+ this._enterButton = controls("fullscreen");
41
+ this._exitButton = controls("windowed");
42
+ this._playButton = controls("play");
43
+ this._pauseButton = controls("pause");
44
+ this._listeners = [];
45
+ this._listen(this._dialog, "show.bs.modal", (event) => this._onShow(event));
46
+ this._listen(this._window, "resize", () => this._layoutStage());
47
+ this._listen(this._dialog, "shown.bs.modal", () => this._carouselInstance());
48
+ this._listen(this._carousel, "slide.bs.carousel", (event) => this.show(event.to, false));
49
+ this._listen(this._carousel, "slid.bs.carousel", () => this._restartTimer());
50
+ this._listen(this._dialog, "keydown", (event) => this._onKeydown(event));
51
+ this._listen(this._dialog, "hidden.bs.modal", () => {
52
+ this.play(false);
53
+ this.fullscreen(false);
54
+ });
55
+ const actions = [
56
+ [controls("previous"), () => this._step(false)],
57
+ [controls("next"), () => this._step(true)],
58
+ [this._playButton, () => this.play(true, true)],
59
+ [this._pauseButton, () => this.play(false, true)],
60
+ [this._enterButton, () => this.fullscreen(true, true)],
61
+ [this._exitButton, () => this.fullscreen(false, true)]
62
+ ];
63
+ for (const [button, action] of actions) {
64
+ if (button) this._listen(button, "click", action);
65
+ }
66
+ instances.set(element, this);
67
+ }
68
+ get index() {
69
+ const active = this._slides.findIndex((slide) => slide.classList.contains("active"));
70
+ return Math.max(active, 0);
71
+ }
72
+ /** Marks photo `index` current, loads it and its neighbours. */
73
+ show(index, activate = true) {
74
+ const count = this._slides.length;
75
+ if (!count) return this;
76
+ const current = (Number(index) % count + count) % count;
77
+ if (activate) {
78
+ this._slides.forEach((slide2, position) => {
79
+ slide2.classList.toggle("active", position === current);
80
+ });
81
+ }
82
+ for (const offset of [0, 1, -1]) {
83
+ this._load(this._slides[(current + offset + count) % count]);
84
+ }
85
+ const slide = this._slides[current];
86
+ if (this._caption) {
87
+ this._caption.textContent = slide.getAttribute("data-gallery-slide-title") || slide.querySelector("img")?.alt || "";
88
+ }
89
+ if (this._credit) {
90
+ const credit = slide.getAttribute("data-gallery-slide-credit") || "";
91
+ this._credit.textContent = credit;
92
+ this._credit.classList.toggle("d-none", !credit);
93
+ }
94
+ if (this._counter) {
95
+ const template = this._counter.getAttribute("data-gallery-counter") || "{current} / {total}";
96
+ this._counter.textContent = template.replaceAll("{current}", String(current + 1)).replaceAll("{total}", String(count));
97
+ }
98
+ return this;
99
+ }
100
+ /** Starts or stops the slideshow. */
101
+ play(on = true, moveFocus = false) {
102
+ this._playing = Boolean(on) && Boolean(this._carouselInstance());
103
+ this._progress?.classList.toggle("d-none", !this._playing);
104
+ this._restartTimer();
105
+ this._playButton?.classList.toggle("d-none", this._playing);
106
+ this._pauseButton?.classList.toggle("d-none", !this._playing);
107
+ this._counter?.setAttribute("aria-live", this._playing ? "off" : "polite");
108
+ if (moveFocus) (this._playing ? this._pauseButton : this._playButton)?.focus();
109
+ return this;
110
+ }
111
+ get isFullscreen() {
112
+ return Boolean(this._frame?.classList.contains("modal-fullscreen"));
113
+ }
114
+ /** Switches between the large dialog and full screen. */
115
+ fullscreen(on = true, moveFocus = false) {
116
+ if (!this._frame) return this;
117
+ const value = Boolean(on);
118
+ this._resize(() => {
119
+ this._frame.classList.toggle("modal-fullscreen", value);
120
+ this._layoutStage();
121
+ });
122
+ if (moveFocus) (value ? this._exitButton : this._enterButton)?.focus();
123
+ return this;
124
+ }
125
+ dispose() {
126
+ this._stopTimer();
127
+ for (const [target, type, listener] of this._listeners) {
128
+ target.removeEventListener(type, listener);
129
+ }
130
+ this._listeners = [];
131
+ instances.delete(this._element);
132
+ }
133
+ _listen(target, type, listener) {
134
+ target.addEventListener(type, listener);
135
+ this._listeners.push([target, type, listener]);
136
+ }
137
+ // The dialog fills the screen after the full-screen button, or below the
138
+ // breakpoint of Bootstrap's .modal-fullscreen-{breakpoint}-down.
139
+ _fillsScreen() {
140
+ if (this.isFullscreen) return true;
141
+ const breakpoint = this._frame?.className.match(/\bmodal-fullscreen-(\w+)-down\b/)?.[1];
142
+ if (!breakpoint) return false;
143
+ const width = Number.parseFloat(this._window.getComputedStyle(this._element.ownerDocument.documentElement).getPropertyValue(`--bs-breakpoint-${breakpoint}`));
144
+ return Number.isFinite(width) && this._window.matchMedia(`(max-width: ${width - 0.02}px)`).matches;
145
+ }
146
+ // Runs one photo's time on the Progress bar, then shows the next photo.
147
+ _restartTimer() {
148
+ this._stopTimer();
149
+ if (!this._playing) return;
150
+ const advance = () => {
151
+ if (this._playing) this._step(true);
152
+ };
153
+ if (typeof this._progressBar?.animate === "function") {
154
+ const timer = this._progressBar.animate(
155
+ [{ width: "0%" }, { width: "100%" }],
156
+ { duration: SLIDESHOW_INTERVAL, easing: "linear", fill: "forwards" }
157
+ );
158
+ timer.addEventListener("finish", () => {
159
+ if (this._timer === timer) advance();
160
+ });
161
+ this._timer = timer;
162
+ } else {
163
+ this._timeout = this._window.setTimeout(advance, SLIDESHOW_INTERVAL);
164
+ }
165
+ }
166
+ _stopTimer() {
167
+ this._timer?.cancel();
168
+ this._timer = null;
169
+ this._window.clearTimeout(this._timeout);
170
+ }
171
+ // Animates the dialog from its old box to the new one: the content keeps
172
+ // its final layout and starts scaled to where it was (first, last, invert,
173
+ // play). Without motion the change applies at once.
174
+ _resize(change) {
175
+ const content = this._frame.querySelector(".modal-content");
176
+ const reduced = this._window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
177
+ if (!content || typeof content.animate !== "function" || reduced || !this._dialog.classList.contains("show")) {
178
+ change();
179
+ return;
180
+ }
181
+ this._resizing?.cancel();
182
+ const first = content.getBoundingClientRect();
183
+ change();
184
+ const last = content.getBoundingClientRect();
185
+ if (!first.width || !first.height || !last.width || !last.height) return;
186
+ this._resizing = content.animate([
187
+ {
188
+ transformOrigin: "0 0",
189
+ transform: `translate(${first.left - last.left}px, ${first.top - last.top}px) scale(${first.width / last.width}, ${first.height / last.height})`
190
+ },
191
+ { transformOrigin: "0 0", transform: "none" }
192
+ ], { duration: RESIZE_DURATION, easing: "cubic-bezier(0.2, 0, 0, 1)" });
193
+ }
194
+ _layoutStage() {
195
+ const fill = this._fillsScreen();
196
+ this._stage?.classList.toggle("gallery-stage", !fill);
197
+ this._enterButton?.classList.toggle("d-none", fill);
198
+ this._exitButton?.classList.toggle("d-none", !this.isFullscreen);
199
+ }
200
+ _onShow(event) {
201
+ this._layoutStage();
202
+ const trigger = event.relatedTarget;
203
+ const value = trigger?.closest?.("[data-gallery-index]")?.getAttribute("data-gallery-index");
204
+ this.show(value === null || value === void 0 ? 0 : Number.parseInt(value, 10) || 0);
205
+ }
206
+ _onKeydown(event) {
207
+ if (!["ArrowLeft", "ArrowRight"].includes(event.key)) return;
208
+ if (this._carousel.contains(event.target)) return;
209
+ event.preventDefault();
210
+ const rtl = this._element.ownerDocument.documentElement.dir === "rtl";
211
+ this._step(event.key === "ArrowRight" !== rtl);
212
+ }
213
+ _step(forward) {
214
+ const carousel = this._carouselInstance();
215
+ if (!carousel) return;
216
+ if (forward) carousel.next();
217
+ else carousel.prev();
218
+ }
219
+ _carouselInstance(create = true) {
220
+ const Carousel = this._window.bootstrap?.Carousel;
221
+ if (!Carousel) return null;
222
+ if (!create) return Carousel.getInstance(this._carousel);
223
+ return Carousel.getOrCreateInstance(this._carousel, {
224
+ interval: false,
225
+ pause: false,
226
+ ride: false,
227
+ keyboard: true,
228
+ touch: true,
229
+ wrap: true
230
+ });
231
+ }
232
+ _load(slide) {
233
+ const image = slide?.querySelector("img[data-gallery-src]");
234
+ if (!image || image.hasAttribute("src")) return;
235
+ const srcset = image.getAttribute("data-gallery-srcset");
236
+ if (srcset) image.setAttribute("srcset", srcset);
237
+ image.setAttribute("src", image.getAttribute("data-gallery-src"));
238
+ }
239
+ }
240
+ function initImageGalleries(root = document) {
241
+ const galleries = Array.from(root.querySelectorAll("[data-image-gallery]")).map((element) => ImageGallery.getOrCreateInstance(element));
242
+ return () => galleries.forEach((gallery) => gallery.dispose());
243
+ }
244
+ export {
245
+ ImageGallery as default,
246
+ initImageGalleries
247
+ };