Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Link for Astro

Astro anchors for page paths, external URLs, phone numbers, and obfuscated email.

Page
Button
External
Astro docs
Phone
+1 555 555 0100
Email
hello@example.com

Installation

bunx --bun shadcn@latest add @gnostic/link

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 site BASE_URL. The anchor gets rel="prefetch" unless you set prefetch={false}.
  • An http or https URL opens in a new tab. The anchor gets rel="noopener nofollow". Set externalInNewTab={false} to keep the same tab.
  • A value that starts with # stays on the current page.
  • A mailto: or tel: value is stored as base64 in data-contact. The rendered anchor uses href="#". 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

PropType
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.

Typestring
externalInNewTab?boolean

Open an external URL in a new tab.

Typeboolean
Defaulttrue
prefetch?boolean

Add `rel="prefetch"` on an internal path.

Typeboolean
Defaulttrue

Anchor 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.

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.