Skip to content

Commit

Permalink
feat(vue,nuxt): loaded event when image is loaded
Browse files Browse the repository at this point in the history
Closes #32, #40
  • Loading branch information
johannschopplich committed Jan 7, 2024
1 parent ae317f3 commit eb043d9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/integrations/nuxt.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ The `UnLazyImage` component accepts the following props:
| `preload` | Boolean | A flag to indicate whether the image should be preloaded, even if it's not in the viewport yet. |
| `ssr` | Boolean | Whether the ThumbHash or BlurHash should be decoded on the server. Overrides the global module configuration if set. |

### Emitted Events

| Event | Description |
| --- | --- | --- |
| `loaded` | Emitted when the image has been loaded. The event payload is the image element itself. |

## Examples

::: tip
Expand Down
6 changes: 6 additions & 0 deletions docs/integrations/vue.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ The `UnLazyImage` component accepts the following props:
| `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. |
| `preload` | Boolean | A flag to indicate whether the image should be preloaded, even if it's not in the viewport yet. |

### Emitted Events

| Event | Description |
| --- | --- | --- |
| `loaded` | Emitted when the image has been loaded. The event payload is the image element itself. |

## Examples

::: code-group
Expand Down
9 changes: 8 additions & 1 deletion packages/nuxt/src/runtime/components/UnLazyImage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ const props = withDefaults(
},
)
const emit = defineEmits<{
(event: 'loaded', image: HTMLImageElement): void
}>()
const unlazy = useRuntimeConfig().public.unlazy as ModuleOptions
const hash = computed(() => props.thumbhash || props.blurhash)
Expand Down Expand Up @@ -113,7 +117,10 @@ watchEffect(() => {
return
// Placeholder is already decoded
cleanup = lazyLoad(target.value, { hash: false })
cleanup = lazyLoad(target.value, {
hash: false,
onImageLoad: image => emit('loaded', image),
})
})
onBeforeUnmount(() => {
Expand Down
5 changes: 5 additions & 0 deletions packages/vue/src/components/UnLazyImage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const props = defineProps<{
preload?: boolean
}>()
const emit = defineEmits<{
(event: 'loaded', image: HTMLImageElement): void
}>()
const target = ref<HTMLImageElement | undefined>()
let cleanup: (() => void) | undefined
Expand All @@ -48,6 +52,7 @@ watchEffect(() => {
hash: props.thumbhash || props.blurhash,
hashType: props.thumbhash ? 'thumbhash' : 'blurhash',
placeholderSize: props.placeholderSize,
onImageLoad: image => emit('loaded', image),
})
})
Expand Down

0 comments on commit eb043d9

Please sign in to comment.