svelte-intersection-observer 0.10.2 → 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,42 +1,27 @@
1
1
  {
2
2
  "name": "svelte-intersection-observer",
3
- "version": "0.10.2",
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",
7
+ "type": "module",
8
+ "svelte": "./index.js",
9
+ "main": "./index.js",
10
+ "types": "./index.d.ts",
11
11
  "exports": {
12
12
  ".": {
13
- "types": "./types/index.d.ts",
14
- "svelte": "./src/index.js"
13
+ "types": "./index.d.ts",
14
+ "svelte": "./index.js"
15
15
  },
16
- "./src/*.svelte": {
17
- "types": "./types/*.svelte.d.ts",
18
- "import": "./src/*.svelte"
16
+ "./*.svelte": {
17
+ "types": "./*.svelte.d.ts",
18
+ "import": "./*.svelte"
19
19
  },
20
- "./src/*": {
21
- "types": "./types/*.d.ts",
22
- "import": "./src/*.js"
20
+ "./*": {
21
+ "types": "./*.d.ts",
22
+ "import": "./*.js"
23
23
  }
24
24
  },
25
- "scripts": {
26
- "dev": "rollup -cw",
27
- "build": "rollup -c",
28
- "prepack": "BUNDLE=true rollup -c",
29
- "test:types": "svelte-check --workspace test",
30
- "test:examples": "node test/examples",
31
- "format": "prettier --ignore-path .gitignore --write '.'"
32
- },
33
- "devDependencies": {
34
- "prettier": "^3.0.0",
35
- "prettier-plugin-svelte": "^3.0.0",
36
- "svelte": "^4.1.0",
37
- "svelte-check": "^3.4.6",
38
- "svelte-readme": "^3.6.3"
39
- },
40
25
  "repository": {
41
26
  "type": "git",
42
27
  "url": "https://github.com/metonym/svelte-intersection-observer.git"
@@ -44,16 +29,11 @@
44
29
  "homepage": "https://github.com/metonym/svelte-intersection-observer",
45
30
  "bugs": "https://github.com/metonym/svelte-intersection-observer/issues",
46
31
  "keywords": [
32
+ "svelte",
33
+ "svelte component",
47
34
  "intersection observer",
48
35
  "viewport",
49
36
  "lazy-loading",
50
- "conditional",
51
- "svelte",
52
- "svelte component"
53
- ],
54
- "files": [
55
- "lib",
56
- "src",
57
- "types"
37
+ "conditional"
58
38
  ]
59
- }
39
+ }
package/CHANGELOG.md DELETED
@@ -1,124 +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.2](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.10.2) - 2023-07-20
9
-
10
- **Fixes**
11
-
12
- - add `exports` field to `package.json`
13
-
14
- ## [0.10.1](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.10.1) - 2023-07-20
15
-
16
- **Fixes**
17
-
18
- - fix `threshold` prop type to be `number | number[]`
19
-
20
- ## [0.10.0](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.10.0) - 2021-12-29
21
-
22
- **Features**
23
-
24
- - mark `observer` for garbage collection after disconnecting
25
-
26
- **Documentation**
27
-
28
- - make prop descriptions consistent with docs
29
-
30
- **Refactoring**
31
-
32
- - omit redundant `null` from `element` and `root` types as `HTMLElement` is already nullable
33
-
34
- ## [0.9.2](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.9.2) - 2021-11-26
35
-
36
- **Documentation**
37
-
38
- - add `let:` directive example
39
- - update component prop descriptions
40
- - use Svelte syntax highlighting for `on:observe`, `on:intersect` examples
41
-
42
- ## [0.9.1](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.9.1) - 2021-10-25
43
-
44
- **Documentation**
45
-
46
- - update the "Once" example to include the `"svelte-intersection-observer"` import
47
- - rename example `svite` to `vite`
48
-
49
- **Refactoring**
50
-
51
- - inline `entry` prop typedef
52
- - remove `@event`, `@slot` artifacts used by sveld to generate initial TypeScript definitions
53
-
54
- ## [0.9.0](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.9.0) - 2021-10-05
55
-
56
- - improve TypeScript definitions for dispatched events
57
- - `on:observe`: `event.detail.isIntersecting` is a `boolean`
58
- - `on:intersect`: `event.detail.isIntersecting` can only be `true`
59
-
60
- ## [0.8.0](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.8.0) - 2021-09-02
61
-
62
- - use `.svelte.d.ts` extension for component TypeScript definition
63
-
64
- ## [0.7.1](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.7.1) - 2021-07-05
65
-
66
- **Documentation**
67
-
68
- - add description for basic usage
69
- - add `on:observe` example
70
- - explain difference between `on:observe` and `on:intersect`
71
- - document `IntersectionObserverEntry` interface
72
- - re-order prop table so that `once` and `intersecting` are more prominent
73
-
74
- ## [0.7.0](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.7.0) - 2021-04-23
75
-
76
- **Features**
77
-
78
- - re-initialize observer if `rootMargin` changes
79
-
80
- ## [0.6.1](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.6.1) - 2021-04-03
81
-
82
- **Fixes**
83
-
84
- - move intersection observer instantiation to `onMount` to work in hydration use cases
85
-
86
- ## [0.6.0](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.6.0) - 2021-02-24
87
-
88
- **Features**
89
-
90
- - export `observer` prop (type `IntersectionObserver`)
91
- - dispatch "intersect" event if the observed element `isIntersecting` the viewport
92
-
93
- ## [0.5.0](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.5.0) - 2021-01-20
94
-
95
- **Features**
96
-
97
- - Add support for Server-Side Rendering (SSR)
98
-
99
- ## [0.4.0](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.4.0) - 2020-12-20
100
-
101
- **Features**
102
-
103
- - Export `intersecting` prop
104
-
105
- **Fixes**
106
-
107
- - Remove observer from module context to allow multiple component instantiations
108
- - Fix prop type for `entry`
109
-
110
- ## [0.3.0](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.3.0) - 2020-11-23
111
-
112
- - Export `entry` as a reactive prop
113
-
114
- ## [0.2.0](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.2.0) - 2020-11-18
115
-
116
- - Add TypeScript definitions
117
-
118
- ## [0.1.1](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.1.1) - 2020-04-05
119
-
120
- - Only `disconnect` observer in `onDestroy` lifecycle method
121
-
122
- ## [0.1.0](https://github.com/metonym/svelte-intersection-observer/releases/tag/v0.1.0) - 2020-04-05
123
-
124
- - Initial release