Skip to content
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

Add onTooltipPress callback to various tooltip components #55454

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
2 changes: 2 additions & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4610,6 +4610,8 @@ const CONST = {
TOOLBAR: 'toolbar',
/** Use for navigation elements */
NAVIGATION: 'navigation',
/** Use for Tooltips */
TOOLTIP: 'tooltip',
},
TRANSLATION_KEYS: {
ATTACHMENT: 'common.attachment',
Expand Down
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
5 changes: 5 additions & 0 deletions src/components/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
import useThemeStyles from '@hooks/useThemeStyles';
import ControlSelection from '@libs/ControlSelection';
import convertToLTR from '@libs/convertToLTR';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';

Check failure on line 13 in src/components/MenuItem.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Namespace imports from @libs are not allowed. Use named imports instead. Example: import { method } from "@libs/module"
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';

Check failure on line 18 in src/components/MenuItem.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Namespace imports from @userActions are not allowed. Use named imports instead. Example: import { action } from "@userActions/module"
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 @@
/** 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 @@
tooltipShiftHorizontal = 0,
tooltipShiftVertical = 0,
renderTooltipContent,
onEducationTooltipPress,
additionalIconStyles,
shouldShowSelectedItemCheck = false,
shouldIconUseAutoWidthStyle = false,
Expand Down Expand Up @@ -601,6 +605,7 @@
shiftHorizontal={tooltipShiftHorizontal}
shiftVertical={tooltipShiftVertical}
shouldTeleportPortalToModalLayer={shouldTeleportPortalToModalLayer}
onTooltipPress={onEducationTooltipPress}
>
<View>
<Hoverable>
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
55 changes: 37 additions & 18 deletions src/components/ThreeDotsMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
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';

Check failure on line 13 in src/components/ThreeDotsMenu/index.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Namespace imports from @libs are not allowed. Use named imports instead. Example: import { method } from "@libs/module"
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 @@
shouldSetModalVisibility = true,
disabled = false,
hideProductTrainingTooltip,
renderProductTrainingTooltipContent,
shouldShowProductTrainingTooltip = false,
}: ThreeDotsMenuProps) {
const [modal] = useOnyx(ONYXKEYS.MODAL);

Expand All @@ -55,27 +59,42 @@
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. */
Expand All @@ -94,7 +113,7 @@
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;
16 changes: 15 additions & 1 deletion src/components/Tooltip/BaseGenericTooltip/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {View} from 'react-native';
import type {View as RNView} from 'react-native';
import Animated, {useAnimatedStyle, useSharedValue} from 'react-native-reanimated';
import TransparentOverlay from '@components/AutoCompleteSuggestions/AutoCompleteSuggestionsPortal/TransparentOverlay/TransparentOverlay';
import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeedback';
import Text from '@components/Text';
import useStyleUtils from '@hooks/useStyleUtils';
import CONST from '@src/CONST';
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,6 +115,18 @@ function BaseGenericTooltip({
);
}

const contentWithOrWithoutPressable = isEducationTooltip ? (
<PressableWithoutFeedback
onPress={onTooltipPress}
role={CONST.ROLE.TOOLTIP}
accessibilityLabel={CONST.ROLE.TOOLTIP}
>
{content}
</PressableWithoutFeedback>
) : (
content
);

return (
<Portal hostName={shouldTeleportPortalToModalLayer ? 'modal' : undefined}>
{shouldUseOverlay && <TransparentOverlay onPress={onHideTooltip} />}
Expand All @@ -133,7 +147,7 @@ function BaseGenericTooltip({
setWrapperMeasuredHeight(height);
}}
>
{content}
{contentWithOrWithoutPressable}
<View style={pointerWrapperStyle}>
<View style={pointerStyle} />
</View>
Expand Down
Loading
Loading