Skip to content

Commit f64b14b

Browse files
Brayan Martínez Santanasherwinski
authored andcommitted
feat(passive): Add passive option for listeners
- Improve performance for mobile devices on touchstart and touchmove - Eliminate the warning of LightHouse - No need to pass passive option in removeEventListener Resolve #640
1 parent 4267801 commit f64b14b

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ var options = {
136136
touchBoundingBox: false,
137137
// A DOM element to append the bounding box to.
138138
boundingBoxContainer: document.body,
139+
// If true, the events related to handleTouch use passive listeners in
140+
// order to improve performance for touch devices.
141+
handleTouch: false,
139142
};
140143

141144
new Drift(document.querySelector('img'), options);

src/js/Drift.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ export default class Drift {
7070
const touchBoundingBox = options["touchBoundingBox"] || false;
7171
// A DOM element to append the bounding box to.
7272
const boundingBoxContainer = options["boundingBoxContainer"] || document.body;
73+
// If true, the events related to handleTouch use passive listeners in
74+
// order to improve performance for touch devices.
75+
const passive = options["passive"] || false;
7376

7477
if (inlinePane !== true && !isDOMElement(paneContainer)) {
7578
throw new TypeError("`paneContainer` must be a DOM element when `inlinePane !== true`");
@@ -98,6 +101,7 @@ export default class Drift {
98101
hoverBoundingBox,
99102
touchBoundingBox,
100103
boundingBoxContainer,
104+
passive,
101105
};
102106

103107
if (this.settings.injectBaseStyles) {
@@ -152,6 +156,7 @@ export default class Drift {
152156
namespace: this.settings.namespace,
153157
zoomFactor: this.settings.zoomFactor,
154158
boundingBoxContainer: this.settings.boundingBoxContainer,
159+
passive: this.settings.passive,
155160
});
156161
}
157162

src/js/Trigger.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default class Trigger {
2222
namespace = null,
2323
zoomFactor = throwIfMissing(),
2424
boundingBoxContainer = throwIfMissing(),
25+
passive = false,
2526
} = options;
2627

2728
this.settings = {
@@ -38,6 +39,7 @@ export default class Trigger {
3839
namespace,
3940
zoomFactor,
4041
boundingBoxContainer,
42+
passive,
4143
};
4244

4345
if (this.settings.hoverBoundingBox || this.settings.touchBoundingBox) {
@@ -76,14 +78,15 @@ export default class Trigger {
7678
this.settings.el.addEventListener("mouseleave", this._hide, false);
7779
this.settings.el.addEventListener("mousemove", this._handleMovement, false);
7880

81+
const isPassive = { passive: this.settings.passive };
7982
if (this.settings.handleTouch) {
80-
this.settings.el.addEventListener("touchstart", this._handleEntry, false);
83+
this.settings.el.addEventListener("touchstart", this._handleEntry, isPassive);
8184
this.settings.el.addEventListener("touchend", this._hide, false);
82-
this.settings.el.addEventListener("touchmove", this._handleMovement, false);
85+
this.settings.el.addEventListener("touchmove", this._handleMovement, isPassive);
8386
} else {
84-
this.settings.el.addEventListener("touchstart", this._preventDefault, false);
87+
this.settings.el.addEventListener("touchstart", this._preventDefault, isPassive);
8588
this.settings.el.addEventListener("touchend", this._preventDefault, false);
86-
this.settings.el.addEventListener("touchmove", this._preventDefault, false);
89+
this.settings.el.addEventListener("touchmove", this._preventDefault, isPassive);
8790
}
8891
}
8992

0 commit comments

Comments
 (0)