Skip to content

Commit 7152d98

Browse files
authored
Merge pull request Expensify#87291 from truph01/fix/fixNonInteractivePressableA11y
fix: remove onclick event listener if not interactive
2 parents 2273f92 + 35c8502 commit 7152d98

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

src/components/MenuItem.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type {ImageContentFit} from 'expo-image';
22
import type {ReactElement, ReactNode, Ref} from 'react';
3-
import React, {useMemo, useRef} from 'react';
3+
import React, {useEffect, useMemo, useRef} from 'react';
44
import type {GestureResponderEvent, Role, StyleProp, TextStyle, ViewStyle} from 'react-native';
55
import {View} from 'react-native';
66
import type {ValueOf} from 'type-fest';
@@ -618,6 +618,18 @@ function MenuItem({
618618
const {isExecuting} = useMenuItemGroupState() ?? {};
619619
const {singleExecution, waitForNavigate} = useMenuItemGroupActions() ?? {};
620620
const popoverAnchor = useRef<View>(null);
621+
const pressableRef = useRef<View>(null);
622+
useEffect(() => {
623+
const element = pressableRef.current;
624+
if (interactive || !element || typeof HTMLElement === 'undefined' || !(element instanceof HTMLElement) || typeof element.onclick === 'undefined') {
625+
return;
626+
}
627+
// React Native Web's Pressable always attaches an onClick handler to the DOM element.
628+
// TalkBack on Android web uses the presence of a click event listener to determine whether
629+
// an element is clickable and announces "double tap to activate" even for non-interactive elements.
630+
// Removing the onclick property prevents TalkBack from treating the element as clickable.
631+
element.onclick = null;
632+
}, [interactive]);
621633
const deviceHasHoverSupport = hasHoverSupport();
622634
const isCompactMenu = useIsCompactMenu();
623635
const isCompactPopoverItem = isCompactMenu && !isSmallScreenWidth && !shouldIgnoreCompactStyle;
@@ -861,7 +873,7 @@ function MenuItem({
861873
}
862874
disabledStyle={shouldUseDefaultCursorWhenDisabled && [styles.cursorDefault]}
863875
disabled={disabled || isExecuting}
864-
ref={mergeRefs(ref, popoverAnchor)}
876+
ref={mergeRefs(ref, popoverAnchor, pressableRef)}
865877
role={interactive ? role : undefined}
866878
accessibilityLabel={accessibilityLabelWithContextMenuHint}
867879
accessibilityHint={accessibilityHint}

0 commit comments

Comments
 (0)