How Can I Format Text in the Block Content Editor to Reflect the Custom Style?

9 replies
Last updated: May 20, 2020
I'm adding a custom style to a block content/richtext editor field and was trying to find out how I can format the text in the editor to reflect the different style visually in order for editors to "get it". There's no mention of that in the docs over at https://www.sanity.io/docs/configuration so I was wondering - is it actually possible?
AI Update

Yes, it's absolutely possible to customize how text appears in the Portable Text Editor to visually reflect your custom styles! The key is using the component property when defining your custom decorators or block styles in your schema.

When you create a custom decorator or block style, you can specify both an icon and a component to control how the styled content appears in the editor itself. This gives your editors a visual preview of what they're creating, making the editing experience much more intuitive.

Custom Decorators

For custom decorators (like highlight, superscript, subscript), you pass a React component to the component property. Here's a practical example from the official documentation on customizing the Portable Text Editor:

import React from 'react'

const HighlightIcon = () => (
  <span style={{ fontWeight: 'bold' }}>H</span>
)

const HighlightDecorator = props => (
  <span style={{ backgroundColor: 'yellow' }}>{props.children}</span>
)

export default {
  name: 'content',
  title: 'Content',
  type: 'array',
  of: [
    {
      type: 'block',
      marks: {
        decorators: [
          { title: 'Strong', value: 'strong' },
          { title: 'Emphasis', value: 'em' },
          { title: 'Code', value: 'code' },
          {
            title: 'Highlight',
            value: 'highlight',
            icon: HighlightIcon,
            component: HighlightDecorator  // This renders the visual style
          }
        ]
      }
    }
  ]
}

The component receives the text content as props.children and you wrap it in whatever JSX/styling you need to represent that style visually.

Custom Block Styles

The same approach works for custom block styles. Here's an example of creating a custom "title" style with different typography:

import React from 'react'

const TitleStyle = props => (
  <span style={{fontFamily: 'Garamond', fontSize: '2em'}}>
    {props.children}
  </span>
)

export default {
  name: 'content',
  title: 'Content',
  type: 'array',
  of: [
    {
      type: 'block',
      styles: [
        {title: 'Normal', value: 'normal'},
        {title: 'H1', value: 'h1'},
        {
          title: 'Title',
          value: 'title',
          component: TitleStyle  // Applies custom style in the editor
        },
      ]
    }
  ]
}

Custom Annotations

For annotations (like links), you can also customize their appearance in the editor using the components.annotation property:

import { LaunchIcon } from '@sanity/icons'

const ExternalLinkRenderer = props => (
  <span>
    {props.renderDefault(props)}
    <a contentEditable={false} href={props.value.href}>
      <LaunchIcon />
    </a>
  </span>
)

export default {
  name: 'content',
  type: 'array',
  of: [
    {
      type: 'block',
      marks: {
        annotations: [
          {
            name: 'link',
            type: 'object',
            title: 'link',
            fields: [{ name: 'url', type: 'url' }],
            components: {
              annotation: ExternalLinkRenderer
            }
          }
        ]
      }
    }
  ]
}

This visual feedback makes it much easier for content editors to understand what they're creating without having to preview on the frontend. For more comprehensive examples and patterns, check out the Ultimate Guide on customizing Portable Text and the common Portable Text Editor patterns documentation.

For reference, I added an object to the styles array:
styles: [
...
        { title: 'Lead', value: 'lead' },
...
]
..and I'd like to make that font just a tad larger. If there's no visual change, it makes it harder for editors to understand what's going on and kinda kills the WYSIWYG experience.
anyone?
Same question here. Did you figure out a way to do this?
I want to have a “heading” style, and perhaps make it bold or slightly larger in the rich text editor in Sanity studio (without using h1-6 tag specifically)
{ name: ‘body’,
title: ‘Body’,
type: ‘array’,
of: [
{
type: ‘block’,
styles: [
{ title: ‘Normal’, value: ‘normal’ },
{ title: ‘Heading’, value: ‘heading’ }
],
marks: {
decorators: [
{ title: ‘Strong’, value: ‘strong’ },
{ title: ‘Emphasis’, value: ‘em’ }
]
}
}
]

}
Maybe you know,
user J
?
I haven't tried this myself, but is this helpful? https://www.sanity.io/docs/customization#block-styles-14d3f8b767ae
Oh, fantastic, thanks
user J
!
Ah, thanks for resurrecting this thread, hehe

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?