marquee-content 5.0.3 → 6.0.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.
package/dist/index.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
 
3
3
  //#region src/marquee-content.css?inline
4
- var marquee_content_default = ":host{--marquee-content-gap:2rem;--marquee-content-distance:0px;--marquee-content-duration:20s;width:100%;color:inherit;font:inherit;pointer-events:none;display:block;position:relative;overflow:hidden}:host([pause-on-hover]:not([pause-on-hover=false])){pointer-events:auto}:host([hidden]){display:none}.marquee-content__viewport{width:100%;overflow:hidden}.marquee-content__track{-webkit-backface-visibility:hidden;backface-visibility:hidden;will-change:transform;width:max-content;display:flex;transform:translate(0,0)}.marquee-content__group{align-items:center;gap:var(--marquee-content-gap);white-space:nowrap;color:inherit;font:inherit;flex:none;padding-inline-end:var(--marquee-content-gap);display:flex}.marquee-content__group>*{flex:none}\n";
4
+ var marquee_content_default = ":host{--marquee-content-gap:2rem;--marquee-content-distance:0px;--marquee-content-duration:20s;width:100%;color:inherit;font:inherit;pointer-events:none;display:block;position:relative;overflow:hidden}:host([data-marquee-content-pause-on-hover]){pointer-events:auto}:host([hidden]){display:none}.marquee-content__viewport{width:100%;overflow:hidden}.marquee-content__track{-webkit-backface-visibility:hidden;backface-visibility:hidden;will-change:transform;width:max-content;display:flex;transform:translate(0,0)}.marquee-content__group{align-items:center;gap:var(--marquee-content-gap);white-space:nowrap;color:inherit;font:inherit;flex:none;padding-inline-end:var(--marquee-content-gap);display:flex}.marquee-content__group>*{flex:none}:host([data-marquee-content-reduced-motion]) .marquee-content__track{will-change:auto;width:100%}:host([data-marquee-content-reduced-motion]) .marquee-content__group{white-space:normal;overflow-wrap:anywhere;flex-wrap:wrap;flex:auto;width:100%;padding-inline-end:0}:host([data-marquee-content-reduced-motion]) .marquee-content__group>*{min-width:0;max-width:100%}\n";
5
5
 
6
6
  //#endregion
7
7
  //#region src/styles.ts
@@ -62,17 +62,19 @@ const OBSERVED_ATTRIBUTES = [
62
62
  "speed",
63
63
  "mode",
64
64
  "direction",
65
- "pause-on-hover"
65
+ "pause-on-hover",
66
+ "responsive-speed"
66
67
  ];
67
68
  const OPTION_ATTRIBUTE_MAP = {
68
69
  speed: "speed",
69
70
  mode: "mode",
70
71
  direction: "direction",
71
- pauseOnHover: "pause-on-hover"
72
+ pauseOnHover: "pause-on-hover",
73
+ responsiveSpeed: "responsive-speed"
72
74
  };
73
75
  const MARQUEE_CONTENT_CSS_PROPS = ["--marquee-content-distance", "--marquee-content-duration"];
74
76
  const SCROLL_TIME_SCALE_SMOOTHING_SECONDS = .12;
75
- const SCROLL_ANIMATION_SEED_ITERATIONS = 500;
77
+ const HTMLElementBase = globalThis.HTMLElement ?? class {};
76
78
  const MARQUEE_CONTENT_RUNTIME_CSS = `
77
79
  @keyframes marquee-content-auto {
78
80
  to {
@@ -103,10 +105,10 @@ const TEMPLATE = `
103
105
  <slot class="marquee-content__source"></slot>
104
106
 
105
107
  <div class="marquee-content__viewport" part="viewport">
106
- <div class="marquee-content__track" part="track" aria-hidden="true"></div>
108
+ <div class="marquee-content__track" part="track" aria-hidden="true" inert></div>
107
109
  </div>
108
110
  `;
109
- var MarqueeContent = class MarqueeContent extends HTMLElement {
111
+ var MarqueeContent = class MarqueeContent extends HTMLElementBase {
110
112
  static get observedAttributes() {
111
113
  return [...OBSERVED_ATTRIBUTES];
112
114
  }
@@ -115,10 +117,12 @@ var MarqueeContent = class MarqueeContent extends HTMLElement {
115
117
  static stylesheetEntry = normalizeStyleInput(marqueeContentCssText);
116
118
  static styleOverridesEntry = normalizeStyleInput(null);
117
119
  static configureStyles(options = {}) {
120
+ if (!("stylesheet" in options) && !("styleOverrides" in options)) return;
118
121
  if ("stylesheet" in options) MarqueeContent.stylesheetEntry = normalizeStyleInput(options.stylesheet ?? marqueeContentCssText);
119
122
  if ("styleOverrides" in options) MarqueeContent.styleOverridesEntry = normalizeStyleInput(options.styleOverrides);
120
123
  MarqueeContent.instances.forEach((instance) => {
121
124
  instance.applyAllStyles();
125
+ instance.scheduleRebuild();
122
126
  });
123
127
  }
124
128
  static setStylesheet(stylesheet) {
@@ -147,6 +151,9 @@ var MarqueeContent = class MarqueeContent extends HTMLElement {
147
151
  reducedMotion = false;
148
152
  isListeningToScroll = false;
149
153
  distance = 0;
154
+ containerWidth = 0;
155
+ group = null;
156
+ layoutPhase = null;
150
157
  scrollAnimation = null;
151
158
  scrollAnimationDuration = 0;
152
159
  scrollAnimationDistance = 0;
@@ -158,6 +165,7 @@ var MarqueeContent = class MarqueeContent extends HTMLElement {
158
165
  lastScrollY = 0;
159
166
  lastScrollTime = 0;
160
167
  constructor() {
168
+ if (typeof HTMLElement === "undefined") throw new Error("[MarqueeContent] Create elements in a browser, after client initialization.");
161
169
  super();
162
170
  this.shadow = this.attachShadow({ mode: "open" });
163
171
  this.applyAllStyles();
@@ -175,21 +183,28 @@ var MarqueeContent = class MarqueeContent extends HTMLElement {
175
183
  if (this.mounted) return;
176
184
  MarqueeContent.instances.add(this);
177
185
  this.mounted = true;
186
+ this.applyAllStyles();
178
187
  this.abortController = new AbortController();
179
188
  this.options = this.resolveOptions();
180
189
  this.lastScrollY = this.getScrollY();
181
190
  this.lastScrollTime = performance.now();
182
191
  this.sourceSlot.addEventListener("slotchange", this.handleSlotChange, { signal: this.abortController.signal });
183
- this.shadow.addEventListener("load", this.scheduleRebuild, {
184
- capture: true,
185
- signal: this.abortController.signal
186
- });
192
+ for (const eventName of ["load", "error"]) {
193
+ this.addEventListener(eventName, this.handleSourceLoad, {
194
+ capture: true,
195
+ signal: this.abortController.signal
196
+ });
197
+ this.shadow.addEventListener(eventName, this.completeLayout, {
198
+ capture: true,
199
+ signal: this.abortController.signal
200
+ });
201
+ }
187
202
  this.addEventListener("pointerenter", this.handlePointerEnter, { signal: this.abortController.signal });
188
203
  this.addEventListener("pointerleave", this.handlePointerLeave, { signal: this.abortController.signal });
189
204
  this.addEventListener("focusin", this.handleFocusIn, { signal: this.abortController.signal });
190
205
  this.addEventListener("focusout", this.handleFocusOut, { signal: this.abortController.signal });
191
206
  window.addEventListener("orientationchange", this.scheduleRebuild, { signal: this.abortController.signal });
192
- this.resizeObserver = new ResizeObserver(this.scheduleRebuild);
207
+ this.resizeObserver = new ResizeObserver(this.handleResize);
193
208
  this.resizeObserver.observe(this);
194
209
  this.intersectionObserver = new IntersectionObserver(this.handleIntersectionChange);
195
210
  this.intersectionObserver.observe(this);
@@ -208,7 +223,7 @@ var MarqueeContent = class MarqueeContent extends HTMLElement {
208
223
  this.options = this.resolveOptions();
209
224
  if (!this.mounted) return;
210
225
  this.updateScrollListener();
211
- this.scheduleRebuild();
226
+ this.applyOptions();
212
227
  }
213
228
  get speed() {
214
229
  return this.options.speed;
@@ -234,6 +249,12 @@ var MarqueeContent = class MarqueeContent extends HTMLElement {
234
249
  set pauseOnHover(value) {
235
250
  this.setNullableAttribute("pause-on-hover", value);
236
251
  }
252
+ get responsiveSpeed() {
253
+ return this.options.responsiveSpeed;
254
+ }
255
+ set responsiveSpeed(value) {
256
+ this.setNullableAttribute("responsive-speed", value);
257
+ }
237
258
  update(options = {}) {
238
259
  Object.entries(options).forEach(([key, value]) => {
239
260
  const attributeName = OPTION_ATTRIBUTE_MAP[key];
@@ -241,7 +262,7 @@ var MarqueeContent = class MarqueeContent extends HTMLElement {
241
262
  });
242
263
  this.options = this.resolveOptions();
243
264
  this.updateScrollListener();
244
- this.scheduleRebuild();
265
+ this.applyOptions();
245
266
  }
246
267
  refresh() {
247
268
  this.scheduleRebuild();
@@ -269,6 +290,9 @@ var MarqueeContent = class MarqueeContent extends HTMLElement {
269
290
  this.intersectionObserver?.disconnect();
270
291
  this.removeReducedMotionWatcher();
271
292
  this.abortController?.abort();
293
+ this.group = null;
294
+ this.layoutPhase = null;
295
+ this.containerWidth = 0;
272
296
  this.resizeObserver = null;
273
297
  this.mutationObserver = null;
274
298
  this.intersectionObserver = null;
@@ -277,14 +301,27 @@ var MarqueeContent = class MarqueeContent extends HTMLElement {
277
301
  this.removeAttribute("data-marquee-content-ready");
278
302
  this.removeAttribute("data-marquee-content-mode");
279
303
  this.removeAttribute("data-marquee-content-pause-on-hover");
304
+ this.removeAttribute("data-marquee-content-reduced-motion");
280
305
  MARQUEE_CONTENT_CSS_PROPS.forEach((prop) => {
281
306
  this.style.removeProperty(prop);
282
307
  });
283
308
  this.distance = 0;
309
+ this.isHovered = false;
310
+ this.isFocused = false;
311
+ this.isInViewport = true;
312
+ this.scrollDirectionFactor = 1;
313
+ this.scrollBoostStartValue = 0;
314
+ this.scrollBoostStartedAt = 0;
284
315
  this.scrollProgress = 0;
285
316
  this.currentTimeScale = 1;
286
317
  this.lastFrameTime = null;
287
318
  }
319
+ handleResize = () => {
320
+ if (this.clientWidth !== this.containerWidth || this.group && this.layoutPhase === null && Math.abs(this.measureGroup(this.group) - this.distance) > .01) this.scheduleRebuild();
321
+ };
322
+ handleSourceLoad = (event) => {
323
+ if (event.target instanceof Node && event.target !== this && this.contains(event.target)) this.scheduleRebuild();
324
+ };
288
325
  handleSlotChange = () => {
289
326
  this.watchSourceNodes();
290
327
  this.scheduleRebuild();
@@ -316,6 +353,7 @@ var MarqueeContent = class MarqueeContent extends HTMLElement {
316
353
  if (this.reducedMotion) this.track.style.transform = "none";
317
354
  else if (this.options.mode === "scroll") this.track.style.transform = "";
318
355
  this.updateAnimationLoopState();
356
+ this.scheduleRebuild();
319
357
  };
320
358
  handleScroll = () => {
321
359
  if (this.options.mode !== "scroll") return;
@@ -364,49 +402,87 @@ var MarqueeContent = class MarqueeContent extends HTMLElement {
364
402
  cancelAnimationFrame(this.rebuildFrameId);
365
403
  this.rebuildFrameId = null;
366
404
  }
405
+ applyOptions() {
406
+ if (!this.mounted) return;
407
+ const progress = this.scrollAnimation ? this.getScrollAnimationProgress() : this.getAutoAnimation()?.effect?.getComputedTiming().progress ?? this.scrollProgress;
408
+ const duration = this.distance / this.getCurrentSpeed(this.clientWidth);
409
+ this.setAttribute("data-marquee-content-mode", this.options.mode);
410
+ this.toggleAttribute("data-marquee-content-pause-on-hover", this.options.pauseOnHover);
411
+ this.toggleAttribute("data-marquee-content-reduced-motion", this.reducedMotion);
412
+ if (this.distance > 0) {
413
+ this.style.setProperty("--marquee-content-distance", `${this.distance}px`);
414
+ this.style.setProperty("--marquee-content-duration", `${duration}s`);
415
+ }
416
+ if (progress != null) this.scrollProgress = progress;
417
+ this.updateAnimationLoopState();
418
+ if (progress != null && this.options.mode === "auto" && !this.reducedMotion) {
419
+ const animation = this.getAutoAnimation();
420
+ if (animation) animation.currentTime = duration * 1e3 * (this.options.direction === "ltr" ? 1 - progress : progress);
421
+ }
422
+ }
423
+ getAutoAnimation() {
424
+ return this.track.getAnimations().find((animation) => "animationName" in animation && animation.animationName === "marquee-content-auto");
425
+ }
367
426
  rebuild() {
368
427
  if (!this.mounted) return;
369
428
  this.options = this.resolveOptions();
370
429
  this.updateScrollListener();
430
+ const progress = this.scrollAnimation ? this.getScrollAnimationProgress() : this.getAutoAnimation()?.effect?.getComputedTiming().progress;
431
+ if (progress != null) this.scrollProgress = progress;
432
+ this.distance = 0;
433
+ this.layoutPhase = null;
434
+ this.updateAnimationLoopState();
371
435
  this.removeAttribute("data-marquee-content-ready");
372
- this.setAttribute("data-marquee-content-mode", this.options.mode);
373
- if (this.options.pauseOnHover) this.setAttribute("data-marquee-content-pause-on-hover", "true");
374
- else this.removeAttribute("data-marquee-content-pause-on-hover");
375
- const containerWidth = this.clientWidth;
376
- if (containerWidth <= 0) {
377
- this.distance = 0;
378
- this.track.replaceChildren();
379
- this.updateAnimationLoopState();
380
- return;
436
+ this.toggleAttribute("data-marquee-content-reduced-motion", this.reducedMotion);
437
+ this.containerWidth = this.clientWidth;
438
+ if (this.group) {
439
+ this.resizeObserver?.unobserve(this.group);
440
+ this.group = null;
381
441
  }
382
442
  this.sourceNodes = this.getSourceNodes();
383
- if (this.sourceNodes.length === 0) {
443
+ if (this.containerWidth <= 0 || this.sourceNodes.length === 0) {
384
444
  this.distance = 0;
385
445
  this.track.replaceChildren();
386
- this.updateAnimationLoopState();
446
+ this.applyOptions();
387
447
  return;
388
448
  }
389
- const group = this.createGroup();
390
- const targetWidth = containerWidth * this.options.minGroupWidthRatio;
391
- this.track.replaceChildren(group);
392
- this.expandGroupToWidth(group, targetWidth);
393
- const clone = group.cloneNode(true);
394
- clone.setAttribute("aria-hidden", "true");
395
- this.track.appendChild(clone);
396
- this.distance = group.scrollWidth;
449
+ this.group = this.createGroup();
450
+ this.track.replaceChildren(this.group);
451
+ this.layoutPhase = "source";
452
+ this.completeLayout();
453
+ }
454
+ completeLayout = () => {
455
+ const group = this.group;
456
+ if (!this.mounted || !group || this.layoutPhase === null) return;
457
+ if (this.hasPendingImages(this.track)) return;
458
+ if (this.layoutPhase === "source") {
459
+ this.layoutPhase = "copies";
460
+ if (!this.reducedMotion) {
461
+ this.expandGroupToWidth(group, this.containerWidth * this.options.minGroupWidthRatio);
462
+ this.track.appendChild(group.cloneNode(true));
463
+ }
464
+ if (this.hasPendingImages(this.track)) return;
465
+ }
466
+ this.layoutPhase = null;
467
+ this.distance = this.measureGroup(group);
468
+ this.resizeObserver?.observe(group);
397
469
  if (this.distance <= 0) {
398
470
  this.track.replaceChildren();
399
- this.updateAnimationLoopState();
471
+ this.applyOptions();
400
472
  return;
401
473
  }
402
- const currentSpeed = this.getCurrentSpeed(containerWidth);
403
- const duration = this.distance / currentSpeed;
404
- this.style.setProperty("--marquee-content-distance", `${this.distance}px`);
405
- this.style.setProperty("--marquee-content-duration", `${duration}s`);
406
- if (this.reducedMotion) this.track.style.transform = "none";
407
- else this.track.style.transform = "";
408
474
  this.setAttribute("data-marquee-content-ready", "true");
409
- this.updateAnimationLoopState();
475
+ this.applyOptions();
476
+ };
477
+ hasPendingImages(root) {
478
+ return Array.from(root.querySelectorAll("img")).some((image) => !image.complete);
479
+ }
480
+ measureGroup(group) {
481
+ const style = getComputedStyle(group);
482
+ const width = Number.parseFloat(style.width);
483
+ if (!Number.isFinite(width)) return group.scrollWidth;
484
+ if (style.boxSizing === "border-box") return width;
485
+ return width + Number.parseFloat(style.paddingLeft) + Number.parseFloat(style.paddingRight) + Number.parseFloat(style.borderLeftWidth) + Number.parseFloat(style.borderRightWidth);
410
486
  }
411
487
  createGroup() {
412
488
  const group = document.createElement("div");
@@ -416,7 +492,7 @@ var MarqueeContent = class MarqueeContent extends HTMLElement {
416
492
  return group;
417
493
  }
418
494
  expandGroupToWidth(group, targetWidth) {
419
- const sourceSetWidth = group.scrollWidth;
495
+ const sourceSetWidth = this.measureGroup(group);
420
496
  if (sourceSetWidth <= 0) return;
421
497
  const repeatCount = Math.ceil(targetWidth / sourceSetWidth);
422
498
  for (let index = 1; index < repeatCount; index += 1) this.appendSourceNodes(group);
@@ -433,6 +509,10 @@ var MarqueeContent = class MarqueeContent extends HTMLElement {
433
509
  if (clone instanceof Element) {
434
510
  const currentPart = clone.getAttribute("part");
435
511
  clone.setAttribute("part", currentPart ? `${currentPart} item` : "item");
512
+ if (clone instanceof HTMLImageElement) clone.loading = "eager";
513
+ clone.querySelectorAll("img").forEach((image) => {
514
+ image.loading = "eager";
515
+ });
436
516
  }
437
517
  return clone;
438
518
  }
@@ -445,7 +525,7 @@ var MarqueeContent = class MarqueeContent extends HTMLElement {
445
525
  watchSourceNodes() {
446
526
  this.mutationObserver?.disconnect();
447
527
  this.mutationObserver = new MutationObserver(this.scheduleRebuild);
448
- this.getSourceNodes().forEach((node) => {
528
+ this.sourceSlot.assignedNodes({ flatten: true }).forEach((node) => {
449
529
  if (node.nodeType === Node.ELEMENT_NODE) {
450
530
  this.mutationObserver?.observe(node, {
451
531
  attributes: true,
@@ -564,8 +644,10 @@ var MarqueeContent = class MarqueeContent extends HTMLElement {
564
644
  iterations: Number.POSITIVE_INFINITY,
565
645
  easing: "linear"
566
646
  });
567
- animation.currentTime = durationMs * (SCROLL_ANIMATION_SEED_ITERATIONS + progress);
568
- animation.playbackRate = this.currentTimeScale;
647
+ const reversed = this.currentTimeScale < 0;
648
+ animation.effect?.updateTiming({ direction: reversed ? "reverse" : "normal" });
649
+ animation.currentTime = durationMs * (reversed ? 1 - progress : progress);
650
+ animation.playbackRate = Math.abs(this.currentTimeScale);
569
651
  this.scrollAnimation = animation;
570
652
  this.scrollAnimationDuration = durationMs;
571
653
  this.scrollAnimationDistance = this.distance;
@@ -585,7 +667,8 @@ var MarqueeContent = class MarqueeContent extends HTMLElement {
585
667
  const currentTime = animation.currentTime;
586
668
  if (typeof currentTime !== "number") return this.scrollProgress;
587
669
  const fraction = currentTime / this.scrollAnimationDuration % 1;
588
- return fraction < 0 ? fraction + 1 : fraction;
670
+ const progress = fraction < 0 ? fraction + 1 : fraction;
671
+ return animation.effect?.getTiming().direction === "reverse" ? (1 - progress) % 1 : progress;
589
672
  }
590
673
  updateScrollPlaybackRate(now, immediate) {
591
674
  if (!this.scrollAnimation) return true;
@@ -602,7 +685,15 @@ var MarqueeContent = class MarqueeContent extends HTMLElement {
602
685
  }
603
686
  const settled = this.scrollBoostStartValue === 0 && Math.abs(target - this.currentTimeScale) < .001;
604
687
  if (settled) this.currentTimeScale = target;
605
- if (this.scrollAnimation.playbackRate !== this.currentTimeScale) this.scrollAnimation.updatePlaybackRate(this.currentTimeScale);
688
+ const reversed = this.currentTimeScale < 0;
689
+ const effect = this.scrollAnimation.effect;
690
+ if (effect && effect.getTiming().direction === "reverse" !== reversed) {
691
+ const progress = this.getScrollAnimationProgress();
692
+ effect.updateTiming({ direction: reversed ? "reverse" : "normal" });
693
+ this.scrollAnimation.currentTime = this.scrollAnimationDuration * (reversed ? 1 - progress : progress);
694
+ }
695
+ const playbackRate = Math.abs(this.currentTimeScale);
696
+ if (this.scrollAnimation.playbackRate !== playbackRate) this.scrollAnimation.updatePlaybackRate(playbackRate);
606
697
  return settled;
607
698
  }
608
699
  getCurrentTimeScale(now) {
@@ -641,7 +732,8 @@ var MarqueeContent = class MarqueeContent extends HTMLElement {
641
732
  speed: this.num("speed", DEFAULT_OPTIONS.speed, 10, 500),
642
733
  mode: this.parseMode(this.getAttribute("mode"), DEFAULT_OPTIONS.mode),
643
734
  direction: this.parseDirection(this.getAttribute("direction"), DEFAULT_OPTIONS.direction),
644
- pauseOnHover: this.parseBoolean(this.getAttribute("pause-on-hover"), DEFAULT_OPTIONS.pauseOnHover)
735
+ pauseOnHover: this.parseBoolean(this.getAttribute("pause-on-hover"), DEFAULT_OPTIONS.pauseOnHover),
736
+ responsiveSpeed: this.parseBoolean(this.getAttribute("responsive-speed"), DEFAULT_OPTIONS.responsiveSpeed)
645
737
  };
646
738
  }
647
739
  num(attributeName, fallback, min, max) {
@@ -703,7 +795,10 @@ var MarqueeContent = class MarqueeContent extends HTMLElement {
703
795
  function defineMarqueeContent(tagName = MARQUEE_CONTENT_TAG, options = {}) {
704
796
  if (typeof window === "undefined" || !window.customElements) return;
705
797
  MarqueeContent.configureStyles(options);
706
- if (!window.customElements.get(tagName)) window.customElements.define(tagName, MarqueeContent);
798
+ if (!window.customElements.get(tagName)) {
799
+ const elementClass = tagName === "marquee-content" ? MarqueeContent : class extends MarqueeContent {};
800
+ window.customElements.define(tagName, elementClass);
801
+ }
707
802
  }
708
803
  function initMarqueeContents(options = {}) {
709
804
  const { tagName = MARQUEE_CONTENT_TAG, ...styleOptions } = options;