Link

GitHub
A wrapper around NuxtLink with extra props.

Usage

The Link component is a wrapper around <NuxtLink> using the custom prop. It provides a few extra props:

  • inactive-class prop to set a class when the link is inactive, active-class is used when active.
  • exact prop to style with active-class when the link is active and the route is exactly the same as the current route.
  • exact-query and exact-hash props to style with active-class when the link is active and the query or hash is exactly the same as the current query or hash.
    • use exact-query="partial" to style with active-class when the link is active and the query partially match the current query.

The incentive behind this is to provide the same API as NuxtLink back in Nuxt 2 / Vue 2. You can read more about it in the Vue Router migration from Vue 2 guide.

It is used by the Breadcrumb, Button, ContextMenu, DropdownMenu and NavigationMenu components.

Tag

The Link components renders an <a> tag when a to prop is provided, otherwise it renders a <button> tag. You can use the as prop to change fallback tag.

<template>
  <PLink as="button">Link</PLink>
</template>
You can inspect the rendered HTML by changing the to prop.

Style

By default, the link has default active and inactive styles, check out the #theme section.

<template>
  <PLink to="/pohon/components/link">Link</PLink>
</template>
Try changing the to prop to see the active and inactive states.

You can override this behavior by using the raw prop and provide your own styles using class, active-class and inactive-class.

<template>
  <PLink raw to="/pohon/components/link" active-class="font-bold" inactive-class="color-text-muted">Link</PLink>
</template>

Locale

The Link component automatically integrates with @nuxtjs/i18n when installed. Internal links are automatically localized using the $localePath helper without requiring manual wrapping.

<template>
  <!-- Automatically becomes /en/about or /fr/about based on current locale -->
  <PLink to="/about">
    About
  </PLink>
</template>
You can still manually use localePath() or localeRoute() if needed.
Learn more about Internationalization in Pohon UI.

API

Props

Prop Default Type
as
'button'
any

The element or component this component should render as when not a link.

type
'button'
"submit" | "reset" | "button"

The type of the button when not a link.

disabled
boolean
active
boolean

Force the link to be active independent of the current route.

exact
boolean

Will only be active if the current route is an exact match.

exactQuery
boolean | "partial"

Allows controlling how the current route query sets the link as active.

exactHash
boolean

Will only be active if the current route hash is an exact match.

inactiveClass
string

The class to apply when the link is inactive.

raw
boolean

When true, only styles from class, activeClass, and inactiveClass will be applied.

locale
string | false | true

Control i18n auto-localization when @nuxtjs/i18n is installed.

  • undefined / true (default): auto-localizes to the current locale using $localePath. Paths already carrying a locale prefix (from e.g. switchLocalePath()) are detected and left untouched to prevent double-prefixing.
  • false: explicitly disables auto-localization.
  • string: localizes to a specific locale (e.g. 'fr').
to
string | et | X

Route Location the link should navigate to when clicked on.

href
string | et | X

An alias for to. If used with to, href will be ignored

external
boolean

Forces the link to be considered as external (true) or internal (false). This is helpful to handle edge-cases

target
null | "_blank" | "_parent" | "_self" | "_top" | string

Where to display the linked URL, as the name for a browsing context.

rel
null | string | "noopener" | "noreferrer" | "nofollow" | "sponsored" | "ugc"

A rel attribute value to apply on the link. Defaults to "noopener noreferrer" for external links.

noRel
boolean

If set to true, no rel attribute will be added to the link

prefetchedClass
string

A class to apply to links that have been prefetched.

prefetch
boolean

When enabled will prefetch middleware, layouts and payloads of links in the viewport.

prefetchOn
"visibility" | "interaction" | Partial<{ visibility: boolean; interaction: boolean; }>

Allows controlling when to prefetch links. By default, prefetch is triggered only on visibility.

noPrefetch
boolean

Escape hatch to disable prefetch attribute.

trailingSlash
"append" | "remove"

An option to either add or remove trailing slashes in the href for this specific link. Overrides the global trailingSlash option if provided.

activeClass
string

Class to apply when the link is active

exactActiveClass
string

Class to apply when the link is exact active

ariaCurrentValue
`'page'`
"page" | "step" | "location" | "date" | "time" | "true" | "false"

Value passed to the attribute aria-current when the link is exact active.

viewTransition
boolean

Pass the returned promise of router.push() to document.startViewTransition() if supported.

replace
boolean

Calls router.replace instead of router.push.

autofocus
false | true | "true" | "false"
form
string
formaction
string
formenctype
string
formmethod
string
formnovalidate
false | true | "true" | "false"
formtarget
string
name
string
download
any
hreflang
string
media
string
ping
string
referrerpolicy
"" | "no-referrer" | "no-referrer-when-downgrade" | "origin" | "origin-when-cross-origin" | "same-origin" | "strict-origin" | "strict-origin-when-cross-origin" | "unsafe-url"
This component also supports all native <a> HTML attributes.

Slots

Slot Type
default
{ active: boolean; }

Theme

app.config.ts
export default defineAppConfig({
  ui: {
    link: {
      base: 'outline-primary/25 focus-visible:outline-3 rounded-md',
      variants: {
        active: {
          true: 'color-primary',
          false: 'color-text-muted'
        },
        disabled: {
          true: 'cursor-not-allowed opacity-75'
        }
      },
      compoundVariants: [
        {
          active: false,
          disabled: false,
          class: [
            'hover:color-text',
            'transition-colors'
          ]
        }
      ]
    }
  }
})
vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from 'pohon-ui/vite'

export default defineConfig({
  plugins: [
    vue(),
    ui({
      ui: {
        link: {
          base: 'outline-primary/25 focus-visible:outline-3 rounded-md',
          variants: {
            active: {
              true: 'color-primary',
              false: 'color-text-muted'
            },
            disabled: {
              true: 'cursor-not-allowed opacity-75'
            }
          },
          compoundVariants: [
            {
              active: false,
              disabled: false,
              class: [
                'hover:color-text',
                'transition-colors'
              ]
            }
          ]
        }
      }
    })
  ]
})

Changelog

No recent changes