Skip to content
Closed
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
20 changes: 14 additions & 6 deletions packages/react-router/src/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ export function useLinkProps<
options: UseLinkPropsOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo>,
): React.AnchorHTMLAttributes<HTMLAnchorElement> {
const router = useRouter()
const isPointerDown = React.useRef(false)
const [isTransitioning, setIsTransitioning] = React.useState(false)

const {
Expand Down Expand Up @@ -571,9 +572,9 @@ export function useLinkProps<
className,
onClick,
onFocus,
onPointerDown,
onMouseEnter,
onMouseLeave,
onTouchStart,
ignoreBlocker,
...rest
} = options
Expand Down Expand Up @@ -641,7 +642,7 @@ export function useLinkProps<
...(onFocus && { onFocus }),
...(onMouseEnter && { onMouseEnter }),
...(onMouseLeave && { onMouseLeave }),
...(onTouchStart && { onTouchStart }),
...(onPointerDown && { onPointerDown }),
}
}

Expand Down Expand Up @@ -684,15 +685,22 @@ export function useLinkProps<
})
}

// The click handler
// The focus handler
const handleFocus = (e: MouseEvent) => {
if (disabled) return
if (preload) {
doPreload()
// If we're clicking/touching, skip focus preload
if (isPointerDown.current) {
isPointerDown.current = false
} else {
doPreload()
}
}
}

const handleTouchStart = handleFocus
const handleOnPointerDown = (e: PointerEvent) => {
isPointerDown.current = true
}

const handleEnter = (e: MouseEvent) => {
if (disabled) return
Expand Down Expand Up @@ -762,11 +770,11 @@ export function useLinkProps<
: next.maskedLocation
? router.history.createHref(next.maskedLocation.href)
: router.history.createHref(next.href),
onPointerDown: composeHandlers([onPointerDown, handleOnPointerDown]),
onClick: composeHandlers([onClick, handleClick]),
onFocus: composeHandlers([onFocus, handleFocus]),
onMouseEnter: composeHandlers([onMouseEnter, handleEnter]),
onMouseLeave: composeHandlers([onMouseLeave, handleLeave]),
onTouchStart: composeHandlers([onTouchStart, handleTouchStart]),
disabled: !!disabled,
target,
...(Object.keys(resolvedStyle).length && { style: resolvedStyle }),
Expand Down