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: adds option to enforce no cover for sanity image #1001

Merged
merged 1 commit into from
Dec 12, 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
48 changes: 37 additions & 11 deletions src/components/image/SanityImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ const useNextSanityGlobalImage = (
const SanityAssetImage = ({
image,
imageProps,
objectFit = "cover",
}: {
image: IImage;
imageProps?: UseNextSanityImageProps;
objectFit?: "cover" | "none";
}) => {
const objectPosition = image.hotspot
? `${image.hotspot.x * 100}% ${image.hotspot.y * 100}%`
Expand All @@ -56,12 +58,16 @@ const SanityAssetImage = ({
width={imageProps.width}
height={imageProps.height}
blurDataURL={image.metadata?.lqip}
style={{
objectFit: "cover",
objectPosition,
maxWidth: "100%",
maxHeight: "100%",
}}
style={
objectFit === "none"
? {}
: {
objectFit: "cover",
objectPosition,
maxWidth: "100%",
maxHeight: "100%",
}
}
/>
);
};
Expand All @@ -76,24 +82,44 @@ export function SanitySharedImage({ image }: { image: IImage }) {
return <SanityAssetImage image={image} imageProps={imageProps} />;
}

function SanityGlobalImage({ image }: { image: IImage }) {
function SanityGlobalImage({
image,
objectFit = "cover",
}: {
image: IImage;
objectFit?: "cover" | "none";
}) {
const imageProps = useNextSanityGlobalImage(image);
return (
<SanityAssetImage image={image} imageProps={imageProps ?? undefined} />
<SanityAssetImage
objectFit={objectFit}
image={image}
imageProps={imageProps ?? undefined}
/>
);
}

export function SanityImage({ image }: { image: IImage }) {
export function SanityImage({
image,
objectFit = "cover",
}: {
image: IImage;
objectFit?: "cover" | "none";
}) {
if (image?.src) {
return (
<Image
alt={image?.alt || ""}
src={image.src.src}
style={{ objectFit: "cover", height: "100%", width: "100%" }}
style={
objectFit === "none"
? {}
: { objectFit: "cover", height: "100%", width: "100%" }
}
width={300}
height={300}
/>
);
}
return <SanityGlobalImage image={image} />;
return <SanityGlobalImage image={image} objectFit={objectFit} />;
}
2 changes: 1 addition & 1 deletion src/components/sections/image-split/ImageSplit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const ImageSplitComponent = ({ section }: ImageSplitProps) => {
{section.imageExtended && (
<div className={imageClass}>
<div>
<SanityImage image={section.imageExtended} />
<SanityImage image={section.imageExtended} objectFit="none" />
</div>
</div>
)}
Expand Down
4 changes: 3 additions & 1 deletion src/components/sections/image-split/image-split.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
display: flex;
flex-direction: column;
gap: 0.5rem;
justify-content: center;
}

.textContainer__link {
Expand All @@ -77,5 +78,6 @@
.image img {
display: block;
margin: 0 auto;
width: 100%;
max-width: 100%;
height: auto;
}
Loading