svelte-intersection-observer 0.10.1 → 1.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.
@@ -1,7 +1,9 @@
1
1
  <script>
2
+ // @ts-check
3
+
2
4
  /**
3
5
  * The HTML Element to observe.
4
- * @type {HTMLElement}
6
+ * @type {null | HTMLElement}
5
7
  */
6
8
  export let element = null;
7
9
 
@@ -21,7 +23,7 @@
21
23
  /**
22
24
  * Specify the containing element.
23
25
  * Defaults to the browser viewport.
24
- * @type {HTMLElement}
26
+ * @type {null | HTMLElement}
25
27
  */
26
28
  export let root = null;
27
29
 
@@ -50,7 +52,10 @@
50
52
 
51
53
  const dispatch = createEventDispatcher();
52
54
 
55
+ /** @type {null | string} */
53
56
  let prevRootMargin = null;
57
+
58
+ /** @type {null | HTMLElement} */
54
59
  let prevElement = null;
55
60
 
56
61
  const initialize = () => {
@@ -61,7 +66,7 @@
61
66
  intersecting = _entry.isIntersecting;
62
67
  });
63
68
  },
64
- { root, rootMargin, threshold }
69
+ { root, rootMargin, threshold },
65
70
  );
66
71
  };
67
72
 
@@ -83,21 +88,21 @@
83
88
  if (entry.isIntersecting) {
84
89
  dispatch("intersect", entry);
85
90
 
86
- if (once) observer.unobserve(element);
91
+ if (element && once) observer?.unobserve(element);
87
92
  }
88
93
  }
89
94
 
90
95
  await tick();
91
96
 
92
97
  if (element !== null && element !== prevElement) {
93
- observer.observe(element);
98
+ observer?.observe(element);
94
99
 
95
- if (prevElement !== null) observer.unobserve(prevElement);
100
+ if (prevElement !== null) observer?.unobserve(prevElement);
96
101
  prevElement = element;
97
102
  }
98
103
 
99
104
  if (prevRootMargin && rootMargin !== prevRootMargin) {
100
- observer.disconnect();
105
+ observer?.disconnect();
101
106
  prevElement = null;
102
107
  initialize();
103
108
  }
@@ -0,0 +1,79 @@
1
+ import type { SvelteComponentTyped } from "svelte";
2
+
3
+ export default class extends SvelteComponentTyped<
4
+ {
5
+ /**
6
+ * The HTML Element to observe.
7
+ * @default null
8
+ */
9
+ element?: null | HTMLElement;
10
+
11
+ /**
12
+ * Set to `true` to unobserve the element
13
+ * after it intersects the viewport.
14
+ * @default false
15
+ */
16
+ once?: boolean;
17
+
18
+ /**
19
+ * `true` if the observed element
20
+ * is intersecting the viewport.
21
+ * @default false
22
+ */
23
+ intersecting?: boolean;
24
+
25
+ /**
26
+ * Specify the containing element.
27
+ * Defaults to the browser viewport.
28
+ * @default null
29
+ */
30
+ root?: null | HTMLElement;
31
+
32
+ /**
33
+ * Margin offset of the containing element.
34
+ * @default "0px"
35
+ */
36
+ rootMargin?: string;
37
+
38
+ /**
39
+ * Percentage of element visibility to trigger an event.
40
+ * Value must be a number between 0 and 1, or an array of numbers between 0 and 1.
41
+ * @default 0
42
+ */
43
+ threshold?: number | number[];
44
+
45
+ /**
46
+ * Observed element metadata.
47
+ * @default null
48
+ */
49
+ entry?: null | IntersectionObserverEntry;
50
+
51
+ /**
52
+ * `IntersectionObserver` instance.
53
+ * @default null
54
+ */
55
+ observer?: null | IntersectionObserver;
56
+ },
57
+ {
58
+ /**
59
+ * Dispatched when the element is first observed
60
+ * and also whenever an intersection event occurs.
61
+ */
62
+ observe: CustomEvent<IntersectionObserverEntry>;
63
+
64
+ /**
65
+ * Dispatched only when the element is intersecting the viewport.
66
+ * `event.detail.isIntersecting` will only be `true`
67
+ */
68
+ intersect: CustomEvent<
69
+ IntersectionObserverEntry & { isIntersecting: true }
70
+ >;
71
+ },
72
+ {
73
+ default: {
74
+ intersecting: boolean;
75
+ entry: null | IntersectionObserverEntry;
76
+ observer: IntersectionObserver;
77
+ };
78
+ }
79
+ > {}
package/README.md CHANGED
@@ -208,21 +208,6 @@ interface IntersectionObserverEntry {
208
208
 
209
209
  </details>
210
210
 
211
- ## Examples
212
-
213
- The [examples folder](examples/) contains sample set-ups.
214
-
215
- - [examples/sveltekit](examples/sveltekit)
216
- - [examples/vite](examples/vite)
217
- - [examples/rollup](examples/rollup)
218
- - [examples/webpack](examples/webpack)
219
-
220
- ## TypeScript support
221
-
222
- Svelte version 3.31 or greater is required to use this module with TypeScript.
223
-
224
- TypeScript definitions for this component are located in the [types folder](types/).
225
-
226
211
  ## Changelog
227
212
 
228
213
  [Changelog](CHANGELOG.md)
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export { default } from "./IntersectionObserver.svelte";
package/index.js ADDED
@@ -0,0 +1 @@
1
+ export { default } from "./IntersectionObserver.svelte";
package/package.json CHANGED
@@ -1,27 +1,26 @@
1
1
  {
2
2
  "name": "svelte-intersection-observer",
3
- "version": "0.10.1",
3
+ "version": "1.0.0",
4
4
  "license": "MIT",
5
5
  "description": "Detect if an element is in the viewport using the Intersection Observer API",
6
6
  "author": "Eric Liu (https://github.com/metonym)",
7
- "svelte": "./src/index.js",
8
- "main": "./lib/index.js",
9
- "module": "./lib/index.mjs",
10
- "types": "./types/index.d.ts",
11
- "scripts": {
12
- "dev": "rollup -cw",
13
- "build": "rollup -c",
14
- "prepack": "BUNDLE=true rollup -c",
15
- "test:types": "svelte-check --workspace test",
16
- "test:examples": "node test/examples",
17
- "format": "prettier --ignore-path .gitignore --write '.'"
18
- },
19
- "devDependencies": {
20
- "prettier": "^3.0.0",
21
- "prettier-plugin-svelte": "^3.0.0",
22
- "svelte": "^4.1.0",
23
- "svelte-check": "^3.4.6",
24
- "svelte-readme": "^3.6.3"
7
+ "type": "module",
8
+ "svelte": "./index.js",
9
+ "main": "./index.js",
10
+ "types": "./index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./index.d.ts",
14
+ "svelte": "./index.js"
15
+ },
16
+ "./*.svelte": {
17
+ "types": "./*.svelte.d.ts",
18
+ "import": "./*.svelte"
19
+ },
20
+ "./*": {
21
+ "types": "./*.d.ts",
22
+ "import": "./*.js"
23
+ }
25
24
  },
26
25
  "repository": {
27
26
  "type": "git",
@@ -30,16 +29,11 @@
30
29
  "homepage": "https://github.com/metonym/svelte-intersection-observer",
31
30
  "bugs": "https://github.com/metonym/svelte-intersection-observer/issues",
32
31
  "keywords": [
32
+ "svelte",
33
+ "svelte component",
33
34
  "intersection observer",
34
35
  "viewport",
35
36
  "lazy-loading",
36
- "conditional",
37
- "svelte",
38
- "svelte component"
39
- ],
40
- "files": [
41
- "lib",
42
- "src",
43
- "types"
37
+ "conditional"
44
38
  ]
45
- }
39
+ }
package/CHANGELOG.md DELETED
@@ -1,118 +0,0 @@
1
- # Changelog
2
-
3
- All notable changes to this project will be documented in this file.
4
-
5
- The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
- and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
-
8
- ## [0.10.1](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.10.1) - 2023-07-20
9
-
10
- **Fixes**
11
-
12
- - fix `threshold` prop type to be `number | number[]`
13
-
14
- ## [0.10.0](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.10.0) - 2021-12-29
15
-
16
- **Features**
17
-
18
- - mark `observer` for garbage collection after disconnecting
19
-
20
- **Documentation**
21
-
22
- - make prop descriptions consistent with docs
23
-
24
- **Refactoring**
25
-
26
- - omit redundant `null` from `element` and `root` types as `HTMLElement` is already nullable
27
-
28
- ## [0.9.2](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.9.2) - 2021-11-26
29
-
30
- **Documentation**
31
-
32
- - add `let:` directive example
33
- - update component prop descriptions
34
- - use Svelte syntax highlighting for `on:observe`, `on:intersect` examples
35
-
36
- ## [0.9.1](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.9.1) - 2021-10-25
37
-
38
- **Documentation**
39
-
40
- - update the "Once" example to include the `"svelte-intersection-observer"` import
41
- - rename example `svite` to `vite`
42
-
43
- **Refactoring**
44
-
45
- - inline `entry` prop typedef
46
- - remove `@event`, `@slot` artifacts used by sveld to generate initial TypeScript definitions
47
-
48
- ## [0.9.0](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.9.0) - 2021-10-05
49
-
50
- - improve TypeScript definitions for dispatched events
51
- - `on:observe`: `event.detail.isIntersecting` is a `boolean`
52
- - `on:intersect`: `event.detail.isIntersecting` can only be `true`
53
-
54
- ## [0.8.0](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.8.0) - 2021-09-02
55
-
56
- - use `.svelte.d.ts` extension for component TypeScript definition
57
-
58
- ## [0.7.1](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.7.1) - 2021-07-05
59
-
60
- **Documentation**
61
-
62
- - add description for basic usage
63
- - add `on:observe` example
64
- - explain difference between `on:observe` and `on:intersect`
65
- - document `IntersectionObserverEntry` interface
66
- - re-order prop table so that `once` and `intersecting` are more prominent
67
-
68
- ## [0.7.0](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.7.0) - 2021-04-23
69
-
70
- **Features**
71
-
72
- - re-initialize observer if `rootMargin` changes
73
-
74
- ## [0.6.1](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.6.1) - 2021-04-03
75
-
76
- **Fixes**
77
-
78
- - move intersection observer instantiation to `onMount` to work in hydration use cases
79
-
80
- ## [0.6.0](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.6.0) - 2021-02-24
81
-
82
- **Features**
83
-
84
- - export `observer` prop (type `IntersectionObserver`)
85
- - dispatch "intersect" event if the observed element `isIntersecting` the viewport
86
-
87
- ## [0.5.0](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.5.0) - 2021-01-20
88
-
89
- **Features**
90
-
91
- - Add support for Server-Side Rendering (SSR)
92
-
93
- ## [0.4.0](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.4.0) - 2020-12-20
94
-
95
- **Features**
96
-
97
- - Export `intersecting` prop
98
-
99
- **Fixes**
100
-
101
- - Remove observer from module context to allow multiple component instantiations
102
- - Fix prop type for `entry`
103
-
104
- ## [0.3.0](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.3.0) - 2020-11-23
105
-
106
- - Export `entry` as a reactive prop
107
-
108
- ## [0.2.0](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.2.0) - 2020-11-18
109
-
110
- - Add TypeScript definitions
111
-
112
- ## [0.1.1](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.1.1) - 2020-04-05
113
-
114
- - Only `disconnect` observer in `onDestroy` lifecycle method
115
-
116
- ## [0.1.0](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.1.0) - 2020-04-05
117
-
118
- - Initial release