improve image component

Co-authored-by: Pukimaa <me@pukima.site>
This commit is contained in:
2024-10-30 12:43:32 +01:00
parent a419d4e8ce
commit 9a651b55f1

View File

@@ -5,7 +5,6 @@ import { client, sanityFetch } from "../../../sanity/client";
import Link from "next/link"; import Link from "next/link";
import Image from "next/image"; import Image from "next/image";
import { Post, SanityImageAsset } from "@/sanity/sanity.types"; import { Post, SanityImageAsset } from "@/sanity/sanity.types";
import urlBuilder from "@sanity/image-url";
import {getImageDimensions} from '@sanity/asset-utils' import {getImageDimensions} from '@sanity/asset-utils'
const POST_QUERY = defineQuery(`*[_type == "post" && slug.current == $slug][0]`); const POST_QUERY = defineQuery(`*[_type == "post" && slug.current == $slug][0]`);
@@ -29,7 +28,7 @@ export async function generateStaticParams() {
export default async function PostPage(props: { params: PageParams }) { export default async function PostPage(props: { params: PageParams }) {
const { slug } = await props.params; const { slug } = await props.params;
const post: Post = (await sanityFetch({ query: POST_QUERY, params: { slug }, stega: true })).data; const post: Post = (await sanityFetch({ query: POST_QUERY, params: { slug } })).data;
if (!post) { if (!post) {
return ( return (
@@ -41,29 +40,41 @@ export default async function PostPage(props: { params: PageParams }) {
const postImageUrl = post.mainImage ? urlFor(post.mainImage)?.width(550).height(310).url() : null; const postImageUrl = post.mainImage ? urlFor(post.mainImage)?.width(550).height(310).url() : null;
function PortableImage({ value, isInline }: { value: SanityImageAsset; isInline: boolean }) { function dynamicHeight(originalHeight: number, originalWidth: number, isInline: boolean) {
const targetWidth = isInline ? 100 : 768;
return (targetWidth * originalHeight) / originalWidth;
}
function PortableImage({ value, isInline }: { value: SanityImageAsset, isInline: boolean } ) {
const {width, height} = getImageDimensions(value) const {width, height} = getImageDimensions(value)
return <Image console.log(isInline, width, height);
src={urlBuilder() return (
<Image
src={imageUrlBuilder()
.projectId(projectId ?? "") .projectId(projectId ?? "")
.dataset(dataset ?? "") .dataset(dataset ?? "")
.image(value) .image(value)
.width(isInline ? 100 : 600) .width(isInline ? 100 : 768)
.fit('max') .fit('max')
.auto('format') .auto('format')
.url()} .url()}
width={isInline ? 100 : 600} width={isInline ? (width >= 100 ? 100 : width) : (width >= 768 ? 768 : width)}
height={height} height={dynamicHeight(height, width, isInline)}
alt={value.altText || ' '} alt={value.altText || ' '}
loading="lazy" loading="lazy"
id={value._id} className="border rounded-lg shadow-md"
style={{ style={{
display: isInline ? 'inline-block' : 'block', display: isInline ? 'inline-block' : 'block',
aspectRatio: width / height, aspectRatio: width / height,
borderRadius: ".6rem",
border: "1px solid rgba(255, 255, 255, .15)",
}} }}
/>; />
)
}
const components = {
types: {
image: PortableImage
}
} }
return ( return (
@@ -83,7 +94,7 @@ export default async function PostPage(props: { params: PageParams }) {
<h1 className="text-4xl font-bold mb-8">{post.title}</h1> <h1 className="text-4xl font-bold mb-8">{post.title}</h1>
<div className="prose"> <div className="prose">
<p>Published: {new Date(post.publishedAt ?? "").toISOString().substring(0, 10)}</p> <p>Published: {new Date(post.publishedAt ?? "").toISOString().substring(0, 10)}</p>
{Array.isArray(post.body) && <PortableText value={post.body} components={ { types: { image: PortableImage } } } />} {Array.isArray(post.body) && <PortableText value={post.body} components={ components } />}
</div> </div>
</main> </main>
); );