forked from ThinkDeepTech/deep-element-behavior
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeep-element-behavior.html
38 lines (35 loc) · 1.17 KB
/
deep-element-behavior.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<script>
window.deep = window.deep || {};
// TODO: JSDoc
const ElementBehaviorImpl = {
properties: {
/* True if the element is visible to the user. False otherwise. */
isVisibleToUser: {
type: Boolean,
value: false,
},
},
attached() {
// NOTE: A null root defaults to the device view port.
this.__visibilityObserver = new IntersectionObserver(this.__updateVisibility.bind(this), {
root: this.parentNode || null,
threshold: [0, 1.0],
});
this.__visibilityObserver.observe(this);
const that = this;
window.addEventListener('checkForIntersections', function() {
that.__visibilityObserver._checkForIntersections();
});
},
detached() {
this.__visibilityObserver.disconnect();
},
__updateVisibility(entries) {
if (!Array.isArray(entries) || !entries.length) return;
const boundingRectangle = entries[0].boundingClientRect || {height: 0, width: 0};
const {height, width} = boundingRectangle;
this.set('isVisibleToUser', Boolean(height) || Boolean(width));
},
};
window.deep.ElementBehavior = [ElementBehaviorImpl];
</script>