Adding icons or custom images to text in Sanity.io
16 replies
Last updated: Aug 23, 2023
N
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!
Thanks!
Aug 3, 2023, 12:15 PM
K
Hey (Removed Name),
Sure you can! I usually do it like this:
Here's the
docs on that. It goes through using the
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: [ ...
docs on that. It goes through using the
@sanity/iconspackage, 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
N
Hi
user B
, thanks! and is it posible to load a custom image instead of an icon? THAnks πAug 3, 2023, 4:05 PM
N
Hi
user B
, i have it! Thanks!Aug 3, 2023, 4:13 PM
K
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
K
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 -
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.
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, } }, }, })
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
N
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
A
can't wait! π
Aug 4, 2023, 1:51 PM
O
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
A
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
A
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
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.