From 49af5ec42e4f03129f7878939538936b1f3c9a92 Mon Sep 17 00:00:00 2001 From: Jakub Magiera Date: Wed, 11 Nov 2026 22:30:45 +0100 Subject: [PATCH 01/12] Collapse the nav bar into a skinny sidebar --- cspell.json | 1 + src/CONST/SENTRY_LABEL.ts | 1 + .../FlatNavCollapseToggle.tsx | 55 ++++ .../FlatNavigationBar/FlatNavCreateButton.tsx | 62 ++-- .../FlatNavigationBar/FlatNavItem.tsx | 111 ++++--- .../FlatNavigationBar/FlatNavLogo.tsx | 8 +- .../Navigation/FlatNavigationBar/index.tsx | 279 ++++++++++++------ .../useNavigationBarWidth.ts | 20 +- src/components/Navigation/SearchSidebar.tsx | 5 +- .../Navigation/SearchSidebarCollapseStore.ts | 95 ++++-- .../Navigators/TabNavigatorBar.tsx | 15 +- src/styles/index.ts | 35 +++ src/styles/variables.ts | 4 + 13 files changed, 508 insertions(+), 183 deletions(-) create mode 100644 src/components/Navigation/FlatNavigationBar/FlatNavCollapseToggle.tsx diff --git a/cspell.json b/cspell.json index 2eaf9f788c42..071695095751 100644 --- a/cspell.json +++ b/cspell.json @@ -314,6 +314,7 @@ "POLICYCHANGELOG_ADD_EMPLOYEE", "POWERFORM", "Parcelable", + "Peekable", "Passwordless", "Payoneer", "Pekao", diff --git a/src/CONST/SENTRY_LABEL.ts b/src/CONST/SENTRY_LABEL.ts index 3964b04a8156..e8188c0e68df 100644 --- a/src/CONST/SENTRY_LABEL.ts +++ b/src/CONST/SENTRY_LABEL.ts @@ -145,6 +145,7 @@ const SENTRY_LABEL = { WORKSPACES: 'NavigationTabBar-Workspaces', ACCOUNT: 'NavigationTabBar-Account', HOME: 'NavigationTabBar-Home', + COLLAPSE: 'NavigationTabBar-Collapse', FLOATING_ACTION_BUTTON: 'NavigationTabBar-FloatingActionButton', FLOATING_RECEIPT_BUTTON: 'NavigationTabBar-FloatingReceiptButton', FLOATING_GPS_BUTTON: 'NavigationTabBar-FloatingGpsButton', diff --git a/src/components/Navigation/FlatNavigationBar/FlatNavCollapseToggle.tsx b/src/components/Navigation/FlatNavigationBar/FlatNavCollapseToggle.tsx new file mode 100644 index 000000000000..2ed4e2682636 --- /dev/null +++ b/src/components/Navigation/FlatNavigationBar/FlatNavCollapseToggle.tsx @@ -0,0 +1,55 @@ +/** The flat navigation bar's toggle for collapsing it into an icon rail and expanding it back. */ +import SidebarLeftIcon from '@assets/images/sidebar-left.svg'; +import SidebarRightIcon from '@assets/images/sidebar-right.svg'; + +import Icon from '@components/Icon'; +import {PressableWithFeedback} from '@components/Pressable'; +import Tooltip from '@components/Tooltip'; + +import useLocalize from '@hooks/useLocalize'; +import useTheme from '@hooks/useTheme'; +import useThemeStyles from '@hooks/useThemeStyles'; + +import variables from '@styles/variables'; + +import CONST from '@src/CONST'; + +import React from 'react'; + +type FlatNavCollapseToggleProps = { + isCollapsed: boolean; + + onPress: () => void; +}; + +function FlatNavCollapseToggle({isCollapsed, onPress}: FlatNavCollapseToggleProps) { + const styles = useThemeStyles(); + const theme = useTheme(); + const {translate} = useLocalize(); + + const label = translate(isCollapsed ? 'reportActionCompose.expand' : 'reportActionCompose.collapse'); + + return ( + + + + + + ); +} + +export default FlatNavCollapseToggle; diff --git a/src/components/Navigation/FlatNavigationBar/FlatNavCreateButton.tsx b/src/components/Navigation/FlatNavigationBar/FlatNavCreateButton.tsx index e8e88170400d..9c6f351a7641 100644 --- a/src/components/Navigation/FlatNavigationBar/FlatNavCreateButton.tsx +++ b/src/components/Navigation/FlatNavigationBar/FlatNavCreateButton.tsx @@ -1,5 +1,6 @@ /** The flat navigation bar's Create button, together with the create menu it opens right below itself. */ import Button from '@components/Button'; +import Tooltip from '@components/Tooltip'; import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset'; import useLocalize from '@hooks/useLocalize'; @@ -7,7 +8,6 @@ import usePopoverPosition from '@hooks/usePopoverPosition'; import useThemeStyles from '@hooks/useThemeStyles'; import CreateMenuPopover from '@pages/inbox/sidebar/FABPopoverContent/CreateMenuPopover'; -import useCreateMenuVisibility from '@pages/inbox/sidebar/FABPopoverContent/useCreateMenuVisibility'; import CONST from '@src/CONST'; import type {AnchorPosition} from '@src/styles'; @@ -16,20 +16,37 @@ import type AnchorAlignment from '@src/types/utils/AnchorAlignment'; import type {ComponentRef} from 'react'; import type {View} from 'react-native'; -import React, {useRef, useState} from 'react'; +import {useRef, useState} from 'react'; const CREATE_MENU_ANCHOR_ALIGNMENT: AnchorAlignment = { horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.LEFT, vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.TOP, }; +type FlatNavCreateButtonProps = { + /** Shrinks the button to a round icon-only "+", for the collapsed bar */ + isCompact?: boolean; + + /** Active/expanded state of the create button */ + isCreateMenuActive: boolean; + + /** Whether the create menu popover is visible */ + isCreateMenuVisible: boolean; + + /** Shows the create menu */ + showCreateMenu: () => void; + + /** Hides the create menu */ + hideCreateMenu: () => void; +}; + /** Opens the same create menu as the floating action button. */ -function FlatNavCreateButton() { +function FlatNavCreateButton({isCompact = false, isCreateMenuActive, isCreateMenuVisible, showCreateMenu, hideCreateMenu}: FlatNavCreateButtonProps) { const styles = useThemeStyles(); const {translate} = useLocalize(); const icons = useMemoizedLazyExpensifyIcons(['Plus']); const {calculatePopoverPosition} = usePopoverPosition(); - const {isCreateMenuActive, isCreateMenuVisible, showCreateMenu, hideCreateMenu} = useCreateMenuVisibility(); + const buttonRef = useRef>(null); const isOpeningRef = useRef(false); const [anchorPosition, setAnchorPosition] = useState(); @@ -61,23 +78,26 @@ function FlatNavCreateButton() { anchorPosition={anchorPosition} anchorAlignment={CREATE_MENU_ANCHOR_ALIGNMENT} /> - + {/* The tooltip stays mounted in both modes and only has text while compact (e.g. for keyboard focus), so collapsing does not remount the button the menu is anchored to. */} + + + ); } diff --git a/src/components/Navigation/FlatNavigationBar/FlatNavItem.tsx b/src/components/Navigation/FlatNavigationBar/FlatNavItem.tsx index b9f930be6218..5f30386f189a 100644 --- a/src/components/Navigation/FlatNavigationBar/FlatNavItem.tsx +++ b/src/components/Navigation/FlatNavigationBar/FlatNavItem.tsx @@ -1,7 +1,9 @@ import Icon from '@components/Icon'; import getTabIconFill from '@components/Navigation/NavigationTabBar/getTabIconFill'; +import {getCollapseFadeStyle} from '@components/Navigation/SearchSidebarCollapseStore'; import {PressableWithFeedback} from '@components/Pressable'; import Text from '@components/Text'; +import Tooltip from '@components/Tooltip'; import useStyleUtils from '@hooks/useStyleUtils'; import useTheme from '@hooks/useTheme'; @@ -17,6 +19,7 @@ import type {ColorValue} from 'react-native'; import React from 'react'; import {View} from 'react-native'; +import Animated from 'react-native-reanimated'; type FlatNavItemProps = { /** Translated row label */ @@ -32,6 +35,9 @@ type FlatNavItemProps = { isSelected: boolean; + /** Whether the row keeps only its icon, so the bar can narrow to an icon rail */ + isCollapsed?: boolean; + /** Draws the row taller than the destination rows, as the Account row is */ isTall?: boolean; @@ -59,6 +65,7 @@ function FlatNavItem({ renderLeftElement, rightElement, isSelected, + isCollapsed = false, isTall = false, statusIndicatorColor, accessibilityLabel, @@ -78,56 +85,64 @@ function FlatNavItem({ return isHovered ? styles.highlightBG : undefined; }; + // The label and right element stay mounted and fade, rather than disappearing the moment the bar starts narrowing. + const collapseFadeStyle = getCollapseFadeStyle(isCollapsed); + + // Always mounted, with the text only while collapsed, so a row does not remount when the bar collapses. + // Hovering expands the bar to reveal the full text; keyboard focus leaves it collapsed, so the tooltip informs keyboard users. return ( - [styles.flatNavigationBarItem, isTall && styles.flatNavigationBarAccountItem, getRowBackgroundStyle(hovered)]} - > - {({hovered}) => { - const rowBackgroundColor = getRowBackgroundStyle(hovered)?.backgroundColor; - return ( - <> - {!!icon && ( - - - {!!statusIndicatorColor && ( - + [styles.flatNavigationBarItem, isTall && styles.flatNavigationBarAccountItem, getRowBackgroundStyle(hovered)]} + > + {({hovered}) => { + const rowBackgroundColor = getRowBackgroundStyle(hovered)?.backgroundColor; + return ( + <> + {!!icon && ( + + - )} - - )} - {renderLeftElement?.({backgroundColor: rowBackgroundColor})} - - {label} - - {rightElement} - - ); - }} - + {!!statusIndicatorColor && ( + + )} + + )} + {renderLeftElement?.({backgroundColor: rowBackgroundColor})} + + + {label} + + + {!!rightElement && {rightElement}} + + ); + }} + + ); } diff --git a/src/components/Navigation/FlatNavigationBar/FlatNavLogo.tsx b/src/components/Navigation/FlatNavigationBar/FlatNavLogo.tsx index 1f3a970bc612..ac47aa9238c3 100644 --- a/src/components/Navigation/FlatNavigationBar/FlatNavLogo.tsx +++ b/src/components/Navigation/FlatNavigationBar/FlatNavLogo.tsx @@ -13,10 +13,13 @@ import React from 'react'; type FlatNavLogoProps = { onPress: () => void; + + /** Whether the navigation bar is visually collapsed, so the logo fades out and stops accepting interaction. */ + isCollapsed?: boolean; }; /** Expensify wordmark at the top of the flat navigation bar, which leads Home like the logo in the icon bar. */ -function FlatNavLogo({onPress}: FlatNavLogoProps) { +function FlatNavLogo({onPress, isCollapsed = false}: FlatNavLogoProps) { const theme = useTheme(); const {translate} = useLocalize(); const icons = useMemoizedLazyExpensifyIcons(['ExpensifyWordmark']); @@ -28,6 +31,9 @@ function FlatNavLogo({onPress}: FlatNavLogoProps) { testID="ExpensifyLogoButton" onPress={onPress} sentryLabel={CONST.SENTRY_LABEL.NAVIGATION_TAB_BAR.EXPENSIFY_LOGO} + aria-hidden={isCollapsed} + tabIndex={isCollapsed ? -1 : 0} + disabled={isCollapsed} > () => setIsSearchTabHovered(false), []); + // A peek has no hover out to end it when the bar goes away while hovered, on a switch to the narrow layout or an unmount. + useEffect(() => endPeek, [endPeek]); + + const {isCreateMenuActive, isCreateMenuVisible, showCreateMenu, hideCreateMenu} = useCreateMenuVisibility(); + const barContainerRef = useRef(null); + const closeFrameIdRef = useRef(null); + const prevIsCreateMenuVisibleRef = useRef(isCreateMenuVisible); + + useEffect(() => { + if (isCreateMenuVisible && closeFrameIdRef.current !== null) { + cancelAnimationFrame(closeFrameIdRef.current); + closeFrameIdRef.current = null; + } + + if (prevIsCreateMenuVisibleRef.current && !isCreateMenuVisible) { + if (closeFrameIdRef.current !== null) { + cancelAnimationFrame(closeFrameIdRef.current); + } + // FAB_OUT dismissal takes ~1ms on web, so giving the modal overlay one animation frame + // allows it to unmount before recalculating whether the pointer is over the bar. + closeFrameIdRef.current = requestAnimationFrame(() => { + closeFrameIdRef.current = null; + const element = barContainerRef.current; + const isHovered = isHTMLElement(element) && element.matches(':hover'); + if (!isHovered) { + endPeek(); + } + }); + } + + prevIsCreateMenuVisibleRef.current = isCreateMenuVisible; + }, [endPeek, isCreateMenuVisible]); + + const handleBarHoverOut = () => { + if (isCreateMenuVisible) { + return; + } + endPeek(); + }; + + useEffect(() => { + return () => { + if (closeFrameIdRef.current === null) { + return; + } + cancelAnimationFrame(closeFrameIdRef.current); + }; + }, []); + const accountLabel = currentUserPersonalDetails.displayName ? currentUserPersonalDetails.displayName : translate('initialSettingsPage.account'); return ( <> {!!isDebugModeEnabled && } - - {(isBarHovered) => ( - - - - - - - - + + {(isBarHovered) => ( + - - - - setIsSearchTabHovered(true)} - onHoverOut={() => setIsSearchTabHovered(false)} - /> - - - - - - - - - - ( - + + - )} - rightElement={} - isSelected={isAccountSelected} - isTall - accessibilityLabel={getTabAccessibilityLabel(`${accountLabel}, ${translate('sidebarScreen.buttonMySettings')}`, !!accountStatus, translate)} - sentryLabel={CONST.SENTRY_LABEL.NAVIGATION_TAB_BAR.ACCOUNT} - onPress={navigateToAccount} - /> - - - )} - + + + + {/* The header and footer stay out of the peek, so pressing the toggle never races the bar widening under the pointer. */} + + + + + + + + + + setIsSearchTabHovered(true)} + onHoverOut={() => setIsSearchTabHovered(false)} + /> + + + + + + + + + + + + ( + + + + )} + rightElement={} + isSelected={isAccountSelected} + isTall + accessibilityLabel={getTabAccessibilityLabel(`${accountLabel}, ${translate('sidebarScreen.buttonMySettings')}`, !!accountStatus, translate)} + sentryLabel={CONST.SENTRY_LABEL.NAVIGATION_TAB_BAR.ACCOUNT} + onPress={navigateToAccount} + /> + + + )} + + ); } diff --git a/src/components/Navigation/FlatNavigationBar/useNavigationBarWidth.ts b/src/components/Navigation/FlatNavigationBar/useNavigationBarWidth.ts index c9334f96a821..edeee153c94e 100644 --- a/src/components/Navigation/FlatNavigationBar/useNavigationBarWidth.ts +++ b/src/components/Navigation/FlatNavigationBar/useNavigationBarWidth.ts @@ -1,16 +1,26 @@ +import {getFlatNavigationBarLayoutWidth} from '@components/Navigation/SearchSidebarCollapseStore'; + +import useOnyx from '@hooks/useOnyx'; + import variables from '@styles/variables'; +import ONYXKEYS from '@src/ONYXKEYS'; + import useShouldUseFlatNavigationBar from './useShouldUseFlatNavigationBar'; -/** Width of the wide layout's left navigation bar for the given bar variant. */ -function getNavigationBarWidth(shouldUseFlatNavigationBar: boolean) { - return shouldUseFlatNavigationBar ? variables.flatNavigationBarWidth : variables.navigationTabBarSize; +/** Width of the wide layout's left navigation bar for the given bar variant. Peeking overlays the content, so only the collapsed flag counts. */ +function getNavigationBarWidth(shouldUseFlatNavigationBar: boolean, isCollapsed = false) { + return shouldUseFlatNavigationBar ? getFlatNavigationBarLayoutWidth(isCollapsed) : variables.navigationTabBarSize; } /** Width of the wide layout's left navigation bar, which everything placed next to it is offset by. */ function useNavigationBarWidth() { - return getNavigationBarWidth(useShouldUseFlatNavigationBar()); + const shouldUseFlatNavigationBar = useShouldUseFlatNavigationBar(); + const [isCollapsed = false] = useOnyx(ONYXKEYS.NVP_SEARCH_SIDEBAR, { + selector: (nvp) => nvp?.isCollapsed ?? false, + }); + + return getNavigationBarWidth(shouldUseFlatNavigationBar, isCollapsed); } export default useNavigationBarWidth; -export {getNavigationBarWidth}; diff --git a/src/components/Navigation/SearchSidebar.tsx b/src/components/Navigation/SearchSidebar.tsx index c0d7d37e9242..45e4c4eca82a 100644 --- a/src/components/Navigation/SearchSidebar.tsx +++ b/src/components/Navigation/SearchSidebar.tsx @@ -29,6 +29,7 @@ import React, {useEffect} from 'react'; import {View} from 'react-native'; import Animated from 'react-native-reanimated'; +import useShouldUseFlatNavigationBar from './FlatNavigationBar/useShouldUseFlatNavigationBar'; import { useSearchSidebarCollapse, useSearchSidebarCollapseFadeStyle, @@ -49,6 +50,7 @@ function SearchSidebar({state}: SearchSidebarProps) { const {isOffline} = useNetwork(); const shouldShowLoadingBarForReports = useLoadingBarVisibility(); const {shouldUseNarrowLayout} = useResponsiveLayout(); + const shouldUseFlatNavigationBar = useShouldUseFlatNavigationBar(); const {isCollapsed, toggleSidebar, startPeek, endPeek} = useSearchSidebarCollapse(); const layoutSpacerStyle = useSearchSidebarLayoutWidthStyle(); const visualSidebarWidthStyle = useSearchSidebarVisualWidthStyle(); @@ -117,7 +119,8 @@ function SearchSidebar({state}: SearchSidebarProps) { shouldDisplaySearch={false} shouldDisplayHelpButton={false} > - {toggleButton} + {/* The flat navigation bar has its own toggle for the one flag both share. */} + {!shouldUseFlatNavigationBar && toggleButton} diff --git a/src/components/Navigation/SearchSidebarCollapseStore.ts b/src/components/Navigation/SearchSidebarCollapseStore.ts index c33e6a9e1c83..fe6374d257a7 100644 --- a/src/components/Navigation/SearchSidebarCollapseStore.ts +++ b/src/components/Navigation/SearchSidebarCollapseStore.ts @@ -7,6 +7,7 @@ import variables from '@styles/variables'; import ONYXKEYS from '@src/ONYXKEYS'; import type {ViewStyle} from 'react-native'; +import type {ValueOf} from 'type-fest'; import {useSyncExternalStore} from 'react'; import {Platform} from 'react-native'; @@ -15,12 +16,27 @@ const SEARCH_SIDEBAR_COLLAPSE_ANIMATION_DURATION_MS = 220; const SEARCH_SIDEBAR_COLLAPSE_TRANSLATE_X = -8; const TOGGLE_BUTTON_COLLAPSED_TRANSLATE_X = -10; +/** + * The surfaces that share the collapsed flag. They collapse together, but each one peeks on its own: hovering the + * flat navigation bar must not widen the Spend sidebar next to it, and the other way round. + */ +const COLLAPSE_SURFACE = { + NAVIGATION_BAR: 'navigationBar', + SEARCH_SIDEBAR: 'searchSidebar', +} as const; + +type CollapseSurface = ValueOf; + const layoutTransitionStyle: ViewStyle = Platform.OS === 'web' ? {transition: `width ${SEARCH_SIDEBAR_COLLAPSE_ANIMATION_DURATION_MS}ms ease, margin-left ${SEARCH_SIDEBAR_COLLAPSE_ANIMATION_DURATION_MS}ms ease`} : {}; const fadeTransitionStyle: ViewStyle = Platform.OS === 'web' ? {transition: `opacity ${SEARCH_SIDEBAR_COLLAPSE_ANIMATION_DURATION_MS}ms ease, transform ${SEARCH_SIDEBAR_COLLAPSE_ANIMATION_DURATION_MS}ms ease`} : {}; -let isPeeking = false; +// The account avatar's box is wider than a row's icon, so collapsed it would sit right of the icon column. +// Sliding it by half that difference lines it up, and a transform animates where a padding change would jump. +const ACCOUNT_AVATAR_COLLAPSED_TRANSLATE_X = -(variables.avatarSizeSmall - variables.iconSizeNormal) / 2; + +let peekingSurface: CollapseSurface | null = null; const listeners = new Set<() => void>(); @@ -38,7 +54,7 @@ function notify() { } function getPeekSnapshot() { - return isPeeking; + return peekingSurface; } function getSearchSidebarWidth(progress: number) { @@ -46,43 +62,48 @@ function getSearchSidebarWidth(progress: number) { } function setSearchSidebarCollapsed(collapsed: boolean) { - isPeeking = false; + peekingSurface = null; notify(); SearchSidebarActions.setCollapsed(collapsed); } -function startPeek(isCollapsed: boolean) { - if (!isCollapsed || isPeeking) { +function startPeek(surface: CollapseSurface, isCollapsed: boolean) { + if (!isCollapsed || peekingSurface !== null) { return; } - isPeeking = true; + peekingSurface = surface; notify(); } -function endPeek() { - if (!isPeeking) { +function endPeek(surface: CollapseSurface) { + if (peekingSurface !== surface) { return; } - isPeeking = false; + peekingSurface = null; notify(); } -function useSearchSidebarCollapse() { +const endPeekBySurface: Record void> = { + [COLLAPSE_SURFACE.NAVIGATION_BAR]: () => endPeek(COLLAPSE_SURFACE.NAVIGATION_BAR), + [COLLAPSE_SURFACE.SEARCH_SIDEBAR]: () => endPeek(COLLAPSE_SURFACE.SEARCH_SIDEBAR), +}; + +function useSearchSidebarCollapse(surface: CollapseSurface = COLLAPSE_SURFACE.SEARCH_SIDEBAR) { const [searchSidebarNVP] = useOnyx(ONYXKEYS.NVP_SEARCH_SIDEBAR); const collapsed = searchSidebarNVP?.isCollapsed ?? false; - const peeking = useSyncExternalStore(subscribe, getPeekSnapshot, getPeekSnapshot); + const peeking = useSyncExternalStore(subscribe, getPeekSnapshot, getPeekSnapshot) === surface; const toggleSidebar = () => setSearchSidebarCollapsed(!collapsed); - const startSidebarPeek = () => startPeek(collapsed); + const startSurfacePeek = () => startPeek(surface, collapsed); return { isCollapsed: collapsed, isPeeking: peeking, isVisuallyCollapsed: collapsed && !peeking, toggleSidebar, - startPeek: startSidebarPeek, - endPeek, + startPeek: startSurfacePeek, + endPeek: endPeekBySurface[surface], }; } @@ -104,9 +125,8 @@ function useSearchSidebarContentOffsetStyle() { return {...layoutTransitionStyle, marginLeft: getSearchSidebarWidth(collapsed ? 1 : 0)}; } -function useSearchSidebarCollapseFadeStyle() { - const {isVisuallyCollapsed} = useSearchSidebarCollapse(); - +/** Fades a label out and nudges it toward the icon column while its surface is visually collapsed. */ +function getCollapseFadeStyle(isVisuallyCollapsed: boolean): ViewStyle { return { ...fadeTransitionStyle, opacity: isVisuallyCollapsed ? 0 : 1, @@ -114,6 +134,12 @@ function useSearchSidebarCollapseFadeStyle() { }; } +function useSearchSidebarCollapseFadeStyle() { + const {isVisuallyCollapsed} = useSearchSidebarCollapse(); + + return getCollapseFadeStyle(isVisuallyCollapsed); +} + function useSearchSidebarToggleButtonStyle() { const {isVisuallyCollapsed} = useSearchSidebarCollapse(); @@ -123,8 +149,43 @@ function useSearchSidebarToggleButtonStyle() { }; } +/** + * Layout space the flat navigation bar reserves. Peeking overlays the content rather than pushing it, so this + * ignores the peek. + */ +function getFlatNavigationBarLayoutWidth(isCollapsed: boolean): number { + return isCollapsed ? variables.flatNavigationBarCollapsedWidth : variables.flatNavigationBarWidth; +} + +/** The layout box the flat navigation bar reserves, which eases with the bar so the content beside it follows. */ +function getFlatNavigationBarLayoutWidthStyle(isCollapsed: boolean): ViewStyle { + return {...layoutTransitionStyle, width: getFlatNavigationBarLayoutWidth(isCollapsed)}; +} + +/** What the flat navigation bar draws, which widens back out while the pointer peeks at it. */ +function getFlatNavigationBarVisualWidthStyle(isVisuallyCollapsed: boolean): ViewStyle { + return {...layoutTransitionStyle, width: getFlatNavigationBarLayoutWidth(isVisuallyCollapsed)}; +} + +/** The shadow only reads while a collapsed bar peeks over the content. Expanded, the bar has nothing to sit above. */ +function getFlatNavigationBarPeekShadowStyle(isPeeking: boolean): ViewStyle { + return {...fadeTransitionStyle, opacity: isPeeking ? 1 : 0}; +} + +function getFlatNavigationBarAccountAvatarStyle(isVisuallyCollapsed: boolean): ViewStyle { + return {...fadeTransitionStyle, transform: [{translateX: isVisuallyCollapsed ? ACCOUNT_AVATAR_COLLAPSED_TRANSLATE_X : 0}]}; +} + export { + COLLAPSE_SURFACE, SEARCH_SIDEBAR_COLLAPSE_ANIMATION_DURATION_MS, + layoutTransitionStyle, + getCollapseFadeStyle, + getFlatNavigationBarAccountAvatarStyle, + getFlatNavigationBarLayoutWidth, + getFlatNavigationBarLayoutWidthStyle, + getFlatNavigationBarPeekShadowStyle, + getFlatNavigationBarVisualWidthStyle, useSearchSidebarCollapse, useSearchSidebarLayoutWidthStyle, useSearchSidebarVisualWidthStyle, diff --git a/src/libs/Navigation/AppNavigator/Navigators/TabNavigatorBar.tsx b/src/libs/Navigation/AppNavigator/Navigators/TabNavigatorBar.tsx index d34a70869dd9..761d1ff88020 100644 --- a/src/libs/Navigation/AppNavigator/Navigators/TabNavigatorBar.tsx +++ b/src/libs/Navigation/AppNavigator/Navigators/TabNavigatorBar.tsx @@ -1,10 +1,11 @@ import {useFullScreenBlockingViewState} from '@components/FullScreenBlockingViewContextProvider'; import FlatNavigationBar from '@components/Navigation/FlatNavigationBar'; -import {getNavigationBarWidth} from '@components/Navigation/FlatNavigationBar/useNavigationBarWidth'; +import useNavigationBarWidth from '@components/Navigation/FlatNavigationBar/useNavigationBarWidth'; import useShouldUseFlatNavigationBar from '@components/Navigation/FlatNavigationBar/useShouldUseFlatNavigationBar'; import NavigationTabBar from '@components/Navigation/NavigationTabBar'; import NAVIGATION_TABS from '@components/Navigation/NavigationTabBar/NAVIGATION_TABS'; import ROUTE_TO_NAVIGATION_TAB from '@components/Navigation/NavigationTabBar/ROUTE_TO_NAVIGATION_TAB'; +import {COLLAPSE_SURFACE, layoutTransitionStyle, useSearchSidebarCollapse} from '@components/Navigation/SearchSidebarCollapseStore'; import {SetNavigatorTabBarVisibleContext} from '@components/Navigation/TabBarBottomContent/NavigatorTabBarVisibilityProvider'; import usePrevious from '@hooks/usePrevious'; @@ -37,7 +38,8 @@ function TabNavigatorBar({state}: Pick) { const styles = useThemeStyles(); const StyleUtils = useStyleUtils(); const shouldUseFlatNavigationBar = useShouldUseFlatNavigationBar(); - const navigationBarWidth = getNavigationBarWidth(shouldUseFlatNavigationBar); + const navigationBarWidth = useNavigationBarWidth(); + const {isPeeking: isNavigationBarPeeking} = useSearchSidebarCollapse(COLLAPSE_SURFACE.NAVIGATION_BAR); const activeRoute = state.routes[state.index]; const selectedTab = ROUTE_TO_NAVIGATION_TAB[activeRoute?.name ?? SCREENS.HOME] ?? NAVIGATION_TABS.HOME; const isAtRoot = isTabRouteAtRoot(activeRoute); @@ -96,9 +98,16 @@ function TabNavigatorBar({state}: Pick) { // When the screen is not blocking the view, we need to raise the tab bar above the screen content so the DebugTabView is visible. // The container also spans the sidebar next to the bar, so floating content such as the debug view is not clipped. // tabNavigatorBarContainer's negative margin gives that width back, leaving only the bar's width as layout space. + // The width is what holds the central pane off the left edge, so it eases with the bar instead of snapping to the new width. + // A peeking bar is raised even over a blocking view, since it is drawn over the content. return ( {shouldUseFlatNavigationBar ? : } diff --git a/src/styles/index.ts b/src/styles/index.ts index c5efae431fd5..d17e4bcbfa92 100644 --- a/src/styles/index.ts +++ b/src/styles/index.ts @@ -885,11 +885,37 @@ const staticStyles = (theme: ThemeColors) => backgroundColor: theme.appBG, }, + // Drawn as its own layer so the shadow can fade on its own, and only while a collapsed bar peeks over the content. + flatNavigationBarPeekShadow: { + ...StyleSheet.absoluteFill, + boxShadow: theme.shadow, + }, + flatNavigationBarHeader: { flexDirection: 'row', alignItems: 'center', + justifyContent: 'flex-end', height: variables.flatNavigationBarHeaderHeight, paddingLeft: variables.flatNavigationBarRowInset + variables.flatNavigationBarRowPadding, + paddingRight: variables.flatNavigationBarHeaderPaddingRight, + // The logo fades out in place while the toggle stays, so the header has to clip the logo as the bar narrows. + overflow: 'hidden', + }, + + // Lets the wordmark fade out in place while the toggle is the only thing left in the header. + flatNavigationBarLogoContainer: { + position: 'absolute', + left: variables.flatNavigationBarRowInset + variables.flatNavigationBarRowPadding, + top: 0, + bottom: 0, + justifyContent: 'center', + }, + + flatNavigationBarCollapseToggle: { + width: variables.iconSizeNormal, + height: variables.iconSizeNormal, + alignItems: 'center', + justifyContent: 'center', }, flatNavigationBarCreateButtonContainer: { @@ -902,6 +928,13 @@ const staticStyles = (theme: ThemeColors) => minHeight: variables.flatNavigationBarCreateButtonHeight, }, + // Collapsed, the Create button shrinks to a circle. Its container's inset centers it in the rail. + flatNavigationBarCreateButtonCompact: { + width: variables.flatNavigationBarCreateButtonHeight, + borderRadius: variables.componentBorderRadiusRounded, + paddingHorizontal: 0, + }, + flatNavigationBarItem: { flexDirection: 'row', alignItems: 'center', @@ -910,6 +943,8 @@ const staticStyles = (theme: ThemeColors) => paddingHorizontal: variables.flatNavigationBarRowPadding, marginHorizontal: variables.flatNavigationBarRowInset, borderRadius: variables.componentBorderRadiusNormal, + // Labels stay mounted while they fade, so a collapsed row has to clip what no longer fits. + overflow: 'hidden', }, flatNavigationBarAccountItem: { diff --git a/src/styles/variables.ts b/src/styles/variables.ts index 851f71e66b55..8e7ea3a2309b 100644 --- a/src/styles/variables.ts +++ b/src/styles/variables.ts @@ -199,6 +199,10 @@ export default { searchSidebarCollapsedWidth: 76, navigationTabBarSize: 72, flatNavigationBarWidth: 232, + // Collapsed, a row shows only its icon: the row's inset (8) and padding (12) on each side around the 20px icon. + flatNavigationBarCollapsedWidth: 60, + // Expanded, the collapse toggle sits this far from the bar's right edge. Collapsed, the bar narrows until the toggle lands on the icon column. + flatNavigationBarHeaderPaddingRight: 20, flatNavigationBarItemHeight: 40, flatNavigationBarAccountItemHeight: 56, // Horizontal inset every row's background sits at, inside the bar's own width. From 04b181419e7dcfb932dc57c5f6ab90ab8227fd8b Mon Sep 17 00:00:00 2001 From: Jakub Magiera Date: Wed, 11 Nov 2026 22:31:03 +0100 Subject: [PATCH 02/12] Add tests for collapsed navigation bar --- tests/ui/FlatNavCreateButtonTest.tsx | 79 +++++- tests/ui/FlatNavigationBarTest.tsx | 235 +++++++++++++++++- .../ui/TabNavigatorBarFlatNavigationTest.tsx | 45 ++++ tests/unit/SearchSidebarCollapseStoreTest.tsx | 161 ++++++++++++ tests/unit/useNavigationBarWidthTest.tsx | 114 +++++++++ 5 files changed, 630 insertions(+), 4 deletions(-) create mode 100644 tests/unit/SearchSidebarCollapseStoreTest.tsx create mode 100644 tests/unit/useNavigationBarWidthTest.tsx diff --git a/tests/ui/FlatNavCreateButtonTest.tsx b/tests/ui/FlatNavCreateButtonTest.tsx index fb64acf8d733..37e8220d618f 100644 --- a/tests/ui/FlatNavCreateButtonTest.tsx +++ b/tests/ui/FlatNavCreateButtonTest.tsx @@ -5,6 +5,8 @@ import {LocaleContextProvider} from '@components/LocaleContextProvider'; import FlatNavCreateButton from '@components/Navigation/FlatNavigationBar/FlatNavCreateButton'; import OnyxListItemProvider from '@components/OnyxListItemProvider'; +import useCreateMenuVisibility from '@pages/inbox/sidebar/FABPopoverContent/useCreateMenuVisibility'; + import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {AnchorPosition} from '@src/styles'; @@ -53,10 +55,23 @@ jest.mock('@react-navigation/native', () => ({ useFocusEffect: jest.fn(), })); -function renderButton() { +function Harness({isCompact = false}: {isCompact?: boolean}) { + const menu = useCreateMenuVisibility(); + return ( + + ); +} + +function renderButton(isCompact = false) { render( - + , ); } @@ -144,4 +159,64 @@ describe('FlatNavCreateButton', () => { expect(mockCalculatePopoverPosition).toHaveBeenCalledTimes(1); expect(screen.getByTestId('CreateMenuPopover')).toBeOnTheScreen(); }); + + it('shrinks to an icon-only button when compact', async () => { + // Given the Create button in a collapsed bar + + // When the button renders + renderButton(true); + await waitForBatchedUpdatesWithAct(); + + // Then the "Create" text is gone, but the button keeps its name for screen readers + expect(screen.queryByText('Create')).not.toBeOnTheScreen(); + expect(screen.getByRole(CONST.ROLE.BUTTON, {name: 'Create'})).toBeOnTheScreen(); + }); + + it('still opens the menu when compact', async () => { + // Given the compact Create button in a collapsed bar + renderButton(true); + await waitForBatchedUpdatesWithAct(); + + // When it is pressed + pressCreate(); + await waitForBatchedUpdatesWithAct(); + + // Then it opens the same menu, so collapsing the bar loses no action + expect(screen.getByTestId('CreateMenuPopover')).toBeOnTheScreen(); + }); + + it('shows the "Create" text when not compact', async () => { + // Given the Create button in the expanded bar + + // When the button renders + renderButton(); + await waitForBatchedUpdatesWithAct(); + + // Then the text is drawn next to the icon + expect(screen.getByText('Create')).toBeOnTheScreen(); + }); + + it('calls showCreateMenu when the button is pressed while inactive', async () => { + // Given show and hide handlers + const showCreateMenu = jest.fn(); + const hideCreateMenu = jest.fn(); + render( + + + , + ); + await waitForBatchedUpdatesWithAct(); + + // When the button is pressed + pressCreate(); + await waitForBatchedUpdatesWithAct(); + + // Then the external show handler is called + expect(showCreateMenu).toHaveBeenCalledTimes(1); + }); }); diff --git a/tests/ui/FlatNavigationBarTest.tsx b/tests/ui/FlatNavigationBarTest.tsx index d70a9f04a01e..f991d3b4db45 100644 --- a/tests/ui/FlatNavigationBarTest.tsx +++ b/tests/ui/FlatNavigationBarTest.tsx @@ -1,4 +1,4 @@ -import {cleanup, fireEvent, render, screen, within} from '@testing-library/react-native'; +import {act, cleanup, fireEvent, render, screen, within} from '@testing-library/react-native'; import ComposeProviders from '@components/ComposeProviders'; import {CurrentUserPersonalDetailsProvider} from '@components/CurrentUserPersonalDetailsProvider'; @@ -11,13 +11,17 @@ import {signOutAndRedirectToSignIn} from '@libs/actions/Session'; import {getIsSearchTabHovered, setIsSearchTabHovered} from '@libs/Navigation/helpers/searchTabPreloadStore'; import Navigation from '@libs/Navigation/Navigation'; +import variables from '@styles/variables'; + import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; +import type {StyleProp, ViewStyle} from 'react-native'; import type {ValueOf} from 'type-fest'; import React from 'react'; +import {StyleSheet} from 'react-native'; import Onyx from 'react-native-onyx'; import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct'; @@ -40,13 +44,57 @@ jest.mock('@components/Navigation/NavigationTabBar/useInsightsTabNavigation', () jest.mock('@hooks/useRestoreWorkspacesTabOnNavigate', () => () => mockNavigateToWorkspaces); jest.mock('@hooks/useInboxTabIndicatorStatus', () => () => mockInboxIndicator); jest.mock('@hooks/useWorkspacesTabIndicatorStatus', () => () => mockWorkspacesIndicator); -jest.mock('@components/Navigation/FlatNavigationBar/FlatNavCreateButton', () => () => null); +jest.mock('@react-navigation/native', () => ({ + ...jest.requireActual>('@react-navigation/native'), + useIsFocused: () => true, + useFocusEffect: jest.fn(), +})); +type MockCreateButtonProps = { + isCompact?: boolean; + isCreateMenuActive?: boolean; + isCreateMenuVisible?: boolean; + showCreateMenu?: () => void; + hideCreateMenu?: () => void; +}; +let mockCreateButtonProps: MockCreateButtonProps | undefined; +jest.mock('@components/Navigation/FlatNavigationBar/FlatNavCreateButton', () => (props: MockCreateButtonProps) => { + mockCreateButtonProps = props; + return null; +}); +let mockIsElementHovered = false; +jest.mock('@libs/isHTMLElement', () => ({ + __esModule: true, + default: (val: unknown): val is HTMLElement => { + if (!mockIsElementHovered || typeof val !== 'object' || val === null) { + return false; + } + const target = val; + // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- attach matches method to test instance for mock hover verification + (target as {matches?: (selector: string) => boolean}).matches = (selector: string) => selector === ':hover'; + return true; + }, +})); jest.mock('@pages/inbox/sidebar/SupportalSwitcherButton', () => () => null); jest.mock('@components/Avatar/AccountNavigationAvatar', () => () => null); jest.mock('@libs/Navigation/Navigation', () => ({ __esModule: true, default: {navigate: jest.fn()}, })); +let mockOuterHoverOut: (() => void) | undefined; +let mockInnerHoverIn: (() => void) | undefined; +jest.mock( + '@components/Hoverable', + () => + ({children, onHoverIn, onHoverOut}: {children: React.ReactNode | ((isHovered: boolean) => React.ReactNode); onHoverIn?: () => void; onHoverOut?: () => void}) => { + if (onHoverOut) { + mockOuterHoverOut = onHoverOut; + } + if (onHoverIn) { + mockInnerHoverIn = onHoverIn; + } + return typeof children === 'function' ? children(false) : children; + }, +); jest.mock('@libs/actions/Session', () => ({ ...jest.requireActual>('@libs/actions/Session'), signOutAndRedirectToSignIn: jest.fn(), @@ -63,6 +111,14 @@ function renderBar(selectedTab: ValueOf) { ); } +function getSavedSidebarState() { + return Onyx.get(ONYXKEYS.NVP_SEARCH_SIDEBAR); +} + +function isStyleProp(value: unknown): value is StyleProp { + return value === undefined || value === null || typeof value === 'object'; +} + function getRow(name: string | RegExp) { return screen.getByRole(CONST.ROLE.TAB, {name}); } @@ -74,6 +130,10 @@ describe('FlatNavigationBar', () => { beforeEach(async () => { jest.clearAllMocks(); + mockCreateButtonProps = undefined; + mockIsElementHovered = false; + mockOuterHoverOut = undefined; + mockInnerHoverIn = undefined; mockInboxIndicator = NO_INDICATOR; mockWorkspacesIndicator = NO_INDICATOR; setIsSearchTabHovered(false); @@ -255,6 +315,177 @@ describe('FlatNavigationBar', () => { expect(Navigation.navigate).toHaveBeenCalledWith(ROUTES.SETTINGS); }); + describe('collapsing', () => { + function getBarWidth() { + const style: unknown = screen.getByTestId('FlatNavigationBar').props.style; + return isStyleProp(style) ? StyleSheet.flatten(style)?.width : undefined; + } + + it('collapses into a rail when the toggle is pressed and saves the state', async () => { + // Given the expanded bar + renderBar(NAVIGATION_TABS.HOME); + await waitForBatchedUpdatesWithAct(); + expect(getBarWidth()).toBe(variables.flatNavigationBarWidth); + + // When the toggle next to the logo is pressed + fireEvent.press(screen.getByRole(CONST.ROLE.BUTTON, {name: 'Collapse'})); + await waitForBatchedUpdatesWithAct(); + + // Then the bar narrows to the rail, the flag is saved for the next load, and the toggle now offers to expand + expect(getBarWidth()).toBe(variables.flatNavigationBarCollapsedWidth); + expect(await getSavedSidebarState()).toEqual({isCollapsed: true}); + expect(screen.getByRole(CONST.ROLE.BUTTON, {name: 'Expand'})).toBeOnTheScreen(); + }); + + it('renders collapsed on load when the saved state says so', async () => { + // Given a collapsed state saved by an earlier session + await Onyx.set(ONYXKEYS.NVP_SEARCH_SIDEBAR, {isCollapsed: true}); + + // When the bar mounts + renderBar(NAVIGATION_TABS.HOME); + await waitForBatchedUpdatesWithAct(); + + // Then it is already the rail, so a reload does not flash the wide bar first + expect(getBarWidth()).toBe(variables.flatNavigationBarCollapsedWidth); + }); + + it('keeps every row reachable by its name while collapsed', async () => { + // Given a collapsed bar with the Inbox flagged for review + mockInboxIndicator = REVIEW_REQUIRED; + await Onyx.set(ONYXKEYS.NVP_SEARCH_SIDEBAR, {isCollapsed: true}); + renderBar(NAVIGATION_TABS.HOME); + await waitForBatchedUpdatesWithAct(); + + // When the rows are read and one is pressed + fireEvent.press(getRow('Spend')); + + // Then the labels are still the rows' accessible names, since only their drawing fades, and a press still navigates + expect(getRow(`Inbox. ${REVIEW_REQUIRED_SUFFIX}`)).toBeOnTheScreen(); + expect(getRow('Workspaces')).toBeOnTheScreen(); + expect(mockNavigateToSearch).toHaveBeenCalledTimes(1); + expect(screen.getAllByTestId('FlatNavItemStatusIndicator')).toHaveLength(1); + }); + + it('disables logo pointer events and accessibility while collapsed', async () => { + // Given a collapsed bar + await Onyx.set(ONYXKEYS.NVP_SEARCH_SIDEBAR, {isCollapsed: true}); + + // When the bar mounts + renderBar(NAVIGATION_TABS.HOME); + await waitForBatchedUpdatesWithAct(); + + // Then the logo container turns off pointer events and the logo button is disabled and hidden from accessibility + expect(screen.getByTestId('FlatNavLogoContainer').props.pointerEvents).toBe('none'); + + const logoButton = screen.getByTestId('ExpensifyLogoButton', {includeHiddenElements: true}); + expect(logoButton.props['aria-hidden']).toBe(true); + expect(logoButton.props.accessibilityState).toEqual(expect.objectContaining({disabled: true})); + }); + + it('keeps the bar peeking when hovering out while the create menu is open', async () => { + // Given a collapsed bar + await Onyx.set(ONYXKEYS.NVP_SEARCH_SIDEBAR, {isCollapsed: true}); + renderBar(NAVIGATION_TABS.HOME); + await waitForBatchedUpdatesWithAct(); + + // When the pointer enters the items, which starts a peek + act(() => { + mockInnerHoverIn?.(); + }); + await waitForBatchedUpdatesWithAct(); + expect(getBarWidth()).toBe(variables.flatNavigationBarWidth); + + // And the Create menu opens + act(() => { + mockCreateButtonProps?.showCreateMenu?.(); + }); + await waitForBatchedUpdatesWithAct(); + + // And the pointer leaves the bar (e.g. moving onto the popover) + act(() => { + mockOuterHoverOut?.(); + }); + await waitForBatchedUpdatesWithAct(); + + // Then the bar stays peeking so the menu does not collapse out from under the pointer + expect(getBarWidth()).toBe(variables.flatNavigationBarWidth); + + // When the menu closes + act(() => { + mockCreateButtonProps?.hideCreateMenu?.(); + }); + await waitForBatchedUpdatesWithAct(); + + // Then the peek ends and the bar collapses back to a rail + expect(getBarWidth()).toBe(variables.flatNavigationBarCollapsedWidth); + }); + + it('keeps the bar peeking after the create menu closes if the pointer is still hovering the bar', async () => { + // Given a collapsed bar currently peeking with the Create menu open + await Onyx.set(ONYXKEYS.NVP_SEARCH_SIDEBAR, {isCollapsed: true}); + renderBar(NAVIGATION_TABS.HOME); + await waitForBatchedUpdatesWithAct(); + + act(() => { + mockInnerHoverIn?.(); + }); + await waitForBatchedUpdatesWithAct(); + act(() => { + mockCreateButtonProps?.showCreateMenu?.(); + }); + await waitForBatchedUpdatesWithAct(); + + // When the menu closes with the pointer still hovering over the bar + mockIsElementHovered = true; + act(() => { + mockCreateButtonProps?.hideCreateMenu?.(); + }); + await waitForBatchedUpdatesWithAct(); + + // Then the bar stays peeking because the cursor is still hovering over it + expect(getBarWidth()).toBe(variables.flatNavigationBarWidth); + + // When the pointer leaves the bar + mockIsElementHovered = false; + act(() => { + mockOuterHoverOut?.(); + }); + await waitForBatchedUpdatesWithAct(); + + // Then the peek ends and the bar collapses back to a rail + expect(getBarWidth()).toBe(variables.flatNavigationBarCollapsedWidth); + }); + + it('cancels the collapse frame if the create menu reopens immediately in the same frame', async () => { + // Given a collapsed bar currently peeking with the Create menu open + await Onyx.set(ONYXKEYS.NVP_SEARCH_SIDEBAR, {isCollapsed: true}); + renderBar(NAVIGATION_TABS.HOME); + await waitForBatchedUpdatesWithAct(); + + act(() => { + mockInnerHoverIn?.(); + }); + await waitForBatchedUpdatesWithAct(); + act(() => { + mockCreateButtonProps?.showCreateMenu?.(); + }); + await waitForBatchedUpdatesWithAct(); + + // When the menu closes, which schedules the hover check for the next frame, and reopens before that frame runs + // (two separate acts, so the closed state really renders instead of being batched away with the reopen) + act(() => { + mockCreateButtonProps?.hideCreateMenu?.(); + }); + act(() => { + mockCreateButtonProps?.showCreateMenu?.(); + }); + await waitForBatchedUpdatesWithAct(); + + // Then the pending check is cancelled, so the bar stays peeking even though the pointer is not over it + expect(getBarWidth()).toBe(variables.flatNavigationBarWidth); + }); + }); + it('sends an anonymous user to sign in instead of Account', async () => { // Given an anonymous session await Onyx.set(ONYXKEYS.SESSION, {authTokenType: CONST.AUTH_TOKEN_TYPES.ANONYMOUS}); diff --git a/tests/ui/TabNavigatorBarFlatNavigationTest.tsx b/tests/ui/TabNavigatorBarFlatNavigationTest.tsx index e60b5dc57c5c..ab49c6d0c6d4 100644 --- a/tests/ui/TabNavigatorBarFlatNavigationTest.tsx +++ b/tests/ui/TabNavigatorBarFlatNavigationTest.tsx @@ -6,14 +6,18 @@ import useResponsiveLayout from '@hooks/useResponsiveLayout'; import TabNavigatorBar from '@libs/Navigation/AppNavigator/Navigators/TabNavigatorBar'; +import variables from '@styles/variables'; + import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import SCREENS from '@src/SCREENS'; import type {BottomTabBarProps} from '@react-navigation/bottom-tabs'; import type {ComponentType} from 'react'; +import type {StyleProp, ViewStyle} from 'react-native'; import React from 'react'; +import {StyleSheet} from 'react-native'; import Onyx from 'react-native-onyx'; import createMock from '../utils/createMock'; @@ -65,6 +69,10 @@ const tabBarState = createMock({ routes: [{key: 'home', name: SCREENS.HOME, params: undefined}], }); +function isStyleProp(value: unknown): value is StyleProp { + return value === undefined || value === null || typeof value === 'object'; +} + function mockLayout(shouldUseNarrowLayout: boolean) { jest.mocked(useResponsiveLayout).mockReturnValue(createMock>({shouldUseNarrowLayout})); } @@ -184,4 +192,41 @@ describe('TabNavigatorBar with the flatNav beta', () => { // Then the icon bar shows, because an override is the way to compare both navigations on the same account expectIconBar(); }); + + describe('container width', () => { + // The bar mocks render nothing of their own, so the container's style is found on the nearest ancestor that sets a width. + function getContainerWidth(testID: string) { + for (let node = screen.getByTestId(testID).parent; node; node = node.parent) { + const style: unknown = node.props.style; + const width = isStyleProp(style) ? StyleSheet.flatten(style)?.width : undefined; + if (width !== undefined) { + return width; + } + } + return undefined; + } + + it('follows the collapsed rail so the central pane moves with the bar', async () => { + // Given a user on the flatNav beta with the bar collapsed + await Onyx.set(ONYXKEYS.BETAS, [CONST.BETAS.FLAT_NAV]); + await Onyx.set(ONYXKEYS.NVP_SEARCH_SIDEBAR, {isCollapsed: true}); + + // When the tab navigator draws its bar + await renderBar(); + + // Then the container reserves only the rail plus the sidebar next to it, which is what offsets the content + expect(getContainerWidth('FlatNavigationBar')).toBe(variables.flatNavigationBarCollapsedWidth + variables.sideBarWithLHBWidth); + }); + + it('keeps the icon bar width when the saved state is collapsed but the beta is off', async () => { + // Given the Spend sidebar collapsed on a user outside the beta + await Onyx.set(ONYXKEYS.NVP_SEARCH_SIDEBAR, {isCollapsed: true}); + + // When the tab navigator draws its bar + await renderBar(); + + // Then the icon bar keeps its own width, so the flag changes nothing outside the beta + expect(getContainerWidth('NavigationTabBar')).toBe(variables.navigationTabBarSize + variables.sideBarWithLHBWidth); + }); + }); }); diff --git a/tests/unit/SearchSidebarCollapseStoreTest.tsx b/tests/unit/SearchSidebarCollapseStoreTest.tsx new file mode 100644 index 000000000000..1bfbf52502ba --- /dev/null +++ b/tests/unit/SearchSidebarCollapseStoreTest.tsx @@ -0,0 +1,161 @@ +import {act, renderHook} from '@testing-library/react-native'; + +import {COLLAPSE_SURFACE, useSearchSidebarCollapse} from '@components/Navigation/SearchSidebarCollapseStore'; + +import ONYXKEYS from '@src/ONYXKEYS'; + +import Onyx from 'react-native-onyx'; + +import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; + +function renderSurfaces() { + return renderHook(() => ({ + bar: useSearchSidebarCollapse(COLLAPSE_SURFACE.NAVIGATION_BAR), + spend: useSearchSidebarCollapse(), + })); +} + +describe('SearchSidebarCollapseStore', () => { + beforeAll(() => { + Onyx.init({keys: ONYXKEYS}); + }); + + afterEach(async () => { + // The peek lives in module state, so a test that leaves one running would leak it into the next. + const {result, unmount} = renderSurfaces(); + act(() => { + result.current.bar.endPeek(); + result.current.spend.endPeek(); + }); + unmount(); + await act(async () => { + await Onyx.clear(); + await waitForBatchedUpdates(); + }); + }); + + it('collapses every surface from the one persisted flag', async () => { + // Given an expanded sidebar and both surfaces mounted + const {result} = renderSurfaces(); + await waitForBatchedUpdates(); + + // When the bar's toggle is pressed + await act(async () => { + result.current.bar.toggleSidebar(); + await waitForBatchedUpdates(); + }); + + // Then both the bar and the Spend column read the same flag, so they collapse together + expect(result.current.bar.isCollapsed).toBe(true); + expect(result.current.spend.isCollapsed).toBe(true); + }); + + it('reads a flag that was persisted before the surfaces mounted', async () => { + // Given a collapsed state saved by an earlier session, as after a page reload + await act(async () => { + await Onyx.set(ONYXKEYS.NVP_SEARCH_SIDEBAR, {isCollapsed: true}); + await waitForBatchedUpdates(); + }); + + // When the surfaces mount + const {result} = renderSurfaces(); + await waitForBatchedUpdates(); + + // Then they start collapsed, with no toggle press needed + expect(result.current.bar.isVisuallyCollapsed).toBe(true); + expect(result.current.spend.isVisuallyCollapsed).toBe(true); + }); + + it('peeks only the surface the pointer is on', async () => { + // Given both surfaces collapsed + await act(async () => { + await Onyx.set(ONYXKEYS.NVP_SEARCH_SIDEBAR, {isCollapsed: true}); + await waitForBatchedUpdates(); + }); + const {result} = renderSurfaces(); + await waitForBatchedUpdates(); + + // When the pointer enters the navigation bar + act(() => { + result.current.bar.startPeek(); + }); + + // Then the bar widens while the Spend column next to it stays collapsed + expect(result.current.bar.isPeeking).toBe(true); + expect(result.current.bar.isVisuallyCollapsed).toBe(false); + expect(result.current.spend.isPeeking).toBe(false); + expect(result.current.spend.isVisuallyCollapsed).toBe(true); + }); + + it('ignores a peek on another surface while one is already peeking', async () => { + // Given the bar peeking + await act(async () => { + await Onyx.set(ONYXKEYS.NVP_SEARCH_SIDEBAR, {isCollapsed: true}); + await waitForBatchedUpdates(); + }); + const {result} = renderSurfaces(); + await waitForBatchedUpdates(); + act(() => { + result.current.bar.startPeek(); + }); + + // When the Spend column tries to peek too, and then ends its own peek + act(() => { + result.current.spend.startPeek(); + result.current.spend.endPeek(); + }); + + // Then the bar keeps peeking, because only the surface that started a peek can end it + expect(result.current.bar.isPeeking).toBe(true); + expect(result.current.spend.isPeeking).toBe(false); + }); + + it('does not peek an expanded surface', async () => { + // Given an expanded sidebar + const {result} = renderSurfaces(); + await waitForBatchedUpdates(); + + // When the pointer enters the bar + act(() => { + result.current.bar.startPeek(); + }); + + // Then nothing peeks, since there is nothing collapsed to widen + expect(result.current.bar.isPeeking).toBe(false); + }); + + it('ends the peek when the sidebar is toggled', async () => { + // Given the bar peeking over a collapsed sidebar + await act(async () => { + await Onyx.set(ONYXKEYS.NVP_SEARCH_SIDEBAR, {isCollapsed: true}); + await waitForBatchedUpdates(); + }); + const {result} = renderSurfaces(); + await waitForBatchedUpdates(); + act(() => { + result.current.bar.startPeek(); + }); + + // When the toggle expands the sidebar + await act(async () => { + result.current.bar.toggleSidebar(); + await waitForBatchedUpdates(); + }); + + // Then the bar is expanded for good and no peek is left over to collapse it again later + expect(result.current.bar.isCollapsed).toBe(false); + expect(result.current.bar.isPeeking).toBe(false); + }); + + it('maintains a stable endPeek callback reference across renders', () => { + // Given a surface hook mounted + const {result, rerender} = renderHook(() => useSearchSidebarCollapse(COLLAPSE_SURFACE.NAVIGATION_BAR)); + const initialEndPeek = result.current.endPeek; + + // When the hook rerenders + rerender({}); + + // Then the endPeek reference is identical, so the bar's unmount cleanup that depends on it does not end a peek on every render + expect(result.current.endPeek).toBe(initialEndPeek); + }); +}); diff --git a/tests/unit/useNavigationBarWidthTest.tsx b/tests/unit/useNavigationBarWidthTest.tsx new file mode 100644 index 000000000000..519046f03921 --- /dev/null +++ b/tests/unit/useNavigationBarWidthTest.tsx @@ -0,0 +1,114 @@ +import {act, renderHook} from '@testing-library/react-native'; + +import useNavigationBarWidth from '@components/Navigation/FlatNavigationBar/useNavigationBarWidth'; +import {COLLAPSE_SURFACE, useSearchSidebarCollapse} from '@components/Navigation/SearchSidebarCollapseStore'; + +import variables from '@styles/variables'; + +import ONYXKEYS from '@src/ONYXKEYS'; + +import Onyx from 'react-native-onyx'; + +import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; + +let mockShouldUseFlatNavigationBar = true; +jest.mock('@components/Navigation/FlatNavigationBar/useShouldUseFlatNavigationBar', () => ({ + __esModule: true, + default: () => mockShouldUseFlatNavigationBar, +})); + +describe('useNavigationBarWidth', () => { + beforeAll(() => { + Onyx.init({keys: ONYXKEYS}); + }); + + beforeEach(() => { + mockShouldUseFlatNavigationBar = true; + }); + + afterEach(async () => { + // The peek lives in module state, so a test that leaves one running would leak it into the next. + const {result, unmount} = renderHook(() => useSearchSidebarCollapse(COLLAPSE_SURFACE.NAVIGATION_BAR)); + act(() => { + result.current.endPeek(); + }); + unmount(); + await act(async () => { + await Onyx.clear(); + await waitForBatchedUpdates(); + }); + }); + + it('returns the standard navigationTabBarSize when flat navigation is not active', () => { + // Given flat navigation disabled + mockShouldUseFlatNavigationBar = false; + + // When reading the navigation bar width + const {result} = renderHook(() => useNavigationBarWidth()); + + // Then it returns the classic icon bar width + expect(result.current).toBe(variables.navigationTabBarSize); + }); + + it('returns expanded width when flat navigation is active and not collapsed', async () => { + // Given flat navigation active and no collapsed state saved + + // When reading the navigation bar width + const {result} = renderHook(() => useNavigationBarWidth()); + await waitForBatchedUpdates(); + + // Then it returns the full flat navigation bar width + expect(result.current).toBe(variables.flatNavigationBarWidth); + }); + + it('returns collapsed width when flat navigation is collapsed', async () => { + // Given flat navigation collapsed in Onyx + await act(async () => { + await Onyx.set(ONYXKEYS.NVP_SEARCH_SIDEBAR, {isCollapsed: true}); + await waitForBatchedUpdates(); + }); + + // When reading the navigation bar width + const {result} = renderHook(() => useNavigationBarWidth()); + await waitForBatchedUpdates(); + + // Then it returns the skinny rail width + expect(result.current).toBe(variables.flatNavigationBarCollapsedWidth); + }); + + it('does not re-render or change value when a surface starts or ends peeking', async () => { + // Given flat navigation collapsed + await act(async () => { + await Onyx.set(ONYXKEYS.NVP_SEARCH_SIDEBAR, {isCollapsed: true}); + await waitForBatchedUpdates(); + }); + + let renderCount = 0; + const {result} = renderHook(() => { + renderCount += 1; + return useNavigationBarWidth(); + }); + await waitForBatchedUpdates(); + const initialRenderCount = renderCount; + + // When peeking starts on the navigation bar + const {result: surfaceStore} = renderHook(() => useSearchSidebarCollapse(COLLAPSE_SURFACE.NAVIGATION_BAR)); + act(() => { + surfaceStore.current.startPeek(); + }); + await waitForBatchedUpdates(); + + // Then the width stays the collapsed layout width, since a peek overlays the content instead of pushing it, and the hook does not re-render + expect(result.current).toBe(variables.flatNavigationBarCollapsedWidth); + expect(renderCount).toBe(initialRenderCount); + + // When peeking ends + act(() => { + surfaceStore.current.endPeek(); + }); + await waitForBatchedUpdates(); + + // Then it still does not re-render + expect(renderCount).toBe(initialRenderCount); + }); +}); From 7000a6fb08531699a16612790af777437e17671b Mon Sep 17 00:00:00 2001 From: Jakub Magiera Date: Fri, 13 Nov 2026 09:52:53 +0100 Subject: [PATCH 03/12] Move collapse fade inside FlatNavAccountStatus to prevent phantom gap --- .../FlatNavAccountStatus.tsx | 61 +++++++++++-------- .../FlatNavigationBar/FlatNavItem.tsx | 4 +- .../Navigation/FlatNavigationBar/index.tsx | 2 +- 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/src/components/Navigation/FlatNavigationBar/FlatNavAccountStatus.tsx b/src/components/Navigation/FlatNavigationBar/FlatNavAccountStatus.tsx index 09d5a938203a..23e8e5452eed 100644 --- a/src/components/Navigation/FlatNavigationBar/FlatNavAccountStatus.tsx +++ b/src/components/Navigation/FlatNavigationBar/FlatNavAccountStatus.tsx @@ -1,4 +1,5 @@ import Hoverable from '@components/Hoverable'; +import {getCollapseFadeStyle} from '@components/Navigation/SearchSidebarCollapseStore'; import Text from '@components/Text'; import Tooltip from '@components/Tooltip'; @@ -16,13 +17,19 @@ import CONST from '@src/CONST'; import React from 'react'; import {View} from 'react-native'; +import Animated from 'react-native-reanimated'; + +type FlatNavAccountStatusProps = { + /** Whether the navigation bar is visually collapsed */ + isCollapsed?: boolean; +}; /** * The signed-in user's emoji status on the right of the flat navigation bar's Account row, with the status text in a * tooltip. In the expanded bar the status sits here instead of as a badge on the avatar, which would crowd the avatar's * ring and indicator dot. */ -function FlatNavAccountStatus() { +function FlatNavAccountStatus({isCollapsed = false}: FlatNavAccountStatusProps) { const styles = useThemeStyles(); const StyleUtils = useStyleUtils(); const {translate, dateFnsLocale} = useLocalize(); @@ -33,35 +40,39 @@ function FlatNavAccountStatus() { return null; } + const collapseFadeStyle = getCollapseFadeStyle(isCollapsed); + const timezone = currentUserPersonalDetails.timezone?.selected ?? CONST.DEFAULT_TIME_ZONE.selected; const formattedClearAfterDate = DateUtils.getStatusUntilDate(translate, dateFnsLocale, currentUserPersonalDetails.status?.clearAfter ?? '', timezone, timezone); const tooltipText = getStatusTooltipText(currentUserPersonalDetails.status?.text ?? '', formattedClearAfterDate); return ( - - {/* Tooltip shows on mouse handlers it injects into its child, which Hoverable does not forward, so its target is a plain View */} - - - {(isHovered) => ( - - {emojiCode} - - )} - - - + + + {/* Tooltip shows on mouse handlers it injects into its child, which Hoverable does not forward, so its target is a plain View */} + + + {(isHovered) => ( + + {emojiCode} + + )} + + + + ); } diff --git a/src/components/Navigation/FlatNavigationBar/FlatNavItem.tsx b/src/components/Navigation/FlatNavigationBar/FlatNavItem.tsx index 5f30386f189a..63376e144c63 100644 --- a/src/components/Navigation/FlatNavigationBar/FlatNavItem.tsx +++ b/src/components/Navigation/FlatNavigationBar/FlatNavItem.tsx @@ -85,7 +85,7 @@ function FlatNavItem({ return isHovered ? styles.highlightBG : undefined; }; - // The label and right element stay mounted and fade, rather than disappearing the moment the bar starts narrowing. + // The label stays mounted and fades, rather than disappearing the moment the bar starts narrowing. const collapseFadeStyle = getCollapseFadeStyle(isCollapsed); // Always mounted, with the text only while collapsed, so a row does not remount when the bar collapses. @@ -137,7 +137,7 @@ function FlatNavItem({ {label} - {!!rightElement && {rightElement}} + {rightElement} ); }} diff --git a/src/components/Navigation/FlatNavigationBar/index.tsx b/src/components/Navigation/FlatNavigationBar/index.tsx index c79a3fdc9279..533b08ecd06c 100644 --- a/src/components/Navigation/FlatNavigationBar/index.tsx +++ b/src/components/Navigation/FlatNavigationBar/index.tsx @@ -255,7 +255,7 @@ function FlatNavigationBar({selectedTab}: FlatNavigationBarProps) { /> )} - rightElement={} + rightElement={} isSelected={isAccountSelected} isTall accessibilityLabel={getTabAccessibilityLabel(`${accountLabel}, ${translate('sidebarScreen.buttonMySettings')}`, !!accountStatus, translate)} From d984cf3af6a6bbbbbde3b4ec9f79cd8ca0fdfac8 Mon Sep 17 00:00:00 2001 From: Jakub Magiera Date: Fri, 13 Nov 2026 09:59:59 +0100 Subject: [PATCH 04/12] Peek navigation bar on keyboard focus and remove redundant tooltips --- .../FlatNavigationBar/FlatNavCreateButton.tsx | 38 ++-- .../FlatNavigationBar/FlatNavItem.tsx | 103 +++++---- .../Navigation/FlatNavigationBar/index.tsx | 202 +++++++++++------- tests/ui/FlatNavigationBarTest.tsx | 95 +++++++- 4 files changed, 276 insertions(+), 162 deletions(-) diff --git a/src/components/Navigation/FlatNavigationBar/FlatNavCreateButton.tsx b/src/components/Navigation/FlatNavigationBar/FlatNavCreateButton.tsx index 9c6f351a7641..75a0b32c9db8 100644 --- a/src/components/Navigation/FlatNavigationBar/FlatNavCreateButton.tsx +++ b/src/components/Navigation/FlatNavigationBar/FlatNavCreateButton.tsx @@ -1,6 +1,5 @@ /** The flat navigation bar's Create button, together with the create menu it opens right below itself. */ import Button from '@components/Button'; -import Tooltip from '@components/Tooltip'; import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset'; import useLocalize from '@hooks/useLocalize'; @@ -78,26 +77,23 @@ function FlatNavCreateButton({isCompact = false, isCreateMenuActive, isCreateMen anchorPosition={anchorPosition} anchorAlignment={CREATE_MENU_ANCHOR_ALIGNMENT} /> - {/* The tooltip stays mounted in both modes and only has text while compact (e.g. for keyboard focus), so collapsing does not remount the button the menu is anchored to. */} - - - + ); } diff --git a/src/components/Navigation/FlatNavigationBar/FlatNavItem.tsx b/src/components/Navigation/FlatNavigationBar/FlatNavItem.tsx index 63376e144c63..c4d38bf6bb05 100644 --- a/src/components/Navigation/FlatNavigationBar/FlatNavItem.tsx +++ b/src/components/Navigation/FlatNavigationBar/FlatNavItem.tsx @@ -3,7 +3,6 @@ import getTabIconFill from '@components/Navigation/NavigationTabBar/getTabIconFi import {getCollapseFadeStyle} from '@components/Navigation/SearchSidebarCollapseStore'; import {PressableWithFeedback} from '@components/Pressable'; import Text from '@components/Text'; -import Tooltip from '@components/Tooltip'; import useStyleUtils from '@hooks/useStyleUtils'; import useTheme from '@hooks/useTheme'; @@ -88,61 +87,57 @@ function FlatNavItem({ // The label stays mounted and fades, rather than disappearing the moment the bar starts narrowing. const collapseFadeStyle = getCollapseFadeStyle(isCollapsed); - // Always mounted, with the text only while collapsed, so a row does not remount when the bar collapses. - // Hovering expands the bar to reveal the full text; keyboard focus leaves it collapsed, so the tooltip informs keyboard users. return ( - - [styles.flatNavigationBarItem, isTall && styles.flatNavigationBarAccountItem, getRowBackgroundStyle(hovered)]} - > - {({hovered}) => { - const rowBackgroundColor = getRowBackgroundStyle(hovered)?.backgroundColor; - return ( - <> - {!!icon && ( - - [styles.flatNavigationBarItem, isTall && styles.flatNavigationBarAccountItem, getRowBackgroundStyle(hovered)]} + > + {({hovered}) => { + const rowBackgroundColor = getRowBackgroundStyle(hovered)?.backgroundColor; + return ( + <> + {!!icon && ( + + + {!!statusIndicatorColor && ( + - {!!statusIndicatorColor && ( - - )} - - )} - {renderLeftElement?.({backgroundColor: rowBackgroundColor})} - - - {label} - - - {rightElement} - - ); - }} - - + )} + + )} + {renderLeftElement?.({backgroundColor: rowBackgroundColor})} + + + {label} + + + {rightElement} + + ); + }} + ); } diff --git a/src/components/Navigation/FlatNavigationBar/index.tsx b/src/components/Navigation/FlatNavigationBar/index.tsx index 533b08ecd06c..57d89063a75e 100644 --- a/src/components/Navigation/FlatNavigationBar/index.tsx +++ b/src/components/Navigation/FlatNavigationBar/index.tsx @@ -38,6 +38,7 @@ import SupportalSwitcherButton from '@pages/inbox/sidebar/SupportalSwitcherButto import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; +import type {NativeSyntheticEvent, TargetedEvent} from 'react-native'; import type {ValueOf} from 'type-fest'; import React, {useEffect, useRef} from 'react'; @@ -115,10 +116,38 @@ function FlatNavigationBar({selectedTab}: FlatNavigationBarProps) { prevIsCreateMenuVisibleRef.current = isCreateMenuVisible; }, [endPeek, isCreateMenuVisible]); + // Only keyboard focus counts: an item clicked with the mouse keeps focus too, and must not hold the bar open after the pointer leaves. + const hasKeyboardFocusInside = () => { + const element = barContainerRef.current; + return isHTMLElement(element) && !!element.querySelector(':focus-visible'); + }; + const handleBarHoverOut = () => { + if (isCreateMenuVisible || hasKeyboardFocusInside()) { + return; + } + endPeek(); + }; + + const handleItemsFocus = () => { + startPeek(); + }; + + const handleItemsBlur = (event: NativeSyntheticEvent) => { if (isCreateMenuVisible) { return; } + const currentTarget = event.currentTarget; + // On web the native event is a DOM FocusEvent, whose relatedTarget is the element receiving focus. + const relatedTarget = 'relatedTarget' in event.nativeEvent ? event.nativeEvent.relatedTarget : null; + // Tabbing between items inside the bar keeps the peek open. + if (isHTMLElement(currentTarget) && isHTMLElement(relatedTarget) && currentTarget.contains(relatedTarget)) { + return; + } + const element = barContainerRef.current; + if (isHTMLElement(element) && element.matches(':hover')) { + return; + } endPeek(); }; @@ -168,101 +197,108 @@ function FlatNavigationBar({selectedTab}: FlatNavigationBarProps) { onPress={toggleSidebar} /> - {/* The header and footer stay out of the peek, so pressing the toggle never races the bar widening under the pointer. */} + {/* The header stays out of the peek, so pressing the toggle never races the bar widening under the pointer. */} - - - - - - - - setIsSearchTabHovered(true)} - onHoverOut={() => setIsSearchTabHovered(false)} - /> - + + + + + + + + setIsSearchTabHovered(true)} + onHoverOut={() => setIsSearchTabHovered(false)} + /> + + + + + + + + + ( + + + + )} + rightElement={} + isSelected={isAccountSelected} + isTall + accessibilityLabel={getTabAccessibilityLabel(`${accountLabel}, ${translate('sidebarScreen.buttonMySettings')}`, !!accountStatus, translate)} + sentryLabel={CONST.SENTRY_LABEL.NAVIGATION_TAB_BAR.ACCOUNT} + onPress={navigateToAccount} /> - + - - - - - - ( - - - - )} - rightElement={} - isSelected={isAccountSelected} - isTall - accessibilityLabel={getTabAccessibilityLabel(`${accountLabel}, ${translate('sidebarScreen.buttonMySettings')}`, !!accountStatus, translate)} - sentryLabel={CONST.SENTRY_LABEL.NAVIGATION_TAB_BAR.ACCOUNT} - onPress={navigateToAccount} - /> - )} diff --git a/tests/ui/FlatNavigationBarTest.tsx b/tests/ui/FlatNavigationBarTest.tsx index f991d3b4db45..17c7b46cdea1 100644 --- a/tests/ui/FlatNavigationBarTest.tsx +++ b/tests/ui/FlatNavigationBarTest.tsx @@ -62,15 +62,19 @@ jest.mock('@components/Navigation/FlatNavigationBar/FlatNavCreateButton', () => return null; }); let mockIsElementHovered = false; +let mockIsFocusMovingInside = false; +let mockHasKeyboardFocusInside = false; jest.mock('@libs/isHTMLElement', () => ({ __esModule: true, default: (val: unknown): val is HTMLElement => { - if (!mockIsElementHovered || typeof val !== 'object' || val === null) { + if ((!mockIsElementHovered && !mockIsFocusMovingInside && !mockHasKeyboardFocusInside) || typeof val !== 'object' || val === null) { return false; } - const target = val; - // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- attach matches method to test instance for mock hover verification - (target as {matches?: (selector: string) => boolean}).matches = (selector: string) => selector === ':hover'; + // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- attach DOM methods to the test instance for mock hover and focus verification + const target = val as {matches?: (selector: string) => boolean; contains?: (other: unknown) => boolean; querySelector?: (selector: string) => unknown}; + target.matches = (selector: string) => mockIsElementHovered && selector === ':hover'; + target.contains = () => mockIsFocusMovingInside; + target.querySelector = (selector: string) => (mockHasKeyboardFocusInside && selector === ':focus-visible' ? {} : null); return true; }, })); @@ -132,6 +136,8 @@ describe('FlatNavigationBar', () => { jest.clearAllMocks(); mockCreateButtonProps = undefined; mockIsElementHovered = false; + mockIsFocusMovingInside = false; + mockHasKeyboardFocusInside = false; mockOuterHoverOut = undefined; mockInnerHoverIn = undefined; mockInboxIndicator = NO_INDICATOR; @@ -484,6 +490,87 @@ describe('FlatNavigationBar', () => { // Then the pending check is cancelled, so the bar stays peeking even though the pointer is not over it expect(getBarWidth()).toBe(variables.flatNavigationBarWidth); }); + + it('peeks the bar when items receive keyboard focus and collapses on blur', async () => { + // Given a collapsed bar + await Onyx.set(ONYXKEYS.NVP_SEARCH_SIDEBAR, {isCollapsed: true}); + renderBar(NAVIGATION_TABS.HOME); + await waitForBatchedUpdatesWithAct(); + expect(getBarWidth()).toBe(variables.flatNavigationBarCollapsedWidth); + + // When navigation items gain focus via keyboard navigation + fireEvent(screen.getByTestId('FlatNavPeekableBody'), 'focus'); + await waitForBatchedUpdatesWithAct(); + + // Then the bar peeks open to reveal labels for keyboard users + expect(getBarWidth()).toBe(variables.flatNavigationBarWidth); + + // When focus leaves the navigation items + fireEvent(screen.getByTestId('FlatNavPeekableBody'), 'blur', { + nativeEvent: {}, + }); + await waitForBatchedUpdatesWithAct(); + + // Then the bar collapses back to a skinny rail + expect(getBarWidth()).toBe(variables.flatNavigationBarCollapsedWidth); + }); + + it('keeps the peek open when focus moves between items inside the bar', async () => { + // Given a collapsed bar peeking because an item has keyboard focus + await Onyx.set(ONYXKEYS.NVP_SEARCH_SIDEBAR, {isCollapsed: true}); + renderBar(NAVIGATION_TABS.HOME); + await waitForBatchedUpdatesWithAct(); + fireEvent(screen.getByTestId('FlatNavPeekableBody'), 'focus'); + await waitForBatchedUpdatesWithAct(); + + // When focus moves to another item inside the bar, e.g. tabbing from Home to Inbox + mockIsFocusMovingInside = true; + fireEvent(screen.getByTestId('FlatNavPeekableBody'), 'blur', { + currentTarget: {}, + nativeEvent: {relatedTarget: {}}, + }); + await waitForBatchedUpdatesWithAct(); + + // Then the bar stays peeking, so its labels do not flicker on every Tab press + expect(getBarWidth()).toBe(variables.flatNavigationBarWidth); + }); + + it('keeps the peek open when the pointer leaves while an item has keyboard focus', async () => { + // Given a collapsed bar peeking because an item has keyboard focus + await Onyx.set(ONYXKEYS.NVP_SEARCH_SIDEBAR, {isCollapsed: true}); + renderBar(NAVIGATION_TABS.HOME); + await waitForBatchedUpdatesWithAct(); + mockHasKeyboardFocusInside = true; + fireEvent(screen.getByTestId('FlatNavPeekableBody'), 'focus'); + await waitForBatchedUpdatesWithAct(); + + // When the pointer passes over the bar and leaves it + act(() => { + mockOuterHoverOut?.(); + }); + await waitForBatchedUpdatesWithAct(); + + // Then the bar stays peeking, so the keyboard user still sees the label of the focused item + expect(getBarWidth()).toBe(variables.flatNavigationBarWidth); + }); + + it('collapses when the pointer leaves after an item was focused by a mouse click', async () => { + // Given a collapsed bar peeking with focus inside that is not keyboard focus, as after clicking an item + await Onyx.set(ONYXKEYS.NVP_SEARCH_SIDEBAR, {isCollapsed: true}); + renderBar(NAVIGATION_TABS.HOME); + await waitForBatchedUpdatesWithAct(); + fireEvent(screen.getByTestId('FlatNavPeekableBody'), 'focus'); + await waitForBatchedUpdatesWithAct(); + + // When the pointer leaves the bar + act(() => { + mockOuterHoverOut?.(); + }); + await waitForBatchedUpdatesWithAct(); + + // Then the bar collapses, because mouse focus must not hold it open + expect(getBarWidth()).toBe(variables.flatNavigationBarCollapsedWidth); + }); }); it('sends an anonymous user to sign in instead of Account', async () => { From bce295e90c4f3d8778366fa717685512b7c4f0e8 Mon Sep 17 00:00:00 2001 From: Jakub Magiera Date: Fri, 13 Nov 2026 12:05:55 +0100 Subject: [PATCH 05/12] Rename FlatNavigationBar callbacks to describe behavior --- .../Navigation/FlatNavigationBar/index.tsx | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/components/Navigation/FlatNavigationBar/index.tsx b/src/components/Navigation/FlatNavigationBar/index.tsx index 41e978be0ec7..a4d765f7b76c 100644 --- a/src/components/Navigation/FlatNavigationBar/index.tsx +++ b/src/components/Navigation/FlatNavigationBar/index.tsx @@ -123,18 +123,14 @@ function FlatNavigationBar({selectedTab}: FlatNavigationBarProps) { return isHTMLElement(element) && !!element.querySelector(':focus-visible'); }; - const handleBarHoverOut = () => { + const endPeekIfIdle = () => { if (isCreateMenuVisible || hasKeyboardFocusInside()) { return; } endPeek(); }; - const handleItemsFocus = () => { - startPeek(); - }; - - const handleItemsBlur = (event: NativeSyntheticEvent) => { + const endPeekUnlessStaying = (event: NativeSyntheticEvent) => { if (isCreateMenuVisible) { return; } @@ -170,7 +166,7 @@ function FlatNavigationBar({selectedTab}: FlatNavigationBarProps) { {(isBarHovered) => ( Date: Fri, 13 Nov 2026 12:14:53 +0100 Subject: [PATCH 06/12] Preserve keyboard focus peeking when the Create menu closes --- .../Navigation/FlatNavigationBar/index.tsx | 14 +++++----- tests/ui/FlatNavigationBarTest.tsx | 27 +++++++++++++++++++ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/components/Navigation/FlatNavigationBar/index.tsx b/src/components/Navigation/FlatNavigationBar/index.tsx index a4d765f7b76c..854bf1dd83b1 100644 --- a/src/components/Navigation/FlatNavigationBar/index.tsx +++ b/src/components/Navigation/FlatNavigationBar/index.tsx @@ -92,6 +92,12 @@ function FlatNavigationBar({selectedTab}: FlatNavigationBarProps) { const closeFrameIdRef = useRef(null); const prevIsCreateMenuVisibleRef = useRef(isCreateMenuVisible); + // Only keyboard focus counts: an item clicked with the mouse keeps focus too, and must not hold the bar open after the pointer leaves. + const hasKeyboardFocusInside = () => { + const element = barContainerRef.current; + return isHTMLElement(element) && !!element.querySelector(':focus-visible'); + }; + useEffect(() => { if (isCreateMenuVisible && closeFrameIdRef.current !== null) { cancelAnimationFrame(closeFrameIdRef.current); @@ -108,7 +114,7 @@ function FlatNavigationBar({selectedTab}: FlatNavigationBarProps) { closeFrameIdRef.current = null; const element = barContainerRef.current; const isHovered = isHTMLElement(element) && element.matches(':hover'); - if (!isHovered) { + if (!isHovered && !hasKeyboardFocusInside()) { endPeek(); } }); @@ -117,12 +123,6 @@ function FlatNavigationBar({selectedTab}: FlatNavigationBarProps) { prevIsCreateMenuVisibleRef.current = isCreateMenuVisible; }, [endPeek, isCreateMenuVisible]); - // Only keyboard focus counts: an item clicked with the mouse keeps focus too, and must not hold the bar open after the pointer leaves. - const hasKeyboardFocusInside = () => { - const element = barContainerRef.current; - return isHTMLElement(element) && !!element.querySelector(':focus-visible'); - }; - const endPeekIfIdle = () => { if (isCreateMenuVisible || hasKeyboardFocusInside()) { return; diff --git a/tests/ui/FlatNavigationBarTest.tsx b/tests/ui/FlatNavigationBarTest.tsx index 17c7b46cdea1..87f263a6ed81 100644 --- a/tests/ui/FlatNavigationBarTest.tsx +++ b/tests/ui/FlatNavigationBarTest.tsx @@ -462,6 +462,33 @@ describe('FlatNavigationBar', () => { expect(getBarWidth()).toBe(variables.flatNavigationBarCollapsedWidth); }); + it('keeps the bar peeking after the create menu closes if an item has keyboard focus', async () => { + // Given a collapsed bar currently peeking with the Create menu open and no hover over the bar + await Onyx.set(ONYXKEYS.NVP_SEARCH_SIDEBAR, {isCollapsed: true}); + renderBar(NAVIGATION_TABS.HOME); + await waitForBatchedUpdatesWithAct(); + + act(() => { + mockInnerHoverIn?.(); + }); + await waitForBatchedUpdatesWithAct(); + act(() => { + mockCreateButtonProps?.showCreateMenu?.(); + }); + await waitForBatchedUpdatesWithAct(); + + // When the menu closes while an item inside the bar retains keyboard focus + mockIsElementHovered = false; + mockHasKeyboardFocusInside = true; + act(() => { + mockCreateButtonProps?.hideCreateMenu?.(); + }); + await waitForBatchedUpdatesWithAct(); + + // Then the bar stays peeking so the keyboard user still sees labels + expect(getBarWidth()).toBe(variables.flatNavigationBarWidth); + }); + it('cancels the collapse frame if the create menu reopens immediately in the same frame', async () => { // Given a collapsed bar currently peeking with the Create menu open await Onyx.set(ONYXKEYS.NVP_SEARCH_SIDEBAR, {isCollapsed: true}); From ed810515be3c5fef74eb8208e9f2beb6acb624dc Mon Sep 17 00:00:00 2001 From: Jakub Magiera Date: Fri, 13 Nov 2026 12:16:44 +0100 Subject: [PATCH 07/12] Select surface-specific boolean in collapse peek store snapshot --- .../Navigation/SearchSidebarCollapseStore.ts | 12 ++++---- tests/unit/SearchSidebarCollapseStoreTest.tsx | 29 +++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/components/Navigation/SearchSidebarCollapseStore.ts b/src/components/Navigation/SearchSidebarCollapseStore.ts index fe6374d257a7..d2ab8b60008c 100644 --- a/src/components/Navigation/SearchSidebarCollapseStore.ts +++ b/src/components/Navigation/SearchSidebarCollapseStore.ts @@ -53,10 +53,6 @@ function notify() { } } -function getPeekSnapshot() { - return peekingSurface; -} - function getSearchSidebarWidth(progress: number) { return variables.searchSidebarExpandedWidth + (variables.searchSidebarCollapsedWidth - variables.searchSidebarExpandedWidth) * progress; } @@ -85,6 +81,11 @@ function endPeek(surface: CollapseSurface) { notify(); } +const isSurfacePeekingSnapshots: Record boolean> = { + [COLLAPSE_SURFACE.NAVIGATION_BAR]: () => peekingSurface === COLLAPSE_SURFACE.NAVIGATION_BAR, + [COLLAPSE_SURFACE.SEARCH_SIDEBAR]: () => peekingSurface === COLLAPSE_SURFACE.SEARCH_SIDEBAR, +}; + const endPeekBySurface: Record void> = { [COLLAPSE_SURFACE.NAVIGATION_BAR]: () => endPeek(COLLAPSE_SURFACE.NAVIGATION_BAR), [COLLAPSE_SURFACE.SEARCH_SIDEBAR]: () => endPeek(COLLAPSE_SURFACE.SEARCH_SIDEBAR), @@ -93,7 +94,8 @@ const endPeekBySurface: Record void> = { function useSearchSidebarCollapse(surface: CollapseSurface = COLLAPSE_SURFACE.SEARCH_SIDEBAR) { const [searchSidebarNVP] = useOnyx(ONYXKEYS.NVP_SEARCH_SIDEBAR); const collapsed = searchSidebarNVP?.isCollapsed ?? false; - const peeking = useSyncExternalStore(subscribe, getPeekSnapshot, getPeekSnapshot) === surface; + const isSurfacePeekingSnapshot = isSurfacePeekingSnapshots[surface]; + const peeking = useSyncExternalStore(subscribe, isSurfacePeekingSnapshot, isSurfacePeekingSnapshot); const toggleSidebar = () => setSearchSidebarCollapsed(!collapsed); const startSurfacePeek = () => startPeek(surface, collapsed); diff --git a/tests/unit/SearchSidebarCollapseStoreTest.tsx b/tests/unit/SearchSidebarCollapseStoreTest.tsx index 1bfbf52502ba..750cc35fed9f 100644 --- a/tests/unit/SearchSidebarCollapseStoreTest.tsx +++ b/tests/unit/SearchSidebarCollapseStoreTest.tsx @@ -158,4 +158,33 @@ describe('SearchSidebarCollapseStore', () => { // Then the endPeek reference is identical, so the bar's unmount cleanup that depends on it does not end a peek on every render expect(result.current.endPeek).toBe(initialEndPeek); }); + + it('does not re-render a surface when another surface peeks', async () => { + // Given both surfaces collapsed + await act(async () => { + await Onyx.set(ONYXKEYS.NVP_SEARCH_SIDEBAR, {isCollapsed: true}); + await waitForBatchedUpdates(); + }); + const {result: barResult} = renderHook(() => useSearchSidebarCollapse(COLLAPSE_SURFACE.NAVIGATION_BAR)); + let spendRenderCount = 0; + renderHook(() => { + spendRenderCount++; + return useSearchSidebarCollapse(COLLAPSE_SURFACE.SEARCH_SIDEBAR); + }); + await waitForBatchedUpdates(); + const initialSpendRenderCount = spendRenderCount; + + // When the navigation bar peeks and collapses back + act(() => { + barResult.current.startPeek(); + }); + await waitForBatchedUpdates(); + act(() => { + barResult.current.endPeek(); + }); + await waitForBatchedUpdates(); + + // Then Spend consumers do not re-render because their peek snapshot boolean did not change + expect(spendRenderCount).toBe(initialSpendRenderCount); + }); }); From 8d26a0f08a485a895dd11344406b7446518bd6ec Mon Sep 17 00:00:00 2001 From: Jakub Magiera Date: Fri, 13 Nov 2026 12:17:16 +0100 Subject: [PATCH 08/12] Cleanup style helpers and prop comments --- .../Navigation/FlatNavigationBar/FlatNavAccountStatus.tsx | 1 - .../Navigation/FlatNavigationBar/FlatNavCollapseToggle.tsx | 4 +++- .../Navigation/FlatNavigationBar/FlatNavCreateButton.tsx | 4 ---- src/styles/index.ts | 7 ------- 4 files changed, 3 insertions(+), 13 deletions(-) diff --git a/src/components/Navigation/FlatNavigationBar/FlatNavAccountStatus.tsx b/src/components/Navigation/FlatNavigationBar/FlatNavAccountStatus.tsx index 23e8e5452eed..5c94808afaf5 100644 --- a/src/components/Navigation/FlatNavigationBar/FlatNavAccountStatus.tsx +++ b/src/components/Navigation/FlatNavigationBar/FlatNavAccountStatus.tsx @@ -20,7 +20,6 @@ import {View} from 'react-native'; import Animated from 'react-native-reanimated'; type FlatNavAccountStatusProps = { - /** Whether the navigation bar is visually collapsed */ isCollapsed?: boolean; }; diff --git a/src/components/Navigation/FlatNavigationBar/FlatNavCollapseToggle.tsx b/src/components/Navigation/FlatNavigationBar/FlatNavCollapseToggle.tsx index 2ed4e2682636..725dd15eabe8 100644 --- a/src/components/Navigation/FlatNavigationBar/FlatNavCollapseToggle.tsx +++ b/src/components/Navigation/FlatNavigationBar/FlatNavCollapseToggle.tsx @@ -7,6 +7,7 @@ import {PressableWithFeedback} from '@components/Pressable'; import Tooltip from '@components/Tooltip'; import useLocalize from '@hooks/useLocalize'; +import useStyleUtils from '@hooks/useStyleUtils'; import useTheme from '@hooks/useTheme'; import useThemeStyles from '@hooks/useThemeStyles'; @@ -24,6 +25,7 @@ type FlatNavCollapseToggleProps = { function FlatNavCollapseToggle({isCollapsed, onPress}: FlatNavCollapseToggleProps) { const styles = useThemeStyles(); + const StyleUtils = useStyleUtils(); const theme = useTheme(); const {translate} = useLocalize(); @@ -38,7 +40,7 @@ function FlatNavCollapseToggle({isCollapsed, onPress}: FlatNavCollapseToggleProp accessibilityState={{expanded: !isCollapsed}} sentryLabel={CONST.SENTRY_LABEL.NAVIGATION_TAB_BAR.COLLAPSE} onPress={onPress} - style={styles.flatNavigationBarCollapseToggle} + style={[StyleUtils.getWidthAndHeightStyle(variables.iconSizeNormal), styles.alignItemsCenter, styles.justifyContentCenter]} shouldUseAutoHitSlop > void; - /** Hides the create menu */ hideCreateMenu: () => void; }; diff --git a/src/styles/index.ts b/src/styles/index.ts index 363391f80d8c..8d353f753ff7 100644 --- a/src/styles/index.ts +++ b/src/styles/index.ts @@ -911,13 +911,6 @@ const staticStyles = (theme: ThemeColors) => justifyContent: 'center', }, - flatNavigationBarCollapseToggle: { - width: variables.iconSizeNormal, - height: variables.iconSizeNormal, - alignItems: 'center', - justifyContent: 'center', - }, - // The "y" at the end of the wordmark already leaves empty space on its right, so the badge sits closer than the default badge margin flatNavigationBarEnvironmentBadge: { marginLeft: 6, From a873c6882c7a87b81047df05850a8fc54c36c507 Mon Sep 17 00:00:00 2001 From: Jakub Magiera Date: Fri, 13 Nov 2026 13:00:22 +0100 Subject: [PATCH 09/12] Shrink the Create button with the bar when collapsing --- .../Navigation/FlatNavigationBar/FlatNavCreateButton.tsx | 2 +- src/styles/index.ts | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/components/Navigation/FlatNavigationBar/FlatNavCreateButton.tsx b/src/components/Navigation/FlatNavigationBar/FlatNavCreateButton.tsx index de20798cb35b..5988c336c9f1 100644 --- a/src/components/Navigation/FlatNavigationBar/FlatNavCreateButton.tsx +++ b/src/components/Navigation/FlatNavigationBar/FlatNavCreateButton.tsx @@ -81,7 +81,7 @@ function FlatNavCreateButton({isCompact = false, isCreateMenuActive, isCreateMen accessibilityState={{expanded: isCreateMenuActive}} variant={CONST.BUTTON_VARIANT.SUCCESS} size={CONST.BUTTON_SIZE.SMALL} - style={isCompact ? undefined : styles.w100} + style={styles.w100} innerStyles={[styles.flatNavigationBarCreateButton, isCompact && styles.flatNavigationBarCreateButtonCompact]} onPress={toggleCreateMenu} // Same action as the floating action button it replaces, so it keeps that button's Sentry and FullStory identifier. diff --git a/src/styles/index.ts b/src/styles/index.ts index 8d353f753ff7..ed426cb60245 100644 --- a/src/styles/index.ts +++ b/src/styles/index.ts @@ -926,10 +926,8 @@ const staticStyles = (theme: ThemeColors) => minHeight: variables.flatNavigationBarCreateButtonHeight, }, - // Collapsed, the Create button shrinks to a circle. Its container's inset centers it in the rail. + // Collapsed, the button narrows with its container: rail width minus both insets equals its height, so the pill ends as a circle. flatNavigationBarCreateButtonCompact: { - width: variables.flatNavigationBarCreateButtonHeight, - borderRadius: variables.componentBorderRadiusRounded, paddingHorizontal: 0, }, From 38e422e28e2e7cab152c6a0381a63e4ff4924d35 Mon Sep 17 00:00:00 2001 From: Jakub Magiera Date: Fri, 13 Nov 2026 17:32:20 +0100 Subject: [PATCH 10/12] End the peek only after the Create menu has fully closed --- .../FlatNavigationBar/FlatNavCreateButton.tsx | 6 ++- .../Navigation/FlatNavigationBar/index.tsx | 44 ++++------------- .../FABPopoverContent/CreateMenuPopover.tsx | 6 ++- .../FABPopoverContent/FABPopoverMenu.tsx | 9 +++- tests/ui/FlatNavigationBarTest.tsx | 47 +++++++++++++++++-- 5 files changed, 70 insertions(+), 42 deletions(-) diff --git a/src/components/Navigation/FlatNavigationBar/FlatNavCreateButton.tsx b/src/components/Navigation/FlatNavigationBar/FlatNavCreateButton.tsx index 5988c336c9f1..5a5a6a319261 100644 --- a/src/components/Navigation/FlatNavigationBar/FlatNavCreateButton.tsx +++ b/src/components/Navigation/FlatNavigationBar/FlatNavCreateButton.tsx @@ -33,10 +33,13 @@ type FlatNavCreateButtonProps = { showCreateMenu: () => void; hideCreateMenu: () => void; + + /** Called once the create menu has fully closed and its modal is gone from the screen */ + onCreateMenuHidden?: () => void; }; /** Opens the same create menu as the floating action button. */ -function FlatNavCreateButton({isCompact = false, isCreateMenuActive, isCreateMenuVisible, showCreateMenu, hideCreateMenu}: FlatNavCreateButtonProps) { +function FlatNavCreateButton({isCompact = false, isCreateMenuActive, isCreateMenuVisible, showCreateMenu, hideCreateMenu, onCreateMenuHidden}: FlatNavCreateButtonProps) { const styles = useThemeStyles(); const {translate} = useLocalize(); const icons = useMemoizedLazyExpensifyIcons(['Plus']); @@ -72,6 +75,7 @@ function FlatNavCreateButton({isCompact = false, isCreateMenuActive, isCreateMen anchorRef={buttonRef} anchorPosition={anchorPosition} anchorAlignment={CREATE_MENU_ANCHOR_ALIGNMENT} + onModalHide={onCreateMenuHidden} />