{"version":3,"file":"1618a07b.js","sources":["../../../node_modules/@shopify/hydrogen/dist/esnext/components/Link/Link.client.js"],"sourcesContent":["import React, { useCallback, useEffect, useState } from 'react';\nimport { useLocation } from '../../foundation/Router/BrowserRouter.client.js';\nimport { createPath } from 'history';\nimport { buildPath, useNavigate, } from '../../foundation/useNavigate/useNavigate.js';\nimport { RSC_PATHNAME } from '../../constants.js';\nimport { useInternalServerProps } from '../../foundation/useServerProps/use-server-props.js';\nimport { useBasePath } from '../../foundation/useRouteParams/RouteParamsProvider.client.js';\n/**\n * The `Link` component is used to navigate between routes. Because it renders an underlying `` element, all\n * properties available to the `` element are also available to the `Link` component.\n * For more information, refer to the [`` element documentation](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attributes).\n */\nexport const Link = React.forwardRef(function Link(props, ref) {\n const navigate = useNavigate();\n const location = useLocation();\n const [_, startTransition] = React.useTransition();\n const routeBasePath = useBasePath();\n /**\n * Inspired by Remix's Link component\n */\n const [shouldPrefetch, setShouldPrefetch] = useState(false);\n const [maybePrefetch, setMaybePrefetch] = useState(false);\n const { reloadDocument, target, replace: _replace, onClick, clientState, prefetch = true, scroll = true, } = props;\n const to = buildPath(props.basePath ?? routeBasePath, props.to);\n const internalClick = useCallback((e) => {\n if (onClick)\n onClick(e);\n if (!reloadDocument && // do regular browser stuff\n e.button === 0 && // Ignore everything but left clicks\n (!target || target === '_self') && // Let browser handle \"target=_blank\"\n !isModifiedEvent(e) // Ignore modifier key clicks\n ) {\n e.preventDefault();\n // If the URL hasn't changed, the regular will do a replace\n const replace = !!_replace || createPath(location) === createPath({ pathname: to });\n navigate(to, {\n replace,\n scroll,\n clientState,\n basePath: '/', // path was already resolved with the base\n });\n }\n }, [\n onClick,\n reloadDocument,\n target,\n _replace,\n location,\n to,\n navigate,\n clientState,\n scroll,\n ]);\n const signalPrefetchIntent = () => {\n /**\n * startTransition to yield to more important updates\n */\n startTransition(() => {\n if (prefetch && !!to) {\n setMaybePrefetch(true);\n }\n });\n };\n const cancelPrefetchIntent = () => {\n /**\n * startTransition to yield to more important updates\n */\n startTransition(() => {\n if (prefetch) {\n setMaybePrefetch(false);\n }\n });\n };\n /**\n * Wrapping `maybePrefetch` inside useEffect allows the user to quickly graze over\n * a link without triggering a prefetch.\n */\n useEffect(() => {\n if (maybePrefetch) {\n const id = setTimeout(() => {\n setShouldPrefetch(true);\n }, 100);\n return () => {\n clearTimeout(id);\n };\n }\n }, [maybePrefetch]);\n const onMouseEnter = composeEventHandlers(props.onMouseEnter, signalPrefetchIntent);\n const onMouseLeave = composeEventHandlers(props.onMouseLeave, cancelPrefetchIntent);\n const onFocus = composeEventHandlers(props.onFocus, signalPrefetchIntent);\n const onBlur = composeEventHandlers(props.onBlur, cancelPrefetchIntent);\n const onTouchStart = composeEventHandlers(props.onTouchStart, signalPrefetchIntent);\n return (React.createElement(React.Fragment, null,\n React.createElement(\"a\", { ...without(props, [\n 'to',\n 'replace',\n 'clientState',\n 'reloadDocument',\n 'prefetch',\n 'scroll',\n ]), ref: ref, rel: props.rel ??\n (to.startsWith('http') || to.startsWith('//')\n ? 'noreferrer noopener'\n : undefined), onClick: internalClick, onMouseEnter: onMouseEnter, onMouseLeave: onMouseLeave, onFocus: onFocus, onBlur: onBlur, onTouchStart: onTouchStart, href: to }, props.children),\n shouldPrefetch && React.createElement(Prefetch, { pathname: to })));\n});\nfunction Prefetch({ pathname }) {\n const { getProposedLocationServerProps } = useInternalServerProps();\n const location = useLocation();\n const newPath = createPath({ pathname });\n if (pathname.startsWith('http') || newPath === createPath(location)) {\n return null;\n }\n const newLocation = new URL(newPath, window.location.href);\n const proposedServerState = getProposedLocationServerProps({\n pathname: newLocation.pathname,\n search: newLocation.search,\n });\n const href = `${RSC_PATHNAME}?state=` +\n encodeURIComponent(JSON.stringify(proposedServerState));\n return React.createElement(\"link\", { rel: \"prefetch\", as: \"fetch\", href: href });\n}\n/**\n * Credit: Remix's component.\n */\nexport function composeEventHandlers(theirHandler, ourHandler) {\n return (event) => {\n theirHandler?.(event);\n if (!event.defaultPrevented) {\n ourHandler(event);\n }\n };\n}\nfunction isModifiedEvent(event) {\n return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);\n}\nfunction without(obj, props) {\n const newObj = {};\n for (const key of Object.keys(obj)) {\n if (!props.includes(key)) {\n newObj[key] = obj[key];\n }\n }\n return newObj;\n}\n"],"names":["Link","React","forwardRef","props","ref","navigate","useNavigate","location","useLocation","_","startTransition","useTransition","routeBasePath","useBasePath","shouldPrefetch","setShouldPrefetch","useState","maybePrefetch","setMaybePrefetch","reloadDocument","target","replace","_replace","onClick","clientState","prefetch","scroll","to","buildPath","basePath","internalClick","useCallback","e","button","isModifiedEvent","preventDefault","createPath","pathname","signalPrefetchIntent","cancelPrefetchIntent","useEffect","id","setTimeout","clearTimeout","onMouseEnter","composeEventHandlers","onMouseLeave","onFocus","onBlur","onTouchStart","_jsxs","_Fragment","_jsx","without","rel","startsWith","undefined","children","Prefetch","getProposedLocationServerProps","useInternalServerProps","newPath","newLocation","URL","window","href","proposedServerState","search","RSC_PATHNAME","encodeURIComponent","JSON","stringify","theirHandler","ourHandler","event","defaultPrevented","metaKey","altKey","ctrlKey","shiftKey","obj","newObj","key","Object","keys","includes"],"mappings":"sOAYaA,MAAAA,EAAOC,EAAMC,WAAW,SAAcC,EAAOC,EAAK,SAC3D,MAAMC,EAAWC,IACXC,EAAWC,IACX,CAACC,EAAGC,CAAe,EAAIT,EAAMU,cAAa,EAC1CC,EAAgBC,IAIhB,CAACC,EAAgBC,CAAiB,EAAIC,EAAQ,QAAA,SAAC,EAAK,EACpD,CAACC,EAAeC,CAAgB,EAAIF,EAAQ,QAAA,SAAC,EAAK,EAClD,CAAEG,eAAAA,EAAgBC,OAAAA,EAAQC,QAASC,EAAUC,QAAAA,EAASC,YAAAA,EAAaC,SAAAA,EAAW,GAAMC,OAAAA,EAAS,EAAO,EAAGvB,EACvGwB,EAAKC,GAAUzB,EAAAA,EAAM0B,WAAN1B,KAA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