Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions libs/ui-components/src/components/NavItem/NavItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,18 +18,24 @@ const ActiveChildren: React.FC<ActiveChildrenProps> = ({ isActive, children, set
return children;
};

type NavItemProps = {
to: string;
children: React.ReactNode;
};

const NavItem: React.FC<NavItemProps> = ({ 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 (
<PFNavItem id={to} isActive={isActive}>
<PFNavItem
id={to}
isActive={isActive}
onClick={(event) => {
if (isPlainLeftClick(event as unknown as ClickEvent)) {
event.preventDefault();
tryNavigate(() => navigate(to));
}
}}
>
<NavLink to={to}>
{({ isActive }) => (
<ActiveChildren setIsActive={setIsActive} isActive={isActive}>
Expand Down
8 changes: 2 additions & 6 deletions libs/ui-components/src/hooks/useNavigate.tsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions libs/ui-components/src/utils/browser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, expect, it } from 'vitest';

import { isPlainLeftClick } from './browser';

const click = (overrides: Partial<React.MouseEvent> = {}) => ({ 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);
});
});
12 changes: 12 additions & 0 deletions libs/ui-components/src/utils/browser.ts
Original file line number Diff line number Diff line change
@@ -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;
Loading