-
Notifications
You must be signed in to change notification settings - Fork 215
Fix responsive prop, add a warning when it's not affecting the transformation #176
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
10e88d8
Fix responsive prop, and add a warning when it's not affecting the tr…
nirmaoz 2e69f34
Add responsive test description
nirmaoz 8f0cd98
Add responsive-override description
nirmaoz 1129a2d
Add RESPONSIVE_OVERRIDE_WARNING const
nirmaoz 5730b5c
Add comments explaining the getAttributes() function
nirmaoz df066bd
Remove unneeded import from Image component
nirmaoz fdd5999
Update cloudinary-core to version 2.10.3
nirmaoz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,44 @@ | ||
| /** | ||
| * When 'responsive' is passed to the Image component, cloudinary-core generates a data-src attribute instead of src. | ||
| * Then cloudinary.responsive() is called, it will add a 'src' attribute with calculated width according to the size | ||
| * of the <img> element's container. | ||
| * In these tests we first test for existence of 'data-src' attribute, and then we check that 'src' was also generated. | ||
| * Remember that Cypress removes the need to add a wait or timeout, and waits for a test to be true until it's default | ||
| * timeout is reached. | ||
| */ | ||
| describe('Responsive Image', () => { | ||
| beforeEach(() => { | ||
| // runs before each test in the block | ||
| cy.visit('/'); | ||
| cy.get('#responsiveBtn').click(); // Click on button | ||
| }); | ||
| // Here responsive() is called, but since a specified width value was passed (100) alongside responsive, | ||
| // The responsive() function does not change the width param of the transformation, because the specified width | ||
| // "overrides" the responsiveness. in other words, specifying a width value is stronger then the 'responsive' prop. | ||
| it('Should generate transformation with specified width value', () => { | ||
| cy.get('#responsive-override') | ||
| .should('have.attr', 'data-src').should('equal', 'http://res.cloudinary.com/demo/image/upload/c_scale,w_100/sample') | ||
| cy.get('#responsive-override') | ||
| .should('have.attr', 'src').should('equal', 'http://res.cloudinary.com/demo/image/upload/c_scale,w_100/sample') | ||
| cy.get('#responsive-override') | ||
| .should('have.attr', 'width').should('equal', '100') | ||
| }); | ||
| it('Responsive Image', () => { | ||
| cy.get('#responsive') | ||
| .should('have.attr', 'data-src').should('equal','http://res.cloudinary.com/demo/image/upload/c_scale,w_auto/sample') | ||
| .should('have.attr', 'data-src').should('equal', 'http://res.cloudinary.com/demo/image/upload/c_scale,w_auto/sample') | ||
| cy.get('#responsive') | ||
| .should('have.attr', 'src').should('equal','http://res.cloudinary.com/demo/image/upload/c_scale,w_400/sample') | ||
| .should('have.attr', 'src').should('equal', 'http://res.cloudinary.com/demo/image/upload/c_scale,w_400/sample') | ||
| }); | ||
| it('Disabled Breakpoints', () => { | ||
| cy.get('#responsive') | ||
| .should('have.attr', 'data-src').should('equal','http://res.cloudinary.com/demo/image/upload/c_scale,w_auto/sample') | ||
| .should('have.attr', 'data-src').should('equal', 'http://res.cloudinary.com/demo/image/upload/c_scale,w_auto/sample') | ||
| cy.get('#disable-breakpoints') | ||
| .should('have.attr', 'src').should('equal','http://res.cloudinary.com/demo/image/upload/c_scale,w_330/sample') | ||
| .should('have.attr', 'src').should('equal', 'http://res.cloudinary.com/demo/image/upload/c_scale,w_330/sample') | ||
| }); | ||
| it('Enabled Breakpoints', () => { | ||
| cy.get('#responsive') | ||
| .should('have.attr', 'data-src').should('equal','http://res.cloudinary.com/demo/image/upload/c_scale,w_auto/sample') | ||
| .should('have.attr', 'data-src').should('equal', 'http://res.cloudinary.com/demo/image/upload/c_scale,w_auto/sample') | ||
| cy.get('#breakpoints') | ||
| .should('have.attr', 'src').should('equal','http://res.cloudinary.com/demo/image/upload/c_scale,w_450/sample') | ||
| .should('have.attr', 'src').should('equal', 'http://res.cloudinary.com/demo/image/upload/c_scale,w_450/sample') | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,17 @@ | ||
| import React, {createRef, Fragment} from 'react'; | ||
| import CloudinaryComponent from '../CloudinaryComponent'; | ||
| import {extractCloudinaryProps, getImageTag, makeElementResponsive, getConfiguredCloudinary} from "../../Util"; | ||
| import {extractCloudinaryProps, getImageTag, makeElementResponsive} from "../../Util"; | ||
| import {Util} from "cloudinary-core"; | ||
| import PropTypes from "prop-types"; | ||
|
|
||
| const RESPONSIVE_OVERRIDE_WARNING = [ | ||
| `Warning: passing a number value for width cancels the 'responsive' prop's effect on the image transformation.`, | ||
| `The 'responsive' prop affects the image transformation only when width === 'auto'.`, | ||
| `Passing 'width="auto" responsive' will affect the actual image width that is fetched from Cloudinary.`, | ||
| `The 'responsive' prop causes the Image component to request an image which width is equal to the width of it's container.`, | ||
| `When passing 'width="auto" responsive', you can set the <img> element width by passing a 'style' prop` | ||
| ].join('\n'); | ||
|
|
||
| /** | ||
| * A component representing a Cloudinary served image | ||
| */ | ||
|
|
@@ -19,7 +27,12 @@ class Image extends CloudinaryComponent { | |
| * @return true when this image element should be made responsive, false otherwise. | ||
| */ | ||
| isResponsive = () => { | ||
| return this.props.responsive && this.imgElement && this.imgElement.current; | ||
| const {responsive, width} = this.getExtendedProps(); | ||
| if (responsive && width !== 'auto') { | ||
| console.warn(RESPONSIVE_OVERRIDE_WARNING); | ||
| } | ||
|
|
||
| return responsive && this.imgElement && this.imgElement.current; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -44,33 +57,46 @@ class Image extends CloudinaryComponent { | |
|
|
||
| let attributes = {...getImageTag(options).attributes(), ...nonCloudinaryProps}; | ||
|
|
||
| //React requires camelCase instead of snake_case attributes | ||
| attributes = Util.withCamelCaseKeys(attributes); | ||
|
|
||
| // Set placeholder Id | ||
| if (placeholder && attributes.id) { | ||
| attributes.id = attributes.id + '-cld-placeholder'; | ||
| } | ||
|
|
||
| // Set dataSrc if lazy loading and not in view | ||
| if (!isInView && this.shouldLazyLoad(options)) { | ||
| attributes.dataSrc = attributes.dataSrc || attributes.src; | ||
| delete attributes.src; | ||
| } | ||
|
|
||
| // The data-src attribute was turned into dataSrc by the camelCase function, | ||
| // But it's needed by cloudinary-core's responsive() function. Notice that it's not snake_case. | ||
| if (attributes.dataSrc) { | ||
| attributes['data-src'] = attributes.dataSrc; | ||
| } | ||
|
Comment on lines
+76
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ??? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at this again, calculating a new src isn't required, i've updated this part, please see updated commit |
||
|
|
||
| // Remove unneeded attributes, | ||
| ["src", "accessibility", "placeholder"].forEach(attr => { | ||
| ['dataSrc', 'accessibility', 'placeholder', 'breakpoints'].forEach(attr => { | ||
| delete attributes[attr]; | ||
| }); | ||
|
|
||
| // Set src or data-src attribute | ||
| const srcAttrName = isInView || !this.shouldLazyLoad(options) ? "src" : "data-src"; | ||
| attributes[srcAttrName] = getConfiguredCloudinary(options).url(options.publicId, options); | ||
|
|
||
| return attributes; | ||
| } | ||
|
|
||
| /** | ||
| * Update this image using cloudinary-core | ||
| */ | ||
| update = () => { | ||
| const {isInView} = this.state; | ||
|
|
||
| if (this.isResponsive()) { | ||
| const removeListener = makeElementResponsive(this.imgElement.current, this.getOptions()); | ||
| this.listenerRemovers.push(removeListener); | ||
| } | ||
|
|
||
| if (this.shouldLazyLoad(this.getExtendedProps())) { | ||
| if (!isInView && this.shouldLazyLoad(this.getExtendedProps())) { | ||
| Util.detectIntersection(this.imgElement.current, this.onIntersect); | ||
| } | ||
| } | ||
|
|
@@ -153,7 +179,6 @@ class Image extends CloudinaryComponent { | |
| </Fragment> | ||
| ); | ||
| } | ||
|
|
||
| return <img ref={attachRef} {...attributes}/> | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how is this lazy load part related?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I needed to update this because i added withCamelCaseKeys().