fix: Click route shared link should not toggle (#10690)

This commit is contained in:
Tom Moor authored and GitHub committed 2025-11-23 12:28:15 +01:00
1 parent 5bc2e7e62e
commit 5cc7601972
3 files changed
+51 -15

No files matched your search

+3 -9
View File
@@ -12,17 +12,11 @@ type Props = React.ComponentProps<typeof NavLink> & {
| 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;
};
+22 -3
View File
@@ -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<HTMLAnchorElement> {
/** 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<HTMLAnchorElement>) => 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<HTMLAnchorElement>) => {
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(() => {
@@ -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<NavLinkProps, "to"> & {
/** 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<HTMLAnchorElement>;
/** Callback when we expect the user to click on the link. Used for prefetching data. */
onClickIntent?: React.MouseEventHandler<HTMLElement>;
/** Callback fired when the disclosure icon is clicked */
onDisclosureClick?: React.MouseEventHandler<HTMLElement>;
/** 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<HTMLAnchorElement>
) {
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<HTMLAnchorElement>) => {
onDisclosureClick?.(ev);
if (onClick && !disabled && ev.isDefaultPrevented() === false) {
onClick(ev);
}
@@ -112,6 +131,9 @@ function SidebarLink(
const handleDisclosureClick = React.useCallback(
(ev: React.MouseEvent<HTMLElement>) => {
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}
>
<Content>
{expanded !== undefined && (
{hasDisclosure && (
<DisclosureComponent
expanded={expanded}
onClick={preventDefault}