๐Ÿ”ฎ Sanity Create is here. Writing is reinvented. Try now, no developer setup

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!
Aug 3, 2023, 12:15 PM
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.
Aug 3, 2023, 4:02 PM
Hi
user B
, thanks! and is it posible to load a custom image instead of an icon? THAnks ๐Ÿ˜‡
Aug 3, 2023, 4:05 PM
Hi
user B
, i have it! Thanks!
Aug 3, 2023, 4:13 PM
Perfect! Nice work!
Aug 3, 2023, 4:27 PM
user L
you can could use an image if you created a custom input component, I believe.
Aug 3, 2023, 6:42 PM
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.
Aug 3, 2023, 7:55 PM
I ended up just creating a few high level types and then subtypes, that way I could show the preview:
Aug 3, 2023, 7:57 PM
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 ๐Ÿ™‚
Aug 4, 2023, 8:06 AM
Sure!
Aug 4, 2023, 1:44 PM
can't wait! ๐Ÿ˜„
Aug 4, 2023, 1:51 PM
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
Aug 7, 2023, 1:55 PM
dont be shy
user L
๐Ÿ˜„ - we are looking to get that modal working in our projects as soon as possible!
Aug 23, 2023, 3:10 PM
I can't share code, but I can help somebody build it.
Aug 23, 2023, 3:10 PM
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
Aug 23, 2023, 3:12 PM
Yes, a custom component.
Aug 23, 2023, 4:26 PM
I create a "subBlockType" and then created a document which cross references the name and image.
Aug 23, 2023, 4:26 PM

Sanityโ€“ build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?