How to use a Custom Preview Component on the Document View

15 replies
Last updated: Apr 21, 2020
how do I use a custom preview component on the document view?
AI Update

To use a custom preview component on the document view in Sanity Studio, you'll configure it using the Structure Builder's S.view.component() method. This lets you add custom React components as alternative views alongside the default form view.

Here's the basic implementation pattern:

export const structure = (S) =>
  S.list()
    .title('Content')
    .items([
      S.documentTypeListItem('myDocumentType').child(
        S.documentList()
          .title('My Documents')
          .filter('_type == "myDocumentType"')
          .child((documentId) =>
            S.document()
              .documentId(documentId)
              .schemaType('myDocumentType')
              .views([
                S.view.form(), // Default form view
                S.view.component(CustomPreviewComponent)
                  .title('Preview')
                  .icon(EyeIcon) // optional
              ])
          )
      )
    ])

Your custom component receives the document data as props, which you can use to render your preview:

import { useDocumentPane } from 'sanity/desk'

function CustomPreviewComponent(props) {
  const { document, documentId, schemaType } = props
  
  // Access the current document values
  const { displayed } = document
  
  return (
    <div style={{ padding: '1rem' }}>
      <h2>{displayed?.title}</h2>
      {/* Render your custom preview here */}
    </div>
  )
}

export default CustomPreviewComponent

This approach is particularly powerful for:

  • Live previews: Show how content will appear on your actual website
  • Component visualization: Display frontend components with real document data
  • Contextual information: Show related documents, analytics, or metadata
  • Custom interactions: Add specialized editing controls for complex content types

The Component View documentation covers this in detail, including how to access document values and integrate with your frontend components for accurate previews.

say i have an document
video
that i want to show the video preview on the document view
i also reference this video in another document, but when its a reference, I only want to show the
video
document title
how is this accomplished?
Just to confirm I’m understanding fully, you have a custom preview component for your
video
document type, e.g. something like on https://www.sanity.io/guides/portable-text-how-to-add-a-custom-youtube-embed-block . You’re happy to show this preview in
video
, but when you add a reference to the document type you don’t want to see it. Is that correct? What kind of preview does the reference currently show?
yes...
here's some screens
Video document... would like the preview here
When referenced ... this is fine
the reason the reference looks ok, is because I removed the
preview
from the schema, otherwise it was trying to load the videoPreview component
Thanks for the shots, always helps! 🙂 Have you considered adding a video object to your video document (under a different name)? That should let you show the preview on the document but just the title in the reference 📽️
This would require loading the preview component in the object instead.
i had not considered that ... so making a
video
object that goes inside the
video
document. does the object only contain the URL for the video? (i'm sourcing from vimeo)
Yep, I’d say so, unless you want to add more data that specifically belongs to the video like a title or caption etc.
well, i would think i could just use the
document
title... since the document is really about the video
One thing you could consider is whether you need that kind of data in the document where you reference the video.
been thinking the same thing ...
99% of the time, i'm probably also the document editor, and i'll know the video already
however, i do see a scenario down the road where I use this dataset as a source for another Sanity project, and the people using that project will need a visual reference to what the video looks like

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?