@castlenine/svelte-aoe 1.5.0 → 2.0.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,35 @@ 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
+ ## Per-Element Overrides
147
+
148
+ Individual elements can override the global properties using `data-aoe-*` attributes. If an attribute is not set, the value from `<AnimateOnEnter />` is used, which itself falls back to the package default.
149
+
150
+ | Attribute | Type | Overrides |
151
+ | -------------------- | ---------------------------------------------- | ------------ |
152
+ | `data-aoe-root` | CSS selector `string` | `root` |
153
+ | `data-aoe-root-margin` | `string` in pixel (`px`) or percentage (`%`) | `rootMargin` |
154
+ | `data-aoe-threshold` | `number` between `0` and `1.0` | `threshold` |
155
+
156
+ ### Examples
157
+
158
+ ```svelte
159
+ <!-- Uses global defaults -->
160
+ <img data-aoe="fade-up" src="..." />
161
+
162
+ <!-- Overrides threshold only (animates as soon as 1px is visible) -->
163
+ <img data-aoe="fade-up" data-aoe-threshold="0" src="..." />
164
+
165
+ <!-- Overrides rootMargin only (triggers 200px before entering viewport) -->
166
+ <img data-aoe="fade-up" data-aoe-root-margin="200px" src="..." />
167
+
168
+ <!-- Overrides root to a specific scroll container -->
169
+ <img data-aoe="fade-up" data-aoe-root="#scroll-container" src="..." />
170
+
171
+ <!-- Multiple overrides -->
172
+ <img data-aoe="fade-up" data-aoe-threshold="0.8" data-aoe-root-margin="50px" src="..." />
173
+ ```
174
+
156
175
  ---
157
176
 
158
177
  Inspired by [Animate on Scroll](https://michalsnik.github.io/aos/) and forked from [lliamscholtz/svelte-aoe](https://github.com/lliamscholtz/svelte-aoe)
package/dist/index.svelte CHANGED
@@ -1,30 +1,106 @@
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. Per-element `data-aoe-*` attributes: override component props for individual elements
12
+
25
13
  &nbsp;
26
14
 
27
15
  @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
16
  @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
17
  @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
18
  -->
19
+
20
+ <script>import "./animations.css";
21
+ import { onMount } from "svelte";
22
+ export let root = null;
23
+ export let rootMargin = "0px";
24
+ export let threshold = 0.3;
25
+ onMount(() => {
26
+ const DEFAULT_ROOT = root !== null && root !== void 0 ? root : null;
27
+ const DEFAULT_ROOT_MARGIN = rootMargin !== null && rootMargin !== void 0 ? rootMargin : "0px";
28
+ const DEFAULT_THRESHOLD = threshold !== null && threshold !== void 0 ? threshold : 0.3;
29
+ const OBSERVERS = /* @__PURE__ */ new Map();
30
+ const REGISTERED = /* @__PURE__ */ new WeakSet();
31
+ function createObserver(options) {
32
+ const OBSERVER = new IntersectionObserver((entries) => {
33
+ for (const ENTRY of entries) {
34
+ if (ENTRY.isIntersecting) {
35
+ ENTRY.target.classList.add("aoe");
36
+ OBSERVER.unobserve(ENTRY.target);
37
+ }
38
+ }
39
+ }, options);
40
+ return OBSERVER;
41
+ }
42
+ function configKey(rootSelector, rm, th) {
43
+ const TH_STR = Array.isArray(th) ? th.join(",") : String(th);
44
+ return `${rootSelector !== null && rootSelector !== void 0 ? rootSelector : "null"}|${rm}|${TH_STR}`;
45
+ }
46
+ const DEFAULT_KEY = configKey(null, DEFAULT_ROOT_MARGIN, DEFAULT_THRESHOLD);
47
+ const DEFAULT_OBSERVER = createObserver({
48
+ root: DEFAULT_ROOT,
49
+ rootMargin: DEFAULT_ROOT_MARGIN,
50
+ threshold: DEFAULT_THRESHOLD
51
+ });
52
+ OBSERVERS.set(DEFAULT_KEY, DEFAULT_OBSERVER);
53
+ function getOrCreateObserver(key, options) {
54
+ let observer = OBSERVERS.get(key);
55
+ if (!observer) {
56
+ observer = createObserver(options);
57
+ OBSERVERS.set(key, observer);
58
+ }
59
+ return observer;
60
+ }
61
+ function registerElement(el) {
62
+ if (REGISTERED.has(el) || el.classList.contains("aoe"))
63
+ return;
64
+ REGISTERED.add(el);
65
+ const ROOT_ATTR = el.getAttribute("data-aoe-root");
66
+ const ROOT_MARGIN_ATTR = el.getAttribute("data-aoe-root-margin");
67
+ const THRESHOLD_ATTR = el.getAttribute("data-aoe-threshold");
68
+ if (!ROOT_ATTR && !ROOT_MARGIN_ATTR && !THRESHOLD_ATTR) {
69
+ DEFAULT_OBSERVER.observe(el);
70
+ return;
71
+ }
72
+ const EL_ROOT = ROOT_ATTR ? document.querySelector(ROOT_ATTR) : DEFAULT_ROOT;
73
+ const EL_ROOT_MARGIN = ROOT_MARGIN_ATTR !== null && ROOT_MARGIN_ATTR !== void 0 ? ROOT_MARGIN_ATTR : DEFAULT_ROOT_MARGIN;
74
+ const PARSED_THRESHOLD = THRESHOLD_ATTR != null ? parseFloat(THRESHOLD_ATTR) : null;
75
+ const EL_THRESHOLD = PARSED_THRESHOLD != null && !isNaN(PARSED_THRESHOLD) ? PARSED_THRESHOLD : DEFAULT_THRESHOLD;
76
+ const KEY = configKey(ROOT_ATTR, EL_ROOT_MARGIN, EL_THRESHOLD);
77
+ const OBSERVER = getOrCreateObserver(KEY, {
78
+ root: EL_ROOT,
79
+ rootMargin: EL_ROOT_MARGIN,
80
+ threshold: EL_THRESHOLD
81
+ });
82
+ OBSERVER.observe(el);
83
+ }
84
+ document.querySelectorAll("[data-aoe]").forEach((el) => registerElement(el));
85
+ const MUTATION_OBSERVER = new MutationObserver((mutations) => {
86
+ for (const MUTATION of mutations) {
87
+ for (const NODE of MUTATION.addedNodes) {
88
+ if (NODE.nodeType !== Node.ELEMENT_NODE)
89
+ continue;
90
+ const el = NODE;
91
+ if (el.hasAttribute("data-aoe"))
92
+ registerElement(el);
93
+ el.querySelectorAll("[data-aoe]").forEach((child) => registerElement(child));
94
+ }
95
+ }
96
+ });
97
+ MUTATION_OBSERVER.observe(document.body, { childList: true, subtree: true });
98
+ return () => {
99
+ MUTATION_OBSERVER.disconnect();
100
+ for (const OBSERVER of OBSERVERS.values()) {
101
+ OBSERVER.disconnect();
102
+ }
103
+ OBSERVERS.clear();
104
+ };
105
+ });
106
+ </script>
@@ -15,9 +15,16 @@ 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. Per-element `data-aoe-*` attributes: override component props for individual elements
27
+ *
21
28
  * &nbsp;
22
29
  *
23
30
  * @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;AA8G1B,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;;;;;;;;;;;;;;;;GAgBG;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.0.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 .",