Skip to content

Commit

Permalink
feat: emit native <img> events (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
hansds committed Oct 8, 2021
1 parent 22b538c commit f22ae01
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 8 deletions.
18 changes: 18 additions & 0 deletions docs/content/en/3.api/3.events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: Events
category: API
description: 'Listening to native events emitted by the contained img tag'
---

Native events emitted by the `<img>` element contained by `<nuxt-img>` and `<nuxt-picture>` components are re-emitted and can be listened to.

**Example:** Listen to the native `onLoad` event from `<nuxt-img>`

```html
<nuxt-img
src="/images/colors.jpg"
width="500"
height="500"
@load="doSomethingOnLoad"
/>
```
15 changes: 14 additions & 1 deletion playground/pages/nuxt-img.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
<template>
<div>
<nuxt-img src="/images/colors.jpg" width="500" height="500" />
<nuxt-img src="/images/colors.jpg" width="500" height="500" @load="isLoaded = true" />
<p>Received onLoad event: {{ isLoaded }}</p>
</div>
</template>

<script lang="ts">
import Vue from 'vue'
export default Vue.extend({
data () {
return {
isLoaded: false
}
}
})
</script>
15 changes: 14 additions & 1 deletion playground/pages/nuxt-picture.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
<template>
<div>
<nuxt-picture src="/images/colors.jpg" width="500" height="500" />
<nuxt-picture src="/images/colors.jpg" width="500" height="500" @load="isLoaded = true" />
<p>Received onLoad event: {{ isLoaded }}</p>
</div>
</template>

<script lang="ts">
import Vue from 'vue'
export default Vue.extend({
data () {
return {
isLoaded: false
}
}
})
</script>
6 changes: 1 addition & 5 deletions src/runtime/components/nuxt-img.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<template>
<img
:key="nSrc"
:src="nSrc"
v-bind="nAttrs"
>
<img :key="nSrc" :src="nSrc" v-bind="nAttrs" v-on="$listeners">
</template>

<script lang="ts">
Expand Down
1 change: 1 addition & 0 deletions src/runtime/components/nuxt-picture.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
:src="nSources[0].src"
:srcset="nSources[0].srcset"
:sizes="nSources[0].sizes"
v-on="$listeners"
>
</picture>
</template>
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/no-ssr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ describe('browser (ssr: false)', () => {
expect(negativeRequest).toBeFalsy()
})

test('should emit load event', async () => {
await page.waitForEvent('console', (msg) => {
expect(msg.text()).toBe('Image was loaded.')

return true
})
})

test('change image location', async () => {
await page.click('#button')
const positiveRequest = requests.find(request => request.match('/_ipx/s_300x200/1280px-K2_2006b.jpg'))
Expand Down
5 changes: 4 additions & 1 deletion test/fixture/pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div>
It works
<nuxt-img :src="image" width="300" height="200" />
<nuxt-img :src="image" width="300" height="200" @load="onLoad" />
<button id="button" @click="changeImage">
Change Image
</button>
Expand All @@ -18,6 +18,9 @@ export default {
methods: {
changeImage () {
this.image = '/1280px-K2_2006b.jpg'
},
onLoad () {
console.log('Image was loaded.')
}
}
}
Expand Down

1 comment on commit f22ae01

@Rigo-m
Copy link

@Rigo-m Rigo-m commented on f22ae01 Oct 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pi0 this fix is still not present in the npm release (release number not bumped)

Please sign in to comment.