# svelte-link

> Anchor link component for Svelte

Latest version **2.0.0** (published 2024-08-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install svelte-link
pnpm add svelte-link
yarn add svelte-link
bun add svelte-link
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; has provenance.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2024-08-14 |
| First published | 2020-04-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 10 |
| Author | Eric Liu |
| Maintainers | metonym |
| Keywords | svelte, link, prefetch, noopener noreferrer |

## Links

- npm: https://www.npmjs.com/package/svelte-link
- Repository: https://github.com/metonym/svelte-link
- Issues: https://github.com/metonym/svelte-link/issues
- npm.io page: https://npm.io/package/svelte-link

## Recent versions

- 2.0.0 (latest) — 2024-08-14
- 1.4.0 — 2022-04-15
- 1.3.0 — 2022-04-10
- 1.2.0 — 2022-02-25
- 1.1.0 — 2022-01-15
- 1.0.2 — 2020-12-08
- 1.0.1 — 2020-11-30
- 1.0.0 — 2020-11-30
- 0.3.0 — 2020-11-28
- 0.2.0 — 2020-04-19
- 0.1.0 — 2020-04-16

## README

# svelte-link

[![NPM][npm]][npm-url]

<!-- REPO_URL -->

> Anchor link component for Svelte.

<!-- TOC -->

## Installation

```bash
# npm
npm i -D svelte-link

# pnpm
pnpm i -D svelte-link

# Bun
bun i -D svelte-link

# Yarn
yarn add -D svelte-link
```

## Usage

### Basic

```svelte
<script>
  import Link from "svelte-link";
</script>

<Link href="https://github.com/">GitHub</Link>
```

### Preventing the default behavior

Because event modifiers cannot be used on Svelte components, use the mouse event in the forwarded `on:click` event to prevent the default behavior.

```svelte
<Link
  href="https://github.com/"
  on:click={(e) => {
    e.preventDefault();
  }}
>
  GitHub
</Link>
```

### Outbound links

`outbound` is an alias for setting `target="_blank"`. If `rel` is not specified for outbound links, [`rel="noopener noreferrer"` is set](https://developers.google.com/web/tools/lighthouse/audits/noopener).

`outbound` defaults to `true` if `href` points to an external URL. You can override this behaviour by explicitly setting `outbound` to `false`.

```svelte
<Link href="https://github.com/" outbound>GitHub</Link>

<!-- same as -->

<Link href="https://github.com/" target="_blank" rel="noopener noreferrer">
  GitHub
</Link>
```

### Prefetch

Inspired by [Sapper](https://sapper.svelte.dev/docs#prefetch_href), if the non-standard `rel="prefetch"` is present, this component will make a GET request to the `href` value when the user hovers over the link.

```svelte
<Link href="/about" rel="prefetch">About</Link>
```

### Disabled state

Setting `disabled` to `true` will render a `span` element instead of an anchor tag.

```svelte
<Link disabled href="https://github.com/">GitHub</Link>

<!-- <span>GitHub</span> -->
```

### Active state

Set `active` to `true` to signal an active state.

If `true`, the anchor link is given an "active" class with the `aria-current` attribute set to "page."

```svelte no-eval
<script>
  import { page } from "$app/stores";
</script>

<Link href="/" active={$page.url.pathname === "/"}>GitHub</Link>

<!-- <a href="/" class="active" aria-current="page">GitHub</a> -->
```

## API

### Props

| Prop     | Type      | Default value           |
| :------- | :-------- | :---------------------- |
| href     | `string`  | `"javascript:void(0);"` |
| disabled | `boolean` | `false`                 |
| outbound | `boolean` | `undefined`             |
| target   | `string`  | `undefined`             |
| rel      | `string`  | `undefined`             |
| active   | `boolean` | `false`                 |

### Forwarded events

- on:click
- on:mouseover
- on:mouseenter
- on:mouseout
- on:focus
- on:blur
- on:keydown

## Changelog

[Changelog](CHANGELOG.md)

## License

[MIT](LICENSE)

[npm]: https://img.shields.io/npm/v/svelte-link?style=for-the-badge&color=%23ff3e00
[npm-url]: https://npmjs.com/package/svelte-link

---
_Source: https://npm.io/package/svelte-link · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
