marquee-content 4.6.1 → 5.0.0-dev.1
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/LICENSE +2 -2
- package/README.md +103 -43
- package/dist/index.cjs +71 -0
- package/dist/index.d.ts +4 -28
- package/dist/index.es.js +547 -98
- package/dist/index.umd.js +71 -1
- package/dist/marquee-content.css +1 -0
- package/dist/marquee-content.d.ts +163 -0
- package/dist/styles.d.ts +10 -0
- package/package.json +36 -55
- package/dist/index.cjs.js +0 -1
package/dist/index.es.js
CHANGED
|
@@ -1,99 +1,548 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
+
}
|
|
99
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 };
|