diff --git a/src/CONST/index.ts b/src/CONST/index.ts index 1458ce407cb2..35e2c2bcf5ce 100644 --- a/src/CONST/index.ts +++ b/src/CONST/index.ts @@ -7586,6 +7586,7 @@ const CONST = { // that should be removed from the copied contents in the getHTMLOfSelection() method SELECTION_SCRAPER_HIDDEN_ELEMENT: 'selection-scrapper-hidden-element', INNER_BOX_SHADOW_ELEMENT: 'inner-box-shadow-element', + TAB_BAR_LABEL_BOLD_PLACEHOLDER: 'tab-bar-label-bold-placeholder', COPYABLE_TEXT_ELEMENT: 'copyable-text', COPYABLE_ROW_ELEMENT: 'copyable-row', COPYABLE_ROW_CONTINUATION_ELEMENT: 'copyable-row-continuation', diff --git a/src/components/Navigation/FlatNavigationBar/FlatNavItem.tsx b/src/components/Navigation/FlatNavigationBar/FlatNavItem.tsx index c4d38bf6bb05..e7cad6d7ddc4 100644 --- a/src/components/Navigation/FlatNavigationBar/FlatNavItem.tsx +++ b/src/components/Navigation/FlatNavigationBar/FlatNavItem.tsx @@ -11,6 +11,7 @@ import useThemeStyles from '@hooks/useThemeStyles'; import variables from '@styles/variables'; import CONST from '@src/CONST'; +import type {Route} from '@src/ROUTES'; import type IconAsset from '@src/types/utils/IconAsset'; import type {SentryLabel} from '@src/types/utils/SentryLabel'; @@ -50,6 +51,9 @@ type FlatNavItemProps = { onPress: () => void; + /** Route of the destination, so the row can be opened in a new browser tab */ + linkRoute?: Route; + /** Called when the pointer moves onto the row */ onHoverIn?: () => void; @@ -70,6 +74,7 @@ function FlatNavItem({ accessibilityLabel, sentryLabel, onPress, + linkRoute, onHoverIn, onHoverOut, }: FlatNavItemProps) { @@ -90,6 +95,7 @@ function FlatNavItem({ return ( setIsSearchTabHovered(true)} onHoverOut={() => setIsSearchTabHovered(false)} /> @@ -238,6 +247,7 @@ function FlatNavigationBar({selectedTab}: FlatNavigationBarProps) { isSelected={selectedTab === NAVIGATION_TABS.INSIGHTS} sentryLabel={CONST.SENTRY_LABEL.NAVIGATION_TAB_BAR.INSIGHTS} onPress={navigateToInsights} + linkRoute={getSignedInLinkRoute(DEFAULT_INSIGHTS_TAB_ROUTE)} /> @@ -275,6 +286,7 @@ function FlatNavigationBar({selectedTab}: FlatNavigationBarProps) { accessibilityLabel={getTabAccessibilityLabel(`${accountLabel}, ${translate('sidebarScreen.buttonMySettings')}`, !!accountStatus, translate)} sentryLabel={CONST.SENTRY_LABEL.NAVIGATION_TAB_BAR.ACCOUNT} onPress={navigateToAccount} + linkRoute={getSignedInLinkRoute(ROUTES.SETTINGS)} /> diff --git a/src/components/Navigation/NavigationTabBar/InboxTabButton.tsx b/src/components/Navigation/NavigationTabBar/InboxTabButton.tsx index 533028574eae..e91b55ea8f12 100644 --- a/src/components/Navigation/NavigationTabBar/InboxTabButton.tsx +++ b/src/components/Navigation/NavigationTabBar/InboxTabButton.tsx @@ -6,6 +6,7 @@ import useLocalize from '@hooks/useLocalize'; import useThemeStyles from '@hooks/useThemeStyles'; import CONST from '@src/CONST'; +import ROUTES from '@src/ROUTES'; import type {ValueOf} from 'type-fest'; @@ -40,6 +41,7 @@ function WideInboxTabButton({selectedTab, statusIndicatorColor, accessibilityLab return ( ; @@ -25,11 +27,14 @@ function InsightsTabButton({selectedTab, isWideLayout}: InsightsTabButtonProps) const expensifyIcons = useMemoizedLazyExpensifyIcons(['PieChart']); const isSelected = selectedTab === NAVIGATION_TABS.INSIGHTS; const navigateToInsights = useInsightsTabNavigation(isSelected); + const getSignedInLinkRoute = useSignedInLinkRoute(); + const insightsLinkRoute = getSignedInLinkRoute(DEFAULT_INSIGHTS_TAB_ROUTE); if (isWideLayout) { return ( ; @@ -27,6 +29,8 @@ function SearchTabButton({selectedTab, isWideLayout}: SearchTabButtonProps) { const expensifyIcons = useMemoizedLazyExpensifyIcons(['ReceiptMultiple']); const searchAccessibilityState = {selected: selectedTab === NAVIGATION_TABS.SEARCH}; const navigateToSearch = useSearchTabNavigation(selectedTab === NAVIGATION_TABS.SEARCH); + const getSignedInLinkRoute = useSignedInLinkRoute(); + const searchTabRoute = getSignedInLinkRoute(useStoredSearchTabRoute()); // The wide button gets no hover out when it goes away while hovered, on a switch to the narrow layout or an unmount. useEffect(() => () => setIsSearchTabHovered(false), [isWideLayout]); @@ -35,6 +39,7 @@ function SearchTabButton({selectedTab, isWideLayout}: SearchTabButtonProps) { return ( {label} - - {label} - + /> ); } diff --git a/src/components/Navigation/NavigationTabBar/TabBarLabelBoldPlaceholder/index.native.tsx b/src/components/Navigation/NavigationTabBar/TabBarLabelBoldPlaceholder/index.native.tsx new file mode 100644 index 000000000000..3bb5b4eb79eb --- /dev/null +++ b/src/components/Navigation/NavigationTabBar/TabBarLabelBoldPlaceholder/index.native.tsx @@ -0,0 +1,19 @@ +import Text from '@components/Text'; + +import React from 'react'; + +import type TabBarLabelBoldPlaceholderProps from './types'; + +function TabBarLabelBoldPlaceholder({label, numberOfLines, style}: TabBarLabelBoldPlaceholderProps) { + return ( + + {label} + + ); +} + +export default TabBarLabelBoldPlaceholder; diff --git a/src/components/Navigation/NavigationTabBar/TabBarLabelBoldPlaceholder/index.tsx b/src/components/Navigation/NavigationTabBar/TabBarLabelBoldPlaceholder/index.tsx new file mode 100644 index 000000000000..6e757025cea2 --- /dev/null +++ b/src/components/Navigation/NavigationTabBar/TabBarLabelBoldPlaceholder/index.tsx @@ -0,0 +1,23 @@ +import Text from '@components/Text'; + +import CONST from '@src/CONST'; + +import React from 'react'; + +import type TabBarLabelBoldPlaceholderProps from './types'; + +/** The label is drawn by a CSS pseudo-element, so it stays out of the text a browser takes when the tab link is dragged or copied. */ +function TabBarLabelBoldPlaceholder({label, numberOfLines, style}: TabBarLabelBoldPlaceholderProps) { + return ( + + {null} + + ); +} + +export default TabBarLabelBoldPlaceholder; diff --git a/src/components/Navigation/NavigationTabBar/TabBarLabelBoldPlaceholder/types.ts b/src/components/Navigation/NavigationTabBar/TabBarLabelBoldPlaceholder/types.ts new file mode 100644 index 000000000000..cb738b5d77bb --- /dev/null +++ b/src/components/Navigation/NavigationTabBar/TabBarLabelBoldPlaceholder/types.ts @@ -0,0 +1,14 @@ +import type {StyleProp, TextStyle} from 'react-native'; + +type TabBarLabelBoldPlaceholderProps = { + /** Tab label whose bold width is reserved. On web it goes in a data attribute instead of the text, so it stays out of the link text */ + label: string; + + /** Number of lines the visible label is limited to */ + numberOfLines: number; + + /** Style of the visible label in bold, so the placeholder takes the same width */ + style: StyleProp; +}; + +export default TabBarLabelBoldPlaceholderProps; diff --git a/src/components/Navigation/NavigationTabBar/WorkspacesTabButton.tsx b/src/components/Navigation/NavigationTabBar/WorkspacesTabButton.tsx index 6d8552d450a7..bb9042998929 100644 --- a/src/components/Navigation/NavigationTabBar/WorkspacesTabButton.tsx +++ b/src/components/Navigation/NavigationTabBar/WorkspacesTabButton.tsx @@ -7,6 +7,7 @@ import useThemeStyles from '@hooks/useThemeStyles'; import useWorkspacesTabIndicatorStatus from '@hooks/useWorkspacesTabIndicatorStatus'; import CONST from '@src/CONST'; +import ROUTES from '@src/ROUTES'; import type {ValueOf} from 'type-fest'; @@ -15,6 +16,7 @@ import React from 'react'; import getTabAccessibilityLabel from './getTabAccessibilityLabel'; import NAVIGATION_TABS from './NAVIGATION_TABS'; import TabBarItem from './TabBarItem'; +import useSignedInLinkRoute from './useSignedInLinkRoute'; type WorkspacesTabButtonProps = { selectedTab: ValueOf; @@ -28,6 +30,8 @@ function WorkspacesTabButton({selectedTab, isWideLayout}: WorkspacesTabButtonPro const {indicatorColor: workspacesTabIndicatorColor, status: workspacesTabIndicatorStatus} = useWorkspacesTabIndicatorStatus(); const navigateToWorkspaces = useRestoreWorkspacesTabOnNavigate(); + const getSignedInLinkRoute = useSignedInLinkRoute(); + const workspacesLinkRoute = getSignedInLinkRoute(ROUTES.WORKSPACES_LIST.route); const workspacesAccessibilityState = {selected: selectedTab === NAVIGATION_TABS.WORKSPACES}; const workspacesStatusIndicatorColor = workspacesTabIndicatorStatus ? workspacesTabIndicatorColor : undefined; @@ -36,6 +40,7 @@ function WorkspacesTabButton({selectedTab, isWideLayout}: WorkspacesTabButtonPro return ( route.name === SCREENS.INSIGHTS)?.params; @@ -16,7 +19,8 @@ function getInsightsTabRoute(rootState: NavigationState | undefined, accessibleD return ROUTES.INSIGHTS.getRoute(lastDashboardID); } - return ROUTES.INSIGHTS.getRoute(CONST.INSIGHTS.DASHBOARD.SPEND); + return DEFAULT_INSIGHTS_TAB_ROUTE; } export default getInsightsTabRoute; +export {DEFAULT_INSIGHTS_TAB_ROUTE}; diff --git a/src/components/Navigation/NavigationTabBar/index.tsx b/src/components/Navigation/NavigationTabBar/index.tsx index add295d0ea3d..8420ec890603 100644 --- a/src/components/Navigation/NavigationTabBar/index.tsx +++ b/src/components/Navigation/NavigationTabBar/index.tsx @@ -18,6 +18,7 @@ import SupportalSwitcherButton from '@pages/inbox/sidebar/SupportalSwitcherButto import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; +import ROUTES from '@src/ROUTES'; import type {ValueOf} from 'type-fest'; @@ -73,6 +74,7 @@ function NavigationTabBar({selectedTab, shouldShowFloatingButtons = true}: Navig accessible testID="ExpensifyLogoButton" onPress={navigateToNewDotHome} + linkRoute={ROUTES.HOME} wrapperStyle={styles.leftNavigationTabBarItem} sentryLabel={CONST.SENTRY_LABEL.NAVIGATION_TAB_BAR.EXPENSIFY_LOGO} > @@ -84,6 +86,7 @@ function NavigationTabBar({selectedTab, shouldShowFloatingButtons = true}: Navig [styles.leftNavigationTabBarItem, hovered && styles.navigationTabBarItemHovered]} @@ -140,6 +143,7 @@ function NavigationTabBar({selectedTab, shouldShowFloatingButtons = true}: Navig > Route | undefined { + const [isAnonymousUser = false] = useOnyx(ONYXKEYS.SESSION, {selector: isAnonymousSessionSelector}); + + return (route: Route) => (isAnonymousUser ? undefined : route); +} + +export default useSignedInLinkRoute; diff --git a/src/components/Navigation/NavigationTabBar/useStoredSearchTabRoute.ts b/src/components/Navigation/NavigationTabBar/useStoredSearchTabRoute.ts new file mode 100644 index 000000000000..1e0966849464 --- /dev/null +++ b/src/components/Navigation/NavigationTabBar/useStoredSearchTabRoute.ts @@ -0,0 +1,24 @@ +import useOnyx from '@hooks/useOnyx'; + +import ONYXKEYS from '@src/ONYXKEYS'; +import ROUTES from '@src/ROUTES'; +import {lastExpensesSearchQuerySelector} from '@src/selectors/SearchFilters'; +import type LastSearchParams from '@src/types/onyx/ReportNavigation'; + +import type {OnyxEntry} from 'react-native-onyx'; + +import {getStoredSearchTabParams} from './getSearchTabRoute'; + +function lastSearchQuerySelector(lastSearchParams: OnyxEntry): LastSearchParams { + return {queryJSON: lastSearchParams?.queryJSON, searchKey: lastSearchParams?.searchKey}; +} + +/** Returns the Spend route a new browser tab opens. A new tab has no navigation history, so it starts from the last search kept in Onyx. */ +function useStoredSearchTabRoute() { + const [lastSearchParams] = useOnyx(ONYXKEYS.REPORT_NAVIGATION_LAST_SEARCH_QUERY, {selector: lastSearchQuerySelector}); + const [lastExpensesSearchQuery] = useOnyx(ONYXKEYS.SEARCH_FILTERS, {selector: lastExpensesSearchQuerySelector}); + + return ROUTES.SEARCH_ROOT.getRoute(getStoredSearchTabParams(lastSearchParams, lastExpensesSearchQuery)); +} + +export default useStoredSearchTabRoute; diff --git a/src/components/Pressable/GenericPressable/implementation/BaseGenericPressable.tsx b/src/components/Pressable/GenericPressable/implementation/BaseGenericPressable.tsx index 53aa5b03bdb3..42fba89239e8 100644 --- a/src/components/Pressable/GenericPressable/implementation/BaseGenericPressable.tsx +++ b/src/components/Pressable/GenericPressable/implementation/BaseGenericPressable.tsx @@ -9,6 +9,8 @@ import useThemeStyles from '@hooks/useThemeStyles'; import Accessibility from '@libs/Accessibility'; import HapticFeedback from '@libs/HapticFeedback'; import mergeRefs from '@libs/mergeRefs'; +import getRoutePath from '@libs/Navigation/helpers/getRoutePath'; +import {isNewTabOrWindowMousePress} from '@libs/Navigation/helpers/openInternalRouteInNewTab'; import {notifyPressedTrigger, registerPressable} from '@libs/NavigationFocusReturn'; import CONST from '@src/CONST'; @@ -55,6 +57,7 @@ function GenericPressable({ dataSet, forwardedFSClass, accessibilityState, + linkRoute, ...rest }: BaseGenericPressableProps) { const styles = useThemeStyles(); @@ -154,7 +157,28 @@ function GenericPressable({ onPressHandler(event); }; - const pressOnce = (event: GestureResponderEvent) => singleExecution(onPressHandler)(event); + const pressOnce = (event?: GestureResponderEvent) => singleExecution(onPressHandler)(event); + + const hasHref = !!linkRoute && !isDisabled && !fullDisabled && interactive; + + // The browser follows the href unless the click is prevented, so only Cmd, Ctrl or Shift clicks on an enabled link reach it and open a new tab or window. + const pressLink = (event?: GestureResponderEvent) => { + if (hasHref && isNewTabOrWindowMousePress(event)) { + return; + } + event?.preventDefault(); + if (isDisabled) { + return; + } + pressOnce(event); + }; + + const getPressHandler = () => { + if (linkRoute) { + return pressLink; + } + return !isDisabled ? pressOnce : undefined; + }; const {shortcutKey, descriptionKey, modifiers} = keyboardShortcut ?? {}; useKeyboardShortcut({shortcutKey, descriptionKey, modifiers} as ValueOf, onKeyboardShortcutPressHandler, { @@ -170,6 +194,7 @@ function GenericPressable({ * If a custom onKeyDown handler is provided, it delegates to that handler. * Otherwise, for elements with role="link", it triggers onPress when Enter is pressed * to comply with W3C APG Link Pattern (https://www.w3.org/WAI/ARIA/apg/patterns/link/). + * An element with an href is skipped, because the browser turns Enter into a click that keeps its modifier keys for pressLink. */ const handleKeyDown = (event: ViewKeyDownEvent) => { if (onKeyDown) { @@ -177,7 +202,7 @@ function GenericPressable({ return; } - if (isRoleLink && event.nativeEvent.key === CONST.KEYBOARD_SHORTCUTS.ENTER.shortcutKey) { + if (isRoleLink && !hasHref && event.nativeEvent.key === CONST.KEYBOARD_SHORTCUTS.ENTER.shortcutKey) { event.preventDefault(); onPressHandler(event.nativeEvent as unknown as KeyboardEvent); } @@ -189,7 +214,7 @@ function GenericPressable({ onLayout={shouldUseAutoHitSlop ? onLayout : undefined} ref={composedRef} disabled={fullDisabled || undefined} - onPress={!isDisabled ? pressOnce : undefined} + onPress={getPressHandler()} onLongPress={!isDisabled && onLongPress ? onLongPressHandler : undefined} onKeyDown={!isDisabled ? handleKeyDown : undefined} onPressIn={!isDisabled ? onPressIn : undefined} @@ -220,6 +245,7 @@ function GenericPressable({ onAccessibilityTap={!isDisabled ? voidOnPressHandler : undefined} accessible={accessible} fsClass={forwardedFSClass} + href={hasHref && linkRoute ? getRoutePath(linkRoute) : undefined} {...rest} onHoverOut={(event) => { if (event?.type === 'pointerenter' || event?.type === 'mouseenter') { diff --git a/src/components/Pressable/GenericPressable/types.ts b/src/components/Pressable/GenericPressable/types.ts index fe6c9b592e21..3a7aa0f3d523 100644 --- a/src/components/Pressable/GenericPressable/types.ts +++ b/src/components/Pressable/GenericPressable/types.ts @@ -3,6 +3,7 @@ import type {ForwardedFSClassProps} from '@libs/Fullstory/types'; import type {Shortcut} from '@libs/KeyboardShortcut'; import type CONST from '@src/CONST'; +import type {Route} from '@src/ROUTES'; import type {WithSentryLabelWhenPressable} from '@src/types/utils/SentryLabel'; import type {ComponentRef, ForwardedRef} from 'react'; @@ -172,6 +173,9 @@ type PressableProps = RNPressableProps & */ shouldAllowTextSelection?: boolean; + /** Renders the pressable as a link to this route on web, so it can be opened in a new tab */ + linkRoute?: Route; + ref?: PressableRef; }; diff --git a/src/libs/Navigation/helpers/getRoutePath.ts b/src/libs/Navigation/helpers/getRoutePath.ts new file mode 100644 index 000000000000..42723d30f541 --- /dev/null +++ b/src/libs/Navigation/helpers/getRoutePath.ts @@ -0,0 +1,8 @@ +import type {Route} from '@src/ROUTES'; + +/** Starts the route at the site root, so a link to it does not resolve against the current page's path. */ +function getRoutePath(route: Route): string { + return route.startsWith('/') ? route : `/${route}`; +} + +export default getRoutePath; diff --git a/src/libs/Navigation/helpers/openInternalRouteInNewTab/index.native.ts b/src/libs/Navigation/helpers/openInternalRouteInNewTab/index.native.ts index 1c079166b48f..9949ad77a6b1 100644 --- a/src/libs/Navigation/helpers/openInternalRouteInNewTab/index.native.ts +++ b/src/libs/Navigation/helpers/openInternalRouteInNewTab/index.native.ts @@ -4,8 +4,10 @@ import type {ModifiedMouseEvent} from './types'; const isModifiedMousePress: (event?: ModifiedMouseEvent) => boolean = () => false; +const isNewTabOrWindowMousePress: (event?: ModifiedMouseEvent) => boolean = () => false; + const openInternalRouteInNewTab: (route: Route, event?: ModifiedMouseEvent) => boolean = () => false; -export {isModifiedMousePress}; +export {isModifiedMousePress, isNewTabOrWindowMousePress}; export type {ModifiedMouseEvent}; export default openInternalRouteInNewTab; diff --git a/src/libs/Navigation/helpers/openInternalRouteInNewTab/index.ts b/src/libs/Navigation/helpers/openInternalRouteInNewTab/index.ts index e0bbce2b3b36..8c2ee58a1246 100644 --- a/src/libs/Navigation/helpers/openInternalRouteInNewTab/index.ts +++ b/src/libs/Navigation/helpers/openInternalRouteInNewTab/index.ts @@ -1,20 +1,32 @@ +import getRoutePath from '@libs/Navigation/helpers/getRoutePath'; + import type {Route} from '@src/ROUTES'; import type {ModifiedMouseEvent, ModifiedMouseEventSource} from './types'; function getRouteURL(route: Route) { - return new URL(route.startsWith('/') ? route : `/${route}`, window.location.origin).toString(); + return new URL(getRoutePath(route), window.location.origin).toString(); } -function isModifiedMousePress(event?: ModifiedMouseEvent) { +function getPrimaryButtonMouseEvent(event?: ModifiedMouseEvent): ModifiedMouseEventSource | undefined { const mouseEvent = event?.nativeEvent && typeof event.nativeEvent === 'object' ? (event.nativeEvent as ModifiedMouseEventSource) : event; if (!mouseEvent || 'key' in mouseEvent) { - return false; + return undefined; } const isPrimaryButton = mouseEvent.button === undefined || mouseEvent.button === 0; - return isPrimaryButton && (!!mouseEvent.metaKey || !!mouseEvent.ctrlKey); + return isPrimaryButton ? mouseEvent : undefined; +} + +function isModifiedMousePress(event?: ModifiedMouseEvent) { + const mouseEvent = getPrimaryButtonMouseEvent(event); + return !!mouseEvent?.metaKey || !!mouseEvent?.ctrlKey; +} + +/** Browsers open a link in a new window on a Shift click, so a real link leaves that click to the browser too. */ +function isNewTabOrWindowMousePress(event?: ModifiedMouseEvent) { + return isModifiedMousePress(event) || !!getPrimaryButtonMouseEvent(event)?.shiftKey; } function openInternalRouteInNewTab(route: Route, event?: ModifiedMouseEvent) { @@ -28,6 +40,6 @@ function openInternalRouteInNewTab(route: Route, event?: ModifiedMouseEvent) { return true; } -export {isModifiedMousePress}; +export {isModifiedMousePress, isNewTabOrWindowMousePress}; export type {ModifiedMouseEvent}; export default openInternalRouteInNewTab; diff --git a/src/libs/Navigation/helpers/openInternalRouteInNewTab/types.ts b/src/libs/Navigation/helpers/openInternalRouteInNewTab/types.ts index f1c30410b64e..01778378bf3a 100644 --- a/src/libs/Navigation/helpers/openInternalRouteInNewTab/types.ts +++ b/src/libs/Navigation/helpers/openInternalRouteInNewTab/types.ts @@ -3,11 +3,12 @@ type ModifiedMouseEvent = { stopPropagation?: () => void; metaKey?: boolean; ctrlKey?: boolean; + shiftKey?: boolean; button?: number; key?: string; nativeEvent?: unknown; }; -type ModifiedMouseEventSource = Pick; +type ModifiedMouseEventSource = Pick; export type {ModifiedMouseEvent, ModifiedMouseEventSource}; diff --git a/src/pages/inbox/sidebar/NavigationTabBarAvatar.tsx b/src/pages/inbox/sidebar/NavigationTabBarAvatar.tsx index 4625cec84305..456a9a4c721e 100644 --- a/src/pages/inbox/sidebar/NavigationTabBarAvatar.tsx +++ b/src/pages/inbox/sidebar/NavigationTabBarAvatar.tsx @@ -1,6 +1,7 @@ import AccountNavigationAvatar from '@components/Avatar/AccountNavigationAvatar'; import getTabAccessibilityLabel from '@components/Navigation/NavigationTabBar/getTabAccessibilityLabel'; import TabBarLabel from '@components/Navigation/NavigationTabBar/TabBarLabel'; +import useSignedInLinkRoute from '@components/Navigation/NavigationTabBar/useSignedInLinkRoute'; import {PressableWithFeedback} from '@components/Pressable'; import useAccountTabIndicatorStatus from '@hooks/useAccountTabIndicatorStatus'; @@ -9,6 +10,7 @@ import useResponsiveLayout from '@hooks/useResponsiveLayout'; import useThemeStyles from '@hooks/useThemeStyles'; import CONST from '@src/CONST'; +import ROUTES from '@src/ROUTES'; import type {StyleProp, ViewStyle} from 'react-native'; @@ -32,10 +34,13 @@ function NavigationTabBarAvatar({onPress, isSelected = false, style}: Navigation const {status} = useAccountTabIndicatorStatus(); const accountAccessibilityState = {selected: isSelected}; + const getSignedInLinkRoute = useSignedInLinkRoute(); + const accountLinkRoute = getSignedInLinkRoute(ROUTES.SETTINGS); return ( jest.requireActual>('@libs/Navigation/helpers/openInternalRouteInNewTab/index.ts')); + +const TEST_ID = 'link-pressable'; + +function renderLinkPressable(onPress: () => void, isDisabled = false, isInteractive = true) { + render( + + Settings + , + ); +} + +describe('Pressable with linkRoute', () => { + it('renders the route as an absolute href', () => { + // Given a pressable linking to the settings route + // When it renders + renderLinkPressable(jest.fn()); + + // Then the href starts at the site root, so it does not resolve against the current page's path + expect(screen.getByTestId(TEST_ID).props.href).toBe(`/${ROUTES.SETTINGS}`); + }); + + it('navigates in the app and stops the browser from loading the href on a plain click', () => { + // Given a pressable linking to the settings route + const onPress = jest.fn(); + const preventDefault = jest.fn(); + renderLinkPressable(onPress); + + // When it is clicked without a modifier key + fireEvent.press(screen.getByTestId(TEST_ID), {nativeEvent: {button: 0}, preventDefault}); + + // Then the browser's page load is prevented and the in-app handler runs instead + expect(preventDefault).toHaveBeenCalled(); + expect(onPress).toHaveBeenCalled(); + }); + + it('leaves a Cmd click to the browser so the route opens in a new tab', () => { + // Given a pressable linking to the settings route + const onPress = jest.fn(); + const preventDefault = jest.fn(); + renderLinkPressable(onPress); + + // When it is clicked with the Cmd key held + fireEvent.press(screen.getByTestId(TEST_ID), {nativeEvent: {button: 0, metaKey: true}, preventDefault}); + + // Then the current tab does not navigate and the browser keeps its default new tab behavior + expect(preventDefault).not.toHaveBeenCalled(); + expect(onPress).not.toHaveBeenCalled(); + }); + + it('leaves a Shift click to the browser so the route opens in a new window', () => { + // Given a pressable linking to the settings route + const onPress = jest.fn(); + const preventDefault = jest.fn(); + renderLinkPressable(onPress); + + // When it is clicked with the Shift key held + fireEvent.press(screen.getByTestId(TEST_ID), {nativeEvent: {button: 0, shiftKey: true}, preventDefault}); + + // Then the current tab does not navigate and the browser keeps its default new window behavior + expect(preventDefault).not.toHaveBeenCalled(); + expect(onPress).not.toHaveBeenCalled(); + }); + + it('leaves Enter to the browser so Cmd or Ctrl reaches the click it fires', () => { + // Given a pressable with the link role linking to the settings route + const onPress = jest.fn(); + const preventDefault = jest.fn(); + renderLinkPressable(onPress); + + // When Enter is pressed on it + fireEvent(screen.getByTestId(TEST_ID), 'keyDown', {key: CONST.KEYBOARD_SHORTCUTS.ENTER.shortcutKey, nativeEvent: {}, preventDefault}); + + // Then the key press does not navigate on its own, so the browser's click decides between this tab and a new one + expect(preventDefault).not.toHaveBeenCalled(); + expect(onPress).not.toHaveBeenCalled(); + }); + + it('renders no href when not interactive', () => { + // Given a pressable linking to the settings route that ignores presses + // When it renders + renderLinkPressable(jest.fn(), false, false); + + // Then there is no href, so a Cmd click cannot open a route a plain click would not + expect(screen.getByTestId(TEST_ID).props.href).toBeUndefined(); + }); + + it('renders no href when disabled', () => { + // Given a disabled pressable linking to the settings route + // When it renders + renderLinkPressable(jest.fn(), true); + + // Then there is no href, so the browser context menu cannot open the route in a new tab + expect(screen.getByTestId(TEST_ID).props.href).toBeUndefined(); + }); + + it('does not follow the href when disabled', () => { + // Given a disabled pressable linking to the settings route + const onPress = jest.fn(); + const preventDefault = jest.fn(); + renderLinkPressable(onPress, true); + + // When it is clicked with the Ctrl key held + fireEvent.press(screen.getByTestId(TEST_ID), {nativeEvent: {button: 0, ctrlKey: true}, preventDefault}); + + // Then neither the browser nor the app navigates + expect(preventDefault).toHaveBeenCalled(); + expect(onPress).not.toHaveBeenCalled(); + }); +}); diff --git a/tests/unit/useSignedInLinkRouteTest.ts b/tests/unit/useSignedInLinkRouteTest.ts new file mode 100644 index 000000000000..acb6dc874966 --- /dev/null +++ b/tests/unit/useSignedInLinkRouteTest.ts @@ -0,0 +1,46 @@ +import {renderHook} from '@testing-library/react-native'; + +import useSignedInLinkRoute from '@components/Navigation/NavigationTabBar/useSignedInLinkRoute'; + +import CONST from '@src/CONST'; +import ONYXKEYS from '@src/ONYXKEYS'; +import ROUTES from '@src/ROUTES'; + +import Onyx from 'react-native-onyx'; + +import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; + +describe('useSignedInLinkRoute', () => { + beforeAll(() => { + Onyx.init({keys: ONYXKEYS}); + }); + + beforeEach(() => { + Onyx.clear(); + return waitForBatchedUpdates(); + }); + + it('returns the route for a signed in user', async () => { + // Given a signed in user + await Onyx.merge(ONYXKEYS.SESSION, {authToken: 'token', email: 'user@expensify.com'}); + await waitForBatchedUpdates(); + + // When the hook resolves the Account tab link + const {result} = renderHook(() => useSignedInLinkRoute()); + + // Then the tab can be opened in a new browser tab + expect(result.current(ROUTES.SETTINGS)).toBe(ROUTES.SETTINGS); + }); + + it('returns no route for an anonymous user', async () => { + // Given an anonymous user, who must sign in before opening the Account tab + await Onyx.merge(ONYXKEYS.SESSION, {authTokenType: CONST.AUTH_TOKEN_TYPES.ANONYMOUS}); + await waitForBatchedUpdates(); + + // When the hook resolves the Account tab link + const {result} = renderHook(() => useSignedInLinkRoute()); + + // Then there is no link, so a Cmd click cannot open the tab without the sign in prompt + expect(result.current(ROUTES.SETTINGS)).toBeUndefined(); + }); +}); diff --git a/web/index.html b/web/index.html index 56913734446f..ffaabc08c111 100644 --- a/web/index.html +++ b/web/index.html @@ -87,6 +87,10 @@ pointer-events: none; z-index: 1000; } + /* Reserve the width of a tab's bold label without putting the label in the link text that browsers drag and copy */ + [data-tab-bar-label-bold-placeholder]::after { + content: attr(data-tab-bar-label-bold-placeholder); + } :focus[data-focusvisible-polyfill]:not([data-programmatic-focus="true"]) { outline: 0; box-shadow: inset 0px 0px 0px 1px #5AB0FF;