A Pen by Duncan Meech on CodePen.
Created
March 13, 2020 13:28
-
-
Save duncanmeech/aa17b0653a13c20071833fddd12eb87b to your computer and use it in GitHub Desktop.
Responsive Picture Demostration
This file contains 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
<div id="root"><div> |
This file contains 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
class ResponsivePicture extends React.Component { | |
render() { | |
const { baseURL } = this.props; | |
return ( | |
<img src={baseURL} /> | |
) | |
} | |
} | |
class Grid extends React.Component { | |
render() { | |
const { children, containerWidth } = this.props; | |
return ( | |
<div className="grid" style={{ width: `calc(${containerWidth}vw - 2rem`}}>{children}</div> | |
) | |
} | |
} | |
class Tile extends React.Component { | |
render() { | |
const { children } = this.props; | |
return ( | |
<div className="tile">{children}</div> | |
) | |
} | |
} | |
// =================================================================================================== | |
// all images are derived from this | |
// =================================================================================================== | |
const urls = [ | |
"https://a0.muscache.com/4ea/air/v2/pictures/965e271b-3a7c-4105-8a68-9f89d11766e6.jpg", | |
"https://a0.muscache.com/4ea/air/v2/pictures/f51ae7fb-255a-47a8-bb29-6f7adc193171.jpg", | |
"https://a0.muscache.com/4ea/air/v2/pictures/ee491326-db08-4d06-8a7d-541b3ee73bed.jpg" | |
] | |
// ==================================================================================================== | |
// Let React do its thing | |
// ==================================================================================================== | |
ReactDOM.render(( | |
<Grid containerWidth={100}> | |
{urls.map(url => ( | |
<Tile> | |
<ResponsivePicture baseURL={url} containerWidth={50} imageWidth={100 / urls.length} /> | |
</Tile> | |
))} | |
</Grid> | |
), document.getElementById('root')); |
This file contains 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
<script src="https://unpkg.com/react@16/umd/react.development.js"></script> | |
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> |
This file contains 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
body { | |
padding: 1rem; | |
margin: 0; | |
} | |
.grid { | |
display: flex; | |
justify-content: space-between; | |
} | |
.tile { | |
background-color: whitesmoke; | |
margin-right: 1rem; | |
width: 100%; | |
&:last-child { | |
margin-right: 0; | |
} | |
} | |
img { | |
width: 100%; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment