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(nuxt-img): add custom slot for full control of rendering #1626

Merged
merged 18 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 9 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
17 changes: 17 additions & 0 deletions docs/content/2.usage/1.nuxt-img.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ To generate special versions of images for screens with increased pixel density.

Display a placeholder image before the actual image is fully loaded.

You may also use the [placeholder slot](/usage/nuxt-img#placeholder-1) to pass complex data, if needed.
danekslama marked this conversation as resolved.
Show resolved Hide resolved

The placeholder prop can be either a string, a boolean, a number, or an array. The usage is shown below for each case.

```html
Expand Down Expand Up @@ -310,6 +312,21 @@ const nonce = useNonce()
</script>
```

## Slots

### `placeholder`

Use this slot to display a placeholder before the actual image loads
danekslama marked this conversation as resolved.
Show resolved Hide resolved
```html
<NuxtImg
src="/images/colors.jpg"
>
<template #placeholder>
I am visible before the image loads!
</template>
</NuxtImg>
```

## Events

Native events emitted by the `<img>` element contained by `<NuxtImg>` and `<NuxtPicture>` components are re-emitted and can be listened to.
Expand Down
24 changes: 17 additions & 7 deletions src/runtime/components/NuxtImg.vue
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
<template>
<img
v-show="isImageLoaded || !$slots.placeholder"
ref="imgEl"
:class="props.placeholder && !placeholderLoaded ? props.placeholderClass : undefined"
:class="props.placeholder && !isImageLoaded ? props.placeholderClass : undefined"
v-bind="{
...isServer ? { onerror: 'this.setAttribute(\'data-error\', 1)' } : {},
...imgAttrs,
...attrs,
}"
:src="src"
>

<slot
danekslama marked this conversation as resolved.
Show resolved Hide resolved
v-if="!isImageLoaded"
name="placeholder"
/>
</template>

<script setup lang="ts">
import { computed, onMounted, ref, useAttrs } from 'vue'
import { computed, onMounted, ref, useAttrs, useSlots } from 'vue'
import { useHead } from '@unhead/vue'
import { useImage } from '../composables'
import { parseSize } from '../utils'
Expand All @@ -36,7 +42,7 @@ const $img = useImage()

const _base = useBaseImage(props)

const placeholderLoaded = ref(false)
const isImageLoaded = ref(false)
const imgEl = ref<HTMLImageElement>()

type AttrsT = typeof _base.attrs.value & {
Expand All @@ -59,7 +65,7 @@ const sizes = computed(() => $img.getSizes(props.src!, {
const imgAttrs = computed(() => {
const attrs: AttrsT = { ..._base.attrs.value, 'data-nuxt-img': '' }

if (!props.placeholder || placeholderLoaded.value) {
if (!props.placeholder || isImageLoaded.value) {
attrs.sizes = sizes.value.sizes
attrs.srcset = sizes.value.srcset
}
Expand All @@ -74,7 +80,7 @@ const placeholder = computed(() => {
placeholder = true
}

if (!placeholder || placeholderLoaded.value) {
if (!placeholder || isImageLoaded.value) {
return false
}

Expand Down Expand Up @@ -135,7 +141,9 @@ const nuxtApp = useNuxtApp()
const initialLoad = nuxtApp.isHydrating

onMounted(() => {
if (placeholder.value) {
const slots = useSlots()

if (placeholder.value || slots.placeholder) {
const img = new Image()

if (mainSrc.value) {
Expand All @@ -148,7 +156,7 @@ onMounted(() => {
}

img.onload = (event) => {
placeholderLoaded.value = true
isImageLoaded.value = true
emit('load', event)
}

Expand All @@ -166,11 +174,13 @@ onMounted(() => {
emit('error', new Event('error'))
}
else {
isImageLoaded.value = true
emit('load', new Event('load'))
}
}

imgEl.value.onload = (event) => {
isImageLoaded.value = true
emit('load', event)
}

Expand Down
Loading
Loading