marquee-content 5.0.0-dev.1 → 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 +141 -79
- 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 -548
- package/dist/index.umd.js +0 -71
- package/dist/marquee-content.d.ts +0 -163
- package/dist/styles.d.ts +0 -10
package/dist/index.es.js
DELETED
|
@@ -1,548 +0,0 @@
|
|
|
1
|
-
//#endregion
|
|
2
|
-
//#region src/styles.ts
|
|
3
|
-
var e = ":host {\n --marquee-content-gap: 2rem;\n --marquee-content-distance: 0px;\n --marquee-content-duration: 20s;\n\n display: block;\n overflow: hidden;\n width: 100%;\n color: inherit;\n font: inherit;\n\n /*\n По умолчанию компонент не ловит pointer events.\n Это удобно для декоративной бегущей строки.\n Если включить pause-on-hover, pointer-events включатся автоматически.\n */\n pointer-events: none;\n}\n\n:host([pause-on-hover]:not([pause-on-hover=\"false\"])) {\n pointer-events: auto;\n}\n\n:host([hidden]) {\n display: none;\n}\n\n.marquee-content__source {\n display: none;\n}\n\n.marquee-content__viewport {\n overflow: hidden;\n width: 100%;\n}\n\n.marquee-content__track {\n display: flex;\n width: max-content;\n transform: translate3d(0, 0, 0);\n will-change: transform;\n}\n\n.marquee-content__group {\n display: flex;\n flex: 0 0 auto;\n align-items: center;\n gap: var(--marquee-content-gap);\n padding-inline-end: var(--marquee-content-gap);\n white-space: nowrap;\n color: inherit;\n font: inherit;\n}\n\n.marquee-content__group > * {\n flex: 0 0 auto;\n}\n\n@media (prefers-reduced-motion: reduce) {\n .marquee-content__track {\n transform: none !important;\n }\n}\n";
|
|
4
|
-
function t(e) {
|
|
5
|
-
if (typeof CSSStyleSheet > "u" || typeof CSSStyleSheet.prototype.replaceSync != "function") return null;
|
|
6
|
-
try {
|
|
7
|
-
let t = new CSSStyleSheet();
|
|
8
|
-
return t.replaceSync(e), t;
|
|
9
|
-
} catch {
|
|
10
|
-
return null;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
function n(e) {
|
|
14
|
-
try {
|
|
15
|
-
return Array.from(e.cssRules).map((e) => e.cssText).join("\n");
|
|
16
|
-
} catch {
|
|
17
|
-
return "";
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
function r(e) {
|
|
21
|
-
return e ? typeof e == "string" ? {
|
|
22
|
-
sheet: t(e),
|
|
23
|
-
cssText: e
|
|
24
|
-
} : {
|
|
25
|
-
sheet: e,
|
|
26
|
-
cssText: n(e)
|
|
27
|
-
} : {
|
|
28
|
-
sheet: null,
|
|
29
|
-
cssText: ""
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
function i(e) {
|
|
33
|
-
return typeof CSSStyleSheet < "u" && "adoptedStyleSheets" in e && typeof CSSStyleSheet.prototype.replaceSync == "function";
|
|
34
|
-
}
|
|
35
|
-
//#endregion
|
|
36
|
-
//#region src/marquee-content.ts
|
|
37
|
-
var a = "marquee-content", o = {
|
|
38
|
-
speed: 90,
|
|
39
|
-
direction: "rtl",
|
|
40
|
-
minGroupWidthRatio: 1.05,
|
|
41
|
-
responsiveSpeed: !0,
|
|
42
|
-
referenceWidth: 1280,
|
|
43
|
-
minSpeedFactor: .55,
|
|
44
|
-
maxSpeedFactor: 1,
|
|
45
|
-
pauseOnHover: !1,
|
|
46
|
-
pauseWhenOutOfView: !0,
|
|
47
|
-
recalculateAfterFontsReady: !0,
|
|
48
|
-
scrollVelocityFactor: 400,
|
|
49
|
-
scrollMinExtraSpeed: .15,
|
|
50
|
-
scrollMaxExtraSpeed: 3,
|
|
51
|
-
scrollInertiaDuration: 1600,
|
|
52
|
-
debug: !1
|
|
53
|
-
}, s = [
|
|
54
|
-
"speed",
|
|
55
|
-
"direction",
|
|
56
|
-
"min-group-width-ratio",
|
|
57
|
-
"responsive-speed",
|
|
58
|
-
"reference-width",
|
|
59
|
-
"min-speed-factor",
|
|
60
|
-
"max-speed-factor",
|
|
61
|
-
"pause-on-hover",
|
|
62
|
-
"pause-when-out-of-view",
|
|
63
|
-
"recalculate-after-fonts-ready",
|
|
64
|
-
"scroll-velocity-factor",
|
|
65
|
-
"scroll-min-extra-speed",
|
|
66
|
-
"scroll-max-extra-speed",
|
|
67
|
-
"scroll-inertia-duration",
|
|
68
|
-
"debug"
|
|
69
|
-
], c = {
|
|
70
|
-
speed: "speed",
|
|
71
|
-
direction: "direction",
|
|
72
|
-
minGroupWidthRatio: "min-group-width-ratio",
|
|
73
|
-
responsiveSpeed: "responsive-speed",
|
|
74
|
-
referenceWidth: "reference-width",
|
|
75
|
-
minSpeedFactor: "min-speed-factor",
|
|
76
|
-
maxSpeedFactor: "max-speed-factor",
|
|
77
|
-
pauseOnHover: "pause-on-hover",
|
|
78
|
-
pauseWhenOutOfView: "pause-when-out-of-view",
|
|
79
|
-
recalculateAfterFontsReady: "recalculate-after-fonts-ready",
|
|
80
|
-
scrollVelocityFactor: "scroll-velocity-factor",
|
|
81
|
-
scrollMinExtraSpeed: "scroll-min-extra-speed",
|
|
82
|
-
scrollMaxExtraSpeed: "scroll-max-extra-speed",
|
|
83
|
-
scrollInertiaDuration: "scroll-inertia-duration",
|
|
84
|
-
debug: "debug"
|
|
85
|
-
}, l = ["--marquee-content-distance", "--marquee-content-duration"], u = "\n <slot class=\"marquee-content__source\"></slot>\n\n <div class=\"marquee-content__viewport\" part=\"viewport\">\n <div class=\"marquee-content__track\" part=\"track\"></div>\n </div>\n", d = class t extends HTMLElement {
|
|
86
|
-
static get observedAttributes() {
|
|
87
|
-
return [...s];
|
|
88
|
-
}
|
|
89
|
-
static instances = /* @__PURE__ */ new Set();
|
|
90
|
-
static stylesheetEntry = r(e);
|
|
91
|
-
static styleOverridesEntry = r(null);
|
|
92
|
-
static configureStyles(n = {}) {
|
|
93
|
-
"stylesheet" in n && (t.stylesheetEntry = r(n.stylesheet ?? e)), "styleOverrides" in n && (t.styleOverridesEntry = r(n.styleOverrides)), t.instances.forEach((e) => {
|
|
94
|
-
e.applyAllStyles();
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
static setStylesheet(e) {
|
|
98
|
-
t.configureStyles({ stylesheet: e });
|
|
99
|
-
}
|
|
100
|
-
static setStyleOverrides(e) {
|
|
101
|
-
t.configureStyles({ styleOverrides: e });
|
|
102
|
-
}
|
|
103
|
-
shadow;
|
|
104
|
-
sourceSlot;
|
|
105
|
-
track;
|
|
106
|
-
options = { ...o };
|
|
107
|
-
sourceNodes = [];
|
|
108
|
-
resizeObserver = null;
|
|
109
|
-
mutationObserver = null;
|
|
110
|
-
intersectionObserver = null;
|
|
111
|
-
abortController = null;
|
|
112
|
-
reducedMotionMediaQuery = null;
|
|
113
|
-
animationFrameId = null;
|
|
114
|
-
rebuildFrameId = null;
|
|
115
|
-
lastFrameTime = null;
|
|
116
|
-
mounted = !1;
|
|
117
|
-
isHovered = !1;
|
|
118
|
-
isInViewport = !0;
|
|
119
|
-
reducedMotion = !1;
|
|
120
|
-
distance = 0;
|
|
121
|
-
offset = 0;
|
|
122
|
-
scrollDirectionFactor = 1;
|
|
123
|
-
scrollBoostStartValue = 0;
|
|
124
|
-
scrollBoostStartedAt = 0;
|
|
125
|
-
lastScrollY = 0;
|
|
126
|
-
lastScrollTime = 0;
|
|
127
|
-
constructor() {
|
|
128
|
-
super(), this.shadow = this.attachShadow({ mode: "open" }), this.applyAllStyles();
|
|
129
|
-
let e = document.createElement("template");
|
|
130
|
-
e.innerHTML = u, this.shadow.appendChild(e.content.cloneNode(!0));
|
|
131
|
-
let t = this.shadow.querySelector("slot.marquee-content__source"), n = this.shadow.querySelector(".marquee-content__track");
|
|
132
|
-
if (!t) throw Error("[MarqueeContent] Source slot was not found.");
|
|
133
|
-
if (!n) throw Error("[MarqueeContent] Track element was not found.");
|
|
134
|
-
this.sourceSlot = t, this.track = n;
|
|
135
|
-
}
|
|
136
|
-
connectedCallback() {
|
|
137
|
-
this.mounted || (t.instances.add(this), this.mounted = !0, this.abortController = new AbortController(), this.options = this.resolveOptions(), this.lastScrollY = this.getScrollY(), this.lastScrollTime = performance.now(), this.sourceSlot.addEventListener("slotchange", this.handleSlotChange, { signal: this.abortController.signal }), this.shadow.addEventListener("load", this.scheduleRebuild, {
|
|
138
|
-
capture: !0,
|
|
139
|
-
signal: this.abortController.signal
|
|
140
|
-
}), this.addEventListener("pointerenter", this.handlePointerEnter, { signal: this.abortController.signal }), this.addEventListener("pointerleave", this.handlePointerLeave, { signal: this.abortController.signal }), window.addEventListener("scroll", this.handleScroll, {
|
|
141
|
-
passive: !0,
|
|
142
|
-
signal: this.abortController.signal
|
|
143
|
-
}), window.addEventListener("orientationchange", this.scheduleRebuild, { signal: this.abortController.signal }), typeof ResizeObserver < "u" ? (this.resizeObserver = new ResizeObserver(this.scheduleRebuild), this.resizeObserver.observe(this)) : window.addEventListener("resize", this.scheduleRebuild, { signal: this.abortController.signal }), "IntersectionObserver" in window && (this.intersectionObserver = new IntersectionObserver(this.handleIntersectionChange), this.intersectionObserver.observe(this)), this.setupReducedMotionWatcher(), this.watchSourceNodes(), this.rebuild(), this.options.recalculateAfterFontsReady && this.rebuildAfterFontsReady());
|
|
144
|
-
}
|
|
145
|
-
disconnectedCallback() {
|
|
146
|
-
this.teardown(), t.instances.delete(this);
|
|
147
|
-
}
|
|
148
|
-
attributeChangedCallback(e, t, n) {
|
|
149
|
-
t !== n && (this.options = this.resolveOptions(), this.mounted && this.scheduleRebuild());
|
|
150
|
-
}
|
|
151
|
-
get speed() {
|
|
152
|
-
return this.options.speed;
|
|
153
|
-
}
|
|
154
|
-
set speed(e) {
|
|
155
|
-
this.setNullableAttribute("speed", e);
|
|
156
|
-
}
|
|
157
|
-
get direction() {
|
|
158
|
-
return this.options.direction;
|
|
159
|
-
}
|
|
160
|
-
set direction(e) {
|
|
161
|
-
this.setNullableAttribute("direction", e);
|
|
162
|
-
}
|
|
163
|
-
get minGroupWidthRatio() {
|
|
164
|
-
return this.options.minGroupWidthRatio;
|
|
165
|
-
}
|
|
166
|
-
set minGroupWidthRatio(e) {
|
|
167
|
-
this.setNullableAttribute("min-group-width-ratio", e);
|
|
168
|
-
}
|
|
169
|
-
get responsiveSpeed() {
|
|
170
|
-
return this.options.responsiveSpeed;
|
|
171
|
-
}
|
|
172
|
-
set responsiveSpeed(e) {
|
|
173
|
-
this.setNullableAttribute("responsive-speed", e);
|
|
174
|
-
}
|
|
175
|
-
get referenceWidth() {
|
|
176
|
-
return this.options.referenceWidth;
|
|
177
|
-
}
|
|
178
|
-
set referenceWidth(e) {
|
|
179
|
-
this.setNullableAttribute("reference-width", e);
|
|
180
|
-
}
|
|
181
|
-
get minSpeedFactor() {
|
|
182
|
-
return this.options.minSpeedFactor;
|
|
183
|
-
}
|
|
184
|
-
set minSpeedFactor(e) {
|
|
185
|
-
this.setNullableAttribute("min-speed-factor", e);
|
|
186
|
-
}
|
|
187
|
-
get maxSpeedFactor() {
|
|
188
|
-
return this.options.maxSpeedFactor;
|
|
189
|
-
}
|
|
190
|
-
set maxSpeedFactor(e) {
|
|
191
|
-
this.setNullableAttribute("max-speed-factor", e);
|
|
192
|
-
}
|
|
193
|
-
get pauseOnHover() {
|
|
194
|
-
return this.options.pauseOnHover;
|
|
195
|
-
}
|
|
196
|
-
set pauseOnHover(e) {
|
|
197
|
-
this.setNullableAttribute("pause-on-hover", e);
|
|
198
|
-
}
|
|
199
|
-
get pauseWhenOutOfView() {
|
|
200
|
-
return this.options.pauseWhenOutOfView;
|
|
201
|
-
}
|
|
202
|
-
set pauseWhenOutOfView(e) {
|
|
203
|
-
this.setNullableAttribute("pause-when-out-of-view", e);
|
|
204
|
-
}
|
|
205
|
-
get recalculateAfterFontsReady() {
|
|
206
|
-
return this.options.recalculateAfterFontsReady;
|
|
207
|
-
}
|
|
208
|
-
set recalculateAfterFontsReady(e) {
|
|
209
|
-
this.setNullableAttribute("recalculate-after-fonts-ready", e);
|
|
210
|
-
}
|
|
211
|
-
get scrollVelocityFactor() {
|
|
212
|
-
return this.options.scrollVelocityFactor;
|
|
213
|
-
}
|
|
214
|
-
set scrollVelocityFactor(e) {
|
|
215
|
-
this.setNullableAttribute("scroll-velocity-factor", e);
|
|
216
|
-
}
|
|
217
|
-
get scrollMinExtraSpeed() {
|
|
218
|
-
return this.options.scrollMinExtraSpeed;
|
|
219
|
-
}
|
|
220
|
-
set scrollMinExtraSpeed(e) {
|
|
221
|
-
this.setNullableAttribute("scroll-min-extra-speed", e);
|
|
222
|
-
}
|
|
223
|
-
get scrollMaxExtraSpeed() {
|
|
224
|
-
return this.options.scrollMaxExtraSpeed;
|
|
225
|
-
}
|
|
226
|
-
set scrollMaxExtraSpeed(e) {
|
|
227
|
-
this.setNullableAttribute("scroll-max-extra-speed", e);
|
|
228
|
-
}
|
|
229
|
-
get scrollInertiaDuration() {
|
|
230
|
-
return this.options.scrollInertiaDuration;
|
|
231
|
-
}
|
|
232
|
-
set scrollInertiaDuration(e) {
|
|
233
|
-
this.setNullableAttribute("scroll-inertia-duration", e);
|
|
234
|
-
}
|
|
235
|
-
get debug() {
|
|
236
|
-
return this.options.debug;
|
|
237
|
-
}
|
|
238
|
-
set debug(e) {
|
|
239
|
-
this.setNullableAttribute("debug", e);
|
|
240
|
-
}
|
|
241
|
-
update(e = {}) {
|
|
242
|
-
Object.entries(e).forEach(([e, t]) => {
|
|
243
|
-
let n = c[e];
|
|
244
|
-
n && this.setNullableAttribute(n, t);
|
|
245
|
-
}), this.options = this.resolveOptions(), this.scheduleRebuild();
|
|
246
|
-
}
|
|
247
|
-
refresh() {
|
|
248
|
-
this.scheduleRebuild();
|
|
249
|
-
}
|
|
250
|
-
adoptStyles(e) {
|
|
251
|
-
t.setStylesheet(e);
|
|
252
|
-
}
|
|
253
|
-
adoptStyleOverrides(e) {
|
|
254
|
-
t.setStyleOverrides(e);
|
|
255
|
-
}
|
|
256
|
-
applyAllStyles() {
|
|
257
|
-
let e = t.stylesheetEntry, n = t.styleOverridesEntry;
|
|
258
|
-
if (i(this.shadow)) {
|
|
259
|
-
let t = [];
|
|
260
|
-
e.sheet && t.push(e.sheet), n.sheet && t.push(n.sheet), this.shadow.adoptedStyleSheets = t, this.removeFallbackStyleElements();
|
|
261
|
-
return;
|
|
262
|
-
}
|
|
263
|
-
this.applyFallbackStyleElement("base", e.cssText), this.applyFallbackStyleElement("overrides", n.cssText);
|
|
264
|
-
}
|
|
265
|
-
applyFallbackStyleElement(e, t) {
|
|
266
|
-
let n = `data-marquee-content-style-${e}`, r = this.shadow.querySelector(`style[${n}]`);
|
|
267
|
-
if (!t) {
|
|
268
|
-
r?.remove();
|
|
269
|
-
return;
|
|
270
|
-
}
|
|
271
|
-
r || (r = document.createElement("style"), r.setAttribute(n, ""), this.shadow.prepend(r)), r.textContent !== t && (r.textContent = t);
|
|
272
|
-
}
|
|
273
|
-
removeFallbackStyleElements() {
|
|
274
|
-
this.shadow.querySelectorAll("style[data-marquee-content-style-base], style[data-marquee-content-style-overrides]").forEach((e) => {
|
|
275
|
-
e.remove();
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
|
-
teardown() {
|
|
279
|
-
this.mounted && (this.mounted = !1, this.stopAnimationLoop(), this.cancelScheduledRebuild(), this.resizeObserver?.disconnect(), this.mutationObserver?.disconnect(), this.intersectionObserver?.disconnect(), this.removeReducedMotionWatcher(), this.abortController?.abort(), this.resizeObserver = null, this.mutationObserver = null, this.intersectionObserver = null, this.abortController = null, this.track.replaceChildren(), this.removeAttribute("data-marquee-content-ready"), this.removeAttribute("data-marquee-content-pause-on-hover"), l.forEach((e) => {
|
|
280
|
-
this.style.removeProperty(e);
|
|
281
|
-
}), this.distance = 0, this.offset = 0, this.lastFrameTime = null);
|
|
282
|
-
}
|
|
283
|
-
handleSlotChange = () => {
|
|
284
|
-
this.watchSourceNodes(), this.scheduleRebuild();
|
|
285
|
-
};
|
|
286
|
-
handlePointerEnter = () => {
|
|
287
|
-
this.isHovered = !0, this.updateAnimationLoopState();
|
|
288
|
-
};
|
|
289
|
-
handlePointerLeave = () => {
|
|
290
|
-
this.isHovered = !1, this.updateAnimationLoopState();
|
|
291
|
-
};
|
|
292
|
-
handleIntersectionChange = (e) => {
|
|
293
|
-
let t = e[0];
|
|
294
|
-
t && (this.isInViewport = t.isIntersecting, this.updateAnimationLoopState());
|
|
295
|
-
};
|
|
296
|
-
handleReducedMotionChange = (e) => {
|
|
297
|
-
this.reducedMotion = e.matches, this.reducedMotion ? this.track.style.transform = "none" : this.applyTransform(), this.updateAnimationLoopState();
|
|
298
|
-
};
|
|
299
|
-
handleScroll = () => {
|
|
300
|
-
if (this.options.direction !== "scroll") return;
|
|
301
|
-
let e = performance.now(), t = this.getScrollY(), n = t - this.lastScrollY, r = Math.max((e - this.lastScrollTime) / 1e3, .016);
|
|
302
|
-
if (this.lastScrollY = t, this.lastScrollTime = e, Math.abs(n) < .5) return;
|
|
303
|
-
let i = n / r;
|
|
304
|
-
this.scrollDirectionFactor = i >= 0 ? 1 : -1;
|
|
305
|
-
let a = this.clamp(i / this.options.scrollVelocityFactor, -this.options.scrollMaxExtraSpeed, this.options.scrollMaxExtraSpeed);
|
|
306
|
-
Math.abs(a) >= this.options.scrollMinExtraSpeed ? (this.scrollBoostStartValue = a, this.scrollBoostStartedAt = e) : this.scrollBoostStartValue = 0, this.updateAnimationLoopState(), this.options.debug && console.info("[MarqueeContent] Scroll update", {
|
|
307
|
-
velocity: i,
|
|
308
|
-
directionFactor: this.scrollDirectionFactor,
|
|
309
|
-
extraSpeed: a
|
|
310
|
-
});
|
|
311
|
-
};
|
|
312
|
-
scheduleRebuild = () => {
|
|
313
|
-
this.mounted && (this.cancelScheduledRebuild(), this.rebuildFrameId = requestAnimationFrame(() => {
|
|
314
|
-
this.rebuildFrameId = null, this.rebuild();
|
|
315
|
-
}));
|
|
316
|
-
};
|
|
317
|
-
cancelScheduledRebuild() {
|
|
318
|
-
this.rebuildFrameId !== null && (cancelAnimationFrame(this.rebuildFrameId), this.rebuildFrameId = null);
|
|
319
|
-
}
|
|
320
|
-
rebuild() {
|
|
321
|
-
if (!this.mounted) return;
|
|
322
|
-
this.options = this.resolveOptions(), this.removeAttribute("data-marquee-content-ready"), this.options.pauseOnHover ? this.setAttribute("data-marquee-content-pause-on-hover", "true") : this.removeAttribute("data-marquee-content-pause-on-hover");
|
|
323
|
-
let e = this.clientWidth;
|
|
324
|
-
if (e <= 0) {
|
|
325
|
-
this.distance = 0, this.track.replaceChildren(), this.updateAnimationLoopState();
|
|
326
|
-
return;
|
|
327
|
-
}
|
|
328
|
-
if (this.sourceNodes = this.getSourceNodes(), this.sourceNodes.length === 0) {
|
|
329
|
-
this.distance = 0, this.track.replaceChildren(), this.updateAnimationLoopState();
|
|
330
|
-
return;
|
|
331
|
-
}
|
|
332
|
-
let t = this.createGroup(), n = e * this.options.minGroupWidthRatio;
|
|
333
|
-
this.track.replaceChildren(t), this.expandGroupToWidth(t, n);
|
|
334
|
-
let r = t.cloneNode(!0);
|
|
335
|
-
if (r.setAttribute("aria-hidden", "true"), this.track.appendChild(r), this.distance = t.scrollWidth, this.offset = this.normalizeOffset(this.offset), this.distance <= 0) {
|
|
336
|
-
this.track.replaceChildren(), this.updateAnimationLoopState();
|
|
337
|
-
return;
|
|
338
|
-
}
|
|
339
|
-
let i = this.getCurrentSpeed(e), a = this.distance / i;
|
|
340
|
-
this.style.setProperty("--marquee-content-distance", `${this.distance}px`), this.style.setProperty("--marquee-content-duration", `${a}s`), this.reducedMotion ? this.track.style.transform = "none" : this.applyTransform(), this.setAttribute("data-marquee-content-ready", "true"), this.updateAnimationLoopState(), this.options.debug && console.info("[MarqueeContent] Rebuilt", {
|
|
341
|
-
containerWidth: e,
|
|
342
|
-
distance: this.distance,
|
|
343
|
-
baseSpeed: this.options.speed,
|
|
344
|
-
currentSpeed: i,
|
|
345
|
-
duration: a,
|
|
346
|
-
direction: this.options.direction,
|
|
347
|
-
responsiveSpeed: this.options.responsiveSpeed
|
|
348
|
-
});
|
|
349
|
-
}
|
|
350
|
-
createGroup() {
|
|
351
|
-
let e = document.createElement("div");
|
|
352
|
-
return e.className = "marquee-content__group", e.setAttribute("part", "group"), this.appendSourceNodes(e), e;
|
|
353
|
-
}
|
|
354
|
-
expandGroupToWidth(e, t) {
|
|
355
|
-
let n = 0;
|
|
356
|
-
for (; e.scrollWidth < t && n < 100;) {
|
|
357
|
-
let t = e.scrollWidth;
|
|
358
|
-
if (this.appendSourceNodes(e), e.scrollWidth <= t) break;
|
|
359
|
-
n += 1;
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
appendSourceNodes(e) {
|
|
363
|
-
let t = document.createDocumentFragment();
|
|
364
|
-
this.sourceNodes.forEach((e) => {
|
|
365
|
-
t.append(this.cloneSourceNode(e));
|
|
366
|
-
}), e.appendChild(t);
|
|
367
|
-
}
|
|
368
|
-
cloneSourceNode(e) {
|
|
369
|
-
let t = e.cloneNode(!0);
|
|
370
|
-
if (t instanceof Element) {
|
|
371
|
-
let e = t.getAttribute("part");
|
|
372
|
-
t.setAttribute("part", e ? `${e} item` : "item");
|
|
373
|
-
}
|
|
374
|
-
return t;
|
|
375
|
-
}
|
|
376
|
-
getSourceNodes() {
|
|
377
|
-
return this.sourceSlot.assignedNodes({ flatten: !0 }).filter((e) => e.nodeType === Node.TEXT_NODE ? !!e.textContent?.trim() : e.nodeType === Node.ELEMENT_NODE);
|
|
378
|
-
}
|
|
379
|
-
watchSourceNodes() {
|
|
380
|
-
this.mutationObserver?.disconnect(), "MutationObserver" in window && (this.mutationObserver = new MutationObserver(this.scheduleRebuild), this.getSourceNodes().forEach((e) => {
|
|
381
|
-
if (e.nodeType === Node.ELEMENT_NODE) {
|
|
382
|
-
this.mutationObserver?.observe(e, {
|
|
383
|
-
attributes: !0,
|
|
384
|
-
childList: !0,
|
|
385
|
-
characterData: !0,
|
|
386
|
-
subtree: !0
|
|
387
|
-
});
|
|
388
|
-
return;
|
|
389
|
-
}
|
|
390
|
-
e.nodeType === Node.TEXT_NODE && this.mutationObserver?.observe(e, { characterData: !0 });
|
|
391
|
-
}));
|
|
392
|
-
}
|
|
393
|
-
setupReducedMotionWatcher() {
|
|
394
|
-
if (!("matchMedia" in window)) return;
|
|
395
|
-
if (this.reducedMotionMediaQuery = window.matchMedia("(prefers-reduced-motion: reduce)"), this.reducedMotion = this.reducedMotionMediaQuery.matches, typeof this.reducedMotionMediaQuery.addEventListener == "function") {
|
|
396
|
-
this.reducedMotionMediaQuery.addEventListener("change", this.handleReducedMotionChange, { signal: this.abortController?.signal });
|
|
397
|
-
return;
|
|
398
|
-
}
|
|
399
|
-
let e = this.reducedMotionMediaQuery;
|
|
400
|
-
typeof e.addListener == "function" && e.addListener(this.handleReducedMotionChange);
|
|
401
|
-
}
|
|
402
|
-
removeReducedMotionWatcher() {
|
|
403
|
-
if (this.reducedMotionMediaQuery) {
|
|
404
|
-
if (typeof this.reducedMotionMediaQuery.removeEventListener == "function") this.reducedMotionMediaQuery.removeEventListener("change", this.handleReducedMotionChange);
|
|
405
|
-
else {
|
|
406
|
-
let e = this.reducedMotionMediaQuery;
|
|
407
|
-
typeof e.removeListener == "function" && e.removeListener(this.handleReducedMotionChange);
|
|
408
|
-
}
|
|
409
|
-
this.reducedMotionMediaQuery = null;
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
updateAnimationLoopState() {
|
|
413
|
-
if (!this.mounted || this.distance <= 0 || this.reducedMotion || !this.canAnimate()) {
|
|
414
|
-
this.stopAnimationLoop();
|
|
415
|
-
return;
|
|
416
|
-
}
|
|
417
|
-
this.startAnimationLoop();
|
|
418
|
-
}
|
|
419
|
-
canAnimate() {
|
|
420
|
-
return !(this.options.pauseOnHover && this.isHovered || this.options.pauseWhenOutOfView && !this.isInViewport);
|
|
421
|
-
}
|
|
422
|
-
startAnimationLoop() {
|
|
423
|
-
this.animationFrameId === null && (this.lastFrameTime = performance.now(), this.animationFrameId = requestAnimationFrame(this.tick));
|
|
424
|
-
}
|
|
425
|
-
stopAnimationLoop() {
|
|
426
|
-
if (this.animationFrameId === null) {
|
|
427
|
-
this.lastFrameTime = null;
|
|
428
|
-
return;
|
|
429
|
-
}
|
|
430
|
-
cancelAnimationFrame(this.animationFrameId), this.animationFrameId = null, this.lastFrameTime = null;
|
|
431
|
-
}
|
|
432
|
-
tick = (e) => {
|
|
433
|
-
if (this.animationFrameId = null, !this.mounted || this.distance <= 0 || this.reducedMotion || !this.canAnimate()) {
|
|
434
|
-
this.stopAnimationLoop();
|
|
435
|
-
return;
|
|
436
|
-
}
|
|
437
|
-
let t = this.lastFrameTime ?? e, n = Math.min((e - t) / 1e3, .064);
|
|
438
|
-
this.lastFrameTime = e;
|
|
439
|
-
let r = this.getCurrentSpeed(this.clientWidth), i = this.getCurrentTimeScale(e);
|
|
440
|
-
this.offset = this.normalizeOffset(this.offset + r * i * n), this.applyTransform(), this.animationFrameId = requestAnimationFrame(this.tick);
|
|
441
|
-
};
|
|
442
|
-
getCurrentTimeScale(e) {
|
|
443
|
-
if (this.options.direction === "rtl") return 1;
|
|
444
|
-
if (this.options.direction === "ltr") return -1;
|
|
445
|
-
let t = this.getScrollBoost(e);
|
|
446
|
-
return this.clamp(this.scrollDirectionFactor + t, -1 - this.options.scrollMaxExtraSpeed, 1 + this.options.scrollMaxExtraSpeed);
|
|
447
|
-
}
|
|
448
|
-
getScrollBoost(e) {
|
|
449
|
-
if (this.scrollBoostStartValue === 0) return 0;
|
|
450
|
-
let t = this.options.scrollInertiaDuration;
|
|
451
|
-
if (t <= 0) return this.scrollBoostStartValue = 0, 0;
|
|
452
|
-
let n = this.clamp((e - this.scrollBoostStartedAt) / t, 0, 1);
|
|
453
|
-
if (n >= 1) return this.scrollBoostStartValue = 0, 0;
|
|
454
|
-
let r = this.easeOutCubic(n);
|
|
455
|
-
return this.scrollBoostStartValue * (1 - r);
|
|
456
|
-
}
|
|
457
|
-
applyTransform() {
|
|
458
|
-
this.track.style.transform = `translate3d(${-this.offset}px, 0, 0)`;
|
|
459
|
-
}
|
|
460
|
-
normalizeOffset(e) {
|
|
461
|
-
if (this.distance <= 0) return 0;
|
|
462
|
-
let t = e % this.distance;
|
|
463
|
-
return t < 0 ? t + this.distance : t;
|
|
464
|
-
}
|
|
465
|
-
getCurrentSpeed(e) {
|
|
466
|
-
if (!this.options.responsiveSpeed) return this.options.speed;
|
|
467
|
-
let t = this.clamp(e / this.options.referenceWidth, this.options.minSpeedFactor, this.options.maxSpeedFactor);
|
|
468
|
-
return this.options.speed * t;
|
|
469
|
-
}
|
|
470
|
-
resolveOptions() {
|
|
471
|
-
let e = this.num("min-speed-factor", o.minSpeedFactor, .1, 2), t = this.num("max-speed-factor", o.maxSpeedFactor, .1, 3);
|
|
472
|
-
return {
|
|
473
|
-
speed: this.num("speed", o.speed, 10, 500),
|
|
474
|
-
direction: this.parseDirection(this.getAttribute("direction"), o.direction),
|
|
475
|
-
minGroupWidthRatio: this.num("min-group-width-ratio", o.minGroupWidthRatio, 1, 5),
|
|
476
|
-
responsiveSpeed: this.parseBoolean(this.getAttribute("responsive-speed"), o.responsiveSpeed),
|
|
477
|
-
referenceWidth: this.num("reference-width", o.referenceWidth, 320, 1e4),
|
|
478
|
-
minSpeedFactor: Math.min(e, t),
|
|
479
|
-
maxSpeedFactor: Math.max(e, t),
|
|
480
|
-
pauseOnHover: this.parseBoolean(this.getAttribute("pause-on-hover"), o.pauseOnHover),
|
|
481
|
-
pauseWhenOutOfView: this.parseBoolean(this.getAttribute("pause-when-out-of-view"), o.pauseWhenOutOfView),
|
|
482
|
-
recalculateAfterFontsReady: this.parseBoolean(this.getAttribute("recalculate-after-fonts-ready"), o.recalculateAfterFontsReady),
|
|
483
|
-
scrollVelocityFactor: this.num("scroll-velocity-factor", o.scrollVelocityFactor, 50, 5e3),
|
|
484
|
-
scrollMinExtraSpeed: this.num("scroll-min-extra-speed", o.scrollMinExtraSpeed, 0, 5),
|
|
485
|
-
scrollMaxExtraSpeed: this.num("scroll-max-extra-speed", o.scrollMaxExtraSpeed, 0, 10),
|
|
486
|
-
scrollInertiaDuration: this.num("scroll-inertia-duration", o.scrollInertiaDuration, 0, 5e3),
|
|
487
|
-
debug: this.parseBoolean(this.getAttribute("debug"), o.debug)
|
|
488
|
-
};
|
|
489
|
-
}
|
|
490
|
-
num(e, t, n, r) {
|
|
491
|
-
return this.clamp(this.parseNumber(this.getAttribute(e), t), n, r);
|
|
492
|
-
}
|
|
493
|
-
parseNumber(e, t) {
|
|
494
|
-
if (e === null || e.trim() === "") return t;
|
|
495
|
-
let n = Number(e);
|
|
496
|
-
return Number.isFinite(n) ? n : t;
|
|
497
|
-
}
|
|
498
|
-
parseBoolean(e, t) {
|
|
499
|
-
if (e === null) return t;
|
|
500
|
-
let n = e.trim().toLowerCase();
|
|
501
|
-
return n === "" || [
|
|
502
|
-
"true",
|
|
503
|
-
"1",
|
|
504
|
-
"yes",
|
|
505
|
-
"on"
|
|
506
|
-
].includes(n) ? !0 : [
|
|
507
|
-
"false",
|
|
508
|
-
"0",
|
|
509
|
-
"no",
|
|
510
|
-
"off"
|
|
511
|
-
].includes(n) ? !1 : t;
|
|
512
|
-
}
|
|
513
|
-
parseDirection(e, t) {
|
|
514
|
-
return e === "rtl" || e === "ltr" || e === "scroll" ? e : t;
|
|
515
|
-
}
|
|
516
|
-
setNullableAttribute(e, t) {
|
|
517
|
-
if (t == null) {
|
|
518
|
-
this.removeAttribute(e);
|
|
519
|
-
return;
|
|
520
|
-
}
|
|
521
|
-
this.setAttribute(e, String(t));
|
|
522
|
-
}
|
|
523
|
-
getScrollY() {
|
|
524
|
-
return window.scrollY || window.pageYOffset || 0;
|
|
525
|
-
}
|
|
526
|
-
clamp(e, t, n) {
|
|
527
|
-
return Math.min(Math.max(e, t), n);
|
|
528
|
-
}
|
|
529
|
-
easeOutCubic(e) {
|
|
530
|
-
return 1 - (1 - e) ** 3;
|
|
531
|
-
}
|
|
532
|
-
rebuildAfterFontsReady() {
|
|
533
|
-
let e = document.fonts;
|
|
534
|
-
e?.ready && e.ready.then(this.scheduleRebuild).catch(() => void 0);
|
|
535
|
-
}
|
|
536
|
-
};
|
|
537
|
-
function f(e = a, t = {}) {
|
|
538
|
-
typeof window > "u" || !window.customElements || (d.configureStyles(t), window.customElements.get(e) || window.customElements.define(e, d));
|
|
539
|
-
}
|
|
540
|
-
function p(e = {}) {
|
|
541
|
-
let { tagName: t = a, ...n } = e;
|
|
542
|
-
f(t, n);
|
|
543
|
-
}
|
|
544
|
-
//#endregion
|
|
545
|
-
//#region src/index.ts
|
|
546
|
-
f();
|
|
547
|
-
//#endregion
|
|
548
|
-
export { a as MARQUEE_CONTENT_TAG, d as MarqueeContent, t as createConstructableStyleSheet, n as cssStyleSheetToText, f as defineMarqueeContent, p as initMarqueeContents, e as marqueeContentCssText, r as normalizeStyleInput };
|
package/dist/index.umd.js
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.MarqueeContent={}))})(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var t=`:host {
|
|
2
|
-
--marquee-content-gap: 2rem;
|
|
3
|
-
--marquee-content-distance: 0px;
|
|
4
|
-
--marquee-content-duration: 20s;
|
|
5
|
-
|
|
6
|
-
display: block;
|
|
7
|
-
overflow: hidden;
|
|
8
|
-
width: 100%;
|
|
9
|
-
color: inherit;
|
|
10
|
-
font: inherit;
|
|
11
|
-
|
|
12
|
-
/*
|
|
13
|
-
По умолчанию компонент не ловит pointer events.
|
|
14
|
-
Это удобно для декоративной бегущей строки.
|
|
15
|
-
Если включить pause-on-hover, pointer-events включатся автоматически.
|
|
16
|
-
*/
|
|
17
|
-
pointer-events: none;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
:host([pause-on-hover]:not([pause-on-hover="false"])) {
|
|
21
|
-
pointer-events: auto;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
:host([hidden]) {
|
|
25
|
-
display: none;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
.marquee-content__source {
|
|
29
|
-
display: none;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
.marquee-content__viewport {
|
|
33
|
-
overflow: hidden;
|
|
34
|
-
width: 100%;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
.marquee-content__track {
|
|
38
|
-
display: flex;
|
|
39
|
-
width: max-content;
|
|
40
|
-
transform: translate3d(0, 0, 0);
|
|
41
|
-
will-change: transform;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
.marquee-content__group {
|
|
45
|
-
display: flex;
|
|
46
|
-
flex: 0 0 auto;
|
|
47
|
-
align-items: center;
|
|
48
|
-
gap: var(--marquee-content-gap);
|
|
49
|
-
padding-inline-end: var(--marquee-content-gap);
|
|
50
|
-
white-space: nowrap;
|
|
51
|
-
color: inherit;
|
|
52
|
-
font: inherit;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
.marquee-content__group > * {
|
|
56
|
-
flex: 0 0 auto;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
@media (prefers-reduced-motion: reduce) {
|
|
60
|
-
.marquee-content__track {
|
|
61
|
-
transform: none !important;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
`;function n(e){if(typeof CSSStyleSheet>`u`||typeof CSSStyleSheet.prototype.replaceSync!=`function`)return null;try{let t=new CSSStyleSheet;return t.replaceSync(e),t}catch{return null}}function r(e){try{return Array.from(e.cssRules).map(e=>e.cssText).join(`
|
|
65
|
-
`)}catch{return``}}function i(e){return e?typeof e==`string`?{sheet:n(e),cssText:e}:{sheet:e,cssText:r(e)}:{sheet:null,cssText:``}}function a(e){return typeof CSSStyleSheet<`u`&&`adoptedStyleSheets`in e&&typeof CSSStyleSheet.prototype.replaceSync==`function`}var o=`marquee-content`,s={speed:90,direction:`rtl`,minGroupWidthRatio:1.05,responsiveSpeed:!0,referenceWidth:1280,minSpeedFactor:.55,maxSpeedFactor:1,pauseOnHover:!1,pauseWhenOutOfView:!0,recalculateAfterFontsReady:!0,scrollVelocityFactor:400,scrollMinExtraSpeed:.15,scrollMaxExtraSpeed:3,scrollInertiaDuration:1600,debug:!1},c=[`speed`,`direction`,`min-group-width-ratio`,`responsive-speed`,`reference-width`,`min-speed-factor`,`max-speed-factor`,`pause-on-hover`,`pause-when-out-of-view`,`recalculate-after-fonts-ready`,`scroll-velocity-factor`,`scroll-min-extra-speed`,`scroll-max-extra-speed`,`scroll-inertia-duration`,`debug`],l={speed:`speed`,direction:`direction`,minGroupWidthRatio:`min-group-width-ratio`,responsiveSpeed:`responsive-speed`,referenceWidth:`reference-width`,minSpeedFactor:`min-speed-factor`,maxSpeedFactor:`max-speed-factor`,pauseOnHover:`pause-on-hover`,pauseWhenOutOfView:`pause-when-out-of-view`,recalculateAfterFontsReady:`recalculate-after-fonts-ready`,scrollVelocityFactor:`scroll-velocity-factor`,scrollMinExtraSpeed:`scroll-min-extra-speed`,scrollMaxExtraSpeed:`scroll-max-extra-speed`,scrollInertiaDuration:`scroll-inertia-duration`,debug:`debug`},u=[`--marquee-content-distance`,`--marquee-content-duration`],d=`
|
|
66
|
-
<slot class="marquee-content__source"></slot>
|
|
67
|
-
|
|
68
|
-
<div class="marquee-content__viewport" part="viewport">
|
|
69
|
-
<div class="marquee-content__track" part="track"></div>
|
|
70
|
-
</div>
|
|
71
|
-
`,f=class e extends HTMLElement{static get observedAttributes(){return[...c]}static instances=new Set;static stylesheetEntry=i(t);static styleOverridesEntry=i(null);static configureStyles(n={}){`stylesheet`in n&&(e.stylesheetEntry=i(n.stylesheet??t)),`styleOverrides`in n&&(e.styleOverridesEntry=i(n.styleOverrides)),e.instances.forEach(e=>{e.applyAllStyles()})}static setStylesheet(t){e.configureStyles({stylesheet:t})}static setStyleOverrides(t){e.configureStyles({styleOverrides:t})}shadow;sourceSlot;track;options={...s};sourceNodes=[];resizeObserver=null;mutationObserver=null;intersectionObserver=null;abortController=null;reducedMotionMediaQuery=null;animationFrameId=null;rebuildFrameId=null;lastFrameTime=null;mounted=!1;isHovered=!1;isInViewport=!0;reducedMotion=!1;distance=0;offset=0;scrollDirectionFactor=1;scrollBoostStartValue=0;scrollBoostStartedAt=0;lastScrollY=0;lastScrollTime=0;constructor(){super(),this.shadow=this.attachShadow({mode:`open`}),this.applyAllStyles();let e=document.createElement(`template`);e.innerHTML=d,this.shadow.appendChild(e.content.cloneNode(!0));let t=this.shadow.querySelector(`slot.marquee-content__source`),n=this.shadow.querySelector(`.marquee-content__track`);if(!t)throw Error(`[MarqueeContent] Source slot was not found.`);if(!n)throw Error(`[MarqueeContent] Track element was not found.`);this.sourceSlot=t,this.track=n}connectedCallback(){this.mounted||(e.instances.add(this),this.mounted=!0,this.abortController=new AbortController,this.options=this.resolveOptions(),this.lastScrollY=this.getScrollY(),this.lastScrollTime=performance.now(),this.sourceSlot.addEventListener(`slotchange`,this.handleSlotChange,{signal:this.abortController.signal}),this.shadow.addEventListener(`load`,this.scheduleRebuild,{capture:!0,signal:this.abortController.signal}),this.addEventListener(`pointerenter`,this.handlePointerEnter,{signal:this.abortController.signal}),this.addEventListener(`pointerleave`,this.handlePointerLeave,{signal:this.abortController.signal}),window.addEventListener(`scroll`,this.handleScroll,{passive:!0,signal:this.abortController.signal}),window.addEventListener(`orientationchange`,this.scheduleRebuild,{signal:this.abortController.signal}),typeof ResizeObserver<`u`?(this.resizeObserver=new ResizeObserver(this.scheduleRebuild),this.resizeObserver.observe(this)):window.addEventListener(`resize`,this.scheduleRebuild,{signal:this.abortController.signal}),`IntersectionObserver`in window&&(this.intersectionObserver=new IntersectionObserver(this.handleIntersectionChange),this.intersectionObserver.observe(this)),this.setupReducedMotionWatcher(),this.watchSourceNodes(),this.rebuild(),this.options.recalculateAfterFontsReady&&this.rebuildAfterFontsReady())}disconnectedCallback(){this.teardown(),e.instances.delete(this)}attributeChangedCallback(e,t,n){t!==n&&(this.options=this.resolveOptions(),this.mounted&&this.scheduleRebuild())}get speed(){return this.options.speed}set speed(e){this.setNullableAttribute(`speed`,e)}get direction(){return this.options.direction}set direction(e){this.setNullableAttribute(`direction`,e)}get minGroupWidthRatio(){return this.options.minGroupWidthRatio}set minGroupWidthRatio(e){this.setNullableAttribute(`min-group-width-ratio`,e)}get responsiveSpeed(){return this.options.responsiveSpeed}set responsiveSpeed(e){this.setNullableAttribute(`responsive-speed`,e)}get referenceWidth(){return this.options.referenceWidth}set referenceWidth(e){this.setNullableAttribute(`reference-width`,e)}get minSpeedFactor(){return this.options.minSpeedFactor}set minSpeedFactor(e){this.setNullableAttribute(`min-speed-factor`,e)}get maxSpeedFactor(){return this.options.maxSpeedFactor}set maxSpeedFactor(e){this.setNullableAttribute(`max-speed-factor`,e)}get pauseOnHover(){return this.options.pauseOnHover}set pauseOnHover(e){this.setNullableAttribute(`pause-on-hover`,e)}get pauseWhenOutOfView(){return this.options.pauseWhenOutOfView}set pauseWhenOutOfView(e){this.setNullableAttribute(`pause-when-out-of-view`,e)}get recalculateAfterFontsReady(){return this.options.recalculateAfterFontsReady}set recalculateAfterFontsReady(e){this.setNullableAttribute(`recalculate-after-fonts-ready`,e)}get scrollVelocityFactor(){return this.options.scrollVelocityFactor}set scrollVelocityFactor(e){this.setNullableAttribute(`scroll-velocity-factor`,e)}get scrollMinExtraSpeed(){return this.options.scrollMinExtraSpeed}set scrollMinExtraSpeed(e){this.setNullableAttribute(`scroll-min-extra-speed`,e)}get scrollMaxExtraSpeed(){return this.options.scrollMaxExtraSpeed}set scrollMaxExtraSpeed(e){this.setNullableAttribute(`scroll-max-extra-speed`,e)}get scrollInertiaDuration(){return this.options.scrollInertiaDuration}set scrollInertiaDuration(e){this.setNullableAttribute(`scroll-inertia-duration`,e)}get debug(){return this.options.debug}set debug(e){this.setNullableAttribute(`debug`,e)}update(e={}){Object.entries(e).forEach(([e,t])=>{let n=l[e];n&&this.setNullableAttribute(n,t)}),this.options=this.resolveOptions(),this.scheduleRebuild()}refresh(){this.scheduleRebuild()}adoptStyles(t){e.setStylesheet(t)}adoptStyleOverrides(t){e.setStyleOverrides(t)}applyAllStyles(){let t=e.stylesheetEntry,n=e.styleOverridesEntry;if(a(this.shadow)){let e=[];t.sheet&&e.push(t.sheet),n.sheet&&e.push(n.sheet),this.shadow.adoptedStyleSheets=e,this.removeFallbackStyleElements();return}this.applyFallbackStyleElement(`base`,t.cssText),this.applyFallbackStyleElement(`overrides`,n.cssText)}applyFallbackStyleElement(e,t){let n=`data-marquee-content-style-${e}`,r=this.shadow.querySelector(`style[${n}]`);if(!t){r?.remove();return}r||(r=document.createElement(`style`),r.setAttribute(n,``),this.shadow.prepend(r)),r.textContent!==t&&(r.textContent=t)}removeFallbackStyleElements(){this.shadow.querySelectorAll(`style[data-marquee-content-style-base], style[data-marquee-content-style-overrides]`).forEach(e=>{e.remove()})}teardown(){this.mounted&&(this.mounted=!1,this.stopAnimationLoop(),this.cancelScheduledRebuild(),this.resizeObserver?.disconnect(),this.mutationObserver?.disconnect(),this.intersectionObserver?.disconnect(),this.removeReducedMotionWatcher(),this.abortController?.abort(),this.resizeObserver=null,this.mutationObserver=null,this.intersectionObserver=null,this.abortController=null,this.track.replaceChildren(),this.removeAttribute(`data-marquee-content-ready`),this.removeAttribute(`data-marquee-content-pause-on-hover`),u.forEach(e=>{this.style.removeProperty(e)}),this.distance=0,this.offset=0,this.lastFrameTime=null)}handleSlotChange=()=>{this.watchSourceNodes(),this.scheduleRebuild()};handlePointerEnter=()=>{this.isHovered=!0,this.updateAnimationLoopState()};handlePointerLeave=()=>{this.isHovered=!1,this.updateAnimationLoopState()};handleIntersectionChange=e=>{let t=e[0];t&&(this.isInViewport=t.isIntersecting,this.updateAnimationLoopState())};handleReducedMotionChange=e=>{this.reducedMotion=e.matches,this.reducedMotion?this.track.style.transform=`none`:this.applyTransform(),this.updateAnimationLoopState()};handleScroll=()=>{if(this.options.direction!==`scroll`)return;let e=performance.now(),t=this.getScrollY(),n=t-this.lastScrollY,r=Math.max((e-this.lastScrollTime)/1e3,.016);if(this.lastScrollY=t,this.lastScrollTime=e,Math.abs(n)<.5)return;let i=n/r;this.scrollDirectionFactor=i>=0?1:-1;let a=this.clamp(i/this.options.scrollVelocityFactor,-this.options.scrollMaxExtraSpeed,this.options.scrollMaxExtraSpeed);Math.abs(a)>=this.options.scrollMinExtraSpeed?(this.scrollBoostStartValue=a,this.scrollBoostStartedAt=e):this.scrollBoostStartValue=0,this.updateAnimationLoopState(),this.options.debug&&console.info(`[MarqueeContent] Scroll update`,{velocity:i,directionFactor:this.scrollDirectionFactor,extraSpeed:a})};scheduleRebuild=()=>{this.mounted&&(this.cancelScheduledRebuild(),this.rebuildFrameId=requestAnimationFrame(()=>{this.rebuildFrameId=null,this.rebuild()}))};cancelScheduledRebuild(){this.rebuildFrameId!==null&&(cancelAnimationFrame(this.rebuildFrameId),this.rebuildFrameId=null)}rebuild(){if(!this.mounted)return;this.options=this.resolveOptions(),this.removeAttribute(`data-marquee-content-ready`),this.options.pauseOnHover?this.setAttribute(`data-marquee-content-pause-on-hover`,`true`):this.removeAttribute(`data-marquee-content-pause-on-hover`);let e=this.clientWidth;if(e<=0){this.distance=0,this.track.replaceChildren(),this.updateAnimationLoopState();return}if(this.sourceNodes=this.getSourceNodes(),this.sourceNodes.length===0){this.distance=0,this.track.replaceChildren(),this.updateAnimationLoopState();return}let t=this.createGroup(),n=e*this.options.minGroupWidthRatio;this.track.replaceChildren(t),this.expandGroupToWidth(t,n);let r=t.cloneNode(!0);if(r.setAttribute(`aria-hidden`,`true`),this.track.appendChild(r),this.distance=t.scrollWidth,this.offset=this.normalizeOffset(this.offset),this.distance<=0){this.track.replaceChildren(),this.updateAnimationLoopState();return}let i=this.getCurrentSpeed(e),a=this.distance/i;this.style.setProperty(`--marquee-content-distance`,`${this.distance}px`),this.style.setProperty(`--marquee-content-duration`,`${a}s`),this.reducedMotion?this.track.style.transform=`none`:this.applyTransform(),this.setAttribute(`data-marquee-content-ready`,`true`),this.updateAnimationLoopState(),this.options.debug&&console.info(`[MarqueeContent] Rebuilt`,{containerWidth:e,distance:this.distance,baseSpeed:this.options.speed,currentSpeed:i,duration:a,direction:this.options.direction,responsiveSpeed:this.options.responsiveSpeed})}createGroup(){let e=document.createElement(`div`);return e.className=`marquee-content__group`,e.setAttribute(`part`,`group`),this.appendSourceNodes(e),e}expandGroupToWidth(e,t){let n=0;for(;e.scrollWidth<t&&n<100;){let t=e.scrollWidth;if(this.appendSourceNodes(e),e.scrollWidth<=t)break;n+=1}}appendSourceNodes(e){let t=document.createDocumentFragment();this.sourceNodes.forEach(e=>{t.append(this.cloneSourceNode(e))}),e.appendChild(t)}cloneSourceNode(e){let t=e.cloneNode(!0);if(t instanceof Element){let e=t.getAttribute(`part`);t.setAttribute(`part`,e?`${e} item`:`item`)}return t}getSourceNodes(){return this.sourceSlot.assignedNodes({flatten:!0}).filter(e=>e.nodeType===Node.TEXT_NODE?!!e.textContent?.trim():e.nodeType===Node.ELEMENT_NODE)}watchSourceNodes(){this.mutationObserver?.disconnect(),`MutationObserver`in window&&(this.mutationObserver=new MutationObserver(this.scheduleRebuild),this.getSourceNodes().forEach(e=>{if(e.nodeType===Node.ELEMENT_NODE){this.mutationObserver?.observe(e,{attributes:!0,childList:!0,characterData:!0,subtree:!0});return}e.nodeType===Node.TEXT_NODE&&this.mutationObserver?.observe(e,{characterData:!0})}))}setupReducedMotionWatcher(){if(!(`matchMedia`in window))return;if(this.reducedMotionMediaQuery=window.matchMedia(`(prefers-reduced-motion: reduce)`),this.reducedMotion=this.reducedMotionMediaQuery.matches,typeof this.reducedMotionMediaQuery.addEventListener==`function`){this.reducedMotionMediaQuery.addEventListener(`change`,this.handleReducedMotionChange,{signal:this.abortController?.signal});return}let e=this.reducedMotionMediaQuery;typeof e.addListener==`function`&&e.addListener(this.handleReducedMotionChange)}removeReducedMotionWatcher(){if(this.reducedMotionMediaQuery){if(typeof this.reducedMotionMediaQuery.removeEventListener==`function`)this.reducedMotionMediaQuery.removeEventListener(`change`,this.handleReducedMotionChange);else{let e=this.reducedMotionMediaQuery;typeof e.removeListener==`function`&&e.removeListener(this.handleReducedMotionChange)}this.reducedMotionMediaQuery=null}}updateAnimationLoopState(){if(!this.mounted||this.distance<=0||this.reducedMotion||!this.canAnimate()){this.stopAnimationLoop();return}this.startAnimationLoop()}canAnimate(){return!(this.options.pauseOnHover&&this.isHovered||this.options.pauseWhenOutOfView&&!this.isInViewport)}startAnimationLoop(){this.animationFrameId===null&&(this.lastFrameTime=performance.now(),this.animationFrameId=requestAnimationFrame(this.tick))}stopAnimationLoop(){if(this.animationFrameId===null){this.lastFrameTime=null;return}cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null,this.lastFrameTime=null}tick=e=>{if(this.animationFrameId=null,!this.mounted||this.distance<=0||this.reducedMotion||!this.canAnimate()){this.stopAnimationLoop();return}let t=this.lastFrameTime??e,n=Math.min((e-t)/1e3,.064);this.lastFrameTime=e;let r=this.getCurrentSpeed(this.clientWidth),i=this.getCurrentTimeScale(e);this.offset=this.normalizeOffset(this.offset+r*i*n),this.applyTransform(),this.animationFrameId=requestAnimationFrame(this.tick)};getCurrentTimeScale(e){if(this.options.direction===`rtl`)return 1;if(this.options.direction===`ltr`)return-1;let t=this.getScrollBoost(e);return this.clamp(this.scrollDirectionFactor+t,-1-this.options.scrollMaxExtraSpeed,1+this.options.scrollMaxExtraSpeed)}getScrollBoost(e){if(this.scrollBoostStartValue===0)return 0;let t=this.options.scrollInertiaDuration;if(t<=0)return this.scrollBoostStartValue=0,0;let n=this.clamp((e-this.scrollBoostStartedAt)/t,0,1);if(n>=1)return this.scrollBoostStartValue=0,0;let r=this.easeOutCubic(n);return this.scrollBoostStartValue*(1-r)}applyTransform(){this.track.style.transform=`translate3d(${-this.offset}px, 0, 0)`}normalizeOffset(e){if(this.distance<=0)return 0;let t=e%this.distance;return t<0?t+this.distance:t}getCurrentSpeed(e){if(!this.options.responsiveSpeed)return this.options.speed;let t=this.clamp(e/this.options.referenceWidth,this.options.minSpeedFactor,this.options.maxSpeedFactor);return this.options.speed*t}resolveOptions(){let e=this.num(`min-speed-factor`,s.minSpeedFactor,.1,2),t=this.num(`max-speed-factor`,s.maxSpeedFactor,.1,3);return{speed:this.num(`speed`,s.speed,10,500),direction:this.parseDirection(this.getAttribute(`direction`),s.direction),minGroupWidthRatio:this.num(`min-group-width-ratio`,s.minGroupWidthRatio,1,5),responsiveSpeed:this.parseBoolean(this.getAttribute(`responsive-speed`),s.responsiveSpeed),referenceWidth:this.num(`reference-width`,s.referenceWidth,320,1e4),minSpeedFactor:Math.min(e,t),maxSpeedFactor:Math.max(e,t),pauseOnHover:this.parseBoolean(this.getAttribute(`pause-on-hover`),s.pauseOnHover),pauseWhenOutOfView:this.parseBoolean(this.getAttribute(`pause-when-out-of-view`),s.pauseWhenOutOfView),recalculateAfterFontsReady:this.parseBoolean(this.getAttribute(`recalculate-after-fonts-ready`),s.recalculateAfterFontsReady),scrollVelocityFactor:this.num(`scroll-velocity-factor`,s.scrollVelocityFactor,50,5e3),scrollMinExtraSpeed:this.num(`scroll-min-extra-speed`,s.scrollMinExtraSpeed,0,5),scrollMaxExtraSpeed:this.num(`scroll-max-extra-speed`,s.scrollMaxExtraSpeed,0,10),scrollInertiaDuration:this.num(`scroll-inertia-duration`,s.scrollInertiaDuration,0,5e3),debug:this.parseBoolean(this.getAttribute(`debug`),s.debug)}}num(e,t,n,r){return this.clamp(this.parseNumber(this.getAttribute(e),t),n,r)}parseNumber(e,t){if(e===null||e.trim()===``)return t;let n=Number(e);return Number.isFinite(n)?n:t}parseBoolean(e,t){if(e===null)return t;let n=e.trim().toLowerCase();return n===``||[`true`,`1`,`yes`,`on`].includes(n)?!0:[`false`,`0`,`no`,`off`].includes(n)?!1:t}parseDirection(e,t){return e===`rtl`||e===`ltr`||e===`scroll`?e:t}setNullableAttribute(e,t){if(t==null){this.removeAttribute(e);return}this.setAttribute(e,String(t))}getScrollY(){return window.scrollY||window.pageYOffset||0}clamp(e,t,n){return Math.min(Math.max(e,t),n)}easeOutCubic(e){return 1-(1-e)**3}rebuildAfterFontsReady(){let e=document.fonts;e?.ready&&e.ready.then(this.scheduleRebuild).catch(()=>void 0)}};function p(e=o,t={}){typeof window>`u`||!window.customElements||(f.configureStyles(t),window.customElements.get(e)||window.customElements.define(e,f))}function m(e={}){let{tagName:t=o,...n}=e;p(t,n)}p(),e.MARQUEE_CONTENT_TAG=o,e.MarqueeContent=f,e.createConstructableStyleSheet=n,e.cssStyleSheetToText=r,e.defineMarqueeContent=p,e.initMarqueeContents=m,e.marqueeContentCssText=t,e.normalizeStyleInput=i});
|