vue-use-intersection-observer 1.0.3 → 1.0.5
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
|
@@ -101,6 +101,8 @@ onBeforeUnmount(() => {
|
|
|
101
101
|
|
|
102
102
|

|
|
103
103
|
|
|
104
|
+

|
|
105
|
+
|
|
104
106
|
### 🛠 API
|
|
105
107
|
|
|
106
108
|
#### `useIntersectionObserver()`
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { inject as
|
|
1
|
+
import { inject as f, onBeforeUnmount as k } from "vue";
|
|
2
2
|
class u {
|
|
3
3
|
buckets = /* @__PURE__ */ new Map();
|
|
4
4
|
buildRootMargin(e = 0) {
|
|
@@ -9,8 +9,8 @@ class u {
|
|
|
9
9
|
return `${e}|${t}`;
|
|
10
10
|
}
|
|
11
11
|
getBucket(e = 0, s = 0) {
|
|
12
|
-
const t = this.getKey(e, s),
|
|
13
|
-
if (
|
|
12
|
+
const t = this.getKey(e, s), r = this.buckets.get(t);
|
|
13
|
+
if (r) return r;
|
|
14
14
|
const o = {
|
|
15
15
|
elements: /* @__PURE__ */ new Map(),
|
|
16
16
|
observer: new IntersectionObserver(
|
|
@@ -19,7 +19,7 @@ class u {
|
|
|
19
19
|
const b = o.elements.get(c.target);
|
|
20
20
|
if (!b) continue;
|
|
21
21
|
const l = c.isIntersecting;
|
|
22
|
-
b.callback(l, c),
|
|
22
|
+
l && (b.callback(l, c), b.once && this.unobserve(c.target));
|
|
23
23
|
}
|
|
24
24
|
},
|
|
25
25
|
{
|
|
@@ -32,7 +32,7 @@ class u {
|
|
|
32
32
|
return this.buckets.set(t, o), o;
|
|
33
33
|
}
|
|
34
34
|
observe(e, s, t = {}) {
|
|
35
|
-
const
|
|
35
|
+
const r = t.offset ?? 0, o = t.threshold ?? 0, i = this.getBucket(r, o);
|
|
36
36
|
i.elements.set(e, {
|
|
37
37
|
callback: s,
|
|
38
38
|
once: t.once ?? !0
|
|
@@ -52,26 +52,26 @@ class u {
|
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
const v = new u(), g = {
|
|
55
|
-
mounted(
|
|
56
|
-
v.observe(
|
|
55
|
+
mounted(n, e) {
|
|
56
|
+
v.observe(n, e.value.callback, e.value);
|
|
57
57
|
},
|
|
58
|
-
unmounted(
|
|
59
|
-
v.unobserve(
|
|
58
|
+
unmounted(n) {
|
|
59
|
+
v.unobserve(n);
|
|
60
60
|
}
|
|
61
61
|
}, a = /* @__PURE__ */ Symbol("VisibilityObserver"), d = {
|
|
62
|
-
install(
|
|
62
|
+
install(n) {
|
|
63
63
|
const e = new u();
|
|
64
|
-
|
|
64
|
+
n.provide(a, e), n.directive("visible", g);
|
|
65
65
|
}
|
|
66
66
|
};
|
|
67
67
|
function h() {
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
}, t = (
|
|
71
|
-
|
|
68
|
+
const n = f(a, null), e = n ?? new u(), s = (r, o) => {
|
|
69
|
+
r.value && e.observe(r.value, o.callback, o);
|
|
70
|
+
}, t = (r) => {
|
|
71
|
+
r.value && e.unobserve(r.value);
|
|
72
72
|
};
|
|
73
|
-
return
|
|
74
|
-
|
|
73
|
+
return k(() => {
|
|
74
|
+
n || e.disconnect();
|
|
75
75
|
}), {
|
|
76
76
|
observe: s,
|
|
77
77
|
unobserve: t
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(r,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],c):(r=typeof globalThis<"u"?globalThis:r||self,c(r.VueVisibilityObserver={},r.Vue))})(this,(function(r,c){"use strict";class l{buckets=new Map;buildRootMargin(e=0){return`0px 0px -${e}px 0px`}getKey(e=0,s=0){const t=Array.isArray(s)?s.join(","):String(s);return`${e}|${t}`}getBucket(e=0,s=0){const t=this.getKey(e,s),i=this.buckets.get(t);if(i)return i;const o={elements:new Map,observer:new IntersectionObserver(u=>{for(const b of u){const v=o.elements.get(b.target);if(!v)continue;const d=b.isIntersecting;v.callback(d,b),
|
|
1
|
+
(function(r,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],c):(r=typeof globalThis<"u"?globalThis:r||self,c(r.VueVisibilityObserver={},r.Vue))})(this,(function(r,c){"use strict";class l{buckets=new Map;buildRootMargin(e=0){return`0px 0px -${e}px 0px`}getKey(e=0,s=0){const t=Array.isArray(s)?s.join(","):String(s);return`${e}|${t}`}getBucket(e=0,s=0){const t=this.getKey(e,s),i=this.buckets.get(t);if(i)return i;const o={elements:new Map,observer:new IntersectionObserver(u=>{for(const b of u){const v=o.elements.get(b.target);if(!v)continue;const d=b.isIntersecting;d&&(v.callback(d,b),v.once&&this.unobserve(b.target))}},{root:null,rootMargin:this.buildRootMargin(e),threshold:s})};return this.buckets.set(t,o),o}observe(e,s,t={}){const i=t.offset??0,o=t.threshold??0,u=this.getBucket(i,o);u.elements.set(e,{callback:s,once:t.once??!0}),u.observer.observe(e)}unobserve(e){for(const[s,t]of this.buckets)if(t.elements.has(e)){t.elements.delete(e),t.observer.unobserve(e),t.elements.size===0&&(t.observer.disconnect(),this.buckets.delete(s));return}}disconnect(){for(const[,e]of this.buckets)e.elements.clear(),e.observer.disconnect();this.buckets.clear()}}const f=new l,y={mounted(n,e){f.observe(n,e.value.callback,e.value)},unmounted(n){f.unobserve(n)}},a=Symbol("VisibilityObserver"),k={install(n){const e=new l;n.provide(a,e),n.directive("visible",y)}};function g(){const n=c.inject(a,null),e=n??new l,s=(i,o)=>{i.value&&e.observe(i.value,o.callback,o)},t=i=>{i.value&&e.unobserve(i.value)};return c.onBeforeUnmount(()=>{n||e.disconnect()}),{observe:s,unobserve:t}}r.VisibilityPlugin=k,r.useIntersectionObserver=g,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})}));
|