Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: emit native <img> events #416

Merged
merged 6 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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