@vdragon/vui-animate 0.0.0-stage → 0.0.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/README.md +169 -2
- package/dist/vui-animate.css +583 -0
- package/dist/vui-animate.js +446 -0
- package/dist/vui-animate.min.css +3 -0
- package/dist/vui-animate.min.css.map +1 -0
- package/dist/vui-animate.min.js +3 -0
- package/dist/vui-animate.min.js.map +7 -0
- package/index.css +7 -0
- package/lib/cjs/index.d.ts +92 -0
- package/lib/cjs/index.js +431 -0
- package/lib/cjs/index.js.map +7 -0
- package/lib/esm/index.d.ts +92 -0
- package/lib/esm/index.d.ts.map +1 -0
- package/lib/esm/index.js +411 -0
- package/lib/esm/index.js.map +7 -0
- package/package.json +80 -5
- package/src/index.ts +560 -0
- package/vui-animate.css +603 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,560 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* vUI - Free & Open-Source Tailwind UI vComponents
|
|
3
|
+
* Plugin: @vdragon/vui-animate
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { VComponent, vComponents, vDom } from '@vdragon/vui/core';
|
|
7
|
+
import type { VPluginInterface } from '@vdragon/vui/core';
|
|
8
|
+
|
|
9
|
+
export type VAnimateTrigger = 'mount' | 'visible' | 'click' | 'hover' | 'manual';
|
|
10
|
+
|
|
11
|
+
export interface VAnimateConfig {
|
|
12
|
+
/** Animation name (e.g. 'fade-up' or 'v-anim-fade-up') */
|
|
13
|
+
animation?: string;
|
|
14
|
+
/** When to trigger animation automatically */
|
|
15
|
+
trigger?: VAnimateTrigger;
|
|
16
|
+
/** Optional selector or element to animate instead of the host element */
|
|
17
|
+
target?: string | HTMLElement | null;
|
|
18
|
+
/** CSS --v-anim-duration (e.g. '450ms', '0.6s', or number in ms) */
|
|
19
|
+
duration?: string | number;
|
|
20
|
+
/** CSS --v-anim-delay (e.g. '120ms', or number in ms) */
|
|
21
|
+
delay?: string | number;
|
|
22
|
+
/** Easing preset ('smooth' | 'spring' | 'snappy') or custom CSS timing function */
|
|
23
|
+
easing?: string;
|
|
24
|
+
/** CSS --v-anim-distance (e.g. '24px', or number in px) */
|
|
25
|
+
distance?: string | number;
|
|
26
|
+
/** CSS --v-anim-scale (e.g. 0.92) */
|
|
27
|
+
scale?: string | number;
|
|
28
|
+
/** CSS --v-anim-blur (e.g. '12px', or number in px) */
|
|
29
|
+
blur?: string | number;
|
|
30
|
+
/** CSS --v-anim-iterations (e.g. 1, 2, 'infinite') */
|
|
31
|
+
iterations?: string | number;
|
|
32
|
+
/** CSS --v-anim-accent color */
|
|
33
|
+
accent?: string;
|
|
34
|
+
/** For 'visible' trigger: only animate once when entering viewport */
|
|
35
|
+
once?: boolean;
|
|
36
|
+
/** For 'visible' trigger: IntersectionObserver threshold (0..1) */
|
|
37
|
+
threshold?: number;
|
|
38
|
+
/** Remove the animation class automatically after animationend */
|
|
39
|
+
cleanup?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface VAnimateStaggerOptions extends VAnimateConfig {
|
|
43
|
+
/** Delay increment between consecutive items in ms (default: 70) */
|
|
44
|
+
step?: number;
|
|
45
|
+
/** Initial delay for the first item in ms (default: 0) */
|
|
46
|
+
startDelay?: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface VAnimateEventPayload {
|
|
50
|
+
element: HTMLElement;
|
|
51
|
+
target: HTMLElement;
|
|
52
|
+
animation: string;
|
|
53
|
+
className: string;
|
|
54
|
+
cancel?: boolean;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const EASING_PRESETS: Record<string, string> = {
|
|
58
|
+
smooth: 'var(--v-anim-ease-smooth)',
|
|
59
|
+
spring: 'var(--v-anim-ease-spring)',
|
|
60
|
+
snappy: 'var(--v-anim-ease-snappy)',
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const CSS_VAR_MAP: Array<[keyof VAnimateConfig, string, string]> = [
|
|
64
|
+
['duration', '--v-anim-duration', 'ms'],
|
|
65
|
+
['delay', '--v-anim-delay', 'ms'],
|
|
66
|
+
['distance', '--v-anim-distance', 'px'],
|
|
67
|
+
['scale', '--v-anim-scale', ''],
|
|
68
|
+
['blur', '--v-anim-blur', 'px'],
|
|
69
|
+
['iterations', '--v-anim-iterations', ''],
|
|
70
|
+
['accent', '--v-anim-accent', ''],
|
|
71
|
+
];
|
|
72
|
+
|
|
73
|
+
function normalizeAnimationClass(animation?: string | null): string {
|
|
74
|
+
const raw = String(animation ?? '').trim();
|
|
75
|
+
if (!raw || raw === 'true' || raw === 'false') {
|
|
76
|
+
return '';
|
|
77
|
+
}
|
|
78
|
+
return raw.startsWith('v-anim-') ? raw : `v-anim-${raw}`;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function formatCssValue(value: string | number | undefined, unit: string): string {
|
|
82
|
+
if (value === undefined || value === null || value === '') {
|
|
83
|
+
return '';
|
|
84
|
+
}
|
|
85
|
+
if (typeof value === 'number') {
|
|
86
|
+
return `${value}${unit}`;
|
|
87
|
+
}
|
|
88
|
+
const trimmed = String(value).trim();
|
|
89
|
+
if (unit && /^-?\d+(\.\d+)?$/.test(trimmed)) {
|
|
90
|
+
return `${trimmed}${unit}`;
|
|
91
|
+
}
|
|
92
|
+
return trimmed;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function toBoolean(value: unknown, fallback: boolean): boolean {
|
|
96
|
+
if (typeof value === 'boolean') return value;
|
|
97
|
+
if (typeof value === 'string') {
|
|
98
|
+
const lower = value.trim().toLowerCase();
|
|
99
|
+
if (lower === 'true') return true;
|
|
100
|
+
if (lower === 'false') return false;
|
|
101
|
+
}
|
|
102
|
+
return fallback;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function toNumber(value: unknown, fallback: number): number {
|
|
106
|
+
const n = Number(value);
|
|
107
|
+
return Number.isFinite(n) ? n : fallback;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export class VAnimate extends VComponent {
|
|
111
|
+
protected override _name: string = 'animate';
|
|
112
|
+
protected override _defaultConfig: VAnimateConfig = {
|
|
113
|
+
animation: '',
|
|
114
|
+
trigger: 'mount',
|
|
115
|
+
target: null,
|
|
116
|
+
duration: '',
|
|
117
|
+
delay: '',
|
|
118
|
+
easing: '',
|
|
119
|
+
distance: '',
|
|
120
|
+
scale: '',
|
|
121
|
+
blur: '',
|
|
122
|
+
iterations: '',
|
|
123
|
+
accent: '',
|
|
124
|
+
once: true,
|
|
125
|
+
threshold: 0.15,
|
|
126
|
+
cleanup: false,
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
protected _targetElement: HTMLElement | null = null;
|
|
130
|
+
protected _activeClass: string = '';
|
|
131
|
+
protected _observer: IntersectionObserver | null = null;
|
|
132
|
+
protected _pendingEndHandler: ((event: AnimationEvent) => void) | null = null;
|
|
133
|
+
protected _pendingResolve: ((el: HTMLElement | null) => void) | null = null;
|
|
134
|
+
|
|
135
|
+
constructor(element: HTMLElement, config?: VAnimateConfig) {
|
|
136
|
+
super();
|
|
137
|
+
|
|
138
|
+
if (!element || this._shouldSkipInit(element)) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
this._init(element);
|
|
143
|
+
this._buildConfig(config);
|
|
144
|
+
|
|
145
|
+
if (!this._element) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Support shorthand: data-v-animate="fade-up"
|
|
150
|
+
const attrValue = this._element.getAttribute('data-v-animate');
|
|
151
|
+
if (
|
|
152
|
+
!this._getOption('animation') &&
|
|
153
|
+
attrValue &&
|
|
154
|
+
attrValue !== 'true' &&
|
|
155
|
+
attrValue !== 'false'
|
|
156
|
+
) {
|
|
157
|
+
this._mergeConfig({ animation: attrValue });
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
this._targetElement = this._resolveTargetElement();
|
|
161
|
+
this._applyCssVars();
|
|
162
|
+
this._bindTrigger();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
protected _resolveTargetElement(): HTMLElement | null {
|
|
166
|
+
const targetOpt = this._getOption('target') as string | HTMLElement | null;
|
|
167
|
+
if (targetOpt instanceof HTMLElement) {
|
|
168
|
+
return targetOpt;
|
|
169
|
+
}
|
|
170
|
+
if (typeof targetOpt === 'string' && targetOpt.trim()) {
|
|
171
|
+
return vDom.getElement(targetOpt.trim());
|
|
172
|
+
}
|
|
173
|
+
return this._element;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
protected _applyCssVars(overrides?: Partial<VAnimateConfig>): void {
|
|
177
|
+
const target = this._targetElement;
|
|
178
|
+
if (!target) return;
|
|
179
|
+
|
|
180
|
+
for (const [key, cssVar, unit] of CSS_VAR_MAP) {
|
|
181
|
+
const rawVal =
|
|
182
|
+
overrides && overrides[key] !== undefined
|
|
183
|
+
? overrides[key]
|
|
184
|
+
: (this._getOption(key) as string | number | undefined);
|
|
185
|
+
const formatted = formatCssValue(rawVal as string | number | undefined, unit);
|
|
186
|
+
if (formatted) {
|
|
187
|
+
target.style.setProperty(cssVar, formatted);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const rawEasing =
|
|
192
|
+
overrides && overrides.easing !== undefined
|
|
193
|
+
? overrides.easing
|
|
194
|
+
: (this._getOption('easing') as string | undefined);
|
|
195
|
+
if (rawEasing && String(rawEasing).trim()) {
|
|
196
|
+
const easingKey = String(rawEasing).trim();
|
|
197
|
+
target.style.setProperty(
|
|
198
|
+
'--v-anim-easing',
|
|
199
|
+
EASING_PRESETS[easingKey] ?? easingKey,
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
protected _bindTrigger(): void {
|
|
205
|
+
if (!this._element) return;
|
|
206
|
+
const trigger = String(this._getOption('trigger') || 'mount').toLowerCase();
|
|
207
|
+
|
|
208
|
+
if (trigger === 'mount') {
|
|
209
|
+
void this.play();
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (trigger === 'click') {
|
|
214
|
+
this._addEventListener(this._element, 'click', () => {
|
|
215
|
+
void this.play();
|
|
216
|
+
});
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (trigger === 'hover') {
|
|
221
|
+
this._addEventListener(this._element, 'mouseenter', () => {
|
|
222
|
+
void this.play();
|
|
223
|
+
});
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (trigger === 'visible') {
|
|
228
|
+
this._observeVisibility();
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
protected _observeVisibility(): void {
|
|
233
|
+
const host = this._element;
|
|
234
|
+
if (!host) return;
|
|
235
|
+
|
|
236
|
+
if (typeof IntersectionObserver === 'undefined') {
|
|
237
|
+
void this.play();
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const threshold = toNumber(this._getOption('threshold'), 0.15);
|
|
242
|
+
const once = toBoolean(this._getOption('once'), true);
|
|
243
|
+
|
|
244
|
+
this._observer = new IntersectionObserver(
|
|
245
|
+
(entries) => {
|
|
246
|
+
for (const entry of entries) {
|
|
247
|
+
if (entry.isIntersecting) {
|
|
248
|
+
void this.play();
|
|
249
|
+
if (once && this._observer) {
|
|
250
|
+
this._observer.disconnect();
|
|
251
|
+
this._observer = null;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
},
|
|
256
|
+
{ threshold },
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
this._observer.observe(host);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
protected _clearPendingEnd(resolveResult: HTMLElement | null = null): void {
|
|
263
|
+
if (this._targetElement && this._pendingEndHandler) {
|
|
264
|
+
this._targetElement.removeEventListener(
|
|
265
|
+
'animationend',
|
|
266
|
+
this._pendingEndHandler,
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
this._pendingEndHandler = null;
|
|
270
|
+
|
|
271
|
+
if (this._pendingResolve) {
|
|
272
|
+
const done = this._pendingResolve;
|
|
273
|
+
this._pendingResolve = null;
|
|
274
|
+
done(resolveResult);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
public getTargetElement(): HTMLElement | null {
|
|
279
|
+
return this._targetElement;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
public setAnimation(animation: string): this {
|
|
283
|
+
this._mergeConfig({ animation });
|
|
284
|
+
return this;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
public play(
|
|
288
|
+
animation?: string,
|
|
289
|
+
options?: Partial<VAnimateConfig>,
|
|
290
|
+
): Promise<HTMLElement | null> {
|
|
291
|
+
if (!this._element) {
|
|
292
|
+
return Promise.resolve(null);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (options) {
|
|
296
|
+
this._mergeConfig(options);
|
|
297
|
+
if (options.target !== undefined) {
|
|
298
|
+
this._targetElement = this._resolveTargetElement();
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const target = this._targetElement ?? this._element;
|
|
303
|
+
if (!target) {
|
|
304
|
+
return Promise.resolve(null);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const animName =
|
|
308
|
+
animation !== undefined
|
|
309
|
+
? animation
|
|
310
|
+
: String(this._getOption('animation') ?? '');
|
|
311
|
+
const className = normalizeAnimationClass(animName);
|
|
312
|
+
if (!className) {
|
|
313
|
+
return Promise.resolve(target);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (animation !== undefined) {
|
|
317
|
+
this._mergeConfig({ animation });
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const startPayload: VAnimateEventPayload = {
|
|
321
|
+
element: this._element,
|
|
322
|
+
target,
|
|
323
|
+
animation: animName,
|
|
324
|
+
className,
|
|
325
|
+
cancel: false,
|
|
326
|
+
};
|
|
327
|
+
void this._fireEvent('start', startPayload);
|
|
328
|
+
this._dispatchEvent('v.animate.start', startPayload);
|
|
329
|
+
if (startPayload.cancel === true) {
|
|
330
|
+
return Promise.resolve(target);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
this._clearPendingEnd(target);
|
|
334
|
+
|
|
335
|
+
// Remove previous or identical animation class, then force reflow to replay cleanly
|
|
336
|
+
if (this._activeClass) {
|
|
337
|
+
target.classList.remove(this._activeClass);
|
|
338
|
+
}
|
|
339
|
+
target.classList.remove(className, 'v-anim-paused');
|
|
340
|
+
this._applyCssVars(options);
|
|
341
|
+
vDom.reflow(target);
|
|
342
|
+
|
|
343
|
+
this._activeClass = className;
|
|
344
|
+
target.classList.add(className);
|
|
345
|
+
|
|
346
|
+
const shouldCleanup = toBoolean(
|
|
347
|
+
options?.cleanup ?? this._getOption('cleanup'),
|
|
348
|
+
false,
|
|
349
|
+
);
|
|
350
|
+
|
|
351
|
+
return new Promise<HTMLElement | null>((resolve) => {
|
|
352
|
+
const finish = () => {
|
|
353
|
+
if (shouldCleanup) {
|
|
354
|
+
target.classList.remove(className);
|
|
355
|
+
if (this._activeClass === className) {
|
|
356
|
+
this._activeClass = '';
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
const endPayload: VAnimateEventPayload = {
|
|
360
|
+
element: this._element ?? target,
|
|
361
|
+
target,
|
|
362
|
+
animation: animName,
|
|
363
|
+
className,
|
|
364
|
+
};
|
|
365
|
+
void this._fireEvent('end', endPayload);
|
|
366
|
+
this._dispatchEvent('v.animate.end', endPayload);
|
|
367
|
+
resolve(target);
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
const onEnd = (event: AnimationEvent) => {
|
|
371
|
+
if (event.target !== target) return;
|
|
372
|
+
target.removeEventListener('animationend', onEnd);
|
|
373
|
+
this._pendingEndHandler = null;
|
|
374
|
+
this._pendingResolve = null;
|
|
375
|
+
finish();
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
this._pendingEndHandler = onEnd;
|
|
379
|
+
this._pendingResolve = resolve;
|
|
380
|
+
target.addEventListener('animationend', onEnd);
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
public replay(
|
|
385
|
+
animation?: string,
|
|
386
|
+
options?: Partial<VAnimateConfig>,
|
|
387
|
+
): Promise<HTMLElement | null> {
|
|
388
|
+
return this.play(animation, options);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
public pause(): this {
|
|
392
|
+
this._targetElement?.classList.add('v-anim-paused');
|
|
393
|
+
return this;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
public resume(): this {
|
|
397
|
+
this._targetElement?.classList.remove('v-anim-paused');
|
|
398
|
+
return this;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
public stop(): this {
|
|
402
|
+
this._clearPendingEnd(this._targetElement);
|
|
403
|
+
if (this._targetElement && this._activeClass) {
|
|
404
|
+
this._targetElement.classList.remove(this._activeClass, 'v-anim-paused');
|
|
405
|
+
}
|
|
406
|
+
this._activeClass = '';
|
|
407
|
+
return this;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
public override dispose(): void {
|
|
411
|
+
this._clearPendingEnd(null);
|
|
412
|
+
if (this._observer) {
|
|
413
|
+
this._observer.disconnect();
|
|
414
|
+
this._observer = null;
|
|
415
|
+
}
|
|
416
|
+
if (this._targetElement && this._activeClass) {
|
|
417
|
+
this._targetElement.classList.remove(this._activeClass, 'v-anim-paused');
|
|
418
|
+
}
|
|
419
|
+
this._activeClass = '';
|
|
420
|
+
super.dispose();
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
public static getInstance(element: HTMLElement | string | null): VAnimate | null {
|
|
424
|
+
return VComponent.getComponentInstance<VAnimate>(element, 'animate');
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
public static getOrCreateInstance(
|
|
428
|
+
element: HTMLElement | string | null,
|
|
429
|
+
config?: VAnimateConfig,
|
|
430
|
+
): VAnimate | null {
|
|
431
|
+
return VComponent.getOrCreateComponentInstance<VAnimate, VAnimateConfig>(
|
|
432
|
+
element,
|
|
433
|
+
'animate',
|
|
434
|
+
VAnimate,
|
|
435
|
+
config,
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
public static createInstances(root: ParentNode = document): void {
|
|
440
|
+
VComponent.createComponentInstances<VAnimate, VAnimateConfig>(
|
|
441
|
+
'animate',
|
|
442
|
+
VAnimate,
|
|
443
|
+
'[data-v-animate]',
|
|
444
|
+
root,
|
|
445
|
+
);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
public static init(): void {
|
|
449
|
+
VAnimate.createInstances();
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Dynamically attach VAnimate to any element or selector at runtime.
|
|
454
|
+
*/
|
|
455
|
+
public static add(
|
|
456
|
+
element: HTMLElement | string | null,
|
|
457
|
+
animation: string,
|
|
458
|
+
options?: VAnimateConfig,
|
|
459
|
+
): VAnimate | null {
|
|
460
|
+
const targetEl = vDom.getElement(element);
|
|
461
|
+
if (!targetEl) return null;
|
|
462
|
+
|
|
463
|
+
const existing = VAnimate.getInstance(targetEl);
|
|
464
|
+
if (existing) {
|
|
465
|
+
if (options?.trigger && options.trigger !== existing.getOption('trigger')) {
|
|
466
|
+
existing.dispose();
|
|
467
|
+
} else {
|
|
468
|
+
existing.setAnimation(animation);
|
|
469
|
+
if (options) {
|
|
470
|
+
existing._mergeConfig(options);
|
|
471
|
+
existing._applyCssVars(options);
|
|
472
|
+
}
|
|
473
|
+
const trigger = options?.trigger ?? 'mount';
|
|
474
|
+
if (trigger === 'mount') {
|
|
475
|
+
void existing.play(animation, options);
|
|
476
|
+
}
|
|
477
|
+
return existing;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
return new VAnimate(targetEl, {
|
|
482
|
+
trigger: 'mount',
|
|
483
|
+
...options,
|
|
484
|
+
animation,
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* One-liner to dynamically play/replay an animation on any element or selector.
|
|
490
|
+
*/
|
|
491
|
+
public static play(
|
|
492
|
+
element: HTMLElement | string | null,
|
|
493
|
+
animation: string,
|
|
494
|
+
options?: VAnimateConfig,
|
|
495
|
+
): Promise<HTMLElement | null> {
|
|
496
|
+
const targetEl = vDom.getElement(element);
|
|
497
|
+
if (!targetEl) return Promise.resolve(null);
|
|
498
|
+
|
|
499
|
+
const instance = VAnimate.getOrCreateInstance(targetEl, {
|
|
500
|
+
trigger: 'manual',
|
|
501
|
+
...options,
|
|
502
|
+
animation,
|
|
503
|
+
});
|
|
504
|
+
if (!instance) return Promise.resolve(null);
|
|
505
|
+
|
|
506
|
+
return instance.play(animation, options);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* Dynamically animate a list of elements or a container's direct children with stagger delay.
|
|
511
|
+
*/
|
|
512
|
+
public static stagger(
|
|
513
|
+
elementsOrSelector: HTMLElement | NodeListOf<HTMLElement> | HTMLElement[] | string,
|
|
514
|
+
animation: string,
|
|
515
|
+
options: VAnimateStaggerOptions = {},
|
|
516
|
+
): Promise<Array<HTMLElement | null>> {
|
|
517
|
+
const { step = 70, startDelay = 0, ...animOptions } = options;
|
|
518
|
+
|
|
519
|
+
let items: HTMLElement[] = [];
|
|
520
|
+
if (typeof elementsOrSelector === 'string') {
|
|
521
|
+
items = Array.from(document.querySelectorAll<HTMLElement>(elementsOrSelector));
|
|
522
|
+
} else if (elementsOrSelector instanceof HTMLElement) {
|
|
523
|
+
items = Array.from(elementsOrSelector.children).filter(
|
|
524
|
+
(child): child is HTMLElement => child instanceof HTMLElement,
|
|
525
|
+
);
|
|
526
|
+
} else {
|
|
527
|
+
items = Array.from(elementsOrSelector);
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
return Promise.all(
|
|
531
|
+
items.map((el, index) =>
|
|
532
|
+
VAnimate.play(el, animation, {
|
|
533
|
+
...animOptions,
|
|
534
|
+
delay: `${startDelay + index * step}ms`,
|
|
535
|
+
}),
|
|
536
|
+
),
|
|
537
|
+
);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
export const vAnimatePlugin: VPluginInterface = {
|
|
542
|
+
name: 'animate',
|
|
543
|
+
component: VAnimate,
|
|
544
|
+
init: () => VAnimate.init(),
|
|
545
|
+
dispose: () => {
|
|
546
|
+
if (typeof document === 'undefined') return;
|
|
547
|
+
document.querySelectorAll<HTMLElement>('[data-v-animate-initialized]').forEach((el) => {
|
|
548
|
+
VAnimate.getInstance(el)?.dispose();
|
|
549
|
+
});
|
|
550
|
+
},
|
|
551
|
+
};
|
|
552
|
+
|
|
553
|
+
// Auto-register with @vdragon/vui plugin host SDK
|
|
554
|
+
if (vComponents && typeof vComponents.use === 'function') {
|
|
555
|
+
vComponents.use(vAnimatePlugin, { globalName: 'VAnimate' });
|
|
556
|
+
} else if (typeof window !== 'undefined') {
|
|
557
|
+
(window as unknown as Record<string, unknown>).VAnimate = VAnimate;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
export default VAnimate;
|