vue-use-intersection-observer 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.
- package/LICENSE +21 -0
- package/README.md +144 -0
- package/dist/composables/useIntersectionObserver.d.ts +10 -0
- package/dist/core/visibilityObserver.d.ts +15 -0
- package/dist/directives/vVisible.d.ts +10 -0
- package/dist/index.d.ts +3 -0
- package/dist/plugin.d.ts +6 -0
- package/dist/vue-use-intersection-observer.es.js +83 -0
- package/dist/vue-use-intersection-observer.umd.js +1 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Alberto Mateo Alcalá
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/Mathiew82/vue-use-intersection-observer/main/logo.png" alt="vue-use-intersection-observer logo" width="300" />
|
|
3
|
+
</div>
|
|
4
|
+
|
|
5
|
+
<div align="center">
|
|
6
|
+
|
|
7
|
+

|
|
8
|
+

|
|
9
|
+

|
|
10
|
+

|
|
11
|
+
[](https://github.com/Mathiew82/vue-use-intersection-observer/blob/master/LICENSE)
|
|
12
|
+
|
|
13
|
+
</div>
|
|
14
|
+
|
|
15
|
+
Simple and lightweight visibility observer for **vue 3**, built on top of `IntersectionObserver`.
|
|
16
|
+
|
|
17
|
+
## ✨ Features
|
|
18
|
+
|
|
19
|
+
- Vue 3 plugin
|
|
20
|
+
- Composable API
|
|
21
|
+
- Uses native `IntersectionObserver`
|
|
22
|
+
- Supports offsets and reactivity
|
|
23
|
+
- One global observer (better performance)
|
|
24
|
+
- Fully written in typescript
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
## 📦 Installation
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm i vue-use-intersection-observer
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## 🚀 How to use
|
|
35
|
+
|
|
36
|
+
### 🔌 Plugin setup
|
|
37
|
+
|
|
38
|
+
main.ts
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { createApp } from 'vue';
|
|
42
|
+
import './style.css';
|
|
43
|
+
import App from './App.vue';
|
|
44
|
+
import { VisibilityPlugin } from 'vue-use-intersection-observer';
|
|
45
|
+
|
|
46
|
+
const app = createApp(App);
|
|
47
|
+
app.use(VisibilityPlugin);
|
|
48
|
+
app.mount('#app');
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### 🧩 Usage (example)
|
|
52
|
+
|
|
53
|
+
In a component
|
|
54
|
+
|
|
55
|
+
```vue
|
|
56
|
+
<template>
|
|
57
|
+
<div
|
|
58
|
+
:style="{
|
|
59
|
+
position: 'fixed',
|
|
60
|
+
top: '20px',
|
|
61
|
+
right: '20px',
|
|
62
|
+
width: '40px',
|
|
63
|
+
height: '40px',
|
|
64
|
+
borderRadius: '50%',
|
|
65
|
+
backgroundColor: isvisible ? 'green' : 'red',
|
|
66
|
+
}"
|
|
67
|
+
/>
|
|
68
|
+
|
|
69
|
+
<div style="height: 150vh" />
|
|
70
|
+
|
|
71
|
+
<div ref="target" style="width: 200px; height: 200px; background: gray">
|
|
72
|
+
Observed Element
|
|
73
|
+
</div>
|
|
74
|
+
</template>
|
|
75
|
+
|
|
76
|
+
<script setup lang="ts">
|
|
77
|
+
import { ref, onMounted, onBeforeUnmount } from 'vue';
|
|
78
|
+
import { useIntersectionObserver } from 'vue-use-intersection-observer';
|
|
79
|
+
|
|
80
|
+
const target = ref<HTMLElement | null>(null);
|
|
81
|
+
const isvisible = ref(false);
|
|
82
|
+
|
|
83
|
+
const { observe, unobserve } = useIntersectionObserver();
|
|
84
|
+
|
|
85
|
+
onMounted(() => {
|
|
86
|
+
observe(target, {
|
|
87
|
+
once: false,
|
|
88
|
+
callback: (visible) => {
|
|
89
|
+
isvisible.value = visible;
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
onBeforeUnmount(() => {
|
|
95
|
+
unobserve(target);
|
|
96
|
+
});
|
|
97
|
+
</script>
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### ▶️ Demo
|
|
101
|
+
|
|
102
|
+

|
|
103
|
+
|
|
104
|
+
### 🛠 API
|
|
105
|
+
|
|
106
|
+
#### `useIntersectionObserver()`
|
|
107
|
+
|
|
108
|
+
Composable used to observe when elements enter or leave the viewport.
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
const { observe, unobserve } = useIntersectionObserver();
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
#### `observe()`
|
|
117
|
+
|
|
118
|
+
Start observing an element.
|
|
119
|
+
|
|
120
|
+
**Parameters:**
|
|
121
|
+
|
|
122
|
+
| Name | Type | Description |
|
|
123
|
+
| ------- | ------- | ---------------------- |
|
|
124
|
+
| target | ref | Element ref to observe |
|
|
125
|
+
| options | Options | Observer configuration |
|
|
126
|
+
|
|
127
|
+
**Options:**
|
|
128
|
+
|
|
129
|
+
| Option | Type | Description | Optional | Default |
|
|
130
|
+
| --------- | --------------- | -------------------------------- | -------- | ------- |
|
|
131
|
+
| callback | function | Called when visibility changes | false | — |
|
|
132
|
+
| offset | number | Trigger before entering viewport | true | `0` |
|
|
133
|
+
| threshold | number or array | Intersection threshold | true | `0` |
|
|
134
|
+
| once | boolean | Trigger only the first time | true | `true` |
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
#### `unobserve()`
|
|
139
|
+
|
|
140
|
+
Stop observing a previously registered element.
|
|
141
|
+
|
|
142
|
+
| Name | Type | Description |
|
|
143
|
+
| ------ | ---- | ----------------------------- |
|
|
144
|
+
| target | ref | Element ref to stop observing |
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Ref } from 'vue';
|
|
2
|
+
import { type VisibilityOptions, type VisibilityCallback } from '../core/visibilityObserver';
|
|
3
|
+
interface ObserveParams extends VisibilityOptions {
|
|
4
|
+
callback: VisibilityCallback;
|
|
5
|
+
}
|
|
6
|
+
export declare function useIntersectionObserver(): {
|
|
7
|
+
observe: (elementRef: Ref<HTMLElement | null>, options: ObserveParams) => void;
|
|
8
|
+
unobserve: (elementRef: Ref<HTMLElement | null>) => void;
|
|
9
|
+
};
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface VisibilityOptions {
|
|
2
|
+
offset?: number;
|
|
3
|
+
threshold?: number | number[];
|
|
4
|
+
once?: boolean;
|
|
5
|
+
}
|
|
6
|
+
export type VisibilityCallback = (isVisible: boolean, entry: IntersectionObserverEntry) => void;
|
|
7
|
+
export declare class VisibilityObserver {
|
|
8
|
+
private buckets;
|
|
9
|
+
private buildRootMargin;
|
|
10
|
+
private getKey;
|
|
11
|
+
private getBucket;
|
|
12
|
+
observe(element: Element, callback: VisibilityCallback, options?: VisibilityOptions): void;
|
|
13
|
+
unobserve(element: Element): void;
|
|
14
|
+
disconnect(): void;
|
|
15
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { DirectiveBinding } from 'vue';
|
|
2
|
+
import { type VisibilityOptions, type VisibilityCallback } from '../core/visibilityObserver';
|
|
3
|
+
interface DirectiveValue extends VisibilityOptions {
|
|
4
|
+
callback: VisibilityCallback;
|
|
5
|
+
}
|
|
6
|
+
export declare const vVisible: {
|
|
7
|
+
mounted(el: HTMLElement, binding: DirectiveBinding<DirectiveValue>): void;
|
|
8
|
+
unmounted(el: HTMLElement): void;
|
|
9
|
+
};
|
|
10
|
+
export {};
|
package/dist/index.d.ts
ADDED
package/dist/plugin.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { InjectionKey, Plugin } from 'vue';
|
|
2
|
+
import { VisibilityObserver, type VisibilityOptions } from './core/visibilityObserver';
|
|
3
|
+
export declare const VisibilityObserverKey: InjectionKey<VisibilityObserver>;
|
|
4
|
+
export interface VisibilityPluginOptions extends Omit<VisibilityOptions, 'once'> {
|
|
5
|
+
}
|
|
6
|
+
export declare const VisibilityPlugin: Plugin;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { inject as k, onBeforeUnmount as f } from "vue";
|
|
2
|
+
class u {
|
|
3
|
+
buckets = /* @__PURE__ */ new Map();
|
|
4
|
+
buildRootMargin(e = 0) {
|
|
5
|
+
return `0px 0px -${e}px 0px`;
|
|
6
|
+
}
|
|
7
|
+
getKey(e = 0, s = 0) {
|
|
8
|
+
const t = Array.isArray(s) ? s.join(",") : String(s);
|
|
9
|
+
return `${e}|${t}`;
|
|
10
|
+
}
|
|
11
|
+
getBucket(e = 0, s = 0) {
|
|
12
|
+
const t = this.getKey(e, s), n = this.buckets.get(t);
|
|
13
|
+
if (n) return n;
|
|
14
|
+
const o = {
|
|
15
|
+
elements: /* @__PURE__ */ new Map(),
|
|
16
|
+
observer: new IntersectionObserver(
|
|
17
|
+
(i) => {
|
|
18
|
+
for (const c of i) {
|
|
19
|
+
const b = o.elements.get(c.target);
|
|
20
|
+
if (!b) continue;
|
|
21
|
+
const l = c.isIntersecting;
|
|
22
|
+
b.callback(l, c), l && b.once && this.unobserve(c.target);
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
root: null,
|
|
27
|
+
rootMargin: this.buildRootMargin(e),
|
|
28
|
+
threshold: s
|
|
29
|
+
}
|
|
30
|
+
)
|
|
31
|
+
};
|
|
32
|
+
return this.buckets.set(t, o), o;
|
|
33
|
+
}
|
|
34
|
+
observe(e, s, t = {}) {
|
|
35
|
+
const n = t.offset ?? 0, o = t.threshold ?? 0, i = this.getBucket(n, o);
|
|
36
|
+
i.elements.set(e, {
|
|
37
|
+
callback: s,
|
|
38
|
+
once: t.once ?? !0
|
|
39
|
+
}), i.observer.observe(e);
|
|
40
|
+
}
|
|
41
|
+
unobserve(e) {
|
|
42
|
+
for (const [s, t] of this.buckets)
|
|
43
|
+
if (t.elements.has(e)) {
|
|
44
|
+
t.elements.delete(e), t.observer.unobserve(e), t.elements.size === 0 && (t.observer.disconnect(), this.buckets.delete(s));
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
disconnect() {
|
|
49
|
+
for (const [, e] of this.buckets)
|
|
50
|
+
e.elements.clear(), e.observer.disconnect();
|
|
51
|
+
this.buckets.clear();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const v = new u(), g = {
|
|
55
|
+
mounted(r, e) {
|
|
56
|
+
v.observe(r, e.value.callback, e.value);
|
|
57
|
+
},
|
|
58
|
+
unmounted(r) {
|
|
59
|
+
v.unobserve(r);
|
|
60
|
+
}
|
|
61
|
+
}, a = /* @__PURE__ */ Symbol("VisibilityObserver"), d = {
|
|
62
|
+
install(r) {
|
|
63
|
+
const e = new u();
|
|
64
|
+
r.provide(a, e), r.directive("visible", g);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
function h() {
|
|
68
|
+
const r = k(a, null), e = r ?? new u(), s = (n, o) => {
|
|
69
|
+
n.value && e.observe(n.value, o.callback, o);
|
|
70
|
+
}, t = (n) => {
|
|
71
|
+
n.value && e.unobserve(n.value);
|
|
72
|
+
};
|
|
73
|
+
return f(() => {
|
|
74
|
+
r || e.disconnect();
|
|
75
|
+
}), {
|
|
76
|
+
observe: s,
|
|
77
|
+
unobserve: t
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
export {
|
|
81
|
+
d as VisibilityPlugin,
|
|
82
|
+
h as useIntersectionObserver
|
|
83
|
+
};
|
|
@@ -0,0 +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),d&&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"})}));
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vue-use-intersection-observer",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Simple visibility observer plugin for Vue 3 using IntersectionObserver.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/vue-use-intersection-observer.umd.js",
|
|
7
|
+
"module": "./dist/vue-use-intersection-observer.es.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"author": {
|
|
10
|
+
"name": "Alberto Mateo Alcalá",
|
|
11
|
+
"url": "https://amateo82.es"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"import": "./dist/vue-use-intersection-observer.es.js",
|
|
18
|
+
"require": "./dist/vue-use-intersection-observer.umd.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"README.md",
|
|
24
|
+
"LICENSE"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"dev": "vite",
|
|
28
|
+
"build": "vite build && tsc -p tsconfig.lib.json",
|
|
29
|
+
"preview": "vite preview",
|
|
30
|
+
"format": "prettier . --write"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"vue": "^3.0.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/node": "^24.10.1",
|
|
37
|
+
"@vitejs/plugin-vue": "^6.0.1",
|
|
38
|
+
"@vue/tsconfig": "^0.8.1",
|
|
39
|
+
"prettier": "^3.7.4",
|
|
40
|
+
"typescript": "~5.9.3",
|
|
41
|
+
"vite": "^7.2.4",
|
|
42
|
+
"vue-tsc": "^3.1.4"
|
|
43
|
+
}
|
|
44
|
+
}
|