Last active
May 18, 2020 22:13
-
-
Save brysonian/d688cbc6e7da55340d5681cc3c873ee9 to your computer and use it in GitHub Desktop.
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
import React, { useState } from 'react'; | |
import PropTypes from 'prop-types'; | |
import { | |
container, | |
cap, | |
fullHeight, | |
} from './style.module.css'; | |
const Image = ({ | |
maxHeight, | |
isFullHeight, | |
img, | |
fullHeight, | |
caption, | |
alt, | |
}) => { | |
const [showBg, setShowBg] = useState(true); | |
const style = { | |
maxHeight, | |
}; | |
if (maxHeight !== 'auto' && isFullHeight === false) { | |
style.width = 'auto'; | |
}; | |
return ( | |
<div className={`${container} ${isFullHeight ? fullHeight : ''}`} style={{ | |
backgroundSize: 'cover', | |
backgroundImage: showBG ? 'url("' + img.placeholder + '")' : 'none' | |
}}> | |
<img | |
style={style} | |
src={img.src} | |
srcSet={img.srcSet} | |
onLoad={() => setShowBg(false)} | |
alt={alt} | |
/> | |
{ | |
caption && ( | |
<div className={cap}> | |
{caption} | |
</div> | |
) | |
} | |
</div> | |
); | |
} | |
Image.defaultProps = { | |
img: null, | |
alt: null, | |
caption: '', | |
fullHeight: false, | |
isFullHeight: false, | |
maxHeight: 'auto', | |
} | |
Image.propTypes = { | |
img: PropTypes.PropTypes.object.isRequired, | |
alt: PropTypes.string.isRequired, | |
caption: PropTypes.string, | |
fillViewport: PropTypes.bool, | |
maxHeight: PropTypes.string, | |
}; | |
export default Image; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment