How to add a custom block content component type to Sanity Studio?

6 replies
Last updated: Jun 23, 2023
Hello team, had a quick question, how do I add a completely custom block content component type to my sanity studio? Is there a guide for this?
AI Update

Absolutely! Adding custom block content components to Portable Text in Sanity Studio is pretty straightforward. Here's how to do it:

Define Your Custom Block Type

First, create your custom block as an object type in your schema. Here's an example of a YouTube embed block:

import {defineType, defineField} from 'sanity'

export const youtube = defineType({
  name: 'youtube',
  type: 'object',
  title: 'YouTube Embed',
  fields: [
    defineField({
      name: 'url',
      type: 'url',
      title: 'YouTube video URL',
    }),
  ]
})

Add It to Your Portable Text Field

Next, include your custom block in the of array of your Portable Text field in your document schema:

defineField({
  name: 'content',
  type: 'array',
  of: [
    {type: 'block'}, // Standard text blocks
    {type: 'youtube'}, // Your custom block
    {type: 'image'}, // You can add multiple custom types
  ]
})

Render It on the Frontend

When displaying the content, you'll need to provide a component for your custom block using @portabletext/react:

import {PortableText} from '@portabletext/react'

const ptComponents = {
  types: {
    youtube: ({value}) => (
      <iframe src={value.url} />
    ),
  }
}

// In your component
<PortableText components={ptComponents} value={content} />

Complete Guide

There's a great step-by-step guide for adding a YouTube embed block that walks through the entire process. The Portable Text editor configuration docs also cover this in detail.

Pro tip: If you'll be using the same custom blocks across multiple places in your app, create a centralized component with all your custom block definitions to avoid duplicating code.

Common custom block use cases include media embeds, code snippets, interactive charts, hero sections, product cards, and basically any specialized content you want to mix with your rich text!

Show original thread
6 replies
The ones I have in the screenshot are plugins I installed, but we have our own types of objects that we have serializers for already.
Need to be able to paste a JSON file into the editor that will be passed to our serializer component
You can probably follow https://www.sanity.io/docs/portable-text-editor-configuration . My guess is you could paste the json into the code and it should work for you
Ah thanks
user P
! I'll give it a try
Then you’ll just need to grab that type on the front end and use your component to render it
It worked out! Thanks again

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?