import React from "react";
import { ExternalLink, Download } from "lucide-react";
import "./AccessibleLink.css";
/**
* AccessibleLink
* - href : destination (required)
* - children : visible label (required, describe the destination)
* - external : opens in new tab (adds rel, icon, SR-only text)
* - download : treat as download; shows a download icon
* - fileMeta : "(PDF, 1.2 MB)" when download=true
*/
export default function AccessibleLink({
href,
children,
external = false,
download = false,
fileMeta,
className = "",
...rest
}) {
const linkClass = [
"link",
external && "link--external",
download && "link--download",
className,
]
.filter(Boolean)
.join(" ");
return (
<a
href={href}
className={linkClass}
{...(external && {
target: "_blank",
rel: "noopener noreferrer",
})}
{...(download && { download: true })}
{...rest}
>
{download && <Download aria-hidden="true" size={14} />}
<span>{children}</span>
{fileMeta && <span className="link__meta">{fileMeta}</span>}
{external && (
<>
<ExternalLink aria-hidden="true" size={14} />
<span className="sr-only">(opens in a new tab)</span>
</>
)}
</a>
);
}
import AccessibleLink from "./AccessibleLink";
export default function Demo() {
return (
<>
{/* Internal link */}
<AccessibleLink href="/pricing">pricing plans</AccessibleLink>
{/* External link (new tab) */}
<AccessibleLink href="https://www.w3.org/WAI/WCAG22/" external>
WCAG 2.2 reference
</AccessibleLink>
{/* Download link with file metadata */}
<AccessibleLink
href="/files/report.pdf"
download
fileMeta="(PDF, 1.2 MB)"
>
Download Q4 report
</AccessibleLink>
</>
);
}