Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BD-46] fix: show skeleton while loading src in CardImageCap #2841

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions src/Card/CardImageCap.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useState } from 'react';
import React, { useContext, useState, useMemo } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import Skeleton from 'react-loading-skeleton';
Expand Down Expand Up @@ -27,6 +27,22 @@ const CardImageCap = React.forwardRef(({
const [showImageCap, setShowImageCap] = useState(false);
const [showLogoCap, setShowLogoCap] = useState(false);

const showSkeleton = useMemo(() => {
let show;
if (src && logoSrc) {
show = !(showImageCap && showLogoCap);
} else if (src) {
show = !showImageCap;
} else if (logoSrc) {
show = !showLogoCap;
}
return show;
}, [src, logoSrc, showImageCap, showLogoCap]);

const imageSkeletonHeight = useMemo(() => (
orientation === 'horizontal' ? '100%' : skeletonHeight
), [orientation, skeletonHeight]);

const wrapperClassName = `pgn__card-wrapper-image-cap ${orientation}`;

if (isLoading) {
Expand All @@ -37,7 +53,7 @@ const CardImageCap = React.forwardRef(({
>
<Skeleton
containerClassName="pgn__card-image-cap-loader"
height={orientation === 'horizontal' ? '100%' : skeletonHeight}
height={imageSkeletonHeight}
width={skeletonWidth}
/>
{logoSkeleton && (
Expand All @@ -57,6 +73,7 @@ const CardImageCap = React.forwardRef(({
if (!altSrc || currentTarget.src.endsWith(altSrc)) {
if (imageKey === 'imageCap') {
currentTarget.src = cardSrcFallbackImg;
setShowImageCap(false);
} else {
setShowLogoCap(false);
}
Expand All @@ -69,9 +86,26 @@ const CardImageCap = React.forwardRef(({

return (
<div className={classNames(className, wrapperClassName)} ref={ref}>
<div
className={classNames('image-loader', className, { show: showSkeleton })}
data-testid="image-loader-wrapper"
>
<Skeleton
containerClassName="pgn__card-image-cap-loader"
height={imageSkeletonHeight}
width={skeletonWidth}
/>
{logoSkeleton && (
<Skeleton
containerClassName="pgn__card-logo-cap"
height={logoSkeletonHeight}
width={logoSkeletonWidth}
/>
)}
</div>
{!!src && (
<img
className={classNames('pgn__card-image-cap', { show: showImageCap })}
className={classNames('pgn__card-image-cap', { show: !showSkeleton })}
src={src}
onError={(event) => handleSrcFallback(event, fallbackSrc, 'imageCap')}
onLoad={() => setShowImageCap(true)}
Expand All @@ -81,7 +115,7 @@ const CardImageCap = React.forwardRef(({
)}
{!!logoSrc && (
<img
className={classNames('pgn__card-logo-cap', { show: showLogoCap })}
className={classNames('pgn__card-logo-cap', { show: !showSkeleton })}
src={logoSrc}
onError={(event) => handleSrcFallback(event, fallbackLogoSrc, 'logoCap')}
onLoad={() => setShowLogoCap(true)}
Expand Down
10 changes: 10 additions & 0 deletions src/Card/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,16 @@ a.pgn__card {
.pgn__card-wrapper-image-cap {
position: relative;

.image-loader {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, rename to pgn__image-loader

display: none;

&.show {
display: block;
height: 100%;
}
}


&.horizontal {
max-width: $card-image-horizontal-max-width;
min-width: $card-image-horizontal-min-width;
Expand Down
Loading