diff --git a/libs/ui-components/src/components/NavItem/NavItem.tsx b/libs/ui-components/src/components/NavItem/NavItem.tsx index 1888103a07..6349b74ece 100644 --- a/libs/ui-components/src/components/NavItem/NavItem.tsx +++ b/libs/ui-components/src/components/NavItem/NavItem.tsx @@ -2,6 +2,8 @@ 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 = { isActive: boolean; @@ -16,18 +18,24 @@ 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 ( - + { + if (isPlainLeftClick(event as unknown as ClickEvent)) { + event.preventDefault(); + tryNavigate(() => navigate(to)); + } + }} + > {({ isActive }) => ( 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;