2
2
<img
3
3
v-if =" !custom"
4
4
ref =" imgEl"
5
- :class =" placeholder && !placeholderLoaded ? placeholderClass : undefined"
6
- v-bind =" {
7
- ...isServer ? { onerror: 'this.setAttribute(\'data-error\', 1)' } : {},
8
- ...imgAttrs,
9
- ...attrs,
10
- }"
5
+ :class =" placeholder ? placeholderClass : undefined"
6
+ v-bind =" imgAttrs"
11
7
:src =" src"
12
8
>
13
9
<slot
14
10
v-else
15
11
v-bind =" {
16
- ...isServer ? { onerror: 'this.setAttribute(\'data-error\', 1)' } : {},
17
- imgAttrs: {
18
- ...imgAttrs,
19
- ...attrs,
20
- },
12
+ imgAttrs,
21
13
isLoaded: placeholderLoaded,
22
14
src,
23
15
}"
@@ -30,11 +22,10 @@ import type { ImgHTMLAttributes } from 'vue'
30
22
import type { ProviderDefaults , ConfiguredImageProviders } from ' @nuxt/image'
31
23
32
24
import { useImage } from ' ../composables'
33
- import { parseSize } from ' ../utils'
34
25
import { prerenderStaticImages } from ' ../utils/prerender'
35
26
import { markFeatureUsage } from ' ../utils/performance'
36
- import { useImageModifiers , useNormalisedAttrs , useProviderOptions } from ' ./_base '
37
- import type { BaseImageProps } from ' ./_base '
27
+ import { useImageProps } from ' ../utils/props '
28
+ import type { BaseImageProps } from ' ../utils/props '
38
29
39
30
import { useHead , useNuxtApp , useRequestEvent } from ' #imports'
40
31
@@ -44,86 +35,73 @@ export interface ImageProps<Provider extends keyof ConfiguredImageProviders> ext
44
35
placeholderClass? : string
45
36
}
46
37
47
- const attrs = useAttrs () as ImgHTMLAttributes
48
38
const props = defineProps <ImageProps <Provider >>()
49
39
50
40
const emit = defineEmits <{
51
41
(event : ' load' , payload : Event ): unknown
52
42
(event : ' error' , payload : string | Event ): unknown
53
43
}>()
54
44
55
- defineSlots <{
56
- default(props : {
57
- imgAttrs: ImgHTMLAttributes
58
- isLoaded: boolean
59
- src? : string
60
- }): any
61
- }>()
62
-
63
- const isServer = import .meta .server
64
-
65
- const modifiers = useImageModifiers (props )
45
+ defineSlots <{ default(props : DefaultSlotProps ): any }>()
66
46
67
47
const $img = useImage ()
68
- const providerOptions = useProviderOptions (props )
48
+ const { providerOptions, normalizedAttrs, imageModifiers } = useImageProps (props )
69
49
70
50
const sizes = computed (() => $img .getSizes (props .src ! , {
71
51
... providerOptions .value ,
72
52
sizes: props .sizes ,
73
53
densities: props .densities ,
74
- modifiers: {
75
- ... modifiers .value ,
76
- width: parseSize (props .width ),
77
- height: parseSize (props .height ),
78
- },
54
+ modifiers: imageModifiers .value ,
79
55
}))
80
56
81
57
const placeholderLoaded = ref (false )
82
- const baseAttrs = useNormalisedAttrs (props )
83
58
59
+ const attrs = useAttrs () as ImgHTMLAttributes
84
60
const imgAttrs = computed (() => ({
85
- ... baseAttrs .value ,
61
+ ... normalizedAttrs .value ,
86
62
' data-nuxt-img' : ' ' ,
87
63
... (! props .placeholder || placeholderLoaded .value )
88
64
? { sizes: sizes .value .sizes , srcset: sizes .value .srcset }
89
65
: {},
66
+ ... import .meta .server ? { onerror: ' this.setAttribute(\' data-error\' , 1)' } : {},
67
+ ... attrs ,
90
68
}))
91
69
92
70
const placeholder = computed (() => {
93
- let placeholder = props .placeholder
94
-
95
- if (placeholder === ' ' ) {
96
- placeholder = true
71
+ if (placeholderLoaded .value ) {
72
+ return false
97
73
}
98
74
99
- if (! placeholder || placeholderLoaded .value ) {
75
+ const placeholder = props .placeholder === ' ' ? [10 , 10 ] : props .placeholder
76
+
77
+ if (! placeholder ) {
100
78
return false
101
79
}
102
80
103
81
if (typeof placeholder === ' string' ) {
104
82
return placeholder
105
83
}
106
84
107
- const size = ( Array .isArray (placeholder )
85
+ const [width = 10 , height = width , quality = 50 , blur = 3 ] = Array .isArray (placeholder )
108
86
? placeholder
109
- : ( typeof placeholder === ' number' ? [placeholder , placeholder ] : [10 , 10 ])) as [ w : number , h : number , q : number , b : number ]
87
+ : typeof placeholder === ' number' ? [placeholder ] : []
110
88
111
89
return $img (props .src ! , {
112
- ... modifiers .value ,
113
- width: size [ 0 ] ,
114
- height: size [ 1 ] ,
115
- quality: size [ 2 ] || 50 ,
116
- blur: size [ 3 ] || 3 ,
90
+ ... imageModifiers .value ,
91
+ width ,
92
+ height ,
93
+ quality ,
94
+ blur ,
117
95
}, providerOptions .value )
118
96
})
119
97
120
98
const mainSrc = computed (() =>
121
99
props .sizes
122
100
? sizes .value .src
123
- : $img (props .src ! , modifiers .value , providerOptions .value ),
101
+ : $img (props .src ! , imageModifiers .value , providerOptions .value ),
124
102
)
125
103
126
- const src = computed (() => placeholder .value ? placeholder . value : mainSrc .value )
104
+ const src = computed (() => placeholder .value || mainSrc .value )
127
105
128
106
if (import .meta .server && props .preload ) {
129
107
const isResponsive = Object .values (sizes .value ).every (v => v )
@@ -203,3 +181,11 @@ onMounted(() => {
203
181
}
204
182
})
205
183
</script >
184
+
185
+ <script lang="ts">
186
+ export interface DefaultSlotProps {
187
+ imgAttrs: ImgHTMLAttributes
188
+ isLoaded: boolean
189
+ src? : string
190
+ }
191
+ </script >
0 commit comments