Block editor style renders as normal `<p>` tag instead of custom component

4 replies
Last updated: Feb 24, 2022
Hello, I’ve added a small item to our styles in our block editor:
  {title: 'Small', value: 'small', blockEditor: { 
    icon: () => <div>Small</div>, 
      render: ({children}) => <span><small>{children}</small></span>
  }}
But the value produced when fetching the data is just a normal
<p>
tag, is there something I’m missing? Thanks!
AI Update

I see what's happening here! The blockEditor configuration you've added only affects how the decorator appears in the Sanity Studio editor - it doesn't affect the actual data structure or how it renders on your frontend.

The decorator configuration in the Studio is working correctly - it's storing the mark in your Portable Text data. However, when you fetch and render that data on your frontend, you need to configure your Portable Text renderer to handle the custom small decorator.

Here's what you need to do on your frontend:

If you're using @portabletext/react:

import { PortableText } from '@portabletext/react'

const components = {
  marks: {
    small: ({children}) => <small>{children}</small>,
  }
}

<PortableText 
  value={yourPortableTextData} 
  components={components}
/>

If you're using the older @sanity/block-content-to-react:

import BlockContent from '@sanity/block-content-to-react'

const serializers = {
  marks: {
    small: ({children}) => <small>{children}</small>,
  }
}

<BlockContent 
  blocks={yourPortableTextData} 
  serializers={serializers}
/>

The key point is that custom decorators need to be defined in two places:

  1. In your Studio schema (which you've already done) - this controls the editing experience
  2. In your frontend renderer (which you need to add) - this controls how the data is displayed

Your Portable Text data structure includes the decorator marks, but without telling your renderer what to do with the small value, it just renders the default <p> tag. Once you add the custom component/serializer on the frontend, your <small> tags will appear correctly!

You can read more about this in the decorators documentation and see similar examples in this answer about custom decorators.

Show original thread
4 replies
const smallStyle = (props) => (
  <span>
    {props.children}
  </span>
);

title: {
    title: "Small",
    value: "small",
    blockEditor: {
      render: smallStyle,
    },
  },

Changed it to an decorator instead, worked better, thanks!
Are you placing it inside the
decorators
?
Yes 🙂 Great.

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?