import classNames from "classnames"; import type { HTMLAttributes } from "react"; import { useEditorHost } from "./EditorHost"; import { isYaakUrl } from "../lib/yaakUrl"; import { Icon } from "./Icon"; export interface ExternalLinkProps extends HTMLAttributes { href: string; noUnderline?: boolean; /** Opens the link; defaults to the host's `openUrl`, then a new browser tab */ onOpen?: (href: string) => void; } /** A link that leaves the app, marked with an external-link icon */ export function ExternalLink({ href, children, noUnderline, className, onOpen, ...other }: ExternalLinkProps) { const host = useEditorHost(); const open = onOpen ?? host.openUrl; const isYaakLink = isYaakUrl(href); return ( // eslint-disable-next-line react/jsx-no-target-blank { if (open == null) return; e.preventDefault(); open(href); }} className={classNames( className, "relative", "inline-flex items-center hover:underline group", !noUnderline && "underline", )} {...other} > {children} ); }