Skip to content
Merged
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
33 changes: 24 additions & 9 deletions core/src/utils/gesture/button-active.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export const createButtonActiveGesture = (
el: HTMLElement,
isButton: (refEl: HTMLElement) => boolean
): Gesture => {
let touchedButton: HTMLElement | undefined;
let currentTouchedButton: HTMLElement | undefined;
let initialTouchedButton: HTMLElement | undefined;

const activateButtonAtPoint = (x: number, y: number, hapticFeedbackFn: () => void) => {
if (typeof (document as any) === 'undefined') { return; }
Expand All @@ -18,30 +19,43 @@ export const createButtonActiveGesture = (
return;
}

if (target !== touchedButton) {
if (target !== currentTouchedButton) {
clearActiveButton();
setActiveButton(target, hapticFeedbackFn);
}
};

const setActiveButton = (button: HTMLElement, hapticFeedbackFn: () => void) => {
touchedButton = button;
const buttonToModify = touchedButton;
currentTouchedButton = button;

if (!initialTouchedButton) {
initialTouchedButton = currentTouchedButton;
}

const buttonToModify = currentTouchedButton;
writeTask(() => buttonToModify.classList.add('ion-activated'));
hapticFeedbackFn();
};

const clearActiveButton = (dispatchClick = false) => {
if (!touchedButton) { return; }
if (!currentTouchedButton) { return; }

const buttonToModify = touchedButton;
const buttonToModify = currentTouchedButton;
writeTask(() => buttonToModify.classList.remove('ion-activated'));

if (dispatchClick) {
touchedButton.click();
/**
* Clicking on one button, but releasing on another button
* does not dispatch a click event in browsers, so we
* need to do it manually here. Some browsers will
* dispatch a click if clicking on one button, dragging over
* another button, and releasing on the original button. In that
* case, we need to make sure we do not cause a double click there.
*/
if (dispatchClick && initialTouchedButton !== currentTouchedButton) {
currentTouchedButton.click();
}

touchedButton = undefined;
currentTouchedButton = undefined;
};

return createGesture({
Expand All @@ -53,6 +67,7 @@ export const createButtonActiveGesture = (
onEnd: () => {
clearActiveButton(true);
hapticSelectionEnd();
initialTouchedButton = undefined;
}
});
};