How do I render the image in Sanity blockcontent into a custom image component?

4 replies
Last updated: Nov 28, 2020
How is it that i render the image in the blockcontent into a custom image component? like in a blogpost where I want to display images in the body section that are uploaded in the body which has block content type… this link (https://github.com/sanity-io/block-content-to-react ) didn't help me, as specifying imageOptions in BlockContent displays images very plainly, i couldn't control the width based on screen sizes and image zoom feature that I could implement if I could use a custom image component
AI Update

I can help you render images in Portable Text with a custom image component! The key is using the modern @portabletext/react library (which replaces the deprecated block-content-to-react) with custom serializers.

Here's how to set up a custom image component in your Portable Text:

import { PortableText } from '@portabletext/react';
import Image from 'next/image'; // or your preferred image component
import imageUrlBuilder from '@sanity/image-url';

// Set up image URL builder
const builder = imageUrlBuilder(client);

function urlFor(source) {
  return builder.image(source);
}

// Your custom image component with full control
const CustomImageComponent = ({ value }) => {
  if (!value?.asset?._ref) {
    return null;
  }

  return (
    <div className="my-8 relative">
      <Image
        src={urlFor(value).width(800).url()}
        alt={value.alt || 'Blog image'}
        width={800}
        height={600}
        className="w-full h-auto cursor-zoom-in"
        onClick={() => {
          // Your custom zoom logic here
        }}
        sizes="(max-width: 640px) 100vw, (max-width: 1024px) 80vw, 800px"
      />
      {value.caption && (
        <p className="text-sm text-gray-600 mt-2">{value.caption}</p>
      )}
    </div>
  );
};

// Define your custom components
const components = {
  types: {
    image: CustomImageComponent,
    // You can add other custom types here
  },
  marks: {
    // Custom marks like links, highlights, etc.
  },
};

// Use in your blog post
function BlogPost({ post }) {
  return (
    <article>
      <h1>{post.title}</h1>
      <PortableText 
        value={post.body} 
        components={components}
      />
    </article>
  );
}

Important differences from the old library:

  1. The prop is now value (not node) - this gives you access to value.asset, value.hotspot, value.crop, and any custom fields
  2. Components are organized under types, marks, list, and block categories
  3. You have complete control over the rendered output

For responsive sizing, you can:

  • Use CSS classes with media queries
  • Use the sizes attribute with Next.js Image
  • Build different image URLs with Sanity's image builder: urlFor(value).width(640).url() for mobile, etc.

For zoom functionality, you can integrate libraries like:

  • react-medium-image-zoom
  • react-image-lightbox
  • Or build your own modal

The custom serializers guide has more examples of advanced customization. The key is that with @portabletext/react, you get the full value object containing all your image data, giving you complete control over rendering!

Show original thread
4 replies
Specify an image serializer.
const serializer = {
  types: {
    image: props => <img ... />
  }
}
I tried that too. But the problem i faced was I couldn’t get to pass the image data in the image tag. For example the only image src that might be helpful is in props.data.asset but that too only has _ref property, which isn’t a link to the image from sanity’s CDN. Do you have any idea where I could get the asset.fluid / asset.fixed data (say if wanted to render it inside gatasby image tag)
Pass the
asset
, (and
hotspot
, and
crop
properties, if present) to SanityImage , like this:
<SanityImage {...props} width={600} alt="" />
thanks a lot. A quick mention, it was
{...props.node}
that made it work for me.

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?