Custom icons for dynamic sections with rich text editor in Sanity

16 replies
Last updated: Aug 23, 2021
I've created a couple of dynamic sections with the rich text editor in sanity. Is it possible to have a custom icon for each section?
AI Update

Yes, it is possible to have a custom icon for each section in your rich text editor! Based on the documentation, you can add custom icons to block types in your Sanity schema.

How to Add Custom Icons

When defining custom block types in your Portable Text array, you can specify an icon property. Here's how:

{
  title: 'Rich text',
  type: 'array',
  of: [
    {type: 'block'},
    {
      type: 'myCustomSection',
      icon: MyCustomIcon  // Your custom icon component
    },
    {
      type: 'anotherSection',
      icon: AnotherIcon
    }
  ]
}

Using Icons from @sanity/icons

Sanity provides a comprehensive icon library through @sanity/icons. You can import and use these icons:

import {DocumentIcon, ImageIcon} from '@sanity/icons'

{
  title: 'Content sections',
  type: 'array',
  of: [
    {type: 'block'},
    {
      type: 'textSection',
      icon: DocumentIcon
    },
    {
      type: 'imageSection', 
      icon: ImageIcon
    }
  ]
}

Custom Icon Components

You can also create your own icon components. The icon property accepts a React component:

{
  type: 'specialSection',
  icon: () => <span style={{fontSize: '1.5em'}}>โญ</span>
}

The icons will appear in the Insert menu and toolbar of the Portable Text Editor, making it easy for content editors to identify and insert different section types. Note that these icon customizations only affect how content appears in the Studio interface - they won't affect how the content is rendered on your frontend (that's handled separately through portable text serialization).

Show original thread
16 replies
cc:
user A
Are you looking for this kind of thing? Thereโ€™s pretty good info in this bit of documentation: https://www.sanity.io/docs/previews-list-views#d82fd035385f
Kindof but I'm looking for similar Image for Rich Text editor previews and not in the list pane.
user R
Might be worth giving it a go. Looking at this tutorial on adding custom elements to the Block editor, the preview bit looks exactly the same: https://www.sanity.io/guides/portable-text-how-to-add-a-custom-youtube-embed-block#8d08a02d3d04 ๐Ÿคท
Nope! It won't show a preview for items in a custom editor modal.Thanks again
user R
Nope! It won't show a preview for items in a custom editor modal.Thanks again
user R
Can you post that part of your schema?
user A
Every data ie title and its contents are read dynamically and I've already shown a preview in editor.
I want to show a custom icon in Editor Modal!
user A
If you put a
preview
on your object, you should be able to set an icon (if by that you mean to replace that folded piece of paper icon) using
media
in the
prepare
function, which allows you to return JSX:

preview: {
  select: {
    title: 'title'
  },
  prepare({ title }) {
    return {
      title,
      media: <>'๐Ÿ™Œ'</>
    };
  },
},
If you put a
preview
on your object, you should be able to set an icon (if by that you mean to replace that folded piece of paper icon) using
media
in the
prepare
function, which allows you to return JSX:

preview: {
  select: {
    title: 'title'
  },
  prepare({ title }) {
    return {
      title,
      media: <>'๐Ÿ™Œ'</>
    };
  },
},
Hi
user A
I tried adding preview but it isn't working for me!
Am I missing out something?
Could you try moving the previews to be siblings of the
fields
arrays on the objects?

{
  type: 'object',
  name: 'question',
  title: 'Question',
  fields: [
    // ...
  ],
  preview: {
    // ...
  },
}
Woo hoo! That worked.Thanks for your awesome support
user A
๐Ÿ™‚
Glad to hear it! ๐Ÿ‘

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?