Skip to content

Commit c26b17b

Browse files
author
utkace
committed
feature: add Intersect component
1 parent c7c1746 commit c26b17b

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/components/Intersect.vue

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<script>
2+
export default {
3+
name: "Intersect",
4+
abstract: true,
5+
props: {
6+
threshold: {
7+
type: Array,
8+
required: false,
9+
default: () => [0, 0.2]
10+
},
11+
root: {
12+
type: HTMLElement,
13+
required: false,
14+
default: () => null
15+
},
16+
rootMargin: {
17+
type: String,
18+
required: false,
19+
default: () => "0px 0px 0px 0px"
20+
}
21+
},
22+
created() {
23+
this.observer = new IntersectionObserver(
24+
entries => {
25+
if (!entries[0].isIntersecting) {
26+
this.$emit("leave", [entries[0]]);
27+
} else {
28+
this.$emit("enter", [entries[0]]);
29+
}
30+
this.$emit("change", [entries[0]]);
31+
},
32+
{
33+
threshold: this.threshold,
34+
root: this.root,
35+
rootMargin: this.rootMargin
36+
}
37+
);
38+
},
39+
mounted() {
40+
this.$nextTick(() => {
41+
if (this.$slots.default && this.$slots.default.length > 1) {
42+
warn(
43+
"[VueIntersect] You may only wrap one element in a <intersect> component."
44+
);
45+
} else if (!this.$slots.default || this.$slots.default.length < 1) {
46+
warn(
47+
"[VueIntersect] You must have one child inside a <intersect> component."
48+
);
49+
return;
50+
}
51+
this.observer.observe(this.$slots.default[0].elm);
52+
});
53+
},
54+
destroyed() {
55+
this.observer.disconnect();
56+
},
57+
render() {
58+
return this.$slots.default ? this.$slots.default[0] : null;
59+
}
60+
};
61+
</script>

0 commit comments

Comments
 (0)