From 5cc7601972e4591e7036760397358d197b70b428 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 23 Nov 2025 12:28:15 +0100 Subject: [PATCH] fix: Click route shared link should not toggle (#10690) --- app/components/NavLink.tsx | 12 ++------ app/components/Sidebar/components/NavLink.tsx | 25 ++++++++++++++-- .../Sidebar/components/SidebarLink.tsx | 29 +++++++++++++++++-- 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/app/components/NavLink.tsx b/app/components/NavLink.tsx index 8b5c0b467e..03302d2de9 100644 --- a/app/components/NavLink.tsx +++ b/app/components/NavLink.tsx @@ -12,17 +12,11 @@ type Props = React.ComponentProps & { | null, location: LocationDescriptorObject ) => React.ReactNode; - /** - * If true, the tab will only be active if the path matches exactly. - */ + /** If true, the tab will only be active if the path matches exactly */ exact?: boolean; - /** - * CSS properties to apply to the link when it is active. - */ + /** CSS properties to apply to the link when it is active */ activeStyle?: React.CSSProperties; - /** - * The path to match against the current location. - */ + /** The path to match against the current location */ to: LocationDescriptor; }; diff --git a/app/components/Sidebar/components/NavLink.tsx b/app/components/Sidebar/components/NavLink.tsx index faa9cdb9a2..b72f8e7401 100644 --- a/app/components/Sidebar/components/NavLink.tsx +++ b/app/components/Sidebar/components/NavLink.tsx @@ -29,18 +29,33 @@ const normalizeToLocation = ( const joinClassnames = (...classnames: (string | undefined)[]) => classnames.filter((i) => i).join(" "); +/** + * Props for the NavLink component. + * Extends standard anchor element attributes with React Router navigation functionality. + */ export interface Props extends React.AnchorHTMLAttributes { + /** CSS class name to apply when the link is active */ activeClassName?: string; + /** Inline styles to apply when the link is active */ activeStyle?: React.CSSProperties; + /** Whether to automatically scroll the link into view when it becomes active */ scrollIntoViewIfNeeded?: boolean; + /** If true, only matches when the path matches the location.pathname exactly */ exact?: boolean; + /** If true, use history.replace instead of history.push when navigating */ replace?: boolean; + /** Custom function to determine if the link is active */ isActive?: (match: match | null, location: Location) => boolean; + /** The location to match against. Defaults to the current history location */ location?: Location; + /** If true, trailing slashes on the path will be considered when matching */ strict?: boolean; + /** The location to navigate to. Can be a string path or location descriptor object */ to: LocationDescriptor; + /** Custom component to use instead of the default anchor element */ component?: React.ComponentType; - onBeforeClick?: () => void; + /** Callback fired when an active link is clicked */ + onActiveClick?: (event: React.MouseEvent) => void; } /** @@ -59,7 +74,7 @@ const NavLink = ({ style: styleProp, scrollIntoViewIfNeeded, onClick, - onBeforeClick, + onActiveClick, to, ...rest }: Props) => { @@ -126,6 +141,10 @@ const NavLink = ({ (event: React.MouseEvent) => { onClick?.(event); + if (isActive) { + onActiveClick?.(event); + } + if (shouldFastClick(event)) { event.currentTarget.focus(); @@ -138,7 +157,7 @@ const NavLink = ({ }); } }, - [onClick, navigateTo, shouldFastClick] + [onClick, navigateTo, isActive, shouldFastClick] ); React.useEffect(() => { diff --git a/app/components/Sidebar/components/SidebarLink.tsx b/app/components/Sidebar/components/SidebarLink.tsx index aa0c17c162..4406405e20 100644 --- a/app/components/Sidebar/components/SidebarLink.tsx +++ b/app/components/Sidebar/components/SidebarLink.tsx @@ -15,26 +15,46 @@ import { ActionV2WithChildren } from "~/types"; import { ContextMenu } from "~/components/Menu/ContextMenu"; import { useTranslation } from "react-i18next"; +/** + * Props for the SidebarLink component. + * Extends NavLink props with additional sidebar-specific functionality. + */ type Props = Omit & { + /** The location to navigate to when the link is clicked */ to?: LocationDescriptor; + /** Ref callback to access the underlying HTML element */ innerRef?: (ref: HTMLElement | null | undefined) => void; + /** Callback fired when the link is clicked */ onClick?: React.MouseEventHandler; /** Callback when we expect the user to click on the link. Used for prefetching data. */ onClickIntent?: React.MouseEventHandler; + /** Callback fired when the disclosure icon is clicked */ onDisclosureClick?: React.MouseEventHandler; + /** Icon to display on the left side of the link */ icon?: React.ReactNode; + /** Text label or content to display for the link */ label?: React.ReactNode; + /** Optional menu to display on hover or interaction */ menu?: React.ReactNode; + /** Whether to show an unread badge indicator */ unreadBadge?: boolean; + /** Whether to show action buttons on hover */ showActions?: boolean; + /** Whether the link is disabled and non-interactive */ disabled?: boolean; + /** Whether the link is currently active */ active?: boolean; /** If set, a disclosure will be rendered to the left of any icon */ expanded?: boolean; + /** Whether this link is the current active drop target for drag and drop */ isActiveDrop?: boolean; + /** Whether this link represents a draft document */ isDraft?: boolean; + /** Nesting depth level for indentation (0-based) */ depth?: number; + /** Whether to automatically scroll this link into view if needed */ scrollIntoViewIfNeeded?: boolean; + /** Optional context menu action to display */ contextAction?: ActionV2WithChildren; }; @@ -72,6 +92,7 @@ function SidebarLink( }: Props, ref: React.RefObject ) { + const hasDisclosure = expanded !== undefined; const { t } = useTranslation(); const theme = useTheme(); const { handleMouseEnter, handleMouseLeave } = useClickIntent(onClickIntent); @@ -101,8 +122,6 @@ function SidebarLink( const handleClick = React.useCallback( (ev: React.MouseEvent) => { - onDisclosureClick?.(ev); - if (onClick && !disabled && ev.isDefaultPrevented() === false) { onClick(ev); } @@ -112,6 +131,9 @@ function SidebarLink( const handleDisclosureClick = React.useCallback( (ev: React.MouseEvent) => { + if (!hasDisclosure) { + return; + } ev.preventDefault(); ev.stopPropagation(); onDisclosureClick?.(ev); @@ -130,6 +152,7 @@ function SidebarLink( style={style} activeStyle={isActiveDrop ? activeDropStyle : activeStyle} onClick={handleClick} + onActiveClick={handleDisclosureClick} onMouseEnter={handleMouseEnter} onMouseLeave={handleMouseLeave} onDragEnter={handleMouseEnter} @@ -143,7 +166,7 @@ function SidebarLink( {...rest} > - {expanded !== undefined && ( + {hasDisclosure && (