Link for Astro
Astro anchors for page paths, external URLs, phone numbers, and obfuscated email.
Installation
bunx --bun shadcn@latest add @gnostic/linknpx shadcn@latest add @gnostic/linkpnpm dlx shadcn@latest add @gnostic/linkCopy the files
src/components/ui/link/index.tssrc/components/ui/link/link.astrosrc/components/ui/link/types.ts
Usage
import { Link } from "@/components/ui/link"
<Link href="/components/button">Button</Link>
<Link href="https://docs.astro.build">Astro docs</Link>
<Link href="tel:+15555550100">+1 555 555 0100</Link>
<Link href="mailto:hello@example.com">hello@example.com</Link>
Link uses the same variant and size values as Button.
The size default is lg. Use variant="link" for a text link.
Destinations
Link reads the start of href and sets the anchor behavior.
- A site path stays on this site. A path that starts with
/receives the siteBASE_URL. The anchor getsrel="prefetch"unless you setprefetch={false}. - An
httporhttpsURL opens in a new tab. The anchor getsrel="noopener nofollow". SetexternalInNewTab={false}to keep the same tab. - A value that starts with
#stays on the current page. - A
mailto:ortel:value is stored as base64 indata-contact. The rendered anchor useshref="#". A click reads that value and opens the mail or phone app. The address stays out of the page source.
Set target or rel when a link needs a value other than these defaults.
Props
LinkProps
hrefstring
Destination address. A site path stays on this site. An `http` URL opens in a new tab. A `mailto:` or `tel:` value is stored until the visitor clicks.
stringexternalInNewTab?boolean
Open an external URL in a new tab.
booleantrueprefetch?boolean
Add `rel="prefetch"` on an internal path.
booleantrueAnchor attributes such as class, target, title, and rel pass through.
target and rel replace the defaults when you set them.
Read the installed source for the component.
When to use Link
Use Link for a destination. Use Button for an action that stays on the page, such as submitting a form. Put the label in the default slot. Link accepts the Button variants, so a destination can look like a button or a text link.
Keyboard and behavior
Link is a native anchor. Enter activates it. An external URL opens a new tab. A phone or email click uses one page script. That script decodes data-contact and opens the address. The same script handles contact links added after the first render.
Theme and layout
The styles come from Button variants and the shared theme tokens. Use the default variant for a primary destination and variant="link" for text in a sentence. A class on one link does not change the site palette.