Skip to content

Commit eb043d9

Browse files
feat(vue,nuxt): loaded event when image is loaded
Closes #32, #40
1 parent ae317f3 commit eb043d9

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

docs/integrations/nuxt.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ The `UnLazyImage` component accepts the following props:
9696
| `preload` | Boolean | A flag to indicate whether the image should be preloaded, even if it's not in the viewport yet. |
9797
| `ssr` | Boolean | Whether the ThumbHash or BlurHash should be decoded on the server. Overrides the global module configuration if set. |
9898

99+
### Emitted Events
100+
101+
| Event | Description |
102+
| --- | --- | --- |
103+
| `loaded` | Emitted when the image has been loaded. The event payload is the image element itself. |
104+
99105
## Examples
100106

101107
::: tip

docs/integrations/vue.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ The `UnLazyImage` component accepts the following props:
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. |
6969
| `preload` | Boolean | A flag to indicate whether the image should be preloaded, even if it's not in the viewport yet. |
7070

71+
### Emitted Events
72+
73+
| Event | Description |
74+
| --- | --- | --- |
75+
| `loaded` | Emitted when the image has been loaded. The event payload is the image element itself. |
76+
7177
## Examples
7278

7379
::: code-group

packages/nuxt/src/runtime/components/UnLazyImage.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ const props = withDefaults(
6464
},
6565
)
6666
67+
const emit = defineEmits<{
68+
(event: 'loaded', image: HTMLImageElement): void
69+
}>()
70+
6771
const unlazy = useRuntimeConfig().public.unlazy as ModuleOptions
6872
const hash = computed(() => props.thumbhash || props.blurhash)
6973
@@ -113,7 +117,10 @@ watchEffect(() => {
113117
return
114118
115119
// Placeholder is already decoded
116-
cleanup = lazyLoad(target.value, { hash: false })
120+
cleanup = lazyLoad(target.value, {
121+
hash: false,
122+
onImageLoad: image => emit('loaded', image),
123+
})
117124
})
118125
119126
onBeforeUnmount(() => {

packages/vue/src/components/UnLazyImage.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ const props = defineProps<{
2828
preload?: boolean
2929
}>()
3030
31+
const emit = defineEmits<{
32+
(event: 'loaded', image: HTMLImageElement): void
33+
}>()
34+
3135
const target = ref<HTMLImageElement | undefined>()
3236
let cleanup: (() => void) | undefined
3337
@@ -48,6 +52,7 @@ watchEffect(() => {
4852
hash: props.thumbhash || props.blurhash,
4953
hashType: props.thumbhash ? 'thumbhash' : 'blurhash',
5054
placeholderSize: props.placeholderSize,
55+
onImageLoad: image => emit('loaded', image),
5156
})
5257
})
5358

0 commit comments

Comments
 (0)