diff --git a/app/components/MobileNav.tsx b/app/components/MobileNav.tsx index db2e86f98..678eed45a 100644 --- a/app/components/MobileNav.tsx +++ b/app/components/MobileNav.tsx @@ -58,6 +58,7 @@ type SidebarData = RootLoaderData["sidebar"] | undefined; type PanelType = (typeof PANEL_TYPES)[number]; type PanelIds = Record; type PanelToggleHandler = (event: React.ToggleEvent) => void; +type TabClickHandler = (event: React.MouseEvent) => void; const MOBILE_NAV_ID = "mobile-nav"; const PANEL_TYPES = ["menu", "friends", "tourneys", "chat", "you"] as const; @@ -71,6 +72,10 @@ const PANEL_TYPES = ["menu", "friends", "tourneys", "chat", "you"] as const; * * The bar itself is a manual popover shown on hydration so it lives in the top * layer; before that it is a plain fixed bar. + * + * Once hydrated, while a panel is open every tab targets it, so pressing a tab + * does not light dismiss it. The tab's click then swaps the panels in one go, + * as on touch devices the click can come frames after the press. */ export function MobileNav({ sidebarData }: { sidebarData: SidebarData }) { const [activePanel, setActivePanel] = React.useState(null); @@ -143,9 +148,23 @@ export function MobileNav({ sidebarData }: { sidebarData: SidebarData }) { } }; - // a panel taking another one's place appears in place instead of sliding - // up again; decided as the tab is pressed, before the popovers switch - const rememberOpenPanel = () => setSkipAnimation(activePanel !== null); + const onTabClick = + (panel: PanelType): TabClickHandler => + (event) => { + const target = document.getElementById(panelIds[panel]); + if (!target) return; + event.preventDefault(); + + if (target.matches(":popover-open")) { + target.hidePopover(); + return; + } + + // a panel taking another one's place appears in place instead of sliding up again + setSkipAnimation(activePanel !== null); + setActivePanel(panel); + target.showPopover({ source: event.currentTarget }); + }; return (
void; + onTabClick: (panel: PanelType) => TabClickHandler; isLoggedIn: boolean; hasUnseenNotifications: boolean; hasFriendInSendouQ: boolean; @@ -225,17 +244,26 @@ function MobileTabBar({ const { t } = useTranslation(["front", "common"]); const chatContext = useChatContext(); const isHydrated = useHydrated(); - const panelIdOnceHydrated = (panel: PanelType) => - isHydrated ? panelIds[panel] : undefined; + const tabProps = ( + panel: PanelType, + { worksBeforeHydration = false } = {}, + ) => ({ + popoverTarget: + activePanel !== null + ? panelIds[activePanel] + : isHydrated || worksBeforeHydration + ? panelIds[panel] + : undefined, + isActive: activePanel === panel, + onClick: onTabClick(panel), + }); return (