-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Chrome: Adding a ResponsiveImage component to wrap the featured image #911
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
Changes from 1 commit
b3fa3c3
50bde7e
2d054a6
406eb1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
… props @aduth
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import './style.scss'; | ||
|
|
||
| function ResponsiveImage( { src, alt, naturalWidth, naturalHeight } ) { | ||
| const imageStyle = { | ||
| paddingBottom: ( naturalHeight / naturalWidth * 100 ) + '%', | ||
| }; | ||
| return ( | ||
| <div className="components-responsive-image"> | ||
| <div style={ imageStyle } /> | ||
| <img src={ src } className="components-responsive-image__image" alt={ alt } /> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| export default ResponsiveImage; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| .components-responsive-image { | ||
| position: relative; | ||
| max-width: 100%; | ||
| } | ||
|
|
||
| .components-responsive-image__image { | ||
| position: absolute; | ||
| top: 0; | ||
| right: 0; | ||
| bottom: 0; | ||
| left: 0; | ||
| width: 100%; | ||
| height: 100%; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ import { connect } from 'react-redux'; | |
| */ | ||
| import { Component } from 'element'; | ||
| import { __ } from 'i18n'; | ||
| import { Button, PanelBody, Spinner } from 'components'; | ||
| import { Button, PanelBody, Spinner, ResponsiveImage } from 'components'; | ||
| import { MediaUploadButton } from 'blocks'; | ||
|
|
||
| /** | ||
|
|
@@ -50,21 +50,17 @@ class FeaturedImage extends Component { | |
| return; | ||
| } | ||
| this.setState( { loading: true } ); | ||
| const mediaIdToLoad = this.props.featuredImageId; | ||
| this.fetchMediaRequest = new wp.api.models.Media( { id: mediaIdToLoad } ).fetch() | ||
| if ( this.fetchMediaRequest ) { | ||
| this.fetchMediaRequest.abort(); | ||
| } | ||
| this.fetchMediaRequest = new wp.api.models.Media( { id: this.props.featuredImageId } ).fetch() | ||
| .done( ( media ) => { | ||
| if ( this.props.featuredImageId !== mediaIdToLoad ) { | ||
| return; | ||
| } | ||
| this.setState( { | ||
| loading: false, | ||
| media, | ||
| } ); | ||
| } ) | ||
| .fail( () => { | ||
| if ( this.props.featuredImageId !== mediaIdToLoad ) { | ||
|
||
| return; | ||
| } | ||
| this.setState( { | ||
| loading: false, | ||
| } ); | ||
|
|
@@ -80,15 +76,16 @@ class FeaturedImage extends Component { | |
| <div className="editor-featured-image__content"> | ||
| { !! featuredImageId && | ||
| <MediaUploadButton | ||
| buttonProps={ { className: 'button-link' } } | ||
| buttonProps={ { className: 'button-link editor-featured-image__preview' } } | ||
| onSelect={ onUpdateImage } | ||
| type="image" | ||
| > | ||
| { media && | ||
| <img | ||
| className="editor-featured-image__preview" | ||
| <ResponsiveImage | ||
| src={ media.source_url } | ||
| alt={ __( 'Featured image' ) } | ||
| naturalWidth={ media.media_details.width } | ||
| naturalHeight={ media.media_details.height } | ||
| /> | ||
| } | ||
| { loading && <Spinner /> } | ||
|
|
||
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.
In the future, or perhaps even now, we may want to make this component a bit more generic as a responsive panel. The padding technique is rarely used with images, except in this case where we don't know the rendered width, and often more for background images or video elements. It could also help avoid the need to overwhelm the props with individual
imgattributes we want to assign, if we instead accepted theimgas a child.Not too concerned about this being addressed here.
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 tried to handle this, I've renamed
ResponsiveImageasResponsiveWrapperand it works by checking that the children is a single React Element and then cloning the element to add the class name. Does this sound good for you?