-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #214 from NYPL/DR-3056/feature/next-image
DR-3056: Next Image in Card component
- Loading branch information
Showing
14 changed files
with
139 additions
and
46 deletions.
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
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,49 @@ | ||
import Image from "next/image"; | ||
import React from "react"; | ||
import { useState } from "react"; | ||
import { DCCardProps } from "./card"; | ||
|
||
interface CardImageProps extends Pick<DCCardProps, "record"> { | ||
imageHeight: number; | ||
} | ||
|
||
export const CardImage = ({ record, imageHeight }: CardImageProps) => { | ||
const [imageSrc, setImageSrc] = useState( | ||
record.imageID ? record.imageURL : "/noImage.png" | ||
); | ||
const initialImageHeight = 144; | ||
return ( | ||
<div | ||
style={{ | ||
overflow: "hidden", | ||
height: imageHeight, | ||
}} | ||
> | ||
<Image | ||
src={imageSrc} | ||
alt="" | ||
id={ | ||
record.imageID | ||
? `image-${record.imageID}` | ||
: `no-image-${record.imageID}` | ||
} | ||
sizes="(max-width: 480px) 100vw, (max-width: 1024px) 50vw, 25vw" | ||
style={{ | ||
width: "100%", | ||
minHeight: "100%", | ||
height: "auto", | ||
}} | ||
width={initialImageHeight * 2} | ||
height={initialImageHeight} | ||
onError={(_event) => { | ||
console.warn( | ||
`CardImage: Card image failed to load, fallback image loaded instead. ImageURL: ${record.imageURL}` | ||
); | ||
setImageSrc("/noImage.png"); | ||
}} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default CardImage; |
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
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,25 @@ | ||
import { useState, useEffect } from "react"; | ||
|
||
/** | ||
* Calculates the height of the image in the DCCard component as it resizes with the window, | ||
* returns it to the CardImage so its parent container can cut off overflow accordingly. | ||
*/ | ||
|
||
export function useCardImageHeight(cardRef) { | ||
const [imageHeight, setImageHeight] = useState(144); | ||
useEffect(() => { | ||
const updateImageHeight = () => { | ||
if (cardRef.current) { | ||
const image = cardRef.current as HTMLElement; | ||
const imageWidth = image.offsetWidth; | ||
|
||
setImageHeight(0.5 * imageWidth); | ||
} | ||
}; | ||
updateImageHeight(); | ||
window.addEventListener("resize", updateImageHeight); | ||
|
||
return () => window.removeEventListener("resize", updateImageHeight); | ||
}, []); | ||
return imageHeight; | ||
} |
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { StaticImageData } from "next/image"; | ||
export interface ExploreFurtherDataType { | ||
title: string; | ||
url: string; | ||
description: string; | ||
imgAlt: string; | ||
image: string; | ||
image: StaticImageData; | ||
} |
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
Oops, something went wrong.