Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Open editor and preview pane in split view all the time

By Sigurd Heggemsnes

Open editor and preview pane in split view all the time

auto-preview-pane.component.tsx

// ./sanity/components/auto-preivew-pane.component.tsx

import { useEffect } from "react"
import { usePaneRouter } from "sanity/desk"

export const AutoPreviewPane = () => {
  const {
    setView,
    duplicateCurrent,
    groupIndex,
    hasGroupSiblings,
    routerPanesState,
  } = usePaneRouter()

  useEffect(() => {
    // Check if "preview" is active
    const isPreviewActive = routerPanesState.some((group) =>
      group.some((pane) => pane.params?.view === "preview")
    )

    if (!isPreviewActive) {
      if (hasGroupSiblings) {
        if (groupIndex === 1) {
          setView("preview")
        }
      } else {
        duplicateCurrent()
      }
    }
    // Ignoring this because adding deps causes lots of loops
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [])

  return null
}

article.document.tsx

// ./sanity/schemas/article.document.tsx

import { AutoPreviewPane } from "../components/auto-preview-pane.component"


export const article = defineType({
  type: "document",
  name: "article",
  title: "Article",
  fields: [
    defineField({
      type: "string",
      name: "title",
    }),
    defineField({
      type:"string",
      name:"hiddenPreviewField",
      components: {
        field: AutoPreviewPane,
      },
    })
  ],
})

Please note!

  • This snippet assumes you've set up a preview pane using this example.
  • This is a somewhat hacky solution based on the somewhat hidden usePaneRouter. Please adjust it to your needs. If you have groups make sure your component is in the default group for it to load properly.

Why?

On a lot of content types our editors would always want to see the preview of their website. Pressing split content button and then the preview tab has become engrained in my brain so I realised we need another solution.

Contributor

Other schemas by author