From 472b060e6cdc1e6ad935aa87e74eba32d5e2bc75 Mon Sep 17 00:00:00 2001 From: Celia Amador Date: Fri, 13 Nov 2026 13:56:36 +0100 Subject: [PATCH 1/2] Restore navigation guard for NavItems Assisted-by: OpenAI Codex --- .../src/components/NavItem/NavItem.tsx | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/libs/ui-components/src/components/NavItem/NavItem.tsx b/libs/ui-components/src/components/NavItem/NavItem.tsx index 1888103a07..2d20fb9b01 100644 --- a/libs/ui-components/src/components/NavItem/NavItem.tsx +++ b/libs/ui-components/src/components/NavItem/NavItem.tsx @@ -2,6 +2,7 @@ import * as React from 'react'; import { NavItem as PFNavItem } from '@patternfly/react-core'; import { useAppContext } from '../../hooks/useAppContext'; +import { useNavigationGuard } from '../common/NavigationGuardContext'; type ActiveChildrenProps = { isActive: boolean; @@ -16,18 +17,23 @@ const ActiveChildren: React.FC = ({ isActive, children, set return children; }; -type NavItemProps = { - to: string; - children: React.ReactNode; -}; - -const NavItem: React.FC = ({ to, children }) => { +const NavItem = ({ to, children }: React.PropsWithChildren<{ to: string }>) => { const { - router: { NavLink }, + router: { NavLink, useNavigate: useRouterNavigate }, } = useAppContext(); + const { tryNavigate } = useNavigationGuard(); + const navigate = useRouterNavigate(); const [isActive, setIsActive] = React.useState(false); return ( - + { + const ev = event as unknown as React.MouseEvent; + ev.preventDefault(); + tryNavigate(() => navigate(to)); + }} + > {({ isActive }) => ( From e43a3acb769710591239702f60128eabd4f70617 Mon Sep 17 00:00:00 2001 From: Celia Amador Date: Mon, 16 Nov 2026 09:21:34 +0100 Subject: [PATCH 2/2] Detect plain left click Assisted-by: OpenAI Codex --- .../src/components/NavItem/NavItem.tsx | 8 +++++--- libs/ui-components/src/hooks/useNavigate.tsx | 8 ++------ libs/ui-components/src/utils/browser.test.ts | 19 +++++++++++++++++++ libs/ui-components/src/utils/browser.ts | 12 ++++++++++++ 4 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 libs/ui-components/src/utils/browser.test.ts create mode 100644 libs/ui-components/src/utils/browser.ts diff --git a/libs/ui-components/src/components/NavItem/NavItem.tsx b/libs/ui-components/src/components/NavItem/NavItem.tsx index 2d20fb9b01..6349b74ece 100644 --- a/libs/ui-components/src/components/NavItem/NavItem.tsx +++ b/libs/ui-components/src/components/NavItem/NavItem.tsx @@ -2,6 +2,7 @@ import * as React from 'react'; import { NavItem as PFNavItem } from '@patternfly/react-core'; import { useAppContext } from '../../hooks/useAppContext'; +import { type ClickEvent, isPlainLeftClick } from '../../utils/browser'; import { useNavigationGuard } from '../common/NavigationGuardContext'; type ActiveChildrenProps = { @@ -29,9 +30,10 @@ const NavItem = ({ to, children }: React.PropsWithChildren<{ to: string }>) => { id={to} isActive={isActive} onClick={(event) => { - const ev = event as unknown as React.MouseEvent; - ev.preventDefault(); - tryNavigate(() => navigate(to)); + if (isPlainLeftClick(event as unknown as ClickEvent)) { + event.preventDefault(); + tryNavigate(() => navigate(to)); + } }} > diff --git a/libs/ui-components/src/hooks/useNavigate.tsx b/libs/ui-components/src/hooks/useNavigate.tsx index 1995f44b88..d477111f4d 100644 --- a/libs/ui-components/src/hooks/useNavigate.tsx +++ b/libs/ui-components/src/hooks/useNavigate.tsx @@ -1,6 +1,7 @@ import * as React from 'react'; import { type LinkProps as RouterLinkProps } from 'react-router-dom'; import { useNavigationGuard } from '../components/common/NavigationGuardContext'; +import { isPlainLeftClick } from '../utils/browser'; import { useAppContext } from './useAppContext'; export interface NavigateFunction { @@ -130,12 +131,7 @@ export const Link = ({ to, query, onClick, ...rest }: LinkProps) => { {...rest} onClick={(event) => { onClick?.(event); - if (event.defaultPrevented) { - return; - } - const isModified = event.metaKey || event.ctrlKey || event.shiftKey || event.altKey; - // Let the browser handle new-tab/window / non-primary clicks. - if (event.button !== 0 || isModified || (rest.target && rest.target !== '_self')) { + if (event.defaultPrevented || !isPlainLeftClick(event) || (rest.target && rest.target !== '_self')) { return; } // Always route through the guard so we never miss a lock due to stale React state. diff --git a/libs/ui-components/src/utils/browser.test.ts b/libs/ui-components/src/utils/browser.test.ts new file mode 100644 index 0000000000..21e2cbc197 --- /dev/null +++ b/libs/ui-components/src/utils/browser.test.ts @@ -0,0 +1,19 @@ +import { describe, expect, it } from 'vitest'; + +import { isPlainLeftClick } from './browser'; + +const click = (overrides: Partial = {}) => ({ button: 0, ...overrides }) as React.MouseEvent; + +describe('isPlainLeftClick', () => { + it('accepts a plain left click', () => { + expect(isPlainLeftClick(click())).toBe(true); + }); + + it('guards when the event shape is unknown', () => { + expect(isPlainLeftClick({})).toBe(true); + }); + + it.each(['button', 'metaKey', 'ctrlKey', 'shiftKey', 'altKey'] as const)('rejects %s', (key) => { + expect(isPlainLeftClick(click({ [key]: key === 'button' ? 1 : true }))).toBe(false); + }); +}); diff --git a/libs/ui-components/src/utils/browser.ts b/libs/ui-components/src/utils/browser.ts new file mode 100644 index 0000000000..d7232e7393 --- /dev/null +++ b/libs/ui-components/src/utils/browser.ts @@ -0,0 +1,12 @@ +// PatternFly types its NavItem onClick as a FormEvent. +export type ClickEvent = { + button?: number; + metaKey?: boolean; + ctrlKey?: boolean; + shiftKey?: boolean; + altKey?: boolean; +}; + +// A missing button counts as a plain click. +export const isPlainLeftClick = (event: ClickEvent): boolean => + (event.button ?? 0) === 0 && !event.metaKey && !event.ctrlKey && !event.shiftKey && !event.altKey;