use-scroll-animate 1.2.0 → 1.4.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/CHANGELOG.md +113 -58
- package/LICENSE +21 -21
- package/README.md +217 -214
- package/README_ja.md +106 -69
- package/README_zh.md +106 -69
- package/dist/index.d.mts +302 -0
- package/dist/index.d.ts +302 -0
- package/dist/index.esm.js +1179 -697
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +1193 -708
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1179 -0
- package/dist/index.mjs.map +1 -0
- package/dist/index.umd.js +13 -14
- package/dist/index.umd.js.map +1 -1
- package/dist/types/core.d.ts +41 -7
- package/dist/types/index.d.ts +36 -33
- package/dist/types/presets.d.ts +15 -15
- package/dist/types/react.d.ts +29 -18
- package/dist/types/sequence.d.ts +38 -0
- package/dist/types/stagger.d.ts +25 -0
- package/dist/types/types.d.ts +139 -123
- package/dist/types/vue.d.ts +28 -18
- package/package.json +81 -35
- package/CONTRIBUTING.md +0 -76
- package/examples/react/App.tsx +0 -61
- package/examples/vanilla/index.html +0 -201
- package/rollup.config.js +0 -41
- package/src/core.ts +0 -373
- package/src/index.ts +0 -48
- package/src/presets.ts +0 -148
- package/src/react.ts +0 -175
- package/src/types.ts +0 -161
- package/src/vue.ts +0 -118
- package/tsconfig.json +0 -17
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* use-scroll-animate - Core Type Definitions
|
|
3
|
+
* A lightweight, high-performance scroll animation library
|
|
4
|
+
*/
|
|
5
|
+
/** Built-in animation presets */
|
|
6
|
+
type AnimationPreset = 'fade-in' | 'fade-in-up' | 'fade-in-down' | 'fade-in-left' | 'fade-in-right' | 'zoom-in' | 'zoom-out' | 'flip-x' | 'flip-y' | 'slide-up' | 'slide-down' | 'slide-left' | 'slide-right' | 'bounce' | 'rotate-in' | 'blur-in' | 'skew-in' | 'scale-x' | 'scale-y' | 'shimmer' | 'pulse' | 'swing' | 'scale-up' | 'blur-in-up' | 'flip-up' | 'flip-down' | 'rotate-left' | 'rotate-right' | 'clip-up' | 'clip-down' | 'clip-left' | 'clip-right' | 'clip-circle';
|
|
7
|
+
/**
|
|
8
|
+
* How `onProgress` (and parallax) progress is measured.
|
|
9
|
+
* - `'ratio'` (default): the element's visible ratio (IntersectionObserver `intersectionRatio`).
|
|
10
|
+
* - `'scroll'`: true scroll progress, 0 when the element's top touches the bottom of the
|
|
11
|
+
* viewport and 1 when its bottom leaves the top. Works for elements taller than the screen.
|
|
12
|
+
*/
|
|
13
|
+
type ProgressMode = 'ratio' | 'scroll';
|
|
14
|
+
/** Easing function types */
|
|
15
|
+
type EasingType = 'linear' | 'ease' | 'ease-in' | 'ease-out' | 'ease-in-out' | 'spring' | 'soft-spring' | 'heavy-bounce' | [number, number, number, number] | ((t: number) => number) | string;
|
|
16
|
+
/** Keyframe definition for custom animations */
|
|
17
|
+
interface AnimationKeyframe {
|
|
18
|
+
[property: string]: string | number;
|
|
19
|
+
}
|
|
20
|
+
/** Custom animation definition */
|
|
21
|
+
interface CustomAnimation {
|
|
22
|
+
from: AnimationKeyframe;
|
|
23
|
+
to: AnimationKeyframe;
|
|
24
|
+
}
|
|
25
|
+
/** Parallax configuration */
|
|
26
|
+
interface ParallaxOptions {
|
|
27
|
+
/** Movement on X axis (e.g., '100px', '20%') */
|
|
28
|
+
x?: string | number;
|
|
29
|
+
/** Movement on Y axis (e.g., '100px', '20%') */
|
|
30
|
+
y?: string | number;
|
|
31
|
+
/** Rotation in degrees */
|
|
32
|
+
rotate?: number;
|
|
33
|
+
/** Scale factor */
|
|
34
|
+
scale?: number;
|
|
35
|
+
/** Speed multiplier (default: 1) */
|
|
36
|
+
speed?: number;
|
|
37
|
+
}
|
|
38
|
+
/** Per-element animation options */
|
|
39
|
+
interface AnimateOptions {
|
|
40
|
+
/** Animation preset name, array of presets, or custom animation object */
|
|
41
|
+
animation?: AnimationPreset | AnimationPreset[] | CustomAnimation;
|
|
42
|
+
/** Duration in milliseconds (default: 600) */
|
|
43
|
+
duration?: number;
|
|
44
|
+
/** Delay in milliseconds (default: 0) */
|
|
45
|
+
delay?: number;
|
|
46
|
+
/** Easing function (default: 'ease') */
|
|
47
|
+
easing?: EasingType;
|
|
48
|
+
/** Intersection threshold 0-1 (default: 0.1) */
|
|
49
|
+
threshold?: number | number[];
|
|
50
|
+
/** Root margin for IntersectionObserver (default: '0px') */
|
|
51
|
+
rootMargin?: string;
|
|
52
|
+
/** Whether to replay animation each time element enters viewport (default: false) */
|
|
53
|
+
repeat?: boolean;
|
|
54
|
+
/** Whether to trigger animation only once (default: true if repeat is false) */
|
|
55
|
+
once?: boolean;
|
|
56
|
+
/** Offset in pixels from the viewport edge to trigger animation (default: 0) */
|
|
57
|
+
offset?: number;
|
|
58
|
+
/** Stagger delay for child elements in ms (default: 0) */
|
|
59
|
+
stagger?: number;
|
|
60
|
+
/** Parallax effect configuration */
|
|
61
|
+
parallax?: ParallaxOptions;
|
|
62
|
+
/** Callback fired when animation starts */
|
|
63
|
+
onStart?: (element: Element) => void;
|
|
64
|
+
/** Callback fired when animation completes */
|
|
65
|
+
onComplete?: (element: Element) => void;
|
|
66
|
+
/** Callback fired when element enters viewport */
|
|
67
|
+
onEnter?: (element: Element) => void;
|
|
68
|
+
/** Callback fired when element leaves viewport */
|
|
69
|
+
onLeave?: (element: Element) => void;
|
|
70
|
+
/** Callback fired with scroll progress (0 to 1) */
|
|
71
|
+
onProgress?: (element: Element, progress: number) => void;
|
|
72
|
+
/** How progress for `onProgress`/parallax is measured (default: 'ratio') */
|
|
73
|
+
progressMode?: ProgressMode;
|
|
74
|
+
}
|
|
75
|
+
/** Global configuration for ScrollAnimate instance */
|
|
76
|
+
interface ScrollAnimateConfig {
|
|
77
|
+
/** Default animation preset (default: 'fade-in-up') */
|
|
78
|
+
defaultAnimation?: AnimationPreset | AnimationPreset[] | CustomAnimation;
|
|
79
|
+
/** Default duration in ms (default: 600) */
|
|
80
|
+
defaultDuration?: number;
|
|
81
|
+
/** Default delay in ms (default: 0) */
|
|
82
|
+
defaultDelay?: number;
|
|
83
|
+
/** Default easing (default: 'ease') */
|
|
84
|
+
defaultEasing?: EasingType;
|
|
85
|
+
/** Default threshold (default: 0.1) */
|
|
86
|
+
defaultThreshold?: number | number[];
|
|
87
|
+
/** Default root margin (default: '0px') */
|
|
88
|
+
defaultRootMargin?: string;
|
|
89
|
+
/** Whether animations replay by default (default: false) */
|
|
90
|
+
defaultRepeat?: boolean;
|
|
91
|
+
/** Default once setting (default: true) */
|
|
92
|
+
defaultOnce?: boolean;
|
|
93
|
+
/** Default offset in pixels (default: 0) */
|
|
94
|
+
defaultOffset?: number;
|
|
95
|
+
/** CSS class added before animation (default: 'sa-hidden') */
|
|
96
|
+
hiddenClass?: string;
|
|
97
|
+
/** CSS class added when element is visible (default: 'sa-visible') */
|
|
98
|
+
visibleClass?: string;
|
|
99
|
+
/** Whether to use CSS class-based animation instead of Web Animations API */
|
|
100
|
+
useClassNames?: boolean;
|
|
101
|
+
/** Disable all animations (useful for reduced-motion preference) */
|
|
102
|
+
disabled?: boolean;
|
|
103
|
+
/** Custom IntersectionObserver root element */
|
|
104
|
+
root?: Element | null;
|
|
105
|
+
/**
|
|
106
|
+
* Drop `once` elements from the registry as soon as their entrance animation
|
|
107
|
+
* has been triggered (unless they still need parallax/onProgress), so they can
|
|
108
|
+
* be garbage-collected. They are remembered in a WeakSet, so `init()`/`observe()`
|
|
109
|
+
* never re-hide or replay them. (default: true)
|
|
110
|
+
*/
|
|
111
|
+
autoUnregister?: boolean;
|
|
112
|
+
}
|
|
113
|
+
/** Registered element entry */
|
|
114
|
+
interface AnimatedElement {
|
|
115
|
+
element: Element;
|
|
116
|
+
options: Required<AnimateOptions>;
|
|
117
|
+
observer: IntersectionObserver;
|
|
118
|
+
animated: boolean;
|
|
119
|
+
progressObserver?: IntersectionObserver;
|
|
120
|
+
}
|
|
121
|
+
/** ScrollAnimate public API */
|
|
122
|
+
interface ScrollAnimateInstance {
|
|
123
|
+
/** Observe a single element or CSS selector */
|
|
124
|
+
observe(target: string | Element | NodeList | Element[], options?: AnimateOptions): void;
|
|
125
|
+
/** Stop observing a single element or CSS selector */
|
|
126
|
+
unobserve(target: string | Element | NodeList | Element[]): void;
|
|
127
|
+
/** Observe all elements matching the data-sa attribute */
|
|
128
|
+
init(rootElement?: Element | Document): void;
|
|
129
|
+
/** Destroy the instance and clean up all observers */
|
|
130
|
+
destroy(): void;
|
|
131
|
+
/** Refresh all observers (useful after DOM changes) */
|
|
132
|
+
refresh(): void;
|
|
133
|
+
/** Manually trigger animation on an element */
|
|
134
|
+
animate(target: string | Element, options?: AnimateOptions): void;
|
|
135
|
+
/** Get all currently observed elements */
|
|
136
|
+
getObservedElements(): AnimatedElement[];
|
|
137
|
+
/** Update global configuration */
|
|
138
|
+
configure(config: Partial<ScrollAnimateConfig>): void;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* use-scroll-animate - Core Implementation
|
|
143
|
+
* Uses IntersectionObserver + Web Animations API for zero-dependency,
|
|
144
|
+
* high-performance scroll-triggered animations.
|
|
145
|
+
*/
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* True scroll progress of `el` through the viewport (or `root`): 0 when its top
|
|
149
|
+
* edge reaches the bottom of the viewport, 1 when its bottom edge passes the top.
|
|
150
|
+
* Works for elements taller than the viewport. Returns 0 without a DOM.
|
|
151
|
+
*/
|
|
152
|
+
declare function getScrollProgress(el: Element, root?: Element | null): number;
|
|
153
|
+
declare function createScrollAnimate(userConfig?: ScrollAnimateConfig): ScrollAnimateInstance;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* use-scroll-animate - Staggered children
|
|
157
|
+
* Reveal a container's children one after another when the container scrolls
|
|
158
|
+
* into view, optionally also animating children that are added later.
|
|
159
|
+
*/
|
|
160
|
+
|
|
161
|
+
interface StaggerOptions extends AnimateOptions {
|
|
162
|
+
/** Delay between consecutive children in ms (default: 80) */
|
|
163
|
+
stagger?: number;
|
|
164
|
+
/**
|
|
165
|
+
* Watch the container with a MutationObserver and animate children added
|
|
166
|
+
* later (e.g. infinite lists). Children added after the container was
|
|
167
|
+
* revealed animate when they scroll into view, staggered per batch.
|
|
168
|
+
* (default: false)
|
|
169
|
+
*/
|
|
170
|
+
observeChildren?: boolean;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Animate the children of `container` with a stagger once it enters the
|
|
174
|
+
* viewport. Returns a cleanup function. SSR-safe (no-op without a DOM).
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* const stop = staggerChildren(document.querySelector('ul'), { stagger: 60, observeChildren: true });
|
|
178
|
+
*/
|
|
179
|
+
declare function staggerChildren(container: Element | null | undefined, options?: StaggerOptions, instance?: ScrollAnimateInstance): () => void;
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* use-scroll-animate - Sequence / timeline helper
|
|
183
|
+
* Chain animations on several targets, one after another (or overlapping).
|
|
184
|
+
*/
|
|
185
|
+
|
|
186
|
+
interface SequenceStep extends AnimateOptions {
|
|
187
|
+
/** Selector, Element, NodeList or Element[] to animate in this step */
|
|
188
|
+
target: string | Element | NodeList | Element[];
|
|
189
|
+
/** Pause (ms) after the previous step ends before this one starts. Negative values overlap. (default: 0) */
|
|
190
|
+
gap?: number;
|
|
191
|
+
/** Absolute start time (ms) on the timeline; overrides `gap` */
|
|
192
|
+
at?: number;
|
|
193
|
+
}
|
|
194
|
+
interface SequenceOptions extends AnimateOptions {
|
|
195
|
+
/** Play automatically (once) when this element/selector enters the viewport */
|
|
196
|
+
trigger?: string | Element;
|
|
197
|
+
/** Instance whose global config (easing, classes, `disabled`) is used */
|
|
198
|
+
instance?: ScrollAnimateInstance;
|
|
199
|
+
}
|
|
200
|
+
interface SequenceController {
|
|
201
|
+
/** Play (or replay) the timeline. Resolves when every step has completed, or on `cancel()`. */
|
|
202
|
+
play(): Promise<void>;
|
|
203
|
+
/** Stop the trigger and running animations; elements are left visible. */
|
|
204
|
+
cancel(): void;
|
|
205
|
+
/** Total duration of the timeline in ms (computed for the current DOM). */
|
|
206
|
+
duration(): number;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Build a timeline of animations.
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* sequence([
|
|
213
|
+
* { target: '.title', animation: 'fade-in-up' },
|
|
214
|
+
* { target: '.subtitle', animation: 'blur-in', gap: -300 }, // overlap by 300ms
|
|
215
|
+
* { target: '.card', animation: 'scale-up', stagger: 80 },
|
|
216
|
+
* ], { trigger: '.hero' });
|
|
217
|
+
*/
|
|
218
|
+
declare function sequence(steps: SequenceStep[], options?: SequenceOptions): SequenceController;
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* use-scroll-animate - Animation Presets
|
|
222
|
+
* Defines keyframes for all built-in animation presets
|
|
223
|
+
*/
|
|
224
|
+
|
|
225
|
+
type KeyframeMap = {
|
|
226
|
+
from: Record<string, string | number>;
|
|
227
|
+
to: Record<string, string | number>;
|
|
228
|
+
};
|
|
229
|
+
declare const PRESETS: Record<AnimationPreset, KeyframeMap>;
|
|
230
|
+
declare function resolvePreset(animation: AnimationPreset | AnimationPreset[] | CustomAnimation): KeyframeMap;
|
|
231
|
+
/** Easing to CSS cubic-bezier mapping */
|
|
232
|
+
declare const EASING_MAP: Record<string, string>;
|
|
233
|
+
declare function resolveEasing(easing: EasingType): string;
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* use-scroll-animate - React Integration
|
|
237
|
+
* Provides useScrollAnimate and useScrollStagger hooks for React applications.
|
|
238
|
+
* `useScrollStagger({ observeChildren: true })` also animates children added later.
|
|
239
|
+
*
|
|
240
|
+
* Both hooks are thin wrappers around the core engine, so they share its
|
|
241
|
+
* behaviour: `once`, `offset`, custom easing functions, parallax,
|
|
242
|
+
* `prefers-reduced-motion` support, and proper cleanup on unmount.
|
|
243
|
+
*/
|
|
244
|
+
|
|
245
|
+
type ReactRef<T> = {
|
|
246
|
+
current: T | null;
|
|
247
|
+
};
|
|
248
|
+
declare function createReactHooks(React: {
|
|
249
|
+
useRef: <T>(initial: T | null) => ReactRef<T>;
|
|
250
|
+
useEffect: (effect: () => (() => void) | void, deps?: unknown[]) => void;
|
|
251
|
+
}): {
|
|
252
|
+
useScrollAnimate: (options?: AnimateOptions) => ReactRef<Element>;
|
|
253
|
+
useScrollStagger: (options?: StaggerOptions) => ReactRef<Element>;
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* use-scroll-animate - Vue 3 Integration
|
|
258
|
+
* Provides useScrollAnimate and useScrollStagger composables for Vue 3 applications.
|
|
259
|
+
*
|
|
260
|
+
* A thin wrapper around the core engine, so it shares its behaviour: `once`,
|
|
261
|
+
* `offset`, custom easing functions, parallax, `prefers-reduced-motion`
|
|
262
|
+
* support, and cleanup on unmount.
|
|
263
|
+
*/
|
|
264
|
+
|
|
265
|
+
declare function createVueComposables(Vue: {
|
|
266
|
+
ref: <T>(value: T | null) => {
|
|
267
|
+
value: T | null;
|
|
268
|
+
};
|
|
269
|
+
onMounted: (fn: () => void) => void;
|
|
270
|
+
onUnmounted: (fn: () => void) => void;
|
|
271
|
+
}): {
|
|
272
|
+
useScrollAnimate: (options?: AnimateOptions) => {
|
|
273
|
+
animateRef: {
|
|
274
|
+
value: Element | null;
|
|
275
|
+
};
|
|
276
|
+
};
|
|
277
|
+
useScrollStagger: (options?: StaggerOptions) => {
|
|
278
|
+
staggerRef: {
|
|
279
|
+
value: Element | null;
|
|
280
|
+
};
|
|
281
|
+
};
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Default singleton instance of ScrollAnimate.
|
|
286
|
+
* Ready to use out of the box with sensible defaults.
|
|
287
|
+
*
|
|
288
|
+
* @example
|
|
289
|
+
* ```js
|
|
290
|
+
* import ScrollAnimate from 'use-scroll-animate';
|
|
291
|
+
*
|
|
292
|
+
* // Auto-initialize all elements with data-sa attribute
|
|
293
|
+
* ScrollAnimate.init();
|
|
294
|
+
*
|
|
295
|
+
* // Or manually observe elements
|
|
296
|
+
* ScrollAnimate.observe('.my-element', { animation: 'fade-in-up' });
|
|
297
|
+
* ```
|
|
298
|
+
*/
|
|
299
|
+
declare const ScrollAnimate: ScrollAnimateInstance;
|
|
300
|
+
|
|
301
|
+
export { EASING_MAP, PRESETS, createReactHooks, createScrollAnimate, createVueComposables, ScrollAnimate as default, getScrollProgress, resolveEasing, resolvePreset, sequence, staggerChildren };
|
|
302
|
+
export type { AnimateOptions, AnimatedElement, AnimationKeyframe, AnimationPreset, CustomAnimation, EasingType, ParallaxOptions, ProgressMode, ScrollAnimateConfig, ScrollAnimateInstance, SequenceController, SequenceOptions, SequenceStep, StaggerOptions };
|