Skip to content

Commit b4941cf

Browse files
committed
[FEATURE] Use pointer events instead of mouse and touch events
1 parent 9cde6e8 commit b4941cf

File tree

5 files changed

+93
-359
lines changed

5 files changed

+93
-359
lines changed

dist/js/parvus.esm.js

+30-118
Original file line numberDiff line numberDiff line change
@@ -828,9 +828,9 @@ function Parvus(userOptions) {
828828
};
829829

830830
/**
831-
* Clear drag after touchend event
831+
* Clear drag after pointerup event
832832
*
833-
* This function clears the drag state after the touchend event is triggered.
833+
* This function clears the drag state after the pointerup event is triggered.
834834
*/
835835
const clearDrag = () => {
836836
drag = {
@@ -890,11 +890,10 @@ function Parvus(userOptions) {
890890
const TOTAL_TRIGGER_ELEMENTS = TRIGGER_ELEMENTS.length;
891891
const SLIDER = GROUPS[activeGroup].slider;
892892
const SLIDER_ELEMENTS = GROUPS[activeGroup].sliderElements;
893-
const IS_TOUCH = config.simulateTouch || isTouchDevice();
894893
const IS_DRAGGABLE = SLIDER.classList.contains('parvus__slider--is-draggable');
895894

896895
// Add draggable class if neccesary
897-
if (IS_TOUCH && config.swipeClose && !IS_DRAGGABLE || IS_TOUCH && TOTAL_TRIGGER_ELEMENTS > 1 && !IS_DRAGGABLE) {
896+
if (config.swipeClose && !IS_DRAGGABLE || TOTAL_TRIGGER_ELEMENTS > 1 && !IS_DRAGGABLE) {
898897
SLIDER.classList.add('parvus__slider--is-draggable');
899898
} else {
900899
SLIDER.classList.remove('parvus__slider--is-draggable');
@@ -1061,16 +1060,19 @@ function Parvus(userOptions) {
10611060
};
10621061

10631062
/**
1064-
* Event handler for the mousedown event.
1063+
* Event handler for the pointerdown event.
10651064
*
1066-
* This function is called when the mouse button is pressed down.
1067-
* It handles the necessary actions and logic related to the mousedown event.
1065+
* This function is triggered when a pointer becomes active buttons state.
1066+
* It handles the necessary actions and logic related to the pointerdown event.
10681067
*
1069-
* @param {Event} event - The mousedown event object
1068+
* @param {Event} event - The pointerdown event object
10701069
*/
1071-
const mousedownHandler = event => {
1070+
const pointerdownHandler = event => {
10721071
event.preventDefault();
10731072
event.stopPropagation();
1073+
if (event.pointerType === 'mouse' && !config.simulateTouch) {
1074+
return;
1075+
}
10741076
isDraggingX = false;
10751077
isDraggingY = false;
10761078
pointerDown = true;
@@ -1089,14 +1091,14 @@ function Parvus(userOptions) {
10891091
};
10901092

10911093
/**
1092-
* Event handler for the mousemove event.
1094+
* Event handler for the pointermove event.
10931095
*
1094-
* This function is called when the mouse is moved.
1095-
* It handles the necessary actions and logic related to the mousemove event.
1096+
* This function is triggered when a pointer changes coordinates.
1097+
* It handles the necessary actions and logic related to the pointermove event.
10961098
*
1097-
* @param {Event} event - The mousemove event object
1099+
* @param {Event} event - The pointermove event object
10981100
*/
1099-
const mousemoveHandler = event => {
1101+
const pointermoveHandler = event => {
11001102
event.preventDefault();
11011103
if (pointerDown) {
11021104
const {
@@ -1110,79 +1112,16 @@ function Parvus(userOptions) {
11101112
};
11111113

11121114
/**
1113-
* Event handler for the mouseup event.
1114-
*
1115-
* This function is called when a mouse button is released.
1116-
* It handles the necessary actions and logic related to the mouseup event.
1117-
*/
1118-
const mouseupHandler = event => {
1119-
event.stopPropagation();
1120-
pointerDown = false;
1121-
const {
1122-
slider
1123-
} = GROUPS[activeGroup];
1124-
slider.classList.remove('parvus__slider--is-dragging');
1125-
slider.style.willChange = '';
1126-
if (drag.endX || drag.endY) {
1127-
updateAfterDrag();
1128-
}
1129-
clearDrag();
1130-
};
1131-
1132-
/**
1133-
* Event handler for the touchstart event.
1134-
*
1135-
* This function is called when a touch interaction begins.
1136-
* It handles the necessary actions and logic related to the touchstart event.
1137-
*
1138-
* @param {Event} event - The touchstart event object
1139-
*/
1140-
const touchstartHandler = event => {
1141-
event.stopPropagation();
1142-
isDraggingX = false;
1143-
isDraggingY = false;
1144-
const {
1145-
clientX,
1146-
clientY
1147-
} = event.changedTouches[0];
1148-
drag.startX = parseInt(clientX, 10);
1149-
drag.startY = parseInt(clientY, 10);
1150-
const {
1151-
slider
1152-
} = GROUPS[activeGroup];
1153-
slider.classList.add('parvus__slider--is-dragging');
1154-
slider.style.willChange = 'transform';
1155-
lightboxOverlayOpacity = getComputedStyle(lightboxOverlay).opacity;
1156-
};
1157-
1158-
/**
1159-
* Event handler for the touchmove event.
1115+
* Event handler for the pointerup event.
11601116
*
1161-
* This function is called when the touch position changes during a touch interaction.
1162-
* It handles the necessary actions and logic related to the touchmove event.
1117+
* This function is triggered when a pointer is no longer active buttons state.
1118+
* It handles the necessary actions and logic related to the pointerup event.
11631119
*
1164-
* @param {Event} event - The touchmove event object
1120+
* @param {Event} event - The pointerup event object
11651121
*/
1166-
const touchmoveHandler = event => {
1167-
event.preventDefault();
1168-
event.stopPropagation();
1169-
const {
1170-
clientX,
1171-
clientY
1172-
} = event.changedTouches[0];
1173-
drag.endX = parseInt(clientX, 10);
1174-
drag.endY = parseInt(clientY, 10);
1175-
doSwipe();
1176-
};
1177-
1178-
/**
1179-
* Event handler for the touchend event.
1180-
*
1181-
* This function is called when the touch interaction ends. It handles the necessary
1182-
* actions and logic related to the touchend event.
1183-
*/
1184-
const touchendHandler = event => {
1122+
const pointerupHandler = event => {
11851123
event.stopPropagation();
1124+
pointerDown = false;
11861125
const {
11871126
slider
11881127
} = GROUPS[activeGroup];
@@ -1244,19 +1183,10 @@ function Parvus(userOptions) {
12441183
// Click event
12451184
lightbox.addEventListener('click', clickHandler);
12461185

1247-
// Touch events
1248-
if (isTouchDevice()) {
1249-
lightbox.addEventListener('touchstart', touchstartHandler);
1250-
lightbox.addEventListener('touchmove', touchmoveHandler);
1251-
lightbox.addEventListener('touchend', touchendHandler);
1252-
}
1253-
1254-
// Mouse events
1255-
if (config.simulateTouch) {
1256-
lightbox.addEventListener('mousedown', mousedownHandler);
1257-
lightbox.addEventListener('mouseup', mouseupHandler);
1258-
lightbox.addEventListener('mousemove', mousemoveHandler);
1259-
}
1186+
// Pointer events
1187+
lightbox.addEventListener('pointerdown', pointerdownHandler);
1188+
lightbox.addEventListener('pointerup', pointerupHandler);
1189+
lightbox.addEventListener('pointermove', pointermoveHandler);
12601190
};
12611191

12621192
/**
@@ -1276,19 +1206,10 @@ function Parvus(userOptions) {
12761206
// Click event
12771207
lightbox.removeEventListener('click', clickHandler);
12781208

1279-
// Touch events
1280-
if (isTouchDevice()) {
1281-
lightbox.removeEventListener('touchstart', touchstartHandler);
1282-
lightbox.removeEventListener('touchmove', touchmoveHandler);
1283-
lightbox.removeEventListener('touchend', touchendHandler);
1284-
}
1285-
1286-
// Mouse events
1287-
if (config.simulateTouch) {
1288-
lightbox.removeEventListener('mousedown', mousedownHandler);
1289-
lightbox.removeEventListener('mouseup', mouseupHandler);
1290-
lightbox.removeEventListener('mousemove', mousemoveHandler);
1291-
}
1209+
// Pointer events
1210+
lightbox.removeEventListener('pointerdown', pointerdownHandler);
1211+
lightbox.removeEventListener('pointerup', pointerupHandler);
1212+
lightbox.removeEventListener('pointermove', pointermoveHandler);
12921213
};
12931214

12941215
/**
@@ -1319,15 +1240,6 @@ function Parvus(userOptions) {
13191240
return lightbox.hasAttribute('open');
13201241
};
13211242

1322-
/**
1323-
* Check if the device supports touch events
1324-
*
1325-
* @returns {boolean} - True if the device is touch capable, otherwise false
1326-
*/
1327-
const isTouchDevice = () => {
1328-
return 'ontouchstart' in window;
1329-
};
1330-
13311243
/**
13321244
* Get the current index
13331245
*

0 commit comments

Comments
 (0)