@@ -55,28 +55,25 @@ class Image extends CloudinaryComponent {
5555 const options = { ...this . getOptions ( ) , ...additionalOptions } ;
5656 const { nonCloudinaryProps} = extractCloudinaryProps ( options ) ;
5757
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 } ) ;
5960
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+ }
6265
6366 // Set placeholder Id
6467 if ( placeholder && attributes . id ) {
6568 attributes . id = attributes . id + '-cld-placeholder' ;
6669 }
6770
68- // Set dataSrc if lazy loading and not in view
71+ // Set data-src if lazy loading and not in view
6972 if ( ! isInView && this . shouldLazyLoad ( options ) ) {
70- attributes . dataSrc = attributes . dataSrc || attributes . src ;
73+ attributes [ 'data-src' ] = attributes . dataSrc || attributes . src ;
7174 delete attributes . src ;
7275 }
7376
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-
8077 // Remove unneeded attributes,
8178 [ 'dataSrc' , 'accessibility' , 'placeholder' , 'breakpoints' ] . forEach ( attr => {
8279 delete attributes [ attr ] ;
@@ -155,31 +152,38 @@ class Image extends CloudinaryComponent {
155152 } ) ;
156153 } ;
157154
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+
158175 render ( ) {
159- const { attachRef , getAttributes , handleImageLoaded } = this ;
176+ const { isLoaded } = this . state ;
160177 const { children} = this . getExtendedProps ( ) ;
161178 const placeholder = this . getChildPlaceholder ( children ) ;
162- const { isLoaded} = this . state ;
163-
164- const attributes = getAttributes ( ) ;
179+ const attributes = this . getAttributes ( ) ;
165180
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.
167182 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 ) ;
181184 }
182- return < img ref = { attachRef } { ...attributes } />
185+
186+ return this . renderImage ( attributes ) ;
183187 }
184188}
185189
0 commit comments