UNPKG

2.96 kBPlain TextView Raw
1/*
2 * Copyright 2022 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12
13import {isAndroid} from './platform';
14
15// Original licensing for the following method can be found in the
16// NOTICE file in the root directory of this source tree.
17// See https://github.com/facebook/react/blob/3c713d513195a53788b3f8bb4b70279d68b15bcc/packages/react-interactions/events/src/dom/shared/index.js#L74-L87
18
19// Keyboards, Assistive Technologies, and element.click() all produce a "virtual"
20// click event. This is a method of inferring such clicks. Every browser except
21// IE 11 only sets a zero value of "detail" for click events that are "virtual".
22// However, IE 11 uses a zero value for all click events. For IE 11 we rely on
23// the quirk that it produces click events that are of type PointerEvent, and
24// where only the "virtual" click lacks a pointerType field.
25
26export function isVirtualClick(event: MouseEvent | PointerEvent): boolean {
27 // JAWS/NVDA with Firefox.
28 if ((event as any).mozInputSource === 0 && event.isTrusted) {
29 return true;
30 }
31
32 // Android TalkBack's detail value varies depending on the event listener providing the event so we have specific logic here instead
33 // If pointerType is defined, event is from a click listener. For events from mousedown listener, detail === 0 is a sufficient check
34 // to detect TalkBack virtual clicks.
35 if (isAndroid() && (event as PointerEvent).pointerType) {
36 return event.type === 'click' && event.buttons === 1;
37 }
38
39 return event.detail === 0 && !(event as PointerEvent).pointerType;
40}
41
42export function isVirtualPointerEvent(event: PointerEvent) {
43 // If the pointer size is zero, then we assume it's from a screen reader.
44 // Android TalkBack double tap will sometimes return a event with width and height of 1
45 // and pointerType === 'mouse' so we need to check for a specific combination of event attributes.
46 // Cannot use "event.pressure === 0" as the sole check due to Safari pointer events always returning pressure === 0
47 // instead of .5, see https://bugs.webkit.org/show_bug.cgi?id=206216. event.pointerType === 'mouse' is to distingush
48 // Talkback double tap from Windows Firefox touch screen press
49 return (
50 (!isAndroid() && event.width === 0 && event.height === 0) ||
51 (event.width === 1 &&
52 event.height === 1 &&
53 event.pressure === 0 &&
54 event.detail === 0 &&
55 event.pointerType === 'mouse'
56 )
57 );
58}