How to Reduce the Width and Height of the Image Data Type Preview

3 replies
Last updated: Jul 8, 2021
is there any option in Sanity to reduce the width and height of the image data type preview? For icons & small images, this size is too big.
Shall I open an issue in Github?
AI Update

I understand you're looking to reduce the preview size of image fields in Sanity Studio, especially for icons and small images where the default preview size is too large.

Current Situation

Unfortunately, there isn't a built-in schema option to directly control the width and height of image field previews in Sanity Studio. The default preview size is determined by Studio's internal styling and isn't exposed as a configurable property in the schema definition.

Your Options

You can create a custom preview component to control the image size. Based on the guide on enhancing image previews with renderDefault(), you can customize how images appear by using the Form Components API.

Here's an approach for smaller icons:

import createImageUrlBuilder from '@sanity/image-url'
import {useMemo} from 'react'
import {useClient} from 'sanity'

export function SmallIconPreview(props) {
  const {media} = props
  const client = useClient({apiVersion: '2021-03-25'})
  
  const imageUrlBuilder = useMemo(() =>
    createImageUrlBuilder(client),
    [client]
  )

  const imgSrc = useMemo(() =>
    media?._ref && imageUrlBuilder
      .image(media?._ref)
      .width(100) // Smaller width for icons
      .height(100) // Smaller height for icons
      .url(),
    [media?._ref, imageUrlBuilder]
  )

  if (!imgSrc) return null
  
  const newProps = {
    ...props,
    media: <img src={imgSrc} style={{maxWidth: '50px', maxHeight: '50px', objectFit: 'contain'}} />
  }

  return props.renderDefault(newProps)
}

Then use it in your schema:

{
  name: 'icon',
  type: 'image',
  title: 'Icon',
  components: {
    preview: SmallIconPreview
  }
}

This approach uses renderDefault to preserve all the default functionality (click handlers, context menus, etc.) while just swapping out the preview image with a smaller, custom-sized version.

2. Custom CSS (Less Reliable)

You could potentially add custom CSS to your Studio configuration, though this is less maintainable and may break with Studio updates. This would involve targeting the preview container classes, but I don't recommend this approach.

3. Opening a GitHub Issue

Yes, opening a GitHub issue would be valuable! This is a legitimate feature request that would benefit many users. When you open the issue on the sanity-io/sanity repository:

  • Describe your use case (icons and small images appearing too large in previews)
  • Suggest a schema-level option like options: { previewSize: 'small' } or options: { previewMaxWidth: 100, previewMaxHeight: 100 }
  • Reference that this would be useful for arrays of icons, logos, or small graphics
  • Mention that while custom preview components work, a built-in option would improve developer experience

The custom preview component approach is your best current solution, but a built-in schema option would definitely make this easier for everyone. The Sanity team is generally responsive to well-explained feature requests that solve real workflow problems.

Show original thread
3 replies
Can definitely see the need to control the preview size here - if you could open a GH issue, that would be awesome. Thanks
user H
πŸ™‚
Much appreciated!

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?