Skip to content

Commit cbe9e98

Browse files
feat(vue): add preload prop
1 parent 17340e4 commit cbe9e98

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

docs/integrations/vue.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ The `UnLazyImage` component accepts the following props:
6666
| `thumbhash` | String | A ThumbHash string representing the blurry placeholder image. |
6767
| `placeholderSrc` | String | Optional image source URL for a custom placeholder image. Will be ignored if a BlurHash or ThumbHash is provided. |
6868
| `placeholderSize` | Number | The size of the longer edge (width or height) of the BlurHash image to be decoded, depending on the aspect ratio. This option only applies when the `blurhash` prop is used. |
69+
| `preload` | Boolean | A flag to indicate whether the image should be preloaded, even if it's not in the viewport yet. |
6970

7071
## Examples
7172

@@ -96,3 +97,17 @@ The `UnLazyImage` component accepts the following props:
9697
::: tip
9798
In each example, the `sizes` attribute is automatically calculated given the `auto-sizes` prop.
9899
:::
100+
101+
### Preload Image
102+
103+
Useful if the `UnLazyImage` is part of e.g. a slider, and you want to preload the next image.
104+
105+
```vue
106+
<template>
107+
<UnLazyImage
108+
:blurhash="blurhash"
109+
src-set="image-320w.jpg 320w, image-640w.jpg 640w"
110+
auto-sizes
111+
preload
112+
/>
113+
</template>

packages/vue/src/components/UnLazyImage.vue

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup lang="ts">
22
import { onBeforeUnmount, ref, watchEffect } from 'vue'
3-
import { lazyLoad } from 'unlazy'
3+
import { lazyLoad, loadImage } from 'unlazy'
44
import type { ImgHTMLAttributes } from 'vue'
55
66
const props = defineProps<{
@@ -21,6 +21,11 @@ const props = defineProps<{
2121
placeholderSrc?: string
2222
/** The size of the longer edge (width or height) of the BlurHash image to be decoded, depending on the aspect ratio. This option only applies when the `blurhash` prop is used. */
2323
placeholderSize?: number
24+
/**
25+
* A flag to indicate whether the image should be preloaded, even if it's not in the viewport yet.
26+
* @default false
27+
*/
28+
preload?: boolean
2429
}>()
2530
2631
const target = ref<HTMLImageElement | undefined>()
@@ -32,6 +37,11 @@ watchEffect(() => {
3237
if (!target.value)
3338
return
3439
40+
if (props.preload) {
41+
loadImage(target.value)
42+
return
43+
}
44+
3545
cleanup = lazyLoad(target.value, {
3646
hash: props.thumbhash || props.blurhash,
3747
hashType: props.thumbhash ? 'thumbhash' : 'blurhash',

0 commit comments

Comments
 (0)