-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeep-element-behavior.html
More file actions
34 lines (32 loc) · 1.07 KB
/
deep-element-behavior.html
File metadata and controls
34 lines (32 loc) · 1.07 KB
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
<link rel="import" href="../deep-intersection-observer/deep-intersection-observer.html">
<script>
window.deep = window.deep || {};
// TODO: JSDoc
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);
},
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));
}
};
deep.ElementBehavior = [ElementBehaviorImpl];
</script>