Adding icons or custom images to text in Sanity.io

16 replies
Last updated: Aug 23, 2023
Hi there! is it posible to add an icon near the Text?I am using and array ob objects, and would like to have an icon near each title.
Thanks!
AI Update

Yes, it's absolutely possible to add an icon near the text in your array of objects! In Sanity Studio, you can add icons to schema fields, including array items.

Adding Icons to Array Items

First, install the icon package you want to use:

npm install @sanity/icons
# or
yarn add @sanity/icons

Then, in your schema definition (make sure the file has a .jsx or .tsx extension), you can add icons to your array items like this:

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

export default {
  name: 'myDocument',
  type: 'document',
  fields: [
    {
      name: 'myArray',
      type: 'array',
      of: [
        {
          type: 'object',
          name: 'item1',
          title: 'Item Type 1',
          icon: DocumentIcon, // Icon appears next to the title
          fields: [
            {
              name: 'title',
              type: 'string',
              title: 'Title'
            }
          ]
        },
        {
          type: 'object',
          name: 'item2',
          title: 'Item Type 2',
          icon: ImageIcon, // Different icon for this type
          fields: [
            {
              name: 'title',
              type: 'string',
              title: 'Title'
            }
          ]
        }
      ]
    }
  ]
}

Icon Options

You have several options for icons:

  1. @sanity/icons - Official Sanity icon package with icons that match Studio's design
  2. react-icons - Large collection including Font Awesome, Material Design, and more
  3. Custom SVG - Any React component that renders an icon

The icons will appear in the Studio UI next to each array item's title, making it easier to identify different types of objects at a glance. As mentioned in the Icons documentation, helpful icons can improve the editorial experience throughout the studio interface.

Important: Remember that any schema file with icons must have a .jsx or .tsx extension, not .js or .ts, since you're importing React components!

Hey (Removed Name),
Sure you can! I usually do it like this:

import {ImPriceTag as icon} from 'react-icons/im'

export default defineType({
  type: 'object',
  name: 'pricing',
  icon,
  fields: [

...
Here's the
docs on that. It goes through using the
@sanity/icons
package, as well as using
react-icons
. If there's an icon defined on the data type, it will automatically appear if there is no preview set.
Hi
user B
, thanks! and is it posible to load a custom image instead of an icon? THAnks 😇
Hi
user B
, i have it! Thanks!
Perfect! Nice work!
user L
you can could use an image if you created a custom input component, I believe.
Yeah, you might need a custom component for that. I did some experimenting in my studio with an image as a static file like this -
import React from 'react'
import { defineField, defineType } from 'sanity'
// @ts-ignore
import chuckyImage from '../../images/chucky.png'

const Chucky = () => (
  <img src={chuckyImage} alt="Chucky" />
)

export default defineType({
  type: 'object',
  name: 'myAwesomeComponent',
  fields: [
    ... cool fields
  ],
  preview: {
    select: { title: 'title' },
    prepare({ title }) {
      const CHUCKY = Chucky

      return {
        title: `My Awesome Component: ${title}`,
        media: CHUCKY,
      }
    },
  },
})
I didn't have any luck with the
MenuItem , though. The preview did work, but only after I had added the component, and in the content array. Looking through the repo, I didn't see anything that made me think that out of the box, we could use an image.
I ended up just creating a few high level types and then subtypes, that way I could show the preview:
Hi
user L
, I love the image you sent. Could you please help me out a little bit how to create something like that? It looks exactly what we need 🙂
Sure!
can't wait! 😄
This solution is exactly what I’m looking for too. It would be great to use static images in a page builder scenario to preview the block types before adding details. Would be so grateful for any guidance on how to create something like that
dont be shy
user L
😄 - we are looking to get that modal working in our projects as soon as possible!
I can't share code, but I can help somebody build it.
can you share how did you accomplish it without providing any code?Is that a custom component? would love to get some ideas on how we can start this
Yes, a custom component.
I create a "subBlockType" and then created a document which cross references the name and image.

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?