Skip to content

Commit

Permalink
feat: Ignore touch on more interactive elements
Browse files Browse the repository at this point in the history
Fixes #1654.
  • Loading branch information
StarScape committed Mar 19, 2024
1 parent cbb94e3 commit 472280e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/content/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import {
ContentConfigParams,
} from '../common/content-config-params';
import { CopyKeys, CopyType } from '../common/copy-keys';
import { isEditableNode } from '../utils/dom-utils';
import { isEditableNode, isInteractiveElement } from '../utils/dom-utils';
import {
addMarginToPoint,
getMarginAroundPoint,
Expand Down Expand Up @@ -301,9 +301,11 @@ export class ContentHandler {

this.touchClickTracker.onTouchClick = (event: MouseEvent) => {
// Ignore clicks on interactive elements
const possibleTargetElem = event.target as Element | null;
if (
event.target instanceof HTMLAnchorElement ||
event.target instanceof HTMLButtonElement
possibleTargetElem != null &&
possibleTargetElem.nodeType === Node.ELEMENT_NODE &&
isInteractiveElement(possibleTargetElem)
) {
return;
}
Expand Down
18 changes: 18 additions & 0 deletions src/utils/dom-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ export function isEditableNode(node: Node | null): boolean {
return isTextInputNode(node) || isContentEditableNode(node);
}

/**
* Tests whether an element is 'interactive', i.e. an element
* that we should not do lookups on when tapped on mobile.
*/
export function isInteractiveElement(elem: Element) {
return (
elem.tagName === 'A' ||
elem.tagName === 'BUTTON' ||
elem.tagName === 'INPUT' ||
elem.tagName === 'TEXTAREA' ||
elem.tagName === 'SELECT' ||
elem.tagName === 'DATALIST' ||
elem.tagName === 'OPTGROUP' ||
elem.tagName === 'OPTION' ||
isContentEditableNode(elem)
);
}

export interface Focusable {
focus(): void;
}
Expand Down

0 comments on commit 472280e

Please sign in to comment.