Skip to content

Commit d0ff262

Browse files
authored
Merge pull request #60 from lvlsgroup/add-components-fluid-image
prevent page jumps with fluid image component
2 parents e33a5bd + 2c7786a commit d0ff262

File tree

5 files changed

+80
-2
lines changed

5 files changed

+80
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"name": "react-component-lib",
66
"author": "lvlsgroup",
77
"description": "A component library for react",
8-
"version": "0.9.34",
8+
"version": "0.9.35",
99
"main": "src/server/server.js",
1010
"license": "MIT",
1111
"scripts": {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import classNames from 'classnames';
4+
import styles from './fluidImage.scss';
5+
6+
function FluidImage({
7+
src,
8+
classNameContainer,
9+
classNameImage,
10+
aspectRatio,
11+
srcSet,
12+
sizes,
13+
alt,
14+
}) {
15+
const paddingBottomPercentage = `${(1 / aspectRatio) * 100}%`;
16+
17+
return (
18+
<div
19+
className={classNames(styles.fluidImageContainer, classNameContainer)}
20+
style={{ paddingBottom: paddingBottomPercentage }}
21+
>
22+
<img
23+
src={src}
24+
className={classNames(styles.fluidImage, classNameImage)}
25+
alt={alt}
26+
srcSet={srcSet}
27+
sizes={sizes}
28+
/>
29+
</div>
30+
);
31+
}
32+
33+
FluidImage.propTypes = {
34+
src: PropTypes.string,
35+
classNameContainer: PropTypes.string,
36+
classNameImage: PropTypes.string,
37+
alt: PropTypes.string,
38+
aspectRatio: PropTypes.number, // Use this instead of height and width
39+
srcSet: PropTypes.string,
40+
sizes: PropTypes.string,
41+
};
42+
43+
export default FluidImage;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.fluidImageContainer {
2+
position: relative;
3+
4+
.fluidImage {
5+
width: 100%;
6+
position: absolute;
7+
}
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
import FluidImage from '@rc-lib-client/components/images/fluidImage/FluidImage';
3+
4+
function FluidImageDemo() {
5+
return (
6+
<FluidImage
7+
src="https://storage.googleapis.com/acamp-prod/images/landing-pages/camper_2.1.svg"
8+
aspectRatio={344 / 154}
9+
/>
10+
);
11+
}
12+
13+
export { FluidImageDemo };

src/client/components/images/image/Image.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,26 @@ import PropTypes from 'prop-types';
44

55
class Image extends PureComponent {
66
render() {
7-
const { className, src, alt, height, width, ...rest } = this.props;
7+
const {
8+
className,
9+
src,
10+
alt,
11+
height,
12+
width,
13+
srcSet,
14+
sizes,
15+
...rest
16+
} = this.props;
17+
818
return (
919
<img
1020
className={classNames(className)}
1121
src={src}
1222
height={height}
1323
width={width}
1424
alt={alt}
25+
srcSet={srcSet}
26+
sizes={sizes}
1527
{...rest}
1628
/>
1729
);
@@ -24,6 +36,8 @@ Image.propTypes = {
2436
alt: PropTypes.string,
2537
height: PropTypes.string,
2638
width: PropTypes.string,
39+
srcSet: PropTypes.string,
40+
sizes: PropTypes.string,
2741
};
2842

2943
export default Image;

0 commit comments

Comments
 (0)