Skip to content

Commit

Permalink
fix(core): set wheel/touch listeners to passive for better perf (#769)
Browse files Browse the repository at this point in the history
- this fixes warnings shown in Chrome and other browser console mentioning that we should consider using `passive` event listeners
- also uses a polyfill in case the `passive` option is not supported (for example IE)
  • Loading branch information
ghiscoding committed Jul 8, 2023
1 parent b314768 commit 82fb150
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
28 changes: 28 additions & 0 deletions slick.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,32 @@
return Object.entries(obj).length === 0;
}

/**
* Check if `passive` option is supported when adding event listener, follows detection provided in MDN:
* https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#safely_detecting_option_support
*/
function passiveSupported() {
let passiveSupported = false;

try {
const options = {
get passive() {
passiveSupported = true;
return false;
},
};
window.addEventListener('test', null, options);
window.removeEventListener('test', null, options);
} catch (err) {
passiveSupported = false;
}
return passiveSupported;
}

function enablePassiveWhenSupported() {
return passiveSupported() ? { passive: true } : false
}

function noop() { }

function emptyElement(element) {
Expand Down Expand Up @@ -936,6 +962,8 @@
"calculateAvailableSpace": calculateAvailableSpace,
"createDomElement": createDomElement,
"emptyElement": emptyElement,
"passiveSupported": passiveSupported,
"enablePassiveWhenSupported": enablePassiveWhenSupported,
"innerSize": innerSize,
"isEmptyObject": isEmptyObject,
"noop": noop,
Expand Down
8 changes: 4 additions & 4 deletions slick.interactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

if (containerElement) {
containerElement.addEventListener('mousedown', userPressed);
containerElement.addEventListener('touchstart', userPressed);
containerElement.addEventListener('touchstart', userPressed, Slick.Utils.enablePassiveWhenSupported());
}

function executeDragCallbackWhenDefined(callback, e, dd) {
Expand All @@ -52,7 +52,7 @@
function destroy() {
if (containerElement) {
containerElement.removeEventListener('mousedown', userPressed);
containerElement.removeEventListener('touchstart', userPressed);
containerElement.removeEventListener('touchstart', userPressed, Slick.Utils.enablePassiveWhenSupported());
}
}

Expand Down Expand Up @@ -204,7 +204,7 @@
function destroy() {
if (resizeableHandleElement && typeof resizeableHandleElement.removeEventListener === 'function') {
resizeableHandleElement.removeEventListener('mousedown', resizeStartHandler);
resizeableHandleElement.removeEventListener('touchstart', resizeStartHandler);
resizeableHandleElement.removeEventListener('touchstart', resizeStartHandler, Slick.Utils.enablePassiveWhenSupported());
}
}

Expand Down Expand Up @@ -247,7 +247,7 @@

// add event listeners on the draggable element
resizeableHandleElement.addEventListener('mousedown', resizeStartHandler);
resizeableHandleElement.addEventListener('touchstart', resizeStartHandler);
resizeableHandleElement.addEventListener('touchstart', resizeStartHandler, Slick.Utils.enablePassiveWhenSupported());

return { destroy };
}
Expand Down

0 comments on commit 82fb150

Please sign in to comment.