Skip to content

Commit 500854d

Browse files
authored
refactor(tap-click): use pointer events api (#29192)
Issue number: Internal --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> [Amanda pointed out that the ripple effect for the Button inside of InputPasswordToggle was not working](#29175 (comment)). I found out that calling `ev.preventDefault` on `pointerdown` causes `mouseup` to not get fired. On desktop, we rely on `mouseup` to know when to add the ripple effect. (`touchend` is not impacted) Interestingly, calling `ev.preventDefault` on `pointerdown` does **not** prevent `pointerup` from being fired. The idea here is that if we migrate the tap click utility to use the PointerEvents API instead of separate mouse/touch listeners we can keep the existing tap click behavior while also fixing the bug that Amanda noted. ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - Tap click nows listens for the Pointer Events instead of separate mouse/touch events Impact to developers is fairly minimal. There should be no behavior change (other than the bug I noted being fixed). There should be a very small perf boost because this util now only adds 4 event listeners on the document instead of 7 previously. ## Does this introduce a breaking change? - [ ] Yes - [x] No <!-- If this introduces a breaking change: 1. Describe the impact and migration path for existing applications below. 2. Update the BREAKING.md file with the breaking change. 3. Add "BREAKING CHANGE: [...]" to the commit description when merging. See https://github.com/ionic-team/ionic-framework/blob/main/.github/CONTRIBUTING.md#footer for more information. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. --> Reviewers: Please manually test this on desktop devices as well as iOS and Android devices (not Chrome Dev Tools. iOS simulators are fine). Test that components such as `ion-button` correctly add the activated state (or ripple effect for MD). Also verify that the activated state is not added when tapping the button and then scrolling. For desktop, check that right clicking does not add the activated state.
1 parent 9efeb0a commit 500854d

File tree

1 file changed

+6
-41
lines changed

1 file changed

+6
-41
lines changed

core/src/utils/tap-click/index.ts

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { doc } from '@utils/browser';
22

33
import type { Config } from '../../interface';
4-
import { now, pointerCoord } from '../helpers';
4+
import { pointerCoord } from '../helpers';
55

66
export const startTapClick = (config: Config) => {
77
if (doc === undefined) {
88
return;
99
}
1010

11-
let lastTouch = -MOUSE_WAIT * 10;
1211
let lastActivated = 0;
1312

1413
let activatableEle: HTMLElement | undefined;
@@ -18,36 +17,6 @@ export const startTapClick = (config: Config) => {
1817
const useRippleEffect = config.getBoolean('animated', true) && config.getBoolean('rippleEffect', true);
1918
const clearDefers = new WeakMap<HTMLElement, any>();
2019

21-
// Touch Events
22-
const onTouchStart = (ev: TouchEvent) => {
23-
lastTouch = now(ev);
24-
pointerDown(ev);
25-
};
26-
27-
const onTouchEnd = (ev: TouchEvent) => {
28-
lastTouch = now(ev);
29-
pointerUp(ev);
30-
};
31-
32-
const onMouseDown = (ev: MouseEvent) => {
33-
// Ignore right clicks
34-
if (ev.button === 2) {
35-
return;
36-
}
37-
38-
const t = now(ev) - MOUSE_WAIT;
39-
if (lastTouch < t) {
40-
pointerDown(ev);
41-
}
42-
};
43-
44-
const onMouseUp = (ev: MouseEvent) => {
45-
const t = now(ev) - MOUSE_WAIT;
46-
if (lastTouch < t) {
47-
pointerUp(ev);
48-
}
49-
};
50-
5120
const cancelActive = () => {
5221
if (activeDefer) clearTimeout(activeDefer);
5322
activeDefer = undefined;
@@ -57,8 +26,9 @@ export const startTapClick = (config: Config) => {
5726
}
5827
};
5928

60-
const pointerDown = (ev: UIEvent) => {
61-
if (activatableEle) {
29+
const pointerDown = (ev: PointerEvent) => {
30+
// Ignore right clicks
31+
if (activatableEle || ev.button === 2) {
6232
return;
6333
}
6434
setActivatedElement(getActivatableTarget(ev), ev);
@@ -151,9 +121,8 @@ export const startTapClick = (config: Config) => {
151121

152122
doc.addEventListener('ionGestureCaptured', cancelActive);
153123

154-
doc.addEventListener('touchstart', onTouchStart, true);
155-
doc.addEventListener('touchcancel', onTouchEnd, true);
156-
doc.addEventListener('touchend', onTouchEnd, true);
124+
doc.addEventListener('pointerdown', pointerDown, true);
125+
doc.addEventListener('pointerup', pointerUp, true);
157126

158127
/**
159128
* Tap click effects such as the ripple effect should
@@ -168,9 +137,6 @@ export const startTapClick = (config: Config) => {
168137
* ion-content's scroll events.
169138
*/
170139
doc.addEventListener('pointercancel', cancelActive, true);
171-
172-
doc.addEventListener('mousedown', onMouseDown, true);
173-
doc.addEventListener('mouseup', onMouseUp, true);
174140
};
175141

176142
// TODO(FW-2832): type
@@ -213,4 +179,3 @@ const getRippleEffect = (el: HTMLElement) => {
213179
const ACTIVATED = 'ion-activated';
214180
const ADD_ACTIVATED_DEFERS = 100;
215181
const CLEAR_STATE_DEFERS = 150;
216-
const MOUSE_WAIT = 2500;

0 commit comments

Comments
 (0)