@onsvisual/svelte-components 1.0.53 → 1.0.54

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,6 +1,4 @@
1
1
  <script>
2
- import { onMount, onDestroy } from "svelte";
3
-
4
2
  /**
5
3
  * Bind to this variable to monitor whether the component has entered the viewport (boolean true/false).
6
4
  * @type {boolean}
@@ -17,34 +15,31 @@
17
15
  */
18
16
  export let rootMargin = 200;
19
17
 
20
- let el, observer;
21
-
22
18
  const callback = (entries, observer) => {
23
19
  entries.forEach((entry) => {
20
+ console.log(entry);
24
21
  let intersecting = entry.isIntersecting;
25
22
  if (!entered && intersecting) {
26
- observer.unobserve(el);
23
+ observer.unobserve(entry.target);
27
24
  entered = true;
28
25
  }
29
26
  });
30
27
  };
31
28
 
32
- onMount(() => {
29
+ function initObserver(el) {
33
30
  let options = { root: document, rootMargin: `${rootMargin}px` };
34
31
 
35
- observer = new IntersectionObserver(callback, options);
32
+ const observer = new IntersectionObserver(callback, options);
36
33
  observer.observe(el);
37
- });
38
34
 
39
- onDestroy(() => {
40
- if (el && observer) {
41
- observer.unobserve(el);
42
- }
43
- });
35
+ return {
36
+ destroy: () => observer?.unobserve?.(el)
37
+ };
38
+ }
44
39
  </script>
45
40
 
46
41
  {#if entered}
47
42
  <slot />
48
43
  {:else}
49
- <div bind:this={el} style:height="{+initialHeight || 100}px"></div>
44
+ <div use:initObserver style:height="{+initialHeight || 100}px"></div>
50
45
  {/if}
@@ -1,6 +1,6 @@
1
- A wrapper component that uses IntersectionObserver to allow the elements it contains to be lazy loaded, ie. to be initialised/mounted only when they enter the viewport (or come close to it).
1
+ A wrapper component that uses IntersectionObserver to allow the Svelte components or HTML elements it contains to be lazy loaded, ie. to be added to the page (or DOM) only when they enter the viewport.
2
2
 
3
- Optionally, you can use the **initialHeight** to set a placeholder height in pixels (default 400), and you can use **rootMargin** to set how many pixels from the viewport you want lazy loading to kick in (default 200).
3
+ Optionally, you can use the **initialHeight** to set a placeholder height in pixels (default 400), and you can use **rootMargin** to set how many pixels before the viewport you want lazy loading to kick in (default 200).
4
4
 
5
5
  <!-- prettier-ignore -->
6
6
  ```html
@@ -1,5 +1,5 @@
1
1
  <script>
2
- import { onMount, onDestroy, createEventDispatcher } from "svelte";
2
+ import { createEventDispatcher } from "svelte";
3
3
 
4
4
  const dispatch = createEventDispatcher();
5
5
 
@@ -14,8 +14,6 @@
14
14
  */
15
15
  export let rootMargin = 0;
16
16
 
17
- let el, observer;
18
-
19
17
  const callback = (entries) => {
20
18
  entries.forEach((entry) => {
21
19
  let intersecting = entry.isIntersecting;
@@ -30,16 +28,18 @@
30
28
  });
31
29
  };
32
30
 
33
- onMount(() => {
31
+ function initObserver(el) {
34
32
  let options = { root: document, rootMargin: `${rootMargin}px` };
35
33
 
36
- observer = new IntersectionObserver(callback, options);
34
+ const observer = new IntersectionObserver(callback, options);
37
35
  observer.observe(el);
38
- });
39
36
 
40
- onDestroy(() => observer?.unobserve?.(el));
37
+ return {
38
+ destroy: () => observer?.unobserve?.(el)
39
+ };
40
+ }
41
41
  </script>
42
42
 
43
- <div bind:this={el}>
43
+ <div use:initObserver>
44
44
  <slot />
45
45
  </div>
@@ -4,16 +4,21 @@ This component can be useful for triggering actions such as lazy loading of data
4
4
 
5
5
  This component has **enter** and **exit** events, triggered when it enters or leaves the viewport. It also has a **visible** binding which has a value of **true** when it is within the viewport.
6
6
 
7
+ In this demo, you can open your browser's developer console (**F12**) to see the logs for elements entering and existing the viewport.
8
+
7
9
  <!-- prettier-ignore -->
8
10
  ```html
9
11
  <script>
10
12
  import { Observe } from "@onsvisual/svelte-components";
11
13
 
14
+ let visible;
15
+
12
16
  const colors = ["yellow", "green", "blue", "purple", "red", "orange"];
13
17
  </script>
14
18
 
15
19
  {#each colors as color}
16
20
  <Observe
21
+ bind:visible
17
22
  on:enter={() => console.log(`${color} entered`)}
18
23
  on:exit={() => console.log(`${color} exited`)}>
19
24
  <div class="section" style:background={color} style:height="400px"/>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onsvisual/svelte-components",
3
- "version": "1.0.53",
3
+ "version": "1.0.54",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "npm run build:package && npm run build:docs",