UNPKG

1.51 kBPlain TextView Raw
1<script>
2import { memoize } from 'lodash';
3
4const getObserver = memoize(
5 (options) =>
6 new IntersectionObserver((entries) => {
7 entries.forEach((entry) => {
8 entry.target.$_gl_intersectionHandler(entry);
9 });
10 }, options || {})
11);
12
13export default {
14 name: 'GlIntersectionObserver',
15 props: {
16 /**
17 * Extra options to pass directly to the intersection observer API.
18 */
19 options: {
20 type: Object,
21 required: false,
22 default: null,
23 },
24 },
25 mounted() {
26 const observer = getObserver(this.options);
27
28 this.$el.$_gl_intersectionHandler = (entry) => {
29 /**
30 * Emitted when the element's visibility changes
31 */
32 this.$emit('update', entry);
33
34 if (entry.isIntersecting) {
35 /**
36 * Emitted when the element appears on the page
37 */
38 this.$emit('appear');
39 } else {
40 /**
41 * Emitted when the element disappears from the page
42 */
43 this.$emit('disappear');
44 }
45 };
46 this.$el.$_gl_intersectionObserver = observer;
47
48 observer.observe(this.$el);
49 },
50 destroyed() {
51 this.$el.$_gl_intersectionObserver.unobserve(this.$el);
52 delete this.$el.$_gl_intersectionHandler;
53 delete this.$el.$_gl_intersectionObserver;
54 },
55 // Expose getObserver method for tests
56 getObserver,
57};
58</script>
59
60<template>
61 <div>
62 <!--
63 @slot The element you wish to observe, or a fallback if the observer doesn't work.
64 -->
65 <slot></slot>
66 </div>
67</template>