forked from mozilla/activity-stream
/
DSImage.jsx
103 lines (84 loc) · 2.94 KB
/
DSImage.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import {cache} from "./cache";
import React from "react";
import ReactDOM from "react-dom";
export class DSImage extends React.PureComponent {
constructor(props) {
super(props);
this.onOptimizedImageError = this.onOptimizedImageError.bind(this);
this.state = {
isSeen: false,
optimizedImageFailed: false,
};
}
onSeen(entries) {
if (this.state) {
if (entries.some(entry => entry.isIntersecting)) {
if (this.props.optimize) {
this.setState({
containerWidth: ReactDOM.findDOMNode(this).clientWidth,
});
}
this.setState({
isSeen: true,
});
// Stop observing since element has been seen
this.observer.unobserve(ReactDOM.findDOMNode(this));
}
}
}
reformatImageURL(url, width) {
// Change the image URL to request a size tailored for the parent container width
// Also: force JPEG, quality 60, no upscaling, no EXIF data
// Uses Thumbor: https://thumbor.readthedocs.io/en/latest/usage.html
return `https://img-getpocket.cdn.mozilla.net/${width}x0/filters:format(jpeg):quality(60):no_upscale():strip_exif()/${encodeURIComponent(url)}`;
}
componentDidMount() {
this.observer = new IntersectionObserver(this.onSeen.bind(this));
this.observer.observe(ReactDOM.findDOMNode(this));
}
componentWillUnmount() {
// Remove observer on unmount
if (this.observer) {
this.observer.unobserve(ReactDOM.findDOMNode(this));
}
}
render() {
const classNames = `ds-image${this.props.extraClassNames ? ` ${this.props.extraClassNames}` : ``}`;
let img;
if (this.state && this.state.isSeen) {
if (this.props.optimize && this.props.rawSource && !this.state.optimizedImageFailed) {
let source;
let source2x;
if (this.state && this.state.containerWidth) {
let baseSource = this.props.rawSource;
source = this.reformatImageURL(
baseSource,
cache.query(baseSource, this.state.containerWidth, `1x`)
);
source2x = this.reformatImageURL(
baseSource,
cache.query(baseSource, this.state.containerWidth * 2, `2x`)
);
img = (<img onError={this.onOptimizedImageError} src={source} srcSet={`${source2x} 2x`} />);
}
} else {
img = (<img src={this.props.source} />);
}
}
return (
<picture className={classNames}>{img}</picture>
);
}
onOptimizedImageError() {
// This will trigger a re-render and the unoptimized 450px image will be used as a fallback
this.setState({
optimizedImageFailed: true,
});
}
}
DSImage.defaultProps = {
source: null, // The current source style from Pocket API (always 450px)
rawSource: null, // Unadulterated image URL to filter through Thumbor
extraClassNames: null, // Additional classnames to append to component
optimize: true, // Measure parent container to request exact sizes
};