Skip to content
refs
14 lines · 652 BRaw
1import { forwardRef } from "react";
2import { Link } from "react-router";
3
4type AnchorProps = React.AnchorHTMLAttributes<HTMLAnchorElement> & { to?: string };
5
6export function isDocumentHref(href: string): boolean {
7 return !href.startsWith("/") || href.startsWith("//") || /^\/[^/]+\/raw\//.test(href);
8}
9
10export 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});