
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeAbsolutely! Adding custom block content components to Portable Text in Sanity Studio is pretty straightforward. Here's how to do it:
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',
}),
]
})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
]
})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} />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!
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store