svelte-aos 0.0.3 → 0.1.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 CHANGED
@@ -2,234 +2,288 @@
2
2
 
3
3
  Animate On Scroll library for Svelte 5. Trigger CSS animations when elements scroll into view.
4
4
 
5
- ## Quick Start
5
+ This package will give you an easy way to bring scroll-based animations to your Svelte applications using a simple and flexible API.
6
6
 
7
- ```svelte
8
- <script>
9
- import { aosObserver } from 'svelte-aos';
10
- import 'svelte-aos/styles/full.css';
11
- </script>
7
+ ## ✨ Why AOS Svelte?
12
8
 
13
- <main {@attach aosObserver()}>
14
- <div data-aos="fade-up">I animate when scrolled into view</div>
15
- </main>
16
- ```
9
+ - ⚡ **Tiny** — <1KB gzipped JavaScript + <1kb modular CSS
10
+ - 🎯 **Optimized Bundle** Import only what you need for minimal footprint
11
+ - 🚀 **Zero Dependencies** — No external dependencies
12
+ - 🔧 **Universal Compatibility** — Svelte 4 & 5 support (Actions or Attachments)
13
+ - ⚙️ **Highly Performant** — Only tracks and animates elements explicitly marked for animation
14
+ - 🎨 **Fully Customizable** — 30+ pre-built animations with flexible configuration
17
15
 
18
16
  ## Installation
19
17
 
20
18
  ```bash
21
19
  npm install svelte-aos
20
+ # or
21
+ pnpm add svelte-aos
22
+ # or
23
+ yarn add svelte-aos
22
24
  ```
23
25
 
24
- ## Usage
26
+ ## Quick Start
25
27
 
26
- ### 1. Import Styles
28
+ ### 1. Import the styles
27
29
 
28
30
  ```js
29
- // All animations
31
+ // Import all animations
30
32
  import 'svelte-aos/styles/full.css';
31
33
 
32
- // Or selective (smaller bundle)
34
+ // Or import only what you need for smaller bundle
33
35
  import 'svelte-aos/styles/base.css'; // Required
34
- import 'svelte-aos/styles/fade.css'; // Optional
35
- import 'svelte-aos/styles/zoom.css'; // Optional
36
- import 'svelte-aos/styles/flip.css'; // Optional
37
- import 'svelte-aos/styles/slide.css'; // Optional
38
- import 'svelte-aos/styles/easings.css'; // Optional
36
+ import 'svelte-aos/styles/fade.css'; // Only fade animations
39
37
  ```
40
38
 
41
- ### 2. Add Observer
39
+ Or in CSS:
42
40
 
43
- **Svelte 5 attachment:**
41
+ ```css
42
+ /* Import all animations */
43
+ @import 'svelte-aos/styles/full.css';
44
44
 
45
- ```svelte
46
- <main {@attach aosObserver()}>
47
- <!-- elements with data-aos will animate -->
48
- </main>
45
+ /* Or import only what you need */
46
+ @import 'svelte-aos/styles/base.css'; /* Required */
47
+ @import 'svelte-aos/styles/fade.css'; /* Only fade animations */
49
48
  ```
50
49
 
51
- **Svelte action:**
50
+ ### 2. Add the AOS component to your page or layout
52
51
 
53
- ```svelte
54
- <main use:aosAction>
55
- <!-- elements with data-aos will animate -->
56
- </main>
52
+ ```html
53
+ <script>
54
+ import { aosAttachment, aosAction, AOS } from 'svelte-aos';
55
+ </script>
56
+
57
+ <AOS options={{ /* options */ }} />
58
+
59
+ <!-- Rest of your page or layout -->
57
60
  ```
58
61
 
59
- ### 3. Add Animations
62
+ ### 3. Add animations to elements
60
63
 
61
- ```svelte
62
- <div data-aos="fade-up">Fade up</div>
63
- <div data-aos="zoom-in">Zoom in</div>
64
- <div data-aos="flip-left">Flip left</div>
64
+ **Svelte 5.29+ (attachment):**
65
+
66
+ ```html
67
+ <script>
68
+ import { aosAttachment } from 'svelte-aos';
69
+ </script>
70
+
71
+ <!-- Add data-aos only if you want the element to animate on first load when in viewport -->
72
+ <h2 data-aos="fade-up" {@attach aosAttachment({ animation: 'fade-up' })}>
73
+ AOS The Svelte Way
74
+ </h2>
65
75
  ```
66
76
 
67
- Or use the `toAosAttributes` helper to generate attributes programmatically.
77
+ **Svelte 4.x to 5.28 (action):**
68
78
 
69
- ```svelte
79
+ ```html
70
80
  <script>
71
- import { toAosAttributes } from 'svelte-aos';
72
- const attrs = toAosAttributes({
73
- animation: 'fade-up',
74
- delay: 150,
75
- duration: 400,
76
- once: true
77
- });
81
+ import { aosAction } from 'svelte-aos';
78
82
  </script>
79
83
 
80
- <div {...attrs}>Animated</div>
81
- <!-- or inline -->
82
- <div data-aos="fade-up" {...toAosAttributes({ delay: 150, duration: 400, once: true })}>
83
- Animated
84
- </div>
84
+ <h2 data-aos="fade" use:aosAction={{ animation: 'fade' }}>
85
+ AOS The Svelte Way
86
+ </h2>
85
87
  ```
86
88
 
87
- ## Animations
89
+ ## Available Animations
88
90
 
89
91
  ### Fade
90
92
 
91
- ```text
93
+ Opacity + optional translate
94
+
95
+ ```
92
96
  fade fade-up fade-down fade-left fade-right
93
97
  fade-up-left fade-up-right fade-down-left fade-down-right
94
98
  ```
95
99
 
96
100
  ### Zoom
97
101
 
98
- ```text
102
+ Scale + optional translate
103
+
104
+ ```
99
105
  zoom-in zoom-in-up zoom-in-down zoom-in-left zoom-in-right
100
106
  zoom-out zoom-out-up zoom-out-down zoom-out-left zoom-out-right
101
107
  ```
102
108
 
103
109
  ### Flip
104
110
 
105
- ```text
111
+ 3D rotation effects
112
+
113
+ ```
106
114
  flip-up flip-down flip-left flip-right
107
115
  ```
108
116
 
109
117
  ### Slide
110
118
 
111
- ```text
119
+ Translate from edge (no opacity)
120
+
121
+ ```
112
122
  slide-up slide-down slide-left slide-right
113
123
  ```
114
124
 
115
125
  ## Per-Element Options
116
126
 
117
- ```svelte
118
- <!-- Duration (ms) -->
119
- <div data-aos="fade" data-aos-duration="1000">Slow fade</div>
127
+ Customize each element with attachment or action parameters:
120
128
 
121
- <!-- Delay (ms) -->
122
- <div data-aos="fade" data-aos-delay="300">Delayed</div>
129
+ ### Duration
123
130
 
124
- <!-- Easing -->
125
- <div data-aos="fade" data-aos-easing="ease-out-back">Bouncy</div>
131
+ ```svelte
132
+ <!-- Svelte 5.29+ -->
133
+ <div data-aos="fade" {@attach aosAttachment({ animation: 'fade', duration: 1000 })}>
126
134
 
127
- <!-- Animate only once -->
128
- <div data-aos="fade" data-aos-once>Once only</div>
135
+ <!-- Svelte 4.x to 5.28 -->
136
+ <div data-aos="fade" use:aosAction={{ animation: 'fade', duration: 1000 }}>
129
137
  ```
130
138
 
131
- ### Staggered Animation
139
+ ### Delay
132
140
 
133
141
  ```svelte
134
- <div data-aos="fade-up" data-aos-delay="0">First</div>
135
- <div data-aos="fade-up" data-aos-delay="100">Second</div>
136
- <div data-aos="fade-up" data-aos-delay="200">Third</div>
142
+ <!-- Svelte 5.29+ -->
143
+ <div data-aos="fade" {@attach aosAttachment({ animation: 'fade', delay: 300 })}>
144
+
145
+ <!-- Svelte 4.x to 5.28 -->
146
+ <div data-aos="fade" use:aosAction={{ animation: 'fade', delay: 300 }}>
137
147
  ```
138
148
 
139
- ## Easings
149
+ ### Easing
140
150
 
141
- ```text
142
- linear ease ease-in ease-out ease-in-out
143
- ease-in-back ease-out-back ease-in-out-back
144
- ease-in-sine ease-out-sine ease-in-out-sine
145
- ease-in-quad ease-out-quad ease-in-out-quad
146
- ease-in-cubic ease-out-cubic ease-in-out-cubic
147
- ease-in-quart ease-out-quart ease-in-out-quart
151
+ ```svelte
152
+ <!-- Svelte 5.29+ -->
153
+ <div data-aos="fade" {@attach aosAttachment({ animation: 'fade', easing: 'ease-out-back' })}>
154
+
155
+ <!-- Svelte 4.x to 5.28 -->
156
+ <div data-aos="fade" use:aosAction={{ animation: 'fade', easing: 'ease-out-back' }}>
148
157
  ```
149
158
 
150
- ## Global Options
159
+ ### Animate Once
151
160
 
152
161
  ```svelte
153
- <main {@attach aosObserver({
154
- threshold: 0.1, // 0-1, visibility ratio to trigger
155
- offset: 120, // px from viewport edge
156
- duration: 600, // default duration (ms)
157
- delay: 0, // default delay (ms)
158
- easing: 'ease', // default easing
159
- once: false, // animate only once
160
- disable: false, // disable all animations
161
- anchorPlacement: 'top-bottom'
162
- })}>
162
+ <!-- Svelte 5.29+ -->
163
+ <div data-aos="fade" {@attach aosAttachment({ animation: 'fade', once: true })}>
164
+
165
+ <!-- Svelte 4.x to 5.28 -->
166
+ <div data-aos="fade" use:aosAction={{ animation: 'fade', once: true }}>
163
167
  ```
164
168
 
165
- ### Disable Option
169
+ ### Staggered Animation
166
170
 
167
- ```js
168
- // Disable on device type
169
- aosObserver({ disable: 'mobile' }); // 'mobile' | 'tablet' | 'desktop'
171
+ ```svelte
172
+ <div data-aos="fade-up" {@attach aosAttachment({ animation: 'fade-up', delay: 0 })}>First</div>
173
+ <div data-aos="fade-up" {@attach aosAttachment({ animation: 'fade-up', delay: 100 })}>Second</div>
174
+ <div data-aos="fade-up" {@attach aosAttachment({ animation: 'fade-up', delay: 200 })}>Third</div>
175
+ ```
170
176
 
171
- // Disable entirely
172
- aosObserver({ disable: true });
177
+ ## Available Easings
173
178
 
174
- // Custom condition
175
- aosObserver({ disable: () => window.innerWidth < 768 });
179
+ ```
180
+ linear ease ease-in ease-out ease-in-out
181
+ ease-in-back ease-out-back ease-in-out-back
182
+ ease-in-sine ease-out-sine ease-in-out-sine
183
+ ease-in-quad ease-out-quad ease-in-out-quad
184
+ ease-in-cubic ease-out-cubic ease-in-out-cubic
185
+ ease-in-quart ease-out-quart ease-in-out-quart
176
186
  ```
177
187
 
178
- ## API
188
+ ## API Reference
179
189
 
180
- ### aosObserver(options?)
190
+ ### `<AOS />` Component
181
191
 
182
- Svelte 5 attachment. Observes all `[data-aos]` elements inside the container, this is where all the magic happens, it uses a mutation observer and an intersection observer under the hood.
192
+ Add once at the top level of your page or layout. Configures global defaults.
183
193
 
184
194
  ```svelte
185
- <main {@attach aosObserver({ once: true })}>
186
- <div data-aos="fade">...</div>
187
- </main>
195
+ <AOS
196
+ options={{
197
+ offset: 120, // Offset from viewport edge (px)
198
+ delay: 0, // Default delay before animation (ms)
199
+ duration: 400, // Default animation duration (ms)
200
+ easing: 'ease', // Default easing function
201
+ once: false, // Animate only once per element
202
+ anchorPlacement: 'top-bottom', // Which position triggers animation
203
+ disable: false, // Disable all animations
204
+ threshold: 0.1 // IntersectionObserver visibility threshold (0-1)
205
+ }}
206
+ />
188
207
  ```
189
208
 
190
- ### aosAction
209
+ ### `aosAttachment` (Svelte 5.29+)
191
210
 
192
- Svelte action (use: directive). Same options as `aosObserver`.
211
+ Svelte 5 attachment for individual elements.
193
212
 
194
213
  ```svelte
195
- <main use:aosAction={{ threshold: 0.2 }}>
196
- <div data-aos="fade">...</div>
197
- </main>
214
+ <div
215
+ data-aos="fade-up"
216
+ {@attach aosAttachment({
217
+ animation: 'fade-up',
218
+ delay: 150,
219
+ duration: 400,
220
+ once: true
221
+ })}
222
+ >
223
+ Animated
224
+ </div>
198
225
  ```
199
226
 
200
- ### toAosAttributes(options)
227
+ ### `aosAction` (Svelte 4.x to 5.28)
201
228
 
202
- Generate attributes programmatically:
229
+ Svelte action for individual elements.
203
230
 
204
231
  ```svelte
205
- <script>
206
- import { toAosAttributes } from 'svelte-aos';
207
-
208
- const attrs = toAosAttributes({
209
- animation: 'fade-up',
210
- delay: 150,
211
- duration: 400,
212
- once: true
213
- });
214
- </script>
232
+ <div
233
+ data-aos="fade-up"
234
+ use:aosAction={{
235
+ animation: 'fade-up',
236
+ delay: 150,
237
+ duration: 400,
238
+ once: true
239
+ }}
240
+ >
241
+ Animated
242
+ </div>
243
+ ```
215
244
 
216
- <div {...attrs}>Animated</div>
245
+ > 💡 **Note:** The `data-aos` attribute is only needed if you want the element to animate on first page load when already visible in the viewport.
217
246
 
218
- <!-- Equivalent to: -->
219
- <div data-aos="fade-up" data-aos-delay="150" data-aos-duration="400" data-aos-once>
247
+ ### Disable Option
248
+
249
+ Disable all animations globally via the `<AOS />` component:
250
+
251
+ ```svelte
252
+ <AOS
253
+ options={{
254
+ // Disable on specific device type
255
+ disable: 'mobile' // 'mobile' | 'phone' | 'tablet'
256
+
257
+ // Or disable entirely
258
+ disable: true
259
+
260
+ // Or use a custom function
261
+ disable: () => window.innerWidth < 768
262
+ }}
263
+ />
220
264
  ```
221
265
 
222
- ## CSS Files
266
+ ## Selective CSS Imports
267
+
268
+ Only import the animations you use to reduce bundle size:
269
+
270
+ | File | Animations | Required |
271
+ | ------------- | ----------------------------------------- | ----------- |
272
+ | `base.css` | Core setup, CSS variables, reduced motion | ✅ Yes |
273
+ | `fade.css` | fade, fade-up, fade-down, etc. | Optional |
274
+ | `zoom.css` | zoom-in, zoom-out variants | Optional |
275
+ | `flip.css` | flip-up, flip-down, etc. | Optional |
276
+ | `slide.css` | slide-up, slide-down, etc. | Optional |
277
+ | `easings.css` | Custom easing functions | Optional |
278
+ | `full.css` | Everything bundled | Convenience |
223
279
 
224
- | File | Contents |
225
- | ------------- | ---------------------------------------- |
226
- | `base.css` | Core setup, CSS variables (**required**) |
227
- | `fade.css` | Fade animations |
228
- | `zoom.css` | Zoom animations |
229
- | `flip.css` | Flip animations |
230
- | `slide.css` | Slide animations |
231
- | `easings.css` | Custom easing functions |
232
- | `full.css` | All of the above |
280
+ **Example: Only fade animations with custom easings**
281
+
282
+ ```js
283
+ import 'svelte-aos/styles/base.css';
284
+ import 'svelte-aos/styles/fade.css';
285
+ import 'svelte-aos/styles/easings.css';
286
+ ```
233
287
 
234
288
  ## Examples
235
289
 
@@ -237,68 +291,43 @@ Generate attributes programmatically:
237
291
 
238
292
  ```svelte
239
293
  <script>
240
- import { aosObserver } from 'svelte-aos';
294
+ import { aosAttachment, AOS } from 'svelte-aos';
241
295
  import 'svelte-aos/styles/full.css';
242
296
  </script>
243
297
 
244
- <main {@attach aosObserver()}>
298
+ <AOS config={{ once: true, disable: 'mobile' }} />
299
+
300
+ <main>
245
301
  <section>
246
- <h1 data-aos="fade-down">Welcome</h1>
247
- <p data-aos="fade-up" data-aos-delay="100">Scroll to see more</p>
302
+ <h1 data-aos="fade-down" {@attach aosAttachment({ animation: 'fade-down' })}>Welcome</h1>
303
+ <p data-aos="fade-up" {@attach aosAttachment({ animation: 'fade-up', delay: 100 })}>
304
+ Scroll to see more
305
+ </p>
248
306
  </section>
249
307
 
250
308
  <section>
251
- <div data-aos="fade-right">Card 1</div>
252
- <div data-aos="fade-right" data-aos-delay="100">Card 2</div>
253
- <div data-aos="fade-right" data-aos-delay="200">Card 3</div>
309
+ <div data-aos="fade-right" {@attach aosAttachment({ animation: 'fade-right' })}>Card 1</div>
310
+ <div data-aos="fade-right" {@attach aosAttachment({ animation: 'fade-right', delay: 100 })}>
311
+ Card 2
312
+ </div>
313
+ <div data-aos="fade-right" {@attach aosAttachment({ animation: 'fade-right', delay: 200 })}>
314
+ Card 3
315
+ </div>
254
316
  </section>
255
317
  </main>
256
318
  ```
257
319
 
258
- ### Animate Once
259
-
260
- ```svelte
261
- <main {@attach aosObserver({ once: true })}>
262
- <div data-aos="zoom-in">Animates once, stays visible</div>
263
- </main>
264
- ```
265
-
266
- ### With Dynamic Content
320
+ ### With Custom Easing
267
321
 
268
322
  ```svelte
269
- <script>
270
- import { aosObserver } from 'svelte-aos';
271
- import 'svelte-aos/styles/full.css';
272
-
273
- let items = $state([]);
274
-
275
- async function loadMore() {
276
- items = [...items, ...(await fetchItems())];
277
- }
278
- </script>
279
-
280
- <!-- MutationObserver auto-detects new [data-aos] elements -->
281
- <main {@attach aosObserver()}>
282
- {#each items as item, i}
283
- <div data-aos="fade-up" data-aos-delay={i * 50}>{item.name}</div>
284
- {/each}
285
- <button onclick={loadMore}>Load More</button>
286
- </main>
323
+ <div data-aos="zoom-in" {@attach aosAttachment({ animation: 'zoom-in', easing: 'ease-out-back' })}>
324
+ Bouncy zoom effect
325
+ </div>
287
326
  ```
288
327
 
289
- ### Minimal Bundle (Fade Only)
328
+ ### Contributing
290
329
 
291
- ```svelte
292
- <script>
293
- import { aosObserver } from 'svelte-aos';
294
- import 'svelte-aos/styles/base.css';
295
- import 'svelte-aos/styles/fade.css';
296
- </script>
297
-
298
- <main {@attach aosObserver()}>
299
- <div data-aos="fade-up">Only fade animations available</div>
300
- </main>
301
- ```
330
+ Contributions, issues, and feature requests are welcome! No formal contribution process is set up yet, but feel free to open issues or submit pull requests.
302
331
 
303
332
  ## License
304
333
 
@@ -1,7 +1,7 @@
1
1
  <script lang="ts">
2
2
  import { onMount } from 'svelte';
3
3
  import { initAOS } from './aos.js';
4
- import type { AOSOptions } from '../types.js';
4
+ import type { AOSOptions } from './types.js';
5
5
 
6
6
  export let options: AOSOptions = {};
7
7
 
@@ -1,4 +1,4 @@
1
- import type { AOSOptions } from '../types.ts';
1
+ import type { AOSOptions } from './types.ts';
2
2
  interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
3
3
  new (options: import('svelte').ComponentConstructorOptions<Props>): import('svelte').SvelteComponent<Props, Events, Slots> & {
4
4
  $$bindings?: Bindings;
package/dist/aos.d.ts CHANGED
@@ -1,70 +1,15 @@
1
- import type { AnimationType, DisableOption, EasingType, AnchorPlacement } from './types.ts';
2
- /**
3
- * Create a Svelte attachment that observes elements with `data-aos` and toggles
4
- * animation classes when they intersect the viewport.
5
- *
6
- * The attachment will:
7
- * - Apply per-element CSS variables (duration/delay) from `data-aos-*` attributes
8
- * or from the `params` defaults.
9
- * - Add the `.aos-animate` class when the element meets the `threshold`/`rootMargin` criteria.
10
- * - Remove `.aos-animate` on exit unless `once` is true (or element has `data-aos-once="true").
11
- * - If `once` is true, the element will be unobserved after its first entry.
12
- * - Applies `params.easing` to elements that do not have `data-aos-easing` set.
13
- *
14
- * @param {Object} [params] - Global observer and animation options.
15
- */
16
- export declare function aosObserver(params?: {
1
+ import type { AnimationType, AOSOptions, EasingType } from './types.ts';
2
+ export declare function initAOS(options?: AOSOptions): void;
3
+ export type AOSElementOptions = {
4
+ animation?: AnimationType;
17
5
  duration?: number;
18
- threshold?: number;
19
6
  delay?: number;
20
- once?: boolean;
21
- disable?: DisableOption;
22
- anchorPlacement?: AnchorPlacement;
23
- offset?: number;
24
7
  easing?: EasingType;
25
- }): (node: HTMLElement) => () => void;
26
- /**
27
- * This is to use if attachements are not available in your svelte setup.
28
- * @param node - HTML element to attach AOS behavior to
29
- *
30
- * @param params - AOS options
31
- * @returns
32
- */
33
- export declare function aosAction(node: HTMLElement, params?: {
34
- duration?: number;
35
- threshold?: number;
36
- delay?: number;
37
8
  once?: boolean;
38
- disable?: DisableOption;
39
- anchorPlacement?: AnchorPlacement;
40
- offset?: number;
41
- easing?: EasingType;
42
- }): {
9
+ distance?: string;
10
+ };
11
+ export declare function aosAttachment(options?: AOSElementOptions): (node: HTMLElement) => () => void | undefined;
12
+ export declare function aosAction(node: HTMLElement, options?: AOSElementOptions): {
43
13
  update: () => void;
44
14
  destroy(): void;
45
15
  };
46
- /**
47
- * Convert a small configuration object into a map of `data-aos-*` attributes.
48
- *
49
- * Useful for demos or programmatically attaching attributes to an element (e.g. in Svelte
50
- * you can spread the returned object with `{...toAosAttributes({...})}`).
51
- *
52
- * @param {Object} options - Per-element options
53
- * @param {number} [options.delay] - Delay in ms
54
- * @param {number} [options.duration] - Duration in ms
55
- * @param {EasingType} [options.easing] - Easing name
56
- * @param {AnimationType} [options.animation] - Animation name
57
- * @param {boolean} [options.once] - If true, set `data-aos-once` on the element
58
- * @returns {Record<string,string>} Map of attributes (keys are attribute names, values are strings)
59
- *
60
- * @example
61
- * const attrs = toAosAttributes({ animation: 'fade-up', delay: 150, duration: 400 });
62
- * // => { 'data-aos': 'fade-up', 'data-aos-delay': '150', 'data-aos-duration': '400' }
63
- */
64
- export declare function toAosAttributes(options: {
65
- delay?: number;
66
- duration?: number;
67
- easing?: EasingType;
68
- animation?: AnimationType;
69
- once?: boolean;
70
- }): Record<string, string>;