Skip to content

Commit

Permalink
Creating and applying ImagePlaceholder component.
Browse files Browse the repository at this point in the history
  • Loading branch information
erinesullivan committed Oct 31, 2024
1 parent becf4fd commit 9fde689
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/modules/browse/components/ShelfBrowseCarousel/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import './styles.css';
import { Anchor, Icon } from '../../../reusable';
import { Anchor, Icon, ImagePlaceholder } from '../../../reusable';
import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';

const bookCover = (item) => {
let url = 'https://www.syndetics.com/index.php?client=umichaa&pagename=lc.jpg';
['isbn', 'issn', 'oclc'].forEach((parameter) => {
if (item[parameter]) {
url += `&${parameter}=${item[parameter]}`;
}
});
return url;
};

const ShelfBrowseCarousel = ({ callNumber, items, itemsPerPage, setButtonAction, setDisableButton, uid }) => {
const currentItem = (item) => {
return item.call_number === callNumber && item.url.endsWith(uid);
Expand Down Expand Up @@ -125,6 +135,17 @@ const ShelfBrowseCarousel = ({ callNumber, items, itemsPerPage, setButtonAction,
<span className='item-term-title'>Continue browsing in call number list</span>
</>
)}
{!firstOrLastItem && (
<>
<dt className='visually-hidden'>Book cover</dt>
<dd className='item-term-book_cover'>
<ImagePlaceholder
src={bookCover(item)}
alt={`The cover of ${item.title}`}
/>
</dd>
</>
)}
{fields.map((key) => {
return item[key] && (
<React.Fragment key={key}>
Expand Down
3 changes: 3 additions & 0 deletions src/modules/browse/components/ShelfBrowseCarousel/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@
overflow: hidden;
overflow-wrap: anywhere;
}
.shelf-browse-item .item-term-book_cover:not(:has(div)) {
align-self: center;
}

.shelf-browse-item .this-item,
.shelf-browse-item .item-term-title,
Expand Down
46 changes: 46 additions & 0 deletions src/modules/reusable/components/ImagePlaceholder/index.js
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 src/modules/reusable/components/ImagePlaceholder/styles.css
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 */
}
2 changes: 2 additions & 0 deletions src/modules/reusable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ContextProvider from './components/ContextProvider';
import Dialog from './components/Dialog';
import H1 from './components/H1';
import Icon from './components/Icon';
import ImagePlaceholder from './components/ImagePlaceholder';
import Metadata from './components/Metadata';
import Tab from './components/Tab';
import TabPanel from './components/TabPanel';
Expand All @@ -27,6 +28,7 @@ export {
ExpandableProvider,
H1,
Icon,
ImagePlaceholder,
Metadata,
Tab,
Tabs,
Expand Down

0 comments on commit 9fde689

Please sign in to comment.