-
Notifications
You must be signed in to change notification settings - Fork 0
Advanced Image View #56
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
16 commits
Select commit
Hold shift + click to select a range
c24591e
:sparkles: Advanced Image View
JoshuaRogan c04f0cf
:rotating_light: Fix lint
JoshuaRogan 0540dd6
:white_check_mark: Add some more tests
JoshuaRogan d6b84d6
:art: Comments
JoshuaRogan 0eeffda
Rename to Image, minor cleanup
vforge 88cec7d
Move vignetteHelper to utils
vforge 54088dd
:sparkles: New Vignette helper, Fix Image (Img) component
vforge 6238986
:sparkles: New ImagePreload component
vforge ebef2e2
:white_check_mark: Update tests
vforge dd98271
:boom: Cleanup and remove Vignette component
vforge 663a176
:pencil2: Cleanup
vforge 0f55a9a
Merge remote-tracking branch 'origin/master' into advanced-image-view
vforge e68b5e4
:pencil2: Add missing import in components/index
vforge 8003d59
:pencil2: Explain vignette helper better
vforge f3e7d18
:boom: Add utils/vignette to build
vforge ef29d5a
:rotating_light: Linting fixes
vforge 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
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Default: | ||
|
||
```js | ||
<Image | ||
alt="test" | ||
src="https://static.wikia.nocookie.net/2a6c845c-c6e9-472e-abed-24863817b795" | ||
width="160" | ||
height="100" | ||
/> | ||
``` | ||
|
||
Disable lazy loading | ||
|
||
```js | ||
<Image | ||
alt="test" | ||
src="https://static.wikia.nocookie.net/2a6c845c-c6e9-472e-abed-24863817b795" | ||
disableLazy | ||
/> | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Disabled lazyLoading 1`] = ` | ||
<Image | ||
alt="test" | ||
disableLazy={true} | ||
src="http://vignette.wikia-dev.us/22b12324-ab36-4266-87c9-452776157205" | ||
> | ||
<img | ||
alt="test" | ||
className="" | ||
src="http://vignette.wikia-dev.us/22b12324-ab36-4266-87c9-452776157205" | ||
/> | ||
<noscript> | ||
<img | ||
alt="test" | ||
className="" | ||
src="http://vignette.wikia-dev.us/22b12324-ab36-4266-87c9-452776157205" | ||
/> | ||
</noscript> | ||
</Image> | ||
`; | ||
|
||
exports[`Image test 1`] = ` | ||
<Image | ||
alt="test" | ||
disableLazy={false} | ||
src="http://vignette.wikia-dev.us/22b12324-ab36-4266-87c9-452776157205" | ||
> | ||
<ImagePreloader | ||
src="http://vignette.wikia-dev.us/22b12324-ab36-4266-87c9-452776157205" | ||
srcSet={null} | ||
> | ||
<img | ||
alt="test" | ||
className="" | ||
src="http://vignette.wikia-dev.us/22b12324-ab36-4266-87c9-452776157205/smart/width/5/height/5" | ||
/> | ||
</ImagePreloader> | ||
<noscript> | ||
<img | ||
alt="test" | ||
className="" | ||
src="http://vignette.wikia-dev.us/22b12324-ab36-4266-87c9-452776157205" | ||
/> | ||
</noscript> | ||
</Image> | ||
`; | ||
|
||
exports[`Image test for non-vignette 1`] = ` | ||
<Image | ||
alt="test" | ||
disableLazy={false} | ||
src="https://foo.jpg" | ||
> | ||
<img | ||
alt="test" | ||
className="" | ||
src="https://foo.jpg" | ||
/> | ||
</Image> | ||
`; | ||
|
||
exports[`Updating an image src 1`] = ` | ||
<Image | ||
alt="test" | ||
disableLazy={false} | ||
src="http://vignette.wikia-dev.us/22b12324-ab36-4266-87c9-452776157205" | ||
> | ||
<ImagePreloader | ||
src="http://vignette.wikia-dev.us/22b12324-ab36-4266-87c9-452776157205" | ||
srcSet={null} | ||
> | ||
<img | ||
alt="test" | ||
className="" | ||
src="http://vignette.wikia-dev.us/22b12324-ab36-4266-87c9-452776157205/smart/width/5/height/5" | ||
/> | ||
</ImagePreloader> | ||
<noscript> | ||
<img | ||
alt="test" | ||
className="" | ||
src="http://vignette.wikia-dev.us/22b12324-ab36-4266-87c9-452776157205" | ||
/> | ||
</noscript> | ||
</Image> | ||
`; |
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 |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import classNames from 'classnames'; | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
|
||
import ImagePreloader from '../ImagePreloader'; | ||
import { isVignetteUrl, vignette } from '../../utils/vignette'; | ||
|
||
const LAZY_IMAGE_SIZE = 5; | ||
|
||
class Image extends React.Component { | ||
static propTypes = { | ||
alt: PropTypes.string.isRequired, | ||
className: PropTypes.string, | ||
disableLazy: PropTypes.bool, | ||
src: PropTypes.string.isRequired, | ||
srcSet: PropTypes.string, | ||
}; | ||
|
||
static defaultProps = { | ||
className: undefined, | ||
disableLazy: false, | ||
srcSet: undefined, | ||
}; | ||
|
||
state = { | ||
src: this.props.src, | ||
isLimbo: false, | ||
}; | ||
|
||
// When the src changes first replace the src with a temp image so it doesn't stall displaying | ||
// the old image | ||
// https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html#when-to-use-derived-state | ||
static getDerivedStateFromProps(props, state) { | ||
// when only the src changes we are in "limbo" mode | ||
return { | ||
isLimbo: props.src !== state.src, | ||
}; | ||
} | ||
|
||
// after the component updates once we want to | ||
componentDidUpdate() { | ||
if (this.props.src !== this.state.src) { | ||
// this is one of the rare cases to conditionally call setState after a component update | ||
// this allows the images to be removed from the DOM properly | ||
// eslint-disable-next-line react/no-did-update-set-state | ||
this.setState(() => ({ src: this.props.src })); | ||
} | ||
} | ||
|
||
/** | ||
* Create a super low resolution image that will automatically be blurred in most browsers | ||
*/ | ||
getLowResSrcFromVignette() { | ||
return vignette(this.props.src).withSmart(LAZY_IMAGE_SIZE, LAZY_IMAGE_SIZE).get(); | ||
} | ||
|
||
renderPlainImage() { | ||
const { src, alt, className, disableLazy, ...rest } = this.props; | ||
|
||
return <img className={classNames(className)} src={src} alt={alt} {...rest} />; | ||
} | ||
|
||
renderVignetteImage() { | ||
const { src: _skip1, srcSet: _skip2, alt, className, disableLazy, ...rest } = this.props; | ||
|
||
if (disableLazy) { | ||
return this.renderPlainImage(); | ||
} | ||
|
||
return ( | ||
<ImagePreloader src={this.state.src} srcSet={this.props.srcSet}> | ||
{({ src, srcSet, state }) => { | ||
// we will not test the functionality of ImagePreloader here | ||
/* istanbul ignore next */ | ||
if (state !== ImagePreloader.STATE.PENDING) { | ||
return <img className={classNames(className)} src={src} srcSet={srcSet} alt={alt} {...rest} />; | ||
} | ||
|
||
// if the image is loading, render low quality image | ||
return <img className={classNames(className)} src={this.getLowResSrcFromVignette()} alt={alt} {...rest} />; | ||
}} | ||
</ImagePreloader> | ||
); | ||
} | ||
|
||
render() { | ||
const { src, isLimbo } = this.state; | ||
|
||
if (isVignetteUrl(src)) { | ||
// Limbo state happens when only the src and/or srcset is changed | ||
// there is no standard on how to handle the state of the image when the src is changed across browsers | ||
// lets just remove the entire node from html when in limbo | ||
return ( | ||
<React.Fragment> | ||
{!isLimbo && this.renderVignetteImage()} | ||
{/* // support no-js and SSR */} | ||
<noscript> | ||
{this.renderPlainImage()} | ||
</noscript> | ||
</React.Fragment> | ||
); | ||
} | ||
|
||
// if the image is not a Vignette one, just render it and don't care | ||
return this.renderPlainImage(); | ||
} | ||
} | ||
|
||
export default Image; |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { mount } from 'enzyme'; | ||
import React from 'react'; | ||
|
||
import Image from './index'; | ||
|
||
|
||
// jest.mock('../ImagePreloader', () => mockComponent('ImagePreloader')); | ||
|
||
function mountImage(additionalProps = {}) { | ||
const wrapper = mount( | ||
<Image | ||
alt="test" | ||
src="http://vignette.wikia-dev.us/22b12324-ab36-4266-87c9-452776157205" | ||
{...additionalProps} | ||
/>, | ||
); | ||
return wrapper; | ||
} | ||
|
||
test('Image test', () => { | ||
const component = mountImage(); | ||
expect(component).toMatchSnapshot(); | ||
}); | ||
|
||
test('Image test for non-vignette', () => { | ||
const component = mountImage({ src: 'https://foo.jpg' }); | ||
expect(component).toMatchSnapshot(); | ||
}); | ||
|
||
test('Updating an image src', () => { | ||
const component = mountImage(); | ||
|
||
component.setState({ src: 'test' }); | ||
component.update(); | ||
|
||
expect(component).toMatchSnapshot(); | ||
}); | ||
|
||
test('Disabled lazyLoading', () => { | ||
const component = mountImage({ disableLazy: true }); | ||
|
||
expect(component).toMatchSnapshot(); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
`ImagePreloader` component can be used to load (or preload) any image. Both `onChange` and children are functions that are called with `ImagePreloader`'s state with the following: | ||
|
||
* `state` - the current state of the preloading - can be either `pending`, `success` or `error` | ||
All the states are xported via `ImagePreloader.STATE` const. | ||
* `error` - a JavaScript `Error` object (if the `state` is `error`) or null | ||
* `src` - taken from `ImagePreloader`'s props | ||
* `srcSet` - taken from `ImagePreloader`'s props | ||
|
||
### Usage: | ||
|
||
```js static | ||
import ImagePreloader from '@wikia/react-common/components/ImagePreloader'; | ||
|
||
export default ImageWithLoadingMessage = () => ( | ||
<ImagePreloader src="http://path.to.image.jpg"> | ||
{({ state, src }) => { | ||
if (state === ImagePreloader.STATE.PENDING) { | ||
return <span>Loading image...</span>; | ||
} | ||
|
||
if (state === ImagePreloader.STATE.ERROR) { | ||
return <span>Error loading image</span>; | ||
} | ||
|
||
return <img src={src} />; | ||
}} | ||
</ImagePreloader> | ||
); | ||
|
||
``` |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.