Skip to content

Instantly share code, notes, and snippets.

@nk-gears
Forked from tstreamDOTh/ProgressiveImage.js
Created June 19, 2020 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nk-gears/9f6429130c6b8fffc672e6530376b550 to your computer and use it in GitHub Desktop.
Save nk-gears/9f6429130c6b8fffc672e6530376b550 to your computer and use it in GitHub Desktop.
import React, { Component } from "react";
const omit = (obj, omitKey) =>
Object.keys(obj).reduce((result, key) => {
if (key !== omitKey) {
result[key] = obj[key];
}
return result;
}, {});
const overlayStyles = {
position: "absolute",
filter: "blur(1px)",
transition: "opacity ease-in 1000ms",
clipPath: "inset(0)"
};
export default class ProgressiveImage extends Component {
constructor(props) {
super(props);
this.state = { highResImageLoaded: false };
}
render() {
const { overlaySrc } = this.props;
const { highResImageLoaded } = this.state;
let filteredProps = omit(this.props, "overlaySrc");
return (
<span>
<img
{...filteredProps}
onLoad={() => {
this.setState({ highResImageLoaded: true });
}}
ref={img => {
this.highResImage = img;
}}
src={this.props.src}
/>
<img
{...filteredProps}
className={`${this.props.className} ${overlayStyles}`}
{...highResImageLoaded && { style: { opacity: "0" } }}
src={overlaySrc}
/>
</span>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment