Skip to content

Commit 209779b

Browse files
authored
Merge pull request #3 from ThinkDeepTech/hm/impl
Migrated to intersection observers instead of resize observers.
2 parents ec478a3 + 7c87c17 commit 209779b

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

bower.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"description": "Polymer behavior providing useful hooks into HTMLElement state (i.e, element.isVisible if the element is displayed to the user).",
44
"main": "deep-element-behavior.html",
55
"dependencies": {
6+
"deep-intersection-observer": "ThinkDeepTech/deep-intersection-observer#^0.0.1",
67
"polymer": "Polymer/polymer#^1.0 || ^2.0"
78
},
89
"devDependencies": {

deep-element-behavior.html

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<link rel="import" href="../deep-intersection-observer/deep-intersection-observer.html">
2+
13
<script>
24
window.deep = window.deep || {};
35

@@ -8,20 +10,22 @@
810
isVisibleToUser: {
911
type: Boolean,
1012
value: false
11-
},
12-
/* Observer monitoring resize events */
13-
__resizeObserver: Object
13+
}
1414
},
1515
attached() {
16-
this.__resizeObserver = new ResizeObserver(this.__updateVisibility.bind(this));
17-
this.__resizeObserver.observe(this);
16+
// NOTE: A null root defaults to the device view port.
17+
this.__visibilityObserver = new IntersectionObserver(this.__updateVisibility.bind(this), {
18+
root: this.parentNode || null,
19+
threshold: 0.9
20+
});
21+
this.__visibilityObserver.observe(this);
1822
},
1923
detached() {
20-
this.__resizeObserver.disconnect();
24+
this.__visibilityObserver.disconnect();
2125
},
22-
__updateVisibility(resizeEntries) {
23-
if(!Array.isArray(resizeEntries) || !resizeEntries.length) return;
24-
const boundingRectangle = resizeEntries[0].contentRect;
26+
__updateVisibility(entries) {
27+
if(!Array.isArray(entries) || !entries.length) return;
28+
const boundingRectangle = entries[0].boundingClientRect || {height: 0, width: 0};
2529
const {height, width} = boundingRectangle;
2630
this.set('isVisibleToUser', Boolean(height) || Boolean(width));
2731
}

0 commit comments

Comments
 (0)