@@ -55,28 +55,25 @@ class Image extends CloudinaryComponent {
55
55
const options = { ...this . getOptions ( ) , ...additionalOptions } ;
56
56
const { nonCloudinaryProps} = extractCloudinaryProps ( options ) ;
57
57
58
- let attributes = { ...getImageTag ( options ) . attributes ( ) , ...nonCloudinaryProps } ;
58
+ // React requires camelCase instead of snake_case attributes
59
+ const attributes = ( { ...Util . withCamelCaseKeys ( getImageTag ( options ) . attributes ( ) ) , ...nonCloudinaryProps } ) ;
59
60
60
- //React requires camelCase instead of snake_case attributes
61
- attributes = Util . withCamelCaseKeys ( attributes ) ;
61
+ // We want to keep 'data-src' if it exists
62
+ if ( attributes . dataSrc ) {
63
+ attributes [ 'data-src' ] = attributes . dataSrc ;
64
+ }
62
65
63
66
// Set placeholder Id
64
67
if ( placeholder && attributes . id ) {
65
68
attributes . id = attributes . id + '-cld-placeholder' ;
66
69
}
67
70
68
- // Set dataSrc if lazy loading and not in view
71
+ // Set data-src if lazy loading and not in view
69
72
if ( ! isInView && this . shouldLazyLoad ( options ) ) {
70
- attributes . dataSrc = attributes . dataSrc || attributes . src ;
73
+ attributes [ 'data-src' ] = attributes . dataSrc || attributes . src ;
71
74
delete attributes . src ;
72
75
}
73
76
74
- // The data-src attribute was turned into dataSrc by the camelCase function,
75
- // But it's needed by cloudinary-core's responsive() function. Notice that it's not snake_case.
76
- if ( attributes . dataSrc ) {
77
- attributes [ 'data-src' ] = attributes . dataSrc ;
78
- }
79
-
80
77
// Remove unneeded attributes,
81
78
[ 'dataSrc' , 'accessibility' , 'placeholder' , 'breakpoints' ] . forEach ( attr => {
82
79
delete attributes [ attr ] ;
@@ -155,31 +152,38 @@ class Image extends CloudinaryComponent {
155
152
} ) ;
156
153
} ;
157
154
155
+ renderPlaceholder = ( placeholder , attributes ) => {
156
+ attributes . style = { ...( attributes . style || { } ) , opacity : 0 , position : 'absolute' }
157
+ attributes . onLoad = this . handleImageLoaded ;
158
+ const placeholderWrapperStyle = { display : 'inline' }
159
+ const placeholderAttributes = this . getAttributes ( { placeholder : placeholder . props . type } ) ;
160
+
161
+ return (
162
+ < Fragment >
163
+ { this . renderImage ( attributes ) }
164
+ < div style = { placeholderWrapperStyle } >
165
+ < img { ...placeholderAttributes } />
166
+ </ div >
167
+ </ Fragment >
168
+ ) ;
169
+ } ;
170
+
171
+ renderImage = ( attributes ) => {
172
+ return < img ref = { this . attachRef } { ...attributes } />
173
+ }
174
+
158
175
render ( ) {
159
- const { attachRef , getAttributes , handleImageLoaded } = this ;
176
+ const { isLoaded } = this . state ;
160
177
const { children} = this . getExtendedProps ( ) ;
161
178
const placeholder = this . getChildPlaceholder ( children ) ;
162
- const { isLoaded} = this . state ;
163
-
164
- const attributes = getAttributes ( ) ;
179
+ const attributes = this . getAttributes ( ) ;
165
180
166
- //If image wasn't loaded and there's a placeholder then we render it alongside the image .
181
+ //If image wasn't loaded and there's a child placeholder then we render it.
167
182
if ( ! isLoaded && placeholder ) {
168
- const placeholderStyle = { display : isLoaded ? 'none' : 'inline' }
169
- attributes . style = { ...( attributes . style || { } ) , opacity : 0 , position : 'absolute' }
170
- attributes . onLoad = handleImageLoaded ;
171
- const placeholderAttributes = getAttributes ( { placeholder : placeholder . props . type } ) ;
172
-
173
- return (
174
- < Fragment >
175
- < img ref = { attachRef } { ...attributes } />
176
- < div style = { placeholderStyle } >
177
- < img { ...placeholderAttributes } />
178
- </ div >
179
- </ Fragment >
180
- ) ;
183
+ return this . renderPlaceholder ( placeholder , attributes ) ;
181
184
}
182
- return < img ref = { attachRef } { ...attributes } />
185
+
186
+ return this . renderImage ( attributes ) ;
183
187
}
184
188
}
185
189
0 commit comments