Open History to see the last commit
1import React, { Component } from 'react';
2import IObserver from './intersection-observer';
3import cn from 'classnames';
4β
5// import VideoComponent from '../styleguide/video';
6β
7// This component might look a little complex
8// because one could argue that keeping the aspect ratio
9// of an image can be solved with `height: auto`,
10// but it's actually not that easy if you want to prevent
11// element flickering
12β
13// Because if you want to do that, you need to set the aspect
14// ratio of the image's container BEFORE the image loads
15β
16class Image extends Component {
17 constructor(props) {
18 super(props);
19 }
20β
21 static defaultProps = {
22 lazy: true,
23 };
24β
25 state = {
26 src: !this.props.lazy ? this.props.videoSrc || this.props.src : undefined,
27 };
28β
29 handleIntersect = (entry) => {
30 if (entry.isIntersecting) {
31 this.setState({ src: this.props.src });
32 }
33 };
34β
35 onLoad = () => {
36 this.setState({
37 loaded: true,
38 });
39 };
40β
41 render() {
42 const {
43 caption,
44 height: origHeight,
45 lazy,
46 margin = 40,
47 video = false,
48 videoSrc,
49 width: origWidth,
50 captionSpacing = null,
51 oversize = true,
52 borderRadius = false,
53 alt,
54 imageProps,
55 className,
56 children,
57 ...props
58 } = this.props;
59 const height = parseInt(origHeight);
60 const width = parseInt(origWidth);
61β
62 const aspectRatio = String((height / width) * 100) + '%';
63β
64 // if (video || videoSrc) {
65 // return <VideoComponent src={videoSrc} {...this.props} />;
66 // }
67β
68 let src = this.state.src;
69β
70 // if (src && src.indexOf('assets.zeit.co') > -1) {
71 // if (src.indexOf('/q_auto') === -1) {
72 // src = src.replace('upload/', 'upload/q_auto/');
73 // }
74 // }
75β
76 return (
77 <IObserver
78 once
79 onIntersect={this.handleIntersect}
80 rootMargin="20%"
81 disabled={!lazy}
82 >
83 <figure
84 className={cn(className, {
85 oversize: width > 650 && oversize,
86 fadeIn: this.state.loaded,
87 })}
88 {...props}
89 >
90 <main style={{ width }}>
91 <div className="container" style={{ paddingBottom: aspectRatio }}>
92 {src ? (
93 <img
94 decoding="async"
95 loading="lazy"
96 src={src || null}
97 onLoad={this.onLoad}
98 alt={alt}
99 {...imageProps}
100 />
101 ) : (
102 children
103 )}
104 </div>
105β
106 {caption && (
107 <p style={captionSpacing ? { marginTop: captionSpacing } : {}}>
108 {caption}
109 </p>
110 )}
111 </main>
112β
113 <style jsx>{`
114 figure {
115 display: block;
116 text-align: center;
117 margin: ${margin}px 0;
118 }
119β
120 figure.fadeIn {
121 animation: fade-in 0.2s cubic-bezier(0.455, 0.03, 0.515, 0.955)
122 forwards;
123 }
124β
125 figure.fadeIn img {
126 display: block;
127 }
128β
129 @keyframes fade-in {
130 from {
131 /* Don't begin with 0, it gives a feeling of flickering. */
132 opacity: 0.3;
133 }
134 to {
135 opacity: 1;
136 }
137 }
138β
139 main {
140 margin: 0 auto;
141 max-width: 100%;
142 }
143β
144 div {
145 position: relative;
146 }
147β
148 img {
149 display: none;
150 height: 100%;
151 left: 0;
152 position: absolute;
153 top: 0;
154 width: 100%;
155 ${borderRadius ? `border-radius: var(--geist-radius);` : ''};
156 }
157β
158 .container {
159 display: flex;
160 justify-content: center;
161 }
162β
163 p {
164 color: var(--accents-5);
165 font-size: 14px;
166 margin: 10px 0 0 0;
167 text-align: center;
168 }
169β
170 @media (min-width: 992px) {
171 figure.oversize {
172 width: ${width}px;
173 margin: ${margin}px 0 ${margin}px
174 calc(((${width}px - 768px) / 2) * -1);
175 }
176β
177 p {
178 margin: 0;
179 }
180 }
181 `}</style>
182 </figure>
183 </IObserver>
184 );
185 }
186}
187β
188// export const Video = (props) => <Image {...props} video />;
189β
190export default Image;