marquee-content 5.0.0-dev.2 → 5.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/README.md +133 -103
- package/dist/index.cjs +643 -58
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +134 -0
- package/dist/index.d.ts +134 -4
- package/dist/index.js +647 -0
- package/dist/index.js.map +1 -0
- package/dist/marquee-content.css +1 -1
- package/package.json +49 -29
- package/dist/index.es.js +0 -558
- package/dist/index.umd.js +0 -71
- package/dist/marquee-content.d.ts +0 -166
- package/dist/styles.d.ts +0 -10
package/dist/index.js
ADDED
|
@@ -0,0 +1,647 @@
|
|
|
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{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";
|
|
3
|
+
|
|
4
|
+
//#endregion
|
|
5
|
+
//#region src/styles.ts
|
|
6
|
+
const marqueeContentCssText = marquee_content_default;
|
|
7
|
+
function createConstructableStyleSheet(cssText) {
|
|
8
|
+
if (typeof CSSStyleSheet === "undefined" || typeof CSSStyleSheet.prototype.replaceSync !== "function") return null;
|
|
9
|
+
try {
|
|
10
|
+
const sheet = new CSSStyleSheet();
|
|
11
|
+
sheet.replaceSync(cssText);
|
|
12
|
+
return sheet;
|
|
13
|
+
} catch {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function cssStyleSheetToText(sheet) {
|
|
18
|
+
try {
|
|
19
|
+
return Array.from(sheet.cssRules).map((rule) => rule.cssText).join("\n");
|
|
20
|
+
} catch {
|
|
21
|
+
return "";
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function normalizeStyleInput(input) {
|
|
25
|
+
if (!input) return {
|
|
26
|
+
sheet: null,
|
|
27
|
+
cssText: ""
|
|
28
|
+
};
|
|
29
|
+
if (typeof input === "string") return {
|
|
30
|
+
sheet: createConstructableStyleSheet(input),
|
|
31
|
+
cssText: input
|
|
32
|
+
};
|
|
33
|
+
return {
|
|
34
|
+
sheet: input,
|
|
35
|
+
cssText: cssStyleSheetToText(input)
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/marquee-content.ts
|
|
41
|
+
const MARQUEE_CONTENT_TAG = "marquee-content";
|
|
42
|
+
const DEFAULT_OPTIONS = {
|
|
43
|
+
speed: 90,
|
|
44
|
+
mode: "auto",
|
|
45
|
+
direction: "rtl",
|
|
46
|
+
minGroupWidthRatio: 1.05,
|
|
47
|
+
responsiveSpeed: true,
|
|
48
|
+
referenceWidth: 1280,
|
|
49
|
+
minSpeedFactor: .55,
|
|
50
|
+
maxSpeedFactor: 1,
|
|
51
|
+
pauseOnHover: false,
|
|
52
|
+
pauseWhenOutOfView: true,
|
|
53
|
+
recalculateAfterFontsReady: true,
|
|
54
|
+
scrollVelocityFactor: 150,
|
|
55
|
+
scrollMinExtraSpeed: .05,
|
|
56
|
+
scrollMaxExtraSpeed: 5,
|
|
57
|
+
scrollInertiaDuration: 2500
|
|
58
|
+
};
|
|
59
|
+
const OBSERVED_ATTRIBUTES = [
|
|
60
|
+
"speed",
|
|
61
|
+
"mode",
|
|
62
|
+
"direction",
|
|
63
|
+
"pause-on-hover"
|
|
64
|
+
];
|
|
65
|
+
const OPTION_ATTRIBUTE_MAP = {
|
|
66
|
+
speed: "speed",
|
|
67
|
+
mode: "mode",
|
|
68
|
+
direction: "direction",
|
|
69
|
+
pauseOnHover: "pause-on-hover"
|
|
70
|
+
};
|
|
71
|
+
const MARQUEE_CONTENT_CSS_PROPS = ["--marquee-content-distance", "--marquee-content-duration"];
|
|
72
|
+
const MARQUEE_CONTENT_RUNTIME_CSS = `
|
|
73
|
+
@keyframes marquee-content-auto {
|
|
74
|
+
to {
|
|
75
|
+
transform: translate3d(calc(-1 * var(--marquee-content-distance)), 0, 0);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.marquee-content__source {
|
|
80
|
+
position: absolute;
|
|
81
|
+
width: 0;
|
|
82
|
+
height: 0;
|
|
83
|
+
overflow: hidden;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
.marquee-content__source::slotted(*) {
|
|
87
|
+
position: absolute;
|
|
88
|
+
width: 1px;
|
|
89
|
+
height: 1px;
|
|
90
|
+
padding: 0;
|
|
91
|
+
margin: -1px;
|
|
92
|
+
overflow: hidden;
|
|
93
|
+
clip-path: inset(50%);
|
|
94
|
+
white-space: nowrap;
|
|
95
|
+
border: 0;
|
|
96
|
+
}
|
|
97
|
+
`;
|
|
98
|
+
const TEMPLATE = `
|
|
99
|
+
<slot class="marquee-content__source"></slot>
|
|
100
|
+
|
|
101
|
+
<div class="marquee-content__viewport" part="viewport">
|
|
102
|
+
<div class="marquee-content__track" part="track" aria-hidden="true"></div>
|
|
103
|
+
</div>
|
|
104
|
+
`;
|
|
105
|
+
var MarqueeContent = class MarqueeContent extends HTMLElement {
|
|
106
|
+
static get observedAttributes() {
|
|
107
|
+
return [...OBSERVED_ATTRIBUTES];
|
|
108
|
+
}
|
|
109
|
+
static instances = /* @__PURE__ */ new Set();
|
|
110
|
+
static runtimeStylesheetEntry = normalizeStyleInput(MARQUEE_CONTENT_RUNTIME_CSS);
|
|
111
|
+
static stylesheetEntry = normalizeStyleInput(marqueeContentCssText);
|
|
112
|
+
static styleOverridesEntry = normalizeStyleInput(null);
|
|
113
|
+
static configureStyles(options = {}) {
|
|
114
|
+
if ("stylesheet" in options) MarqueeContent.stylesheetEntry = normalizeStyleInput(options.stylesheet ?? marqueeContentCssText);
|
|
115
|
+
if ("styleOverrides" in options) MarqueeContent.styleOverridesEntry = normalizeStyleInput(options.styleOverrides);
|
|
116
|
+
MarqueeContent.instances.forEach((instance) => {
|
|
117
|
+
instance.applyAllStyles();
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
static setStylesheet(stylesheet) {
|
|
121
|
+
MarqueeContent.configureStyles({ stylesheet });
|
|
122
|
+
}
|
|
123
|
+
static setStyleOverrides(styleOverrides) {
|
|
124
|
+
MarqueeContent.configureStyles({ styleOverrides });
|
|
125
|
+
}
|
|
126
|
+
shadow;
|
|
127
|
+
sourceSlot;
|
|
128
|
+
track;
|
|
129
|
+
options = { ...DEFAULT_OPTIONS };
|
|
130
|
+
sourceNodes = [];
|
|
131
|
+
resizeObserver = null;
|
|
132
|
+
mutationObserver = null;
|
|
133
|
+
intersectionObserver = null;
|
|
134
|
+
abortController = null;
|
|
135
|
+
reducedMotionMediaQuery = null;
|
|
136
|
+
animationFrameId = null;
|
|
137
|
+
rebuildFrameId = null;
|
|
138
|
+
lastFrameTime = null;
|
|
139
|
+
mounted = false;
|
|
140
|
+
isHovered = false;
|
|
141
|
+
isFocused = false;
|
|
142
|
+
isInViewport = true;
|
|
143
|
+
reducedMotion = false;
|
|
144
|
+
isListeningToScroll = false;
|
|
145
|
+
distance = 0;
|
|
146
|
+
offset = 0;
|
|
147
|
+
scrollDirectionFactor = 1;
|
|
148
|
+
scrollBoostStartValue = 0;
|
|
149
|
+
scrollBoostStartedAt = 0;
|
|
150
|
+
lastScrollY = 0;
|
|
151
|
+
lastScrollTime = 0;
|
|
152
|
+
constructor() {
|
|
153
|
+
super();
|
|
154
|
+
this.shadow = this.attachShadow({ mode: "open" });
|
|
155
|
+
this.applyAllStyles();
|
|
156
|
+
const template = document.createElement("template");
|
|
157
|
+
template.innerHTML = TEMPLATE;
|
|
158
|
+
this.shadow.appendChild(template.content.cloneNode(true));
|
|
159
|
+
const sourceSlot = this.shadow.querySelector("slot.marquee-content__source");
|
|
160
|
+
const track = this.shadow.querySelector(".marquee-content__track");
|
|
161
|
+
if (!sourceSlot) throw new Error("[MarqueeContent] Source slot was not found.");
|
|
162
|
+
if (!track) throw new Error("[MarqueeContent] Track element was not found.");
|
|
163
|
+
this.sourceSlot = sourceSlot;
|
|
164
|
+
this.track = track;
|
|
165
|
+
}
|
|
166
|
+
connectedCallback() {
|
|
167
|
+
if (this.mounted) return;
|
|
168
|
+
MarqueeContent.instances.add(this);
|
|
169
|
+
this.mounted = true;
|
|
170
|
+
this.abortController = new AbortController();
|
|
171
|
+
this.options = this.resolveOptions();
|
|
172
|
+
this.lastScrollY = this.getScrollY();
|
|
173
|
+
this.lastScrollTime = performance.now();
|
|
174
|
+
this.sourceSlot.addEventListener("slotchange", this.handleSlotChange, { signal: this.abortController.signal });
|
|
175
|
+
this.shadow.addEventListener("load", this.scheduleRebuild, {
|
|
176
|
+
capture: true,
|
|
177
|
+
signal: this.abortController.signal
|
|
178
|
+
});
|
|
179
|
+
this.addEventListener("pointerenter", this.handlePointerEnter, { signal: this.abortController.signal });
|
|
180
|
+
this.addEventListener("pointerleave", this.handlePointerLeave, { signal: this.abortController.signal });
|
|
181
|
+
this.addEventListener("focusin", this.handleFocusIn, { signal: this.abortController.signal });
|
|
182
|
+
this.addEventListener("focusout", this.handleFocusOut, { signal: this.abortController.signal });
|
|
183
|
+
window.addEventListener("orientationchange", this.scheduleRebuild, { signal: this.abortController.signal });
|
|
184
|
+
this.resizeObserver = new ResizeObserver(this.scheduleRebuild);
|
|
185
|
+
this.resizeObserver.observe(this);
|
|
186
|
+
this.intersectionObserver = new IntersectionObserver(this.handleIntersectionChange);
|
|
187
|
+
this.intersectionObserver.observe(this);
|
|
188
|
+
this.setupReducedMotionWatcher();
|
|
189
|
+
this.watchSourceNodes();
|
|
190
|
+
this.updateScrollListener();
|
|
191
|
+
this.rebuild();
|
|
192
|
+
if (this.options.recalculateAfterFontsReady) this.rebuildAfterFontsReady();
|
|
193
|
+
}
|
|
194
|
+
disconnectedCallback() {
|
|
195
|
+
this.teardown();
|
|
196
|
+
MarqueeContent.instances.delete(this);
|
|
197
|
+
}
|
|
198
|
+
attributeChangedCallback(_name, oldValue, newValue) {
|
|
199
|
+
if (oldValue === newValue) return;
|
|
200
|
+
this.options = this.resolveOptions();
|
|
201
|
+
if (!this.mounted) return;
|
|
202
|
+
this.updateScrollListener();
|
|
203
|
+
this.scheduleRebuild();
|
|
204
|
+
}
|
|
205
|
+
get speed() {
|
|
206
|
+
return this.options.speed;
|
|
207
|
+
}
|
|
208
|
+
set speed(value) {
|
|
209
|
+
this.setNullableAttribute("speed", value);
|
|
210
|
+
}
|
|
211
|
+
get mode() {
|
|
212
|
+
return this.options.mode;
|
|
213
|
+
}
|
|
214
|
+
set mode(value) {
|
|
215
|
+
this.setNullableAttribute("mode", value);
|
|
216
|
+
}
|
|
217
|
+
get direction() {
|
|
218
|
+
return this.options.direction;
|
|
219
|
+
}
|
|
220
|
+
set direction(value) {
|
|
221
|
+
this.setNullableAttribute("direction", value);
|
|
222
|
+
}
|
|
223
|
+
get pauseOnHover() {
|
|
224
|
+
return this.options.pauseOnHover;
|
|
225
|
+
}
|
|
226
|
+
set pauseOnHover(value) {
|
|
227
|
+
this.setNullableAttribute("pause-on-hover", value);
|
|
228
|
+
}
|
|
229
|
+
update(options = {}) {
|
|
230
|
+
Object.entries(options).forEach(([key, value]) => {
|
|
231
|
+
const attributeName = OPTION_ATTRIBUTE_MAP[key];
|
|
232
|
+
if (attributeName) this.setNullableAttribute(attributeName, value);
|
|
233
|
+
});
|
|
234
|
+
this.options = this.resolveOptions();
|
|
235
|
+
this.updateScrollListener();
|
|
236
|
+
this.scheduleRebuild();
|
|
237
|
+
}
|
|
238
|
+
refresh() {
|
|
239
|
+
this.scheduleRebuild();
|
|
240
|
+
}
|
|
241
|
+
applyAllStyles() {
|
|
242
|
+
const runtime = MarqueeContent.runtimeStylesheetEntry;
|
|
243
|
+
const base = MarqueeContent.stylesheetEntry;
|
|
244
|
+
const overrides = MarqueeContent.styleOverridesEntry;
|
|
245
|
+
const sheets = [];
|
|
246
|
+
if (runtime.sheet) sheets.push(runtime.sheet);
|
|
247
|
+
if (base.sheet) sheets.push(base.sheet);
|
|
248
|
+
if (overrides.sheet) sheets.push(overrides.sheet);
|
|
249
|
+
this.shadow.adoptedStyleSheets = sheets;
|
|
250
|
+
}
|
|
251
|
+
teardown() {
|
|
252
|
+
if (!this.mounted) return;
|
|
253
|
+
this.mounted = false;
|
|
254
|
+
this.stopAnimationLoop();
|
|
255
|
+
this.stopCssAnimation();
|
|
256
|
+
this.cancelScheduledRebuild();
|
|
257
|
+
this.updateScrollListener(false);
|
|
258
|
+
this.resizeObserver?.disconnect();
|
|
259
|
+
this.mutationObserver?.disconnect();
|
|
260
|
+
this.intersectionObserver?.disconnect();
|
|
261
|
+
this.removeReducedMotionWatcher();
|
|
262
|
+
this.abortController?.abort();
|
|
263
|
+
this.resizeObserver = null;
|
|
264
|
+
this.mutationObserver = null;
|
|
265
|
+
this.intersectionObserver = null;
|
|
266
|
+
this.abortController = null;
|
|
267
|
+
this.track.replaceChildren();
|
|
268
|
+
this.removeAttribute("data-marquee-content-ready");
|
|
269
|
+
this.removeAttribute("data-marquee-content-mode");
|
|
270
|
+
this.removeAttribute("data-marquee-content-pause-on-hover");
|
|
271
|
+
MARQUEE_CONTENT_CSS_PROPS.forEach((prop) => {
|
|
272
|
+
this.style.removeProperty(prop);
|
|
273
|
+
});
|
|
274
|
+
this.distance = 0;
|
|
275
|
+
this.offset = 0;
|
|
276
|
+
this.lastFrameTime = null;
|
|
277
|
+
}
|
|
278
|
+
handleSlotChange = () => {
|
|
279
|
+
this.watchSourceNodes();
|
|
280
|
+
this.scheduleRebuild();
|
|
281
|
+
};
|
|
282
|
+
handlePointerEnter = () => {
|
|
283
|
+
this.isHovered = true;
|
|
284
|
+
this.updateAnimationLoopState();
|
|
285
|
+
};
|
|
286
|
+
handlePointerLeave = () => {
|
|
287
|
+
this.isHovered = false;
|
|
288
|
+
this.updateAnimationLoopState();
|
|
289
|
+
};
|
|
290
|
+
handleFocusIn = () => {
|
|
291
|
+
this.isFocused = true;
|
|
292
|
+
this.updateAnimationLoopState();
|
|
293
|
+
};
|
|
294
|
+
handleFocusOut = () => {
|
|
295
|
+
this.isFocused = false;
|
|
296
|
+
this.updateAnimationLoopState();
|
|
297
|
+
};
|
|
298
|
+
handleIntersectionChange = (entries) => {
|
|
299
|
+
const entry = entries[0];
|
|
300
|
+
if (!entry) return;
|
|
301
|
+
this.isInViewport = entry.isIntersecting;
|
|
302
|
+
this.updateAnimationLoopState();
|
|
303
|
+
};
|
|
304
|
+
handleReducedMotionChange = (event) => {
|
|
305
|
+
this.reducedMotion = event.matches;
|
|
306
|
+
if (this.reducedMotion) this.track.style.transform = "none";
|
|
307
|
+
else if (this.options.mode === "scroll") this.applyTransform();
|
|
308
|
+
this.updateAnimationLoopState();
|
|
309
|
+
};
|
|
310
|
+
handleScroll = () => {
|
|
311
|
+
if (this.options.mode !== "scroll") return;
|
|
312
|
+
const now = performance.now();
|
|
313
|
+
const currentScrollY = this.getScrollY();
|
|
314
|
+
const deltaY = currentScrollY - this.lastScrollY;
|
|
315
|
+
const deltaTime = Math.max((now - this.lastScrollTime) / 1e3, .016);
|
|
316
|
+
this.lastScrollY = currentScrollY;
|
|
317
|
+
this.lastScrollTime = now;
|
|
318
|
+
if (Math.abs(deltaY) < .5) return;
|
|
319
|
+
const velocity = deltaY / deltaTime;
|
|
320
|
+
this.scrollDirectionFactor = velocity >= 0 ? 1 : -1;
|
|
321
|
+
const extraSpeed = this.clamp(velocity / this.options.scrollVelocityFactor, -this.options.scrollMaxExtraSpeed, this.options.scrollMaxExtraSpeed);
|
|
322
|
+
if (Math.abs(extraSpeed) >= this.options.scrollMinExtraSpeed) {
|
|
323
|
+
this.scrollBoostStartValue = extraSpeed;
|
|
324
|
+
this.scrollBoostStartedAt = now;
|
|
325
|
+
} else this.scrollBoostStartValue = 0;
|
|
326
|
+
this.updateAnimationLoopState();
|
|
327
|
+
};
|
|
328
|
+
updateScrollListener(shouldListen = this.mounted && this.options.mode === "scroll") {
|
|
329
|
+
if (shouldListen === this.isListeningToScroll) return;
|
|
330
|
+
if (shouldListen) {
|
|
331
|
+
this.lastScrollY = this.getScrollY();
|
|
332
|
+
this.lastScrollTime = performance.now();
|
|
333
|
+
window.addEventListener("scroll", this.handleScroll, { passive: true });
|
|
334
|
+
} else window.removeEventListener("scroll", this.handleScroll);
|
|
335
|
+
this.isListeningToScroll = shouldListen;
|
|
336
|
+
}
|
|
337
|
+
scheduleRebuild = () => {
|
|
338
|
+
if (!this.mounted) return;
|
|
339
|
+
this.cancelScheduledRebuild();
|
|
340
|
+
this.rebuildFrameId = requestAnimationFrame(() => {
|
|
341
|
+
this.rebuildFrameId = null;
|
|
342
|
+
this.rebuild();
|
|
343
|
+
});
|
|
344
|
+
};
|
|
345
|
+
cancelScheduledRebuild() {
|
|
346
|
+
if (this.rebuildFrameId === null) return;
|
|
347
|
+
cancelAnimationFrame(this.rebuildFrameId);
|
|
348
|
+
this.rebuildFrameId = null;
|
|
349
|
+
}
|
|
350
|
+
rebuild() {
|
|
351
|
+
if (!this.mounted) return;
|
|
352
|
+
this.options = this.resolveOptions();
|
|
353
|
+
this.updateScrollListener();
|
|
354
|
+
this.removeAttribute("data-marquee-content-ready");
|
|
355
|
+
this.setAttribute("data-marquee-content-mode", this.options.mode);
|
|
356
|
+
if (this.options.pauseOnHover) this.setAttribute("data-marquee-content-pause-on-hover", "true");
|
|
357
|
+
else this.removeAttribute("data-marquee-content-pause-on-hover");
|
|
358
|
+
const containerWidth = this.clientWidth;
|
|
359
|
+
if (containerWidth <= 0) {
|
|
360
|
+
this.distance = 0;
|
|
361
|
+
this.track.replaceChildren();
|
|
362
|
+
this.updateAnimationLoopState();
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
this.sourceNodes = this.getSourceNodes();
|
|
366
|
+
if (this.sourceNodes.length === 0) {
|
|
367
|
+
this.distance = 0;
|
|
368
|
+
this.track.replaceChildren();
|
|
369
|
+
this.updateAnimationLoopState();
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
const group = this.createGroup();
|
|
373
|
+
const targetWidth = containerWidth * this.options.minGroupWidthRatio;
|
|
374
|
+
this.track.replaceChildren(group);
|
|
375
|
+
this.expandGroupToWidth(group, targetWidth);
|
|
376
|
+
const clone = group.cloneNode(true);
|
|
377
|
+
clone.setAttribute("aria-hidden", "true");
|
|
378
|
+
this.track.appendChild(clone);
|
|
379
|
+
this.distance = group.scrollWidth;
|
|
380
|
+
this.offset = this.normalizeOffset(this.offset);
|
|
381
|
+
if (this.distance <= 0) {
|
|
382
|
+
this.track.replaceChildren();
|
|
383
|
+
this.updateAnimationLoopState();
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
const currentSpeed = this.getCurrentSpeed(containerWidth);
|
|
387
|
+
const duration = this.distance / currentSpeed;
|
|
388
|
+
this.style.setProperty("--marquee-content-distance", `${this.distance}px`);
|
|
389
|
+
this.style.setProperty("--marquee-content-duration", `${duration}s`);
|
|
390
|
+
if (this.reducedMotion) this.track.style.transform = "none";
|
|
391
|
+
else if (this.options.mode === "scroll") this.applyTransform();
|
|
392
|
+
else this.track.style.transform = "";
|
|
393
|
+
this.setAttribute("data-marquee-content-ready", "true");
|
|
394
|
+
this.updateAnimationLoopState();
|
|
395
|
+
}
|
|
396
|
+
createGroup() {
|
|
397
|
+
const group = document.createElement("div");
|
|
398
|
+
group.className = "marquee-content__group";
|
|
399
|
+
group.setAttribute("part", "group");
|
|
400
|
+
this.appendSourceNodes(group);
|
|
401
|
+
return group;
|
|
402
|
+
}
|
|
403
|
+
expandGroupToWidth(group, targetWidth) {
|
|
404
|
+
const sourceSetWidth = group.scrollWidth;
|
|
405
|
+
if (sourceSetWidth <= 0) return;
|
|
406
|
+
const repeatCount = Math.ceil(targetWidth / sourceSetWidth);
|
|
407
|
+
for (let index = 1; index < repeatCount; index += 1) this.appendSourceNodes(group);
|
|
408
|
+
}
|
|
409
|
+
appendSourceNodes(target) {
|
|
410
|
+
const fragment = document.createDocumentFragment();
|
|
411
|
+
this.sourceNodes.forEach((node) => {
|
|
412
|
+
fragment.append(this.cloneSourceNode(node));
|
|
413
|
+
});
|
|
414
|
+
target.appendChild(fragment);
|
|
415
|
+
}
|
|
416
|
+
cloneSourceNode(node) {
|
|
417
|
+
const clone = node.cloneNode(true);
|
|
418
|
+
if (clone instanceof Element) {
|
|
419
|
+
const currentPart = clone.getAttribute("part");
|
|
420
|
+
clone.setAttribute("part", currentPart ? `${currentPart} item` : "item");
|
|
421
|
+
}
|
|
422
|
+
return clone;
|
|
423
|
+
}
|
|
424
|
+
getSourceNodes() {
|
|
425
|
+
return this.sourceSlot.assignedNodes({ flatten: true }).filter((node) => {
|
|
426
|
+
if (node.nodeType === Node.TEXT_NODE) return Boolean(node.textContent?.trim());
|
|
427
|
+
return node.nodeType === Node.ELEMENT_NODE;
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
watchSourceNodes() {
|
|
431
|
+
this.mutationObserver?.disconnect();
|
|
432
|
+
this.mutationObserver = new MutationObserver(this.scheduleRebuild);
|
|
433
|
+
this.getSourceNodes().forEach((node) => {
|
|
434
|
+
if (node.nodeType === Node.ELEMENT_NODE) {
|
|
435
|
+
this.mutationObserver?.observe(node, {
|
|
436
|
+
attributes: true,
|
|
437
|
+
childList: true,
|
|
438
|
+
characterData: true,
|
|
439
|
+
subtree: true
|
|
440
|
+
});
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
if (node.nodeType === Node.TEXT_NODE) this.mutationObserver?.observe(node, { characterData: true });
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
setupReducedMotionWatcher() {
|
|
447
|
+
if (!("matchMedia" in window)) return;
|
|
448
|
+
this.reducedMotionMediaQuery = window.matchMedia("(prefers-reduced-motion: reduce)");
|
|
449
|
+
this.reducedMotion = this.reducedMotionMediaQuery.matches;
|
|
450
|
+
this.reducedMotionMediaQuery.addEventListener("change", this.handleReducedMotionChange, { signal: this.abortController?.signal });
|
|
451
|
+
}
|
|
452
|
+
removeReducedMotionWatcher() {
|
|
453
|
+
if (!this.reducedMotionMediaQuery) return;
|
|
454
|
+
this.reducedMotionMediaQuery.removeEventListener("change", this.handleReducedMotionChange);
|
|
455
|
+
this.reducedMotionMediaQuery = null;
|
|
456
|
+
}
|
|
457
|
+
updateAnimationLoopState() {
|
|
458
|
+
if (!this.mounted || this.distance <= 0 || this.reducedMotion) {
|
|
459
|
+
this.stopAnimationLoop();
|
|
460
|
+
this.stopCssAnimation();
|
|
461
|
+
if (this.reducedMotion) this.track.style.transform = "none";
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
if (this.options.mode === "scroll") {
|
|
465
|
+
this.stopCssAnimation();
|
|
466
|
+
if (this.canAnimate()) this.startAnimationLoop();
|
|
467
|
+
else this.stopAnimationLoop();
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
this.stopAnimationLoop();
|
|
471
|
+
this.startCssAnimation();
|
|
472
|
+
this.track.style.animationPlayState = this.canAnimate() ? "running" : "paused";
|
|
473
|
+
}
|
|
474
|
+
canAnimate() {
|
|
475
|
+
if (this.options.pauseOnHover && this.isHovered) return false;
|
|
476
|
+
if (this.isFocused) return false;
|
|
477
|
+
if (this.options.pauseWhenOutOfView && !this.isInViewport) return false;
|
|
478
|
+
return true;
|
|
479
|
+
}
|
|
480
|
+
startAnimationLoop() {
|
|
481
|
+
if (this.animationFrameId !== null) return;
|
|
482
|
+
this.lastFrameTime = performance.now();
|
|
483
|
+
this.animationFrameId = requestAnimationFrame(this.tick);
|
|
484
|
+
}
|
|
485
|
+
stopAnimationLoop() {
|
|
486
|
+
if (this.animationFrameId === null) {
|
|
487
|
+
this.lastFrameTime = null;
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
cancelAnimationFrame(this.animationFrameId);
|
|
491
|
+
this.animationFrameId = null;
|
|
492
|
+
this.lastFrameTime = null;
|
|
493
|
+
}
|
|
494
|
+
startCssAnimation() {
|
|
495
|
+
const animationDirection = this.options.direction === "ltr" ? "reverse" : "normal";
|
|
496
|
+
this.track.style.transform = "";
|
|
497
|
+
if (this.track.style.animationName !== "marquee-content-auto") {
|
|
498
|
+
this.track.style.animationName = "marquee-content-auto";
|
|
499
|
+
this.track.style.animationDuration = "var(--marquee-content-duration)";
|
|
500
|
+
this.track.style.animationTimingFunction = "linear";
|
|
501
|
+
this.track.style.animationIterationCount = "infinite";
|
|
502
|
+
}
|
|
503
|
+
if (this.track.style.animationDirection !== animationDirection) this.track.style.animationDirection = animationDirection;
|
|
504
|
+
}
|
|
505
|
+
stopCssAnimation() {
|
|
506
|
+
this.track.style.animationName = "";
|
|
507
|
+
this.track.style.animationDuration = "";
|
|
508
|
+
this.track.style.animationTimingFunction = "";
|
|
509
|
+
this.track.style.animationIterationCount = "";
|
|
510
|
+
this.track.style.animationDirection = "";
|
|
511
|
+
this.track.style.animationPlayState = "";
|
|
512
|
+
}
|
|
513
|
+
tick = (now) => {
|
|
514
|
+
this.animationFrameId = null;
|
|
515
|
+
if (!this.mounted || this.options.mode !== "scroll" || this.distance <= 0 || this.reducedMotion || !this.canAnimate()) {
|
|
516
|
+
this.stopAnimationLoop();
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
const previousTime = this.lastFrameTime ?? now;
|
|
520
|
+
const deltaTime = Math.min((now - previousTime) / 1e3, .064);
|
|
521
|
+
this.lastFrameTime = now;
|
|
522
|
+
const currentSpeed = this.getCurrentSpeed(this.clientWidth);
|
|
523
|
+
const timeScale = this.getCurrentTimeScale(now);
|
|
524
|
+
this.offset = this.normalizeOffset(this.offset + currentSpeed * timeScale * deltaTime);
|
|
525
|
+
this.applyTransform();
|
|
526
|
+
this.animationFrameId = requestAnimationFrame(this.tick);
|
|
527
|
+
};
|
|
528
|
+
getCurrentTimeScale(now) {
|
|
529
|
+
const directionFactor = this.getDirectionFactor();
|
|
530
|
+
if (this.options.mode !== "scroll") return directionFactor;
|
|
531
|
+
const boost = this.getScrollBoost(now);
|
|
532
|
+
const scrollTimeScale = this.scrollDirectionFactor + boost;
|
|
533
|
+
return this.clamp(directionFactor * scrollTimeScale, -1 - this.options.scrollMaxExtraSpeed, 1 + this.options.scrollMaxExtraSpeed);
|
|
534
|
+
}
|
|
535
|
+
getDirectionFactor() {
|
|
536
|
+
return this.options.direction === "ltr" ? -1 : 1;
|
|
537
|
+
}
|
|
538
|
+
getScrollBoost(now) {
|
|
539
|
+
if (this.scrollBoostStartValue === 0) return 0;
|
|
540
|
+
const duration = this.options.scrollInertiaDuration;
|
|
541
|
+
if (duration <= 0) {
|
|
542
|
+
this.scrollBoostStartValue = 0;
|
|
543
|
+
return 0;
|
|
544
|
+
}
|
|
545
|
+
const progress = this.clamp((now - this.scrollBoostStartedAt) / duration, 0, 1);
|
|
546
|
+
if (progress >= 1) {
|
|
547
|
+
this.scrollBoostStartValue = 0;
|
|
548
|
+
return 0;
|
|
549
|
+
}
|
|
550
|
+
const easedProgress = this.easeOutCubic(progress);
|
|
551
|
+
return this.scrollBoostStartValue * (1 - easedProgress);
|
|
552
|
+
}
|
|
553
|
+
applyTransform() {
|
|
554
|
+
this.track.style.transform = `translate3d(${-this.offset}px, 0, 0)`;
|
|
555
|
+
}
|
|
556
|
+
normalizeOffset(value) {
|
|
557
|
+
if (this.distance <= 0) return 0;
|
|
558
|
+
const normalized = value % this.distance;
|
|
559
|
+
return normalized < 0 ? normalized + this.distance : normalized;
|
|
560
|
+
}
|
|
561
|
+
getCurrentSpeed(containerWidth) {
|
|
562
|
+
if (!this.options.responsiveSpeed) return this.options.speed;
|
|
563
|
+
const factor = this.clamp(containerWidth / this.options.referenceWidth, this.options.minSpeedFactor, this.options.maxSpeedFactor);
|
|
564
|
+
return this.options.speed * factor;
|
|
565
|
+
}
|
|
566
|
+
resolveOptions() {
|
|
567
|
+
return {
|
|
568
|
+
...DEFAULT_OPTIONS,
|
|
569
|
+
speed: this.num("speed", DEFAULT_OPTIONS.speed, 10, 500),
|
|
570
|
+
mode: this.parseMode(this.getAttribute("mode"), DEFAULT_OPTIONS.mode),
|
|
571
|
+
direction: this.parseDirection(this.getAttribute("direction"), DEFAULT_OPTIONS.direction),
|
|
572
|
+
pauseOnHover: this.parseBoolean(this.getAttribute("pause-on-hover"), DEFAULT_OPTIONS.pauseOnHover)
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
num(attributeName, fallback, min, max) {
|
|
576
|
+
return this.clamp(this.parseNumber(this.getAttribute(attributeName), fallback), min, max);
|
|
577
|
+
}
|
|
578
|
+
parseNumber(value, fallback) {
|
|
579
|
+
if (value === null || value.trim() === "") return fallback;
|
|
580
|
+
const parsed = Number(value);
|
|
581
|
+
return Number.isFinite(parsed) ? parsed : fallback;
|
|
582
|
+
}
|
|
583
|
+
parseBoolean(value, fallback) {
|
|
584
|
+
if (value === null) return fallback;
|
|
585
|
+
const normalizedValue = value.trim().toLowerCase();
|
|
586
|
+
if (normalizedValue === "") return true;
|
|
587
|
+
if ([
|
|
588
|
+
"true",
|
|
589
|
+
"1",
|
|
590
|
+
"yes",
|
|
591
|
+
"on"
|
|
592
|
+
].includes(normalizedValue)) return true;
|
|
593
|
+
if ([
|
|
594
|
+
"false",
|
|
595
|
+
"0",
|
|
596
|
+
"no",
|
|
597
|
+
"off"
|
|
598
|
+
].includes(normalizedValue)) return false;
|
|
599
|
+
return fallback;
|
|
600
|
+
}
|
|
601
|
+
parseMode(value, fallback) {
|
|
602
|
+
if (value === "auto" || value === "scroll") return value;
|
|
603
|
+
return fallback;
|
|
604
|
+
}
|
|
605
|
+
parseDirection(value, fallback) {
|
|
606
|
+
if (value === "rtl" || value === "ltr") return value;
|
|
607
|
+
return fallback;
|
|
608
|
+
}
|
|
609
|
+
setNullableAttribute(name, value) {
|
|
610
|
+
if (value === null || value === void 0) {
|
|
611
|
+
this.removeAttribute(name);
|
|
612
|
+
return;
|
|
613
|
+
}
|
|
614
|
+
this.setAttribute(name, String(value));
|
|
615
|
+
}
|
|
616
|
+
getScrollY() {
|
|
617
|
+
return window.scrollY || window.pageYOffset || 0;
|
|
618
|
+
}
|
|
619
|
+
clamp(value, min, max) {
|
|
620
|
+
return Math.min(Math.max(value, min), max);
|
|
621
|
+
}
|
|
622
|
+
easeOutCubic(value) {
|
|
623
|
+
return 1 - (1 - value) ** 3;
|
|
624
|
+
}
|
|
625
|
+
rebuildAfterFontsReady() {
|
|
626
|
+
const fonts = document.fonts;
|
|
627
|
+
if (!fonts?.ready) return;
|
|
628
|
+
fonts.ready.then(this.scheduleRebuild).catch(() => void 0);
|
|
629
|
+
}
|
|
630
|
+
};
|
|
631
|
+
function defineMarqueeContent(tagName = MARQUEE_CONTENT_TAG, options = {}) {
|
|
632
|
+
if (typeof window === "undefined" || !window.customElements) return;
|
|
633
|
+
MarqueeContent.configureStyles(options);
|
|
634
|
+
if (!window.customElements.get(tagName)) window.customElements.define(tagName, MarqueeContent);
|
|
635
|
+
}
|
|
636
|
+
function initMarqueeContents(options = {}) {
|
|
637
|
+
const { tagName = MARQUEE_CONTENT_TAG, ...styleOptions } = options;
|
|
638
|
+
defineMarqueeContent(tagName, styleOptions);
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
//#endregion
|
|
642
|
+
//#region src/index.ts
|
|
643
|
+
defineMarqueeContent();
|
|
644
|
+
|
|
645
|
+
//#endregion
|
|
646
|
+
export { MARQUEE_CONTENT_TAG, MarqueeContent, createConstructableStyleSheet, cssStyleSheetToText, defineMarqueeContent, initMarqueeContents, marqueeContentCssText, normalizeStyleInput };
|
|
647
|
+
//# sourceMappingURL=index.js.map
|