Skip to content

Add onTooltipPress callback to various tooltip components #55454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4628,6 +4628,8 @@ const CONST = {
TOOLBAR: 'toolbar',
/** Use for navigation elements */
NAVIGATION: 'navigation',
/** Use for Tooltips */
TOOLTIP: 'tooltip',
},
TRANSLATION_KEYS: {
ATTACHMENT: 'common.attachment',
Expand Down
7 changes: 7 additions & 0 deletions src/components/AnimatedPressableWithoutFeedback.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Animated from 'react-native-reanimated';
import PressableWithoutFeedback from './Pressable/PressableWithoutFeedback';

const AnimatedPressableWithoutFeedback = Animated.createAnimatedComponent(PressableWithoutFeedback);
AnimatedPressableWithoutFeedback.displayName = 'AnimatedPressableWithoutFeedback';

export default AnimatedPressableWithoutFeedback;
1 change: 1 addition & 0 deletions src/components/FloatingActionButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ function FloatingActionButton({onPress, isActive, accessibilityLabel, role}: Flo
renderTooltipContent={renderProductTrainingTooltip}
wrapperStyle={styles.productTrainingTooltipWrapper}
shouldHideOnNavigate={false}
onTooltipPress={toggleFabAction}
>
<PressableWithoutFeedback
ref={(el) => {
Expand Down
23 changes: 13 additions & 10 deletions src/components/LHNOptionsList/OptionRowLHN.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ function OptionRowLHN({reportID, isFocused = false, onSelectRow = () => {}, opti
const subscriptAvatarBorderColor = isFocused ? focusedBackgroundColor : theme.sidebar;
const firstIcon = optionItem.icons?.at(0);

const onOptionPress = (event: GestureResponderEvent | KeyboardEvent | undefined) => {
Performance.markStart(CONST.TIMING.OPEN_REPORT);
Timing.start(CONST.TIMING.OPEN_REPORT);

event?.preventDefault();
// Enable Composer to focus on clicking the same chat after opening the context menu.
ReportActionComposeFocusManager.focus();
hideProductTrainingTooltip();
onSelectRow(optionItem, popoverAnchor);
};

return (
<OfflineWithFeedback
pendingAction={optionItem.pendingAction}
Expand All @@ -185,22 +196,14 @@ function OptionRowLHN({reportID, isFocused = false, onSelectRow = () => {}, opti
shiftHorizontal={shouldShowWokspaceChatTooltip ? variables.workspaceLHNtooltipShiftHorizontal : variables.gbrTooltipShiftHorizontal}
shiftVertical={shouldShowWokspaceChatTooltip ? 0 : variables.composerTooltipShiftVertical}
wrapperStyle={styles.productTrainingTooltipWrapper}
onTooltipPress={onOptionPress}
>
<View>
<Hoverable>
{(hovered) => (
<PressableWithSecondaryInteraction
ref={popoverAnchor}
onPress={(event) => {
Performance.markStart(CONST.TIMING.OPEN_REPORT);
Timing.start(CONST.TIMING.OPEN_REPORT);

event?.preventDefault();
// Enable Composer to focus on clicking the same chat after opening the context menu.
ReportActionComposeFocusManager.focus();
hideProductTrainingTooltip();
onSelectRow(optionItem, popoverAnchor);
}}
onPress={onOptionPress}
onMouseDown={(event) => {
// Allow composer blur on right click
if (!event) {
Expand Down
13 changes: 9 additions & 4 deletions src/components/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import ControlSelection from '@libs/ControlSelection';
import convertToLTR from '@libs/convertToLTR';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import {canUseTouchScreen} from '@libs/DeviceCapabilities';
import getButtonState from '@libs/getButtonState';
import Parser from '@libs/Parser';
import type {AvatarSource} from '@libs/UserUtils';
import variables from '@styles/variables';
import * as Session from '@userActions/Session';
import {checkIfActionIsAllowed} from '@userActions/Session';
import CONST from '@src/CONST';
import type {Icon as IconType} from '@src/types/onyx/OnyxCommon';
import type {TooltipAnchorAlignment} from '@src/types/utils/AnchorAlignment';
Expand Down Expand Up @@ -335,6 +335,9 @@ type MenuItemBaseProps = {
/** Render custom content inside the tooltip. */
renderTooltipContent?: () => ReactNode;

/** Callback to fire when the education tooltip is pressed */
onEducationTooltipPress?: () => void;

shouldShowLoadingSpinnerIcon?: boolean;

/** Should selected item be marked with checkmark */
Expand Down Expand Up @@ -459,6 +462,7 @@ function MenuItem(
tooltipShiftHorizontal = 0,
tooltipShiftVertical = 0,
renderTooltipContent,
onEducationTooltipPress,
additionalIconStyles,
shouldShowSelectedItemCheck = false,
shouldIconUseAutoWidthStyle = false,
Expand Down Expand Up @@ -601,13 +605,14 @@ function MenuItem(
shiftHorizontal={tooltipShiftHorizontal}
shiftVertical={tooltipShiftVertical}
shouldTeleportPortalToModalLayer={shouldTeleportPortalToModalLayer}
onTooltipPress={onEducationTooltipPress}
>
<View>
<Hoverable>
{(isHovered) => (
<PressableWithSecondaryInteraction
onPress={shouldCheckActionAllowedOnPress ? Session.checkIfActionIsAllowed(onPressAction, isAnonymousAction) : onPressAction}
onPressIn={() => shouldBlockSelection && shouldUseNarrowLayout && DeviceCapabilities.canUseTouchScreen() && ControlSelection.block()}
onPress={shouldCheckActionAllowedOnPress ? checkIfActionIsAllowed(onPressAction, isAnonymousAction) : onPressAction}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB but checkIfActionIsAllowed is a confusing function name. It sounds like it will return a boolean.

Copy link
Contributor Author

@ishpaul777 ishpaul777 Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is confusing 💯 agree, should I change it to callFnIfActionIsAllowed? I see function is used widely across the codebase happy to raise a follow-up PR just so we keep this PR clean

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do it as a follow up, thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it is #55663

onPressIn={() => shouldBlockSelection && shouldUseNarrowLayout && canUseTouchScreen() && ControlSelection.block()}
onPressOut={ControlSelection.unblock}
onSecondaryInteraction={onSecondaryInteraction}
wrapperStyle={outerWrapperStyle}
Expand Down
2 changes: 2 additions & 0 deletions src/components/PopoverProvider/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const PopoverContext = React.createContext<PopoverContextValue>({
popover: null,
close: () => {},
isOpen: false,
setActivePopoverExtraAnchorRef: () => {},
});

function PopoverContextProvider(props: PopoverContextProps) {
Expand All @@ -15,6 +16,7 @@ function PopoverContextProvider(props: PopoverContextProps) {
close: () => {},
popover: null,
isOpen: false,
setActivePopoverExtraAnchorRef: () => {},
}),
[],
);
Expand Down
26 changes: 24 additions & 2 deletions src/components/PopoverProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const PopoverContext = createContext<PopoverContextValue>({
popoverAnchor: null,
close: () => {},
isOpen: false,
setActivePopoverExtraAnchorRef: () => {},
});

function elementContains(ref: RefObject<View | HTMLElement | Text> | undefined, target: EventTarget | null) {
Expand All @@ -23,6 +24,7 @@ function PopoverContextProvider(props: PopoverContextProps) {
const [isOpen, setIsOpen] = useState(false);
const activePopoverRef = useRef<AnchorRef | null>(null);
const [activePopoverAnchor, setActivePopoverAnchor] = useState<AnchorRef['anchorRef']['current']>(null);
const [activePopoverExtraAnchorRefs, setActivePopoverExtraAnchorRefs] = useState<AnchorRef['extraAnchorRefs']>([]);

const closePopover = useCallback((anchorRef?: RefObject<View | HTMLElement | Text>): boolean => {
if (!activePopoverRef.current || (anchorRef && anchorRef !== activePopoverRef.current.anchorRef)) {
Expand All @@ -41,14 +43,17 @@ function PopoverContextProvider(props: PopoverContextProps) {
if (elementContains(activePopoverRef.current?.ref, e.target) || elementContains(activePopoverRef.current?.anchorRef, e.target)) {
return;
}
if (activePopoverExtraAnchorRefs?.some((ref: RefObject<View | HTMLElement | Text>) => elementContains(ref, e.target))) {
return;
}
const ref = activePopoverRef.current?.anchorRef;
closePopover(ref);
};
document.addEventListener('click', listener, true);
return () => {
document.removeEventListener('click', listener, true);
};
}, [closePopover]);
}, [closePopover, activePopoverExtraAnchorRefs]);

useEffect(() => {
const listener = (e: Event) => {
Expand Down Expand Up @@ -117,16 +122,33 @@ function PopoverContextProvider(props: PopoverContextProps) {
[closePopover],
);

const setActivePopoverExtraAnchorRef = useCallback((extraAnchorRef?: RefObject<View | HTMLDivElement | Text>) => {
if (!extraAnchorRef) {
return;
}
setActivePopoverExtraAnchorRefs((prev: AnchorRef['extraAnchorRefs']) => {
if (!prev) {
return [extraAnchorRef];
}

if (prev?.includes(extraAnchorRef)) {
return prev;
}
return [...prev, extraAnchorRef];
});
}, []);

const contextValue = useMemo(
() => ({
onOpen,
setActivePopoverExtraAnchorRef,
close: closePopover,
// eslint-disable-next-line react-compiler/react-compiler
popover: activePopoverRef.current,
popoverAnchor: activePopoverAnchor,
isOpen,
}),
[onOpen, closePopover, isOpen, activePopoverAnchor],
[onOpen, closePopover, isOpen, activePopoverAnchor, setActivePopoverExtraAnchorRef],
);

return <PopoverContext.Provider value={contextValue}>{props.children}</PopoverContext.Provider>;
Expand Down
2 changes: 2 additions & 0 deletions src/components/PopoverProvider/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ type PopoverContextValue = {
popoverAnchor?: AnchorRef['anchorRef']['current'];
close: (anchorRef?: RefObject<View | HTMLDivElement | Text>) => void;
isOpen: boolean;
setActivePopoverExtraAnchorRef: (ref?: RefObject<View | HTMLDivElement | Text>) => void;
};

type AnchorRef = {
ref: RefObject<View | HTMLDivElement | Text>;
close: (anchorRef?: RefObject<View | HTMLDivElement | Text>) => void;
anchorRef: RefObject<View | HTMLDivElement | Text>;
extraAnchorRefs?: Array<RefObject<View | HTMLDivElement | Text>>;
};

export type {PopoverContextProps, PopoverContextValue, AnchorRef};
1 change: 1 addition & 0 deletions src/components/Search/SearchPageHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ function SearchPageHeader({queryJSON}: SearchPageHeaderProps) {
shiftHorizontal={variables.searchFiltersTooltipShiftHorizontal}
wrapperStyle={styles.productTrainingTooltipWrapper}
renderTooltipContent={renderProductTrainingTooltip}
onTooltipPress={onFiltersButtonPress}
>
<Button
innerStyles={!isCannedQuery && [styles.searchRouterInputResults, styles.borderNone]}
Expand Down
59 changes: 39 additions & 20 deletions src/components/ThreeDotsMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
import PopoverMenu from '@components/PopoverMenu';
import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeedback';
import EducationalTooltip from '@components/Tooltip/EducationalTooltip';
import Tooltip from '@components/Tooltip/PopoverAnchorTooltip';
import useLocalize from '@hooks/useLocalize';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import * as Browser from '@libs/Browser';
import {isMobile} from '@libs/Browser';
import variables from '@styles/variables';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type ThreeDotsMenuProps from './types';
Expand All @@ -30,6 +32,8 @@ function ThreeDotsMenu({
shouldSetModalVisibility = true,
disabled = false,
hideProductTrainingTooltip,
renderProductTrainingTooltipContent,
shouldShowProductTrainingTooltip = false,
}: ThreeDotsMenuProps) {
const [modal] = useOnyx(ONYXKEYS.MODAL);

Expand All @@ -55,31 +59,46 @@ function ThreeDotsMenu({
hidePopoverMenu();
}, [isBehindModal, isPopupMenuVisible]);

const onThreeDotsPress = () => {
if (isPopupMenuVisible) {
hidePopoverMenu();
return;
}
hideProductTrainingTooltip?.();
buttonRef.current?.blur();
showPopoverMenu();
if (onIconPress) {
onIconPress();
}
};

const TooltipToRender = shouldShowProductTrainingTooltip ? EducationalTooltip : Tooltip;
const tooltipProps = shouldShowProductTrainingTooltip
? {
renderTooltipContent: renderProductTrainingTooltipContent,
shouldRender: shouldShowProductTrainingTooltip,
anchorAlignment: {
horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.RIGHT,
vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.BOTTOM,
},
shiftHorizontal: variables.savedSearchShiftHorizontal,
shiftVertical: variables.savedSearchShiftVertical,
wrapperStyle: [styles.mh4, styles.pv2, styles.productTrainingTooltipWrapper],
onTooltipPress: onThreeDotsPress,
}
: {text: translate(iconTooltip), shouldRender: true};

return (
<>
<View>
<Tooltip
text={translate(iconTooltip)}
// We need to hide the extra "More" tooltip when we have an educational tooltip
shouldRender={!hideProductTrainingTooltip}
>
{/* eslint-disable-next-line react/jsx-props-no-spreading */}
<TooltipToRender {...tooltipProps}>
<PressableWithoutFeedback
onPress={() => {
if (isPopupMenuVisible) {
hidePopoverMenu();
return;
}
hideProductTrainingTooltip?.();
buttonRef.current?.blur();
showPopoverMenu();
if (onIconPress) {
onIconPress();
}
}}
onPress={onThreeDotsPress}
disabled={disabled}
onMouseDown={(e) => {
/* Keep the focus state on mWeb like we did on the native apps. */
if (!Browser.isMobile()) {
if (!isMobile()) {
return;
}
e.preventDefault();
Expand All @@ -94,7 +113,7 @@ function ThreeDotsMenu({
fill={iconFill ?? isPopupMenuVisible ? theme.success : theme.icon}
/>
</PressableWithoutFeedback>
</Tooltip>
</TooltipToRender>
</View>
<PopoverMenu
onClose={hidePopoverMenu}
Expand Down
6 changes: 6 additions & 0 deletions src/components/ThreeDotsMenu/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ type ThreeDotsMenuProps = {

/** Function to hide the product training tooltip */
hideProductTrainingTooltip?: () => void;

/** Tooltip content to render */
renderProductTrainingTooltipContent?: () => React.JSX.Element;

/** Should we render the tooltip */
shouldShowProductTrainingTooltip?: boolean;
};

export default ThreeDotsMenuProps;
13 changes: 10 additions & 3 deletions src/components/Tooltip/BaseGenericTooltip/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {View} from 'react-native';
// eslint-disable-next-line no-restricted-imports
import type {View as RNView} from 'react-native';
import Animated, {useAnimatedStyle, useSharedValue} from 'react-native-reanimated';
import AnimatedPressableWithoutFeedback from '@components/AnimatedPressableWithoutFeedback';
import TransparentOverlay from '@components/AutoCompleteSuggestions/AutoCompleteSuggestionsPortal/TransparentOverlay/TransparentOverlay';
import Text from '@components/Text';
import useStyleUtils from '@hooks/useStyleUtils';
Expand Down Expand Up @@ -38,6 +39,7 @@ function BaseGenericTooltip({
onHideTooltip = () => {},
shouldTeleportPortalToModalLayer = false,
isEducationTooltip = false,
onTooltipPress = () => {},
}: BaseGenericTooltipProps) {
// The width of tooltip's inner content. Has to be undefined in the beginning
// as a width of 0 will cause the content to be rendered of a width of 0,
Expand Down Expand Up @@ -113,12 +115,17 @@ function BaseGenericTooltip({
);
}

const AnimatedWrapper = isEducationTooltip ? AnimatedPressableWithoutFeedback : Animated.View;

return (
<Portal hostName={shouldTeleportPortalToModalLayer ? 'modal' : undefined}>
{shouldUseOverlay && <TransparentOverlay onPress={onHideTooltip} />}
<Animated.View
ref={rootWrapper}
<AnimatedWrapper
style={[rootWrapperStyle, animationStyle]}
ref={rootWrapper}
onPress={isEducationTooltip ? onTooltipPress : undefined}
role={isEducationTooltip ? CONST.ROLE.TOOLTIP : undefined}
accessibilityLabel={isEducationTooltip ? CONST.ROLE.TOOLTIP : undefined}
onLayout={(e) => {
const {height, width} = e.nativeEvent.layout;
if (height === wrapperMeasuredHeightAnimated.get()) {
Expand All @@ -137,7 +144,7 @@ function BaseGenericTooltip({
<View style={pointerWrapperStyle}>
<View style={pointerStyle} />
</View>
</Animated.View>
</AnimatedWrapper>
</Portal>
);
}
Expand Down
Loading
Loading