14 lines · 652 BRaw
| 1 | import { forwardRef } from "react"; |
| 2 | import { Link } from "react-router"; |
| 3 | |
| 4 | type AnchorProps = React.AnchorHTMLAttributes<HTMLAnchorElement> & { to?: string }; |
| 5 | |
| 6 | export function isDocumentHref(href: string): boolean { |
| 7 | return !href.startsWith("/") || href.startsWith("//") || /^\/[^/]+\/raw\//.test(href); |
| 8 | } |
| 9 | |
| 10 | export const AppLink = forwardRef<HTMLAnchorElement, AnchorProps>(function AppLink({ href, to, ...rest }, ref) { |
| 11 | const target = href ?? to ?? ""; |
| 12 | if (isDocumentHref(target) || rest.target || rest.download !== undefined) return <a ref={ref} href={target} {...rest} />; |
| 13 | return <Link ref={ref} to={target} prefetch="intent" {...rest} />; |
| 14 | }); |