-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creating and applying
ImagePlaceholder
component.
- Loading branch information
1 parent
becf4fd
commit 9fde689
Showing
5 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import './styles.css'; | ||
import React, { useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
const ImagePlaceholder = ({ alt, src, ...rest }) => { | ||
const [imageLoaded, setImageLoaded] = useState(false); | ||
const [hasError, setHasError] = useState(false); | ||
|
||
const handleLoad = () => { | ||
setImageLoaded(true); | ||
setHasError(false); | ||
}; | ||
|
||
const handleError = () => { | ||
setImageLoaded(false); | ||
setHasError(true); | ||
}; | ||
|
||
return ( | ||
<> | ||
<img | ||
src={src} | ||
alt={alt} | ||
onLoad={handleLoad} | ||
onError={handleError} | ||
style={{ display: imageLoaded && !hasError ? 'block' : 'none' }} | ||
/> | ||
|
||
{(hasError || !imageLoaded) && ( | ||
<div | ||
className='image-placeholder' | ||
{...rest} | ||
> | ||
<div className='content' /> | ||
</div> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
ImagePlaceholder.propTypes = { | ||
alt: PropTypes.string, | ||
src: PropTypes.string | ||
}; | ||
|
||
export default ImagePlaceholder; |
16 changes: 16 additions & 0 deletions
16
src/modules/reusable/components/ImagePlaceholder/styles.css
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.image-placeholder { | ||
background-color: var(--search-color-grey-200); | ||
border-radius: 4px; | ||
padding-top: 150%; | ||
position: relative; | ||
width: 100%; | ||
} | ||
|
||
.image-placeholder .content { | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 100%; | ||
/* Any other styling you need */ | ||
} |
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