Array Previews - How to Add an Image of a Module
13 replies
Last updated: Mar 22, 2021
C
Hi!I have a question about array previews.
The user can choose between these modules and add content to them, but to make it more user-friendly I want to add an image of what the module looks like. (just like a simple wireframe) - any ideas how to do that?
In the picture you´ll see a few added modules, and a list of names:
Here’s the pageBuilder:
And one of the modules:
The user can choose between these modules and add content to them, but to make it more user-friendly I want to add an image of what the module looks like. (just like a simple wireframe) - any ideas how to do that?
In the picture you´ll see a few added modules, and a list of names:
'module-1, module-2….'.It’s in the list of names I want to add a picture.
Here’s the pageBuilder:
export default { name: 'pageBuilder', title: 'Page builder', type: 'object', fields: [ { options: { collapsible: true }, type: 'array', name: 'page', title: 'Page', of: [ { type: 'module1' }, { type: 'module2' }, { type: 'module3' }, { type: 'module4' }, { type: 'module5' }, { type: 'module6' }, { type: 'module7' }, ] }] }
export default { name: 'module1', title: 'Module One', type: 'document', fields: [ { name: 'imageLeft', title: 'Image left', type: 'mainImage' }, { name: 'imageMiddle', title: 'Image middle', type: 'mainImage' }, { name: 'imageRight', title: 'Image right', type: 'mainImage' }, { name: "moduleTitle", title: "Module Title", type: "string", }, { name: 'moduleParagraph', title: 'Block Text', type: 'restrictedBodyText' }, { name: "general", title: "General Content", type: "generalModule", options: { collapsible: true }, }, ], preview: { select: { title: 'moduleTitle', } } }
Mar 22, 2021, 10:04 AM
R
You can do this in each module's preview prop
Mar 22, 2021, 10:24 AM
R
Something like this
preview: {
select: {
name: 'name',
language: 'language',
imageUrl: 'image.asset.url'
},
prepare({ name, language, imageUrl }) {
return {`title:
Banner ${language}: ${name},` `media: imageUrl && <img src={
${imageUrl}?auto=format&w=100} style={{ objectFit: 'cover' }} />`
}
}
}
Mar 22, 2021, 10:24 AM
L
But that only affects modules that are created.I want the picture added to the drop-down visiible in the picture above.
user R
Mar 22, 2021, 10:26 AM
C
I thought it was something similar to the following snippet, which is added to the pagebuilder? - but I can’t get it to work
preview: { select: { tag0: 'tags.0.value', tag1: 'tags.1.value' }, prepare(selection) { return { title: [selection.tag0, selection.tag1].join(', ') } } }
Mar 22, 2021, 10:28 AM
C
the prepare function is used in both cases, I would set it up so that if, for instance, there is no imageLeft, use a placeholder image instead
Mar 22, 2021, 10:28 AM
C
i think you want to use icon: propety
Mar 22, 2021, 10:29 AM
C
thats whats shown in the dropdown
Mar 22, 2021, 10:29 AM
C
so your module will be like
Mar 22, 2021, 10:30 AM
C
export default { name: 'module1', title: 'Module One', type: 'document', icon: ()=><img src="/static/wireframe.png" />, fields: [ { name: 'imageLeft', title: 'Image left', type: 'mainImage' }, { name: 'imageMiddle', title: 'Image middle', type: 'mainImage' }, { name: 'imageRight', title: 'Image right', type: 'mainImage' }, { name: "moduleTitle", title: "Module Title", type: "string", }, { name: 'moduleParagraph', title: 'Block Text', type: 'restrictedBodyText' }, { name: "general", title: "General Content", type: "generalModule", options: { collapsible: true }, }, ], preview: { select: { title: 'moduleTitle', } } }
Mar 22, 2021, 10:30 AM
R
for example
Mar 22, 2021, 10:30 AM
L
user M
- That could be something!Mar 22, 2021, 10:32 AM
L
Mar 22, 2021, 10:39 AM
L
I solved it by using Christophers solution - the difficult part of it all was adding a css class to rewrite the propsed size for an icon. (My images had to be larger to make sense)
Mar 22, 2021, 3:49 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.