Skip to content

@unlazy/vue

The Vue integration provides an UnLazyImage component for your Vue application.

Installation

Install the @unlazy/vue package using your favorite package manager:

bash
pnpm add -D @unlazy/vue
bash
yarn add -D @unlazy/vue
bash
npm install -D @unlazy/vue

Local Import

Import the UnLazyImage component in your Vue single file component and use it in your template:

vue
<script setup lang="ts">
import { UnLazyImage } from '@unlazy/vue/components'
</script>

<template>
  <UnLazyImage
    blurhash="LKO2:N%2Tw=w]~RBVZRi};RPxuwH"
    src-set="image-320w.jpg 320w, image-640w.jpg 640w"
    auto-sizes
  />
</template>

Global Registration

If you prefer to make the UnLazyImage component globally available, you can register it in your main.ts file across your entire Vue application:

ts
// src/main.ts
import Unlazy from '@unlazy/vue'
import { createApp } from 'vue'
import App from './App.vue'

const app = createApp(App)
app.use(Unlazy)
app.mount('#app')

UnLazyImage Component

The UnLazyImage component allows you to easily implement unlazy in your Vue application, providing a smoother image loading experience. The component supports automatic calculation of the sizes attribute with the autoSizes prop. It also enables you to specify a blurhash for the blurry placeholder image.

Props

The UnLazyImage component accepts the following props:

PropTypeDefaultDescription
srcString-Image source URL to be lazy-loaded.
srcSetString-Image source set to be lazy-loaded.
sourcesArray-Array of UnLazySource objects to render a <picture> element.
autoSizesBooleanfalseWhether the sizes attribute should be automatically calculated.
blurhashString-A BlurHash string representing the blurry placeholder image.
thumbhashString-A ThumbHash string representing the blurry placeholder image. If both are provided, thumbhash takes precedence.
placeholderSrcString-Optional image source URL for a custom placeholder image. Ignored if a hash is provided.
placeholderSizeNumber32The size of the longer edge for BlurHash decoding. Ignored for ThumbHash.
preloadBooleanfalseWhether the image should be preloaded immediately, bypassing lazy loading.
loadingString'lazy'Loading strategy for the image ('lazy' or 'eager').

The component also accepts all standard <img> HTML attributes via Vue's attribute inheritance.

Emitted Events

EventPayloadDescription
image-loadHTMLImageElementFired once when the real image finishes loading.
image-errorHTMLImageElement, EventFired when the preload fails.

INFO

Native @error listeners attach to the underlying <img> via Vue's attribute fallthrough and fire for both browser-level errors and the synthetic event unlazy dispatches when its preload fails. Reach for @image-error when you want the unwrapped element with a typed payload.

Reactivity

The component uses Vue's watchEffect to handle reactive updates:

  • Automatically tracks changes to src, srcSet, and hash props
  • Re-initializes lazy loading when relevant props change
  • Cleans up previous listeners before setting up new ones
  • Bypasses lazy loading entirely when preload is true

Examples

html
<UnLazyImage
  width="800"
  height="600"
  blurhash="LKO2:N%2Tw=w]~RBVZRi};RPxuwH"
  src-set="image-320w.jpg 320w, image-640w.jpg 640w"
  auto-sizes
/>
html
<UnLazyImage
  width="800"
  height="600"
  thumbhash="1QcSHQRnh493V4dIh4eXh1h4kJUI"
  src-set="image-320w.jpg 320w, image-640w.jpg 640w"
  auto-sizes
/>
html
<UnLazyImage
  width="800"
  height="600"
  placeholder-src="data:image/svg+xml, ..."
  src-set="image-320w.jpg 320w, image-640w.jpg 640w"
  auto-sizes
/>

TIP

In each example, the sizes attribute is automatically calculated given the auto-sizes prop.

INFO

When using BlurHash, set explicit width and height attributes. See Hash-Based Placeholders for why.

Multiple Image Sources

Pass sources to render a <picture> element with format negotiation and art direction. Each entry becomes a <source> child:

vue
<script setup lang="ts">
import type { UnLazySource } from 'unlazy'
import { UnLazyImage } from '@unlazy/vue/components'

const sources: UnLazySource[] = [
  { type: 'image/avif', srcSet: 'hero.avif 1x, hero@2x.avif 2x' },
  {
    media: '(max-width: 600px)',
    srcSet: 'hero-mobile.jpg',
    width: 480,
    height: 640,
  },
]
</script>

<template>
  <UnLazyImage
    src="/hero.jpg"
    :sources="sources"
    :width="640"
    :height="427"
  />
</template>

TIP

Set width and height per source to prevent layout shift when the breakpoint changes. Modern browsers use the matched <source>'s dimensions for the rendered <img>'s aspect ratio.

Preload Image

Useful if the UnLazyImage is part of e.g. a slider, and you want to preload the next image.

vue
<template>
  <UnLazyImage
    :blurhash="blurhash"
    src-set="image-320w.jpg 320w, image-640w.jpg 640w"
    auto-sizes
    preload
  />
</template>

Released under the MIT License.