@castlenine/svelte-aoe 1.5.0 → 2.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
@@ -23,25 +23,7 @@ npm i @castlenine/svelte-aoe
23
23
 
24
24
  ## Setup
25
25
 
26
- - Import the package
27
-
28
- ```svelte
29
- import AnimateOnEnter from '@castlenine/svelte-aoe';
30
- ```
31
-
32
- - Add the component to your layout/page/component.
33
-
34
- ```svelte
35
- <AnimateOnEnter />
36
- ```
37
-
38
- - Add a `data-aoe` attribute to the element that you want to animate and define an animation.
39
-
40
- ```svelte
41
- <img data-aoe="fade-up" src="https://dummyimage.com/500x300"/>
42
- ```
43
-
44
- ## Example: SvelteKit Global Setup
26
+ 1. Import and add the component **once** in your root layout:
45
27
 
46
28
  File: `src/routes/+layout.svelte`
47
29
 
@@ -55,6 +37,14 @@ File: `src/routes/+layout.svelte`
55
37
  <slot />
56
38
  ```
57
39
 
40
+ 2. Add a `data-aoe` attribute to any element you want to animate:
41
+
42
+ ```svelte
43
+ <img data-aoe="fade-up" src="https://dummyimage.com/500x300"/>
44
+ ```
45
+
46
+ Elements are automatically detected, including those added dynamically by page navigation.
47
+
58
48
  ## Animations
59
49
 
60
50
  ### Normal speed
@@ -153,6 +143,71 @@ You can add your own animations by following the same pattern in your CSS.
153
143
 
154
144
  `threshold` is either a single number or an array of numbers which indicate at what percentage of the target's visibility the observer's callback should be executed. A value of `0.0` or `0` indicates that even a single pixel of the target is visible. A value of `1.0` or `1` indicates that the target is completely visible. Defaults to `0.3` (30%).
155
145
 
146
+ ## Scoped Overrides
147
+
148
+ Set defaults for all `data-aoe` elements within a page or section using `data-aoe-scope` on a container element, or the `<AnimateOnEnterScope>` convenience component. Elements inside a scope inherit its values unless they have their own per-element `data-aoe-*` overrides.
149
+
150
+ ### Using `data-aoe-scope` attribute
151
+
152
+ ```svelte
153
+ <div data-aoe-scope data-aoe-threshold="0.5" data-aoe-root-margin="20px">
154
+ <!-- All elements inside use threshold=0.5 and rootMargin=20px -->
155
+ <img data-aoe="fade-up" src="..." />
156
+
157
+ <!-- Per-element override still takes priority -->
158
+ <img data-aoe="fade-up" data-aoe-threshold="0.8" src="..." />
159
+ </div>
160
+ ```
161
+
162
+ ### Using `<AnimateOnEnterScope>` component
163
+
164
+ ```svelte
165
+ <script>
166
+ import { AnimateOnEnterScope } from '@castlenine/svelte-aoe';
167
+ </script>
168
+
169
+ <AnimateOnEnterScope threshold={0.5} rootMargin="20px">
170
+ <img data-aoe="fade-up" src="..." />
171
+ </AnimateOnEnterScope>
172
+ ```
173
+
174
+ | Property name | Type | Default value |
175
+ | ------------- | -------------------------------------------- | ------------- |
176
+ | `root` | CSS selector `string` | `undefined` |
177
+ | `rootMargin` | `string` in pixel (`px`) or percentage (`%`) | `undefined` |
178
+ | `threshold` | `number` between `0` and `1.0` | `undefined` |
179
+
180
+ Scopes can be nested. The innermost scope wins.
181
+
182
+ ## Per-Element Overrides
183
+
184
+ Individual elements can override scope or global properties using `data-aoe-*` attributes. If an attribute is not set, the value from the nearest `data-aoe-scope` ancestor is used, then `<AnimateOnEnter />`, then the package default.
185
+
186
+ | Attribute | Type | Overrides |
187
+ | -------------------- | ---------------------------------------------- | ------------ |
188
+ | `data-aoe-root` | CSS selector `string` | `root` |
189
+ | `data-aoe-root-margin` | `string` in pixel (`px`) or percentage (`%`) | `rootMargin` |
190
+ | `data-aoe-threshold` | `number` between `0` and `1.0` | `threshold` |
191
+
192
+ ### Examples
193
+
194
+ ```svelte
195
+ <!-- Uses global defaults -->
196
+ <img data-aoe="fade-up" src="..." />
197
+
198
+ <!-- Overrides threshold only (animates as soon as 1px is visible) -->
199
+ <img data-aoe="fade-up" data-aoe-threshold="0" src="..." />
200
+
201
+ <!-- Overrides rootMargin only (triggers 200px before entering viewport) -->
202
+ <img data-aoe="fade-up" data-aoe-root-margin="200px" src="..." />
203
+
204
+ <!-- Overrides root to a specific scroll container -->
205
+ <img data-aoe="fade-up" data-aoe-root="#scroll-container" src="..." />
206
+
207
+ <!-- Multiple overrides -->
208
+ <img data-aoe="fade-up" data-aoe-threshold="0.8" data-aoe-root-margin="50px" src="..." />
209
+ ```
210
+
156
211
  ---
157
212
 
158
213
  Inspired by [Animate on Scroll](https://michalsnik.github.io/aos/) and forked from [lliamscholtz/svelte-aoe](https://github.com/lliamscholtz/svelte-aoe)
@@ -0,0 +1,27 @@
1
+ <!--
2
+ @component
3
+ ### AnimateOnEnterScope
4
+ Convenience wrapper that sets `data-aoe-scope` defaults for all `data-aoe` descendants.
5
+ Elements inside the scope inherit these values unless they have their own `data-aoe-*` overrides.
6
+
7
+ &nbsp;
8
+
9
+ @param {string | undefined} `root` - CSS selector for the root element used as the viewport.
10
+ @param {string | undefined} `rootMargin` - Margin around the root (px or %).
11
+ @param {number | undefined} `threshold` - Visibility percentage (0 to 1) to trigger animations.
12
+ -->
13
+
14
+ <script>export let root = void 0;
15
+ export let rootMargin = void 0;
16
+ export let threshold = void 0;
17
+ </script>
18
+
19
+ <div
20
+ {...$$restProps}
21
+ data-aoe-scope
22
+ data-aoe-root={root}
23
+ data-aoe-root-margin={rootMargin}
24
+ data-aoe-threshold={threshold != null ? String(threshold) : undefined}
25
+ >
26
+ <slot />
27
+ </div>
@@ -0,0 +1,33 @@
1
+ import { SvelteComponentTyped } from "svelte";
2
+ declare const __propDef: {
3
+ props: {
4
+ [x: string]: any;
5
+ root?: string | undefined;
6
+ rootMargin?: string | undefined;
7
+ threshold?: number | undefined;
8
+ };
9
+ events: {
10
+ [evt: string]: CustomEvent<any>;
11
+ };
12
+ slots: {
13
+ default: {};
14
+ };
15
+ };
16
+ export type AnimateOnEnterScopeProps = typeof __propDef.props;
17
+ export type AnimateOnEnterScopeEvents = typeof __propDef.events;
18
+ export type AnimateOnEnterScopeSlots = typeof __propDef.slots;
19
+ /**
20
+ * ### AnimateOnEnterScope
21
+ * Convenience wrapper that sets `data-aoe-scope` defaults for all `data-aoe` descendants.
22
+ * Elements inside the scope inherit these values unless they have their own `data-aoe-*` overrides.
23
+ *
24
+ * &nbsp;
25
+ *
26
+ * @param {string | undefined} `root` - CSS selector for the root element used as the viewport.
27
+ * @param {string | undefined} `rootMargin` - Margin around the root (px or %).
28
+ * @param {number | undefined} `threshold` - Visibility percentage (0 to 1) to trigger animations.
29
+ */
30
+ export default class AnimateOnEnterScope extends SvelteComponentTyped<AnimateOnEnterScopeProps, AnimateOnEnterScopeEvents, AnimateOnEnterScopeSlots> {
31
+ }
32
+ export {};
33
+ //# sourceMappingURL=AnimateOnEnterScope.svelte.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AnimateOnEnterScope.svelte.d.ts","sourceRoot":"","sources":["../src/lib/AnimateOnEnterScope.svelte.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,QAAQ,CAE5C;AAeD,QAAA,MAAM,SAAS;;;eADyE,MAAM,GAAG,SAAS;qBAAe,MAAM,GAAG,SAAS;oBAAc,MAAM,GAAG,SAAS;;;;;;;;CAC3F,CAAC;AACjF,MAAM,MAAM,wBAAwB,GAAG,OAAO,SAAS,CAAC,KAAK,CAAC;AAC9D,MAAM,MAAM,yBAAyB,GAAG,OAAO,SAAS,CAAC,MAAM,CAAC;AAChE,MAAM,MAAM,wBAAwB,GAAG,OAAO,SAAS,CAAC,KAAK,CAAC;AAE9D;;;;;;;;;;GAUG;AACH,MAAM,CAAC,OAAO,OAAO,mBAAoB,SAAQ,oBAAoB,CAAC,wBAAwB,EAAE,yBAAyB,EAAE,wBAAwB,CAAC;CACnJ"}
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  import AnimateOnEnter from './index.svelte';
2
2
  export default AnimateOnEnter;
3
+ export { default as AnimateOnEnterScope } from './AnimateOnEnterScope.svelte';
3
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,gBAAgB,CAAC;AAE5C,eAAe,cAAc,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,gBAAgB,CAAC;AAE5C,eAAe,cAAc,CAAC;AAC9B,OAAO,EAAE,OAAO,IAAI,mBAAmB,EAAE,MAAM,8BAA8B,CAAC"}
package/dist/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  import AnimateOnEnter from './index.svelte';
2
2
  export default AnimateOnEnter;
3
+ export { default as AnimateOnEnterScope } from './AnimateOnEnterScope.svelte';
package/dist/index.svelte CHANGED
@@ -1,30 +1,115 @@
1
- <script>import "./animations.css";
2
- import { onMount } from "svelte";
3
- export let root = null;
4
- export let rootMargin = "0px";
5
- export let threshold = 0.3;
6
- onMount(() => {
7
- const OBSERVER = new IntersectionObserver((entries) => {
8
- entries.forEach((entry) => {
9
- if (entry.isIntersecting) {
10
- entry.target.classList.add("aoe");
11
- OBSERVER.unobserve(entry.target);
12
- }
13
- });
14
- }, { root, rootMargin, threshold });
15
- document.querySelectorAll("[data-aoe]").forEach((el) => OBSERVER.observe(el));
16
- return () => OBSERVER.disconnect();
17
- });
18
- </script>
19
-
20
1
  <!--
21
2
  @component
22
- ### AnimationOnEnter (AOE)
3
+ ### AnimateOnEnter (AOE)
23
4
  This component is used to animate elements when they enter the viewport with the help of [Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API).
24
5
 
6
+ Place this component once in your root layout. Elements with `data-aoe` attributes will be automatically detected, including dynamically added elements from page navigation.
7
+
8
+ #### Configuration cascade
9
+ 1. Package defaults: `root: null`, `rootMargin: '0px'`, `threshold: 0.3`
10
+ 2. Component props: override package defaults globally
11
+ 3. `data-aoe-scope` on a container: override component props for a page or section
12
+ 4. Per-element `data-aoe-*` attributes: override scope or component props for individual elements
13
+
25
14
  &nbsp;
26
15
 
27
16
  @param {Document | Element | null | undefined} `root` - The element that is used as the viewport for checking visibility of the target. Must be the ancestor of the target. Defaults to the browser viewport if not specified or if `null`.
28
17
  @param {string | undefined} `rootMargin` - Margin around the root. Can have values similar to the CSS margin property, e.g. "10px 20px 30px 40px" (top, right, bottom, left). The values can be percentages. Defaults to '0px' (no margin).
29
18
  @param {number | number[]} `threshold` - Either a single number or an array of numbers which indicate at what percentage of the target's visibility the observer's callback should be executed. A value of 0.0 indicates that even a single pixel of the target is visible. A value of 1.0 indicates that the target is completely visible. Defaults to 0.3 (30%).
30
19
  -->
20
+
21
+ <script>import "./animations.css";
22
+ import { onMount } from "svelte";
23
+ export let root = null;
24
+ export let rootMargin = "0px";
25
+ export let threshold = 0.3;
26
+ onMount(() => {
27
+ const DEFAULT_ROOT = root !== null && root !== void 0 ? root : null;
28
+ const DEFAULT_ROOT_MARGIN = rootMargin !== null && rootMargin !== void 0 ? rootMargin : "0px";
29
+ const DEFAULT_THRESHOLD = threshold !== null && threshold !== void 0 ? threshold : 0.3;
30
+ const OBSERVERS = /* @__PURE__ */ new Map();
31
+ const REGISTERED = /* @__PURE__ */ new WeakSet();
32
+ function createObserver(options) {
33
+ const OBSERVER = new IntersectionObserver((entries) => {
34
+ for (const ENTRY of entries) {
35
+ if (ENTRY.isIntersecting) {
36
+ ENTRY.target.classList.add("aoe");
37
+ OBSERVER.unobserve(ENTRY.target);
38
+ }
39
+ }
40
+ }, options);
41
+ return OBSERVER;
42
+ }
43
+ function configKey(rootSelector, rm, th) {
44
+ const TH_STR = Array.isArray(th) ? th.join(",") : String(th);
45
+ return `${rootSelector !== null && rootSelector !== void 0 ? rootSelector : "null"}|${rm}|${TH_STR}`;
46
+ }
47
+ const DEFAULT_KEY = configKey(null, DEFAULT_ROOT_MARGIN, DEFAULT_THRESHOLD);
48
+ const DEFAULT_OBSERVER = createObserver({
49
+ root: DEFAULT_ROOT,
50
+ rootMargin: DEFAULT_ROOT_MARGIN,
51
+ threshold: DEFAULT_THRESHOLD
52
+ });
53
+ OBSERVERS.set(DEFAULT_KEY, DEFAULT_OBSERVER);
54
+ function getOrCreateObserver(key, options) {
55
+ let observer = OBSERVERS.get(key);
56
+ if (!observer) {
57
+ observer = createObserver(options);
58
+ OBSERVERS.set(key, observer);
59
+ }
60
+ return observer;
61
+ }
62
+ function resolveConfig(el) {
63
+ var _a, _b;
64
+ const ROOT_ATTR = el.getAttribute("data-aoe-root");
65
+ const ROOT_MARGIN_ATTR = el.getAttribute("data-aoe-root-margin");
66
+ const THRESHOLD_ATTR = el.getAttribute("data-aoe-threshold");
67
+ const SCOPE = el.closest("[data-aoe-scope]");
68
+ const ROOT_SELECTOR = (_a = ROOT_ATTR !== null && ROOT_ATTR !== void 0 ? ROOT_ATTR : SCOPE === null || SCOPE === void 0 ? void 0 : SCOPE.getAttribute("data-aoe-root")) !== null && _a !== void 0 ? _a : null;
69
+ const ROOT_MARGIN = (_b = ROOT_MARGIN_ATTR !== null && ROOT_MARGIN_ATTR !== void 0 ? ROOT_MARGIN_ATTR : SCOPE === null || SCOPE === void 0 ? void 0 : SCOPE.getAttribute("data-aoe-root-margin")) !== null && _b !== void 0 ? _b : DEFAULT_ROOT_MARGIN;
70
+ const RAW_THRESHOLD = THRESHOLD_ATTR !== null && THRESHOLD_ATTR !== void 0 ? THRESHOLD_ATTR : SCOPE === null || SCOPE === void 0 ? void 0 : SCOPE.getAttribute("data-aoe-threshold");
71
+ const PARSED = RAW_THRESHOLD != null ? parseFloat(RAW_THRESHOLD) : NaN;
72
+ const THRESHOLD = !isNaN(PARSED) ? PARSED : DEFAULT_THRESHOLD;
73
+ return { rootSelector: ROOT_SELECTOR, rootMargin: ROOT_MARGIN, threshold: THRESHOLD };
74
+ }
75
+ function registerElement(el) {
76
+ if (REGISTERED.has(el) || el.classList.contains("aoe"))
77
+ return;
78
+ REGISTERED.add(el);
79
+ const { rootSelector: ROOT_SELECTOR, rootMargin: ROOT_MARGIN, threshold: THRESHOLD } = resolveConfig(el);
80
+ if (!ROOT_SELECTOR && ROOT_MARGIN === DEFAULT_ROOT_MARGIN && THRESHOLD === DEFAULT_THRESHOLD) {
81
+ DEFAULT_OBSERVER.observe(el);
82
+ return;
83
+ }
84
+ const EL_ROOT = ROOT_SELECTOR ? document.querySelector(ROOT_SELECTOR) : DEFAULT_ROOT;
85
+ const KEY = configKey(ROOT_SELECTOR, ROOT_MARGIN, THRESHOLD);
86
+ const OBSERVER = getOrCreateObserver(KEY, {
87
+ root: EL_ROOT,
88
+ rootMargin: ROOT_MARGIN,
89
+ threshold: THRESHOLD
90
+ });
91
+ OBSERVER.observe(el);
92
+ }
93
+ document.querySelectorAll("[data-aoe]").forEach((el) => registerElement(el));
94
+ const MUTATION_OBSERVER = new MutationObserver((mutations) => {
95
+ for (const MUTATION of mutations) {
96
+ for (const NODE of MUTATION.addedNodes) {
97
+ if (NODE.nodeType !== Node.ELEMENT_NODE)
98
+ continue;
99
+ const el = NODE;
100
+ if (el.hasAttribute("data-aoe"))
101
+ registerElement(el);
102
+ el.querySelectorAll("[data-aoe]").forEach((child) => registerElement(child));
103
+ }
104
+ }
105
+ });
106
+ MUTATION_OBSERVER.observe(document.body, { childList: true, subtree: true });
107
+ return () => {
108
+ MUTATION_OBSERVER.disconnect();
109
+ for (const OBSERVER of OBSERVERS.values()) {
110
+ OBSERVER.disconnect();
111
+ }
112
+ OBSERVERS.clear();
113
+ };
114
+ });
115
+ </script>
@@ -15,9 +15,17 @@ export type IndexProps = typeof __propDef.props;
15
15
  export type IndexEvents = typeof __propDef.events;
16
16
  export type IndexSlots = typeof __propDef.slots;
17
17
  /**
18
- * ### AnimationOnEnter (AOE)
18
+ * ### AnimateOnEnter (AOE)
19
19
  * This component is used to animate elements when they enter the viewport with the help of [Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API).
20
20
  *
21
+ * Place this component once in your root layout. Elements with `data-aoe` attributes will be automatically detected, including dynamically added elements from page navigation.
22
+ *
23
+ * #### Configuration cascade
24
+ * 1. Package defaults: `root: null`, `rootMargin: '0px'`, `threshold: 0.3`
25
+ * 2. Component props: override package defaults globally
26
+ * 3. `data-aoe-scope` on a container: override component props for a page or section
27
+ * 4. Per-element `data-aoe-*` attributes: override scope or component props for individual elements
28
+ *
21
29
  * &nbsp;
22
30
  *
23
31
  * @param {Document | Element | null | undefined} `root` - The element that is used as the viewport for checking visibility of the target. Must be the ancestor of the target. Defaults to the browser viewport if not specified or if `null`.
@@ -1 +1 @@
1
- {"version":3,"file":"index.svelte.d.ts","sourceRoot":"","sources":["../src/lib/index.svelte.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,QAAQ,CAE5C;AACD,OAAO,kBAAkB,CAAC;AAqC1B,QAAA,MAAM,SAAS;;eADyE,QAAQ,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS;qBAAe,MAAM,GAAG,SAAS;oBAAc,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS;;;;;;CAChJ,CAAC;AAC1D,MAAM,MAAM,UAAU,GAAG,OAAO,SAAS,CAAC,KAAK,CAAC;AAChD,MAAM,MAAM,WAAW,GAAG,OAAO,SAAS,CAAC,MAAM,CAAC;AAClD,MAAM,MAAM,UAAU,GAAG,OAAO,SAAS,CAAC,KAAK,CAAC;AAEhD;;;;;;;;;GASG;AACH,MAAM,CAAC,OAAO,OAAO,KAAM,SAAQ,oBAAoB,CAAC,UAAU,EAAE,WAAW,EAAE,UAAU,CAAC;CAC3F"}
1
+ {"version":3,"file":"index.svelte.d.ts","sourceRoot":"","sources":["../src/lib/index.svelte.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,QAAQ,CAE5C;AACD,OAAO,kBAAkB,CAAC;AA6H1B,QAAA,MAAM,SAAS;;eADyE,QAAQ,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS;qBAAe,MAAM,GAAG,SAAS;oBAAc,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS;;;;;;CAChJ,CAAC;AAC1D,MAAM,MAAM,UAAU,GAAG,OAAO,SAAS,CAAC,KAAK,CAAC;AAChD,MAAM,MAAM,WAAW,GAAG,OAAO,SAAS,CAAC,MAAM,CAAC;AAClD,MAAM,MAAM,UAAU,GAAG,OAAO,SAAS,CAAC,KAAK,CAAC;AAEhD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,OAAO,OAAO,KAAM,SAAQ,oBAAoB,CAAC,UAAU,EAAE,WAAW,EAAE,UAAU,CAAC;CAC3F"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@castlenine/svelte-aoe",
3
- "version": "1.5.0",
3
+ "version": "2.1.0",
4
4
  "description": "A Svelte component to animate elements, with no dependencies.",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -45,7 +45,7 @@
45
45
  "build": "npm run check && vite build",
46
46
  "preview": "vite preview",
47
47
  "package": "npm run remove-dist-folder && svelte-kit sync && svelte-package && publint",
48
- "prepublishOnly": "npm run package",
48
+ "prepublish-only": "npm run package",
49
49
  "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
50
50
  "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
51
51
  "eslint": "eslint --ignore-path ./.eslintignore .",