Skip to content

Commit e03d5b4

Browse files
committed
fix: move logic to MenuItem
1 parent d720579 commit e03d5b4

2 files changed

Lines changed: 16 additions & 19 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}

src/components/MenuItemWithTopDescription.tsx

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import React, {useEffect, useRef} from 'react';
2-
import type {View} from 'react-native';
1+
import React from 'react';
32
import useAnimatedHighlightStyle from '@hooks/useAnimatedHighlightStyle';
43
import useTheme from '@hooks/useTheme';
5-
import mergeRefs from '@libs/mergeRefs';
64
import MenuItem from './MenuItem';
75
import type {MenuItemProps} from './MenuItem';
86

@@ -18,24 +16,11 @@ function MenuItemWithTopDescription({highlighted, outerWrapperStyle, ref, ...pro
1816
highlightColor: theme.messageHighlightBG,
1917
itemEnterDelay: 0,
2018
});
21-
const pressableRef = useRef<View>(null);
22-
useEffect(() => {
23-
const element = pressableRef.current;
24-
if (props.interactive || !element || typeof HTMLElement === 'undefined' || !(element instanceof HTMLElement) || typeof element.onclick === 'undefined') {
25-
return;
26-
}
27-
// React Native Web's Pressable always attaches an onClick handler to the DOM element.
28-
// TalkBack on Android web uses the presence of a click event listener to determine whether
29-
// an element is clickable and announces "double tap to activate" even for non-interactive elements.
30-
// Removing the onclick property prevents TalkBack from treating the element as clickable.
31-
element.onclick = null;
32-
}, [props.interactive]);
33-
3419
return (
3520
<MenuItem
3621
// eslint-disable-next-line react/jsx-props-no-spreading
3722
{...props}
38-
ref={mergeRefs(ref, pressableRef)}
23+
ref={ref}
3924
shouldShowBasicTitle
4025
shouldShowDescriptionOnTop
4126
outerWrapperStyle={highlighted ? highlightedOuterWrapperStyle : outerWrapperStyle}

0 commit comments

Comments
 (0)