Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): set wheel/touch listeners to passive for better perf #769

Merged
merged 1 commit into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions slick.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,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 offset(el) {
Expand Down Expand Up @@ -1063,6 +1089,8 @@
"calculateAvailableSpace": calculateAvailableSpace,
"createDomElement": createDomElement,
"emptyElement": emptyElement,
"passiveSupported": passiveSupported,
"enablePassiveWhenSupported": enablePassiveWhenSupported,
"innerSize": innerSize,
"isEmptyObject": isEmptyObject,
"noop": noop,
Expand Down
20 changes: 10 additions & 10 deletions slick.interactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* code refs:
* https://betterprogramming.pub/perfecting-drag-and-drop-in-pure-vanilla-javascript-a761184b797a
* available optional options:
* - containerElement: container DOM element, defaults to "document"
* - containerElement: container DOM element, defaults to "document"
* - allowDragFrom: when defined, only allow dragging from an element that matches a specific query selector
* - onDragInit: drag initialized callback
* - onDragStart: drag started callback
Expand Down 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 @@ -130,13 +130,13 @@
let { element, onMouseWheel } = options;

function destroy() {
element.removeEventListener('wheel', wheelHandler, false);
element.removeEventListener('mousewheel', wheelHandler, false);
element.removeEventListener('wheel', wheelHandler, Slick.Utils.enablePassiveWhenSupported());
element.removeEventListener('mousewheel', wheelHandler, Slick.Utils.enablePassiveWhenSupported());
}

function init() {
element.addEventListener('wheel', wheelHandler, false);
element.addEventListener('mousewheel', wheelHandler, false);
element.addEventListener('wheel', wheelHandler, Slick.Utils.enablePassiveWhenSupported());
element.addEventListener('mousewheel', wheelHandler, Slick.Utils.enablePassiveWhenSupported());
}

// copy over the same event handler code used in jquery.mousewheel
Expand Down Expand Up @@ -191,7 +191,7 @@
* - onResizeStart: resize start callback
* - onResize: resizing callback
* - onResizeEnd: resize ended callback
* @param {Object} options
* @param {Object} options
* @returns - Resizable instance which includes destroy method
* @class Resizable
*/
Expand All @@ -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