MCPcopy
hub / github.com/vercel/next.js / linkClicked

Function linkClicked

packages/next/src/client/app-dir/link.tsx:254–317  ·  view source on GitHub ↗
(
  e: React.MouseEvent,
  href: string,
  linkInstanceRef: React.RefObject<LinkInstance | null>,
  replace?: boolean,
  scroll?: boolean,
  onNavigate?: OnNavigateEventHandler,
  transitionTypes?: string[]
)

Source from the content-addressed store, hash-verified

252}
253
254function linkClicked(
255 e: React.MouseEvent,
256 href: string,
257 linkInstanceRef: React.RefObject<LinkInstance | null>,
258 replace?: boolean,
259 scroll?: boolean,
260 onNavigate?: OnNavigateEventHandler,
261 transitionTypes?: string[]
262): void {
263 if (typeof window !== 'undefined') {
264 const { nodeName } = e.currentTarget
265
266 // anchors inside an svg have a lowercase nodeName
267 const isAnchorNodeName = nodeName.toUpperCase() === 'A'
268 if (
269 (isAnchorNodeName && isModifiedEvent(e)) ||
270 e.currentTarget.hasAttribute('download')
271 ) {
272 // ignore click for browser’s default behavior
273 return
274 }
275
276 if (!isLocalURL(href)) {
277 if (replace) {
278 // browser default behavior does not replace the history state
279 // so we need to do it manually
280 e.preventDefault()
281 location.replace(href)
282 }
283
284 // ignore click for browser’s default behavior
285 return
286 }
287
288 e.preventDefault()
289
290 if (onNavigate) {
291 let isDefaultPrevented = false
292
293 onNavigate({
294 preventDefault: () => {
295 isDefaultPrevented = true
296 },
297 })
298
299 if (isDefaultPrevented) {
300 return
301 }
302 }
303
304 const { dispatchNavigateAction } =
305 require('../components/app-router-instance') as typeof import('../components/app-router-instance')
306
307 React.startTransition(() => {
308 dispatchNavigateAction(
309 href,
310 replace ? 'replace' : 'push',
311 scroll === false ? ScrollBehavior.NoScroll : ScrollBehavior.Default,

Callers 1

onClickFunction · 0.70

Calls 6

isLocalURLFunction · 0.90
dispatchNavigateActionFunction · 0.85
toUpperCaseMethod · 0.80
isModifiedEventFunction · 0.70
replaceMethod · 0.65
requireFunction · 0.50

Tested by

no test coverage detected