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

fix: improve loading state handling and image rendering in WorkCard image #45

Merged
merged 1 commit into from
Nov 14, 2024
Merged
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
14 changes: 8 additions & 6 deletions components/shared/workCard/WorkCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type WorkCardProps = {
}

const WorkCard = ({ work }: WorkCardProps) => {
const { data: dataCovers } = useGetCoverCollection({
const { data: dataCovers, isLoading: isLoadingCovers } = useGetCoverCollection({
type: "pid",
identifiers: [getAllWorkPids(work).join(", ")],
sizes: [
Expand Down Expand Up @@ -67,11 +67,13 @@ const WorkCard = ({ work }: WorkCardProps) => {
</Badge>
)}
<div className="relative mx-auto flex h-full w-full items-center">
<WorkCardImage
lowResSrc={lowResCover || ""}
src={coverSrc?.[0] || ""}
alt="work image"
/>
{!isLoadingCovers && (
<WorkCardImage
lowResSrc={lowResCover || ""}
src={coverSrc?.[0] || ""}
alt={`${work.titles.full[0]} cover billede`}
/>
)}
</div>
<div className="my-auto flex min-h-[15%] items-center py-3 md:py-4">
<WorkCardAvailabilityRow materialTypes={work.materialTypes} />
Expand Down
86 changes: 44 additions & 42 deletions components/shared/workCard/WorkCardImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const WorkCardImage: FC<Props> = ({ src, lowResSrc, alt }) => {

return (
<div className="flex h-full w-full items-center" ref={ref}>
{!imageError && (
{!imageError && src ? (
<Tilt
scale={1.05}
transitionSpeed={2500}
Expand All @@ -45,48 +45,50 @@ export const WorkCardImage: FC<Props> = ({ src, lowResSrc, alt }) => {
tiltReverse={true}
className={"relative m-auto"}
style={{ paddingTop, width: `min(100%,${imageWidthByContainerHeight}px)` }}>
<Image
src={lowResSrc}
alt={alt}
height={imageHeight}
width={imageWidth}
sizes="20px"
loading="eager"
className={cn(
`absolute inset-0 h-auto w-full overflow-hidden rounded-sm object-contain transition-all duration-500
will-change-transform`,
imageLoaded ? "shadow-none" : "shadow-cover-picture"
)}
onLoad={({ target }) => {
// get the intrinsic dimensions of the image
const { naturalWidth, naturalHeight } = target as HTMLImageElement
setImageHeight(naturalHeight)
setImageWidth(naturalWidth)
}}
/>
<Image
src={src}
alt={alt}
height={imageHeight}
width={imageWidth}
sizes="100vw"
loading="lazy"
className={cn(
`absolute inset-0 h-auto w-full overflow-hidden rounded-sm object-contain shadow-cover-picture
transition-all duration-500 will-change-transform`,
imageLoaded ? "opacity-100" : "opacity-0"
)}
onLoad={() => {
setImageLoaded(true)
}}
onError={() => {
setImageError(true)
}}
/>
{lowResSrc && (
<Image
src={lowResSrc}
alt={alt}
height={imageHeight}
width={imageWidth}
sizes="20px"
loading="eager"
className={cn(
`absolute inset-0 h-auto w-full overflow-hidden rounded-sm object-contain transition-all duration-500
will-change-transform`,
imageLoaded ? "shadow-none" : "shadow-cover-picture"
)}
onLoad={({ target }) => {
// get the intrinsic dimensions of the image
const { naturalWidth, naturalHeight } = target as HTMLImageElement
setImageHeight(naturalHeight)
setImageWidth(naturalWidth)
}}
/>
)}
{src && (
<Image
src={src}
alt={alt}
height={imageHeight}
width={imageWidth}
sizes="100vw"
loading="lazy"
className={cn(
`absolute inset-0 h-auto w-full overflow-hidden rounded-sm object-contain shadow-cover-picture
transition-all duration-500 will-change-transform`,
imageLoaded ? "opacity-100" : "opacity-0"
)}
onLoad={() => {
setImageLoaded(true)
}}
onError={() => {
setImageError(true)
}}
/>
)}
</Tilt>
)}

{imageError && (
) : (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
Expand Down
Loading