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