Joint session with Vercel: How to build intelligent storefronts (May 15th)

Structure Tool Home Page

Insert a home page component onto the blank default space shown in the Structure tool

By Jordan Lawrence

Install command

npm i sanity-plugin-blank-space

sanity-plugin-blank-space

A Sanity Studio plugin that renders a custom component in the structure tool's empty pane area - it's the love story your Studio's blank space has been waiting for. Perfect for welcome screens, dashboards, or getting-started guides, and works all too well with any existing structure configuration.

Demo of sanity-plugin-blank-space showing a custom welcome pane in Sanity Studio

Installation

npm install sanity-plugin-blank-space

Usage

Add the plugin to your sanity.config.ts and shake off the empty pane - no changes to existing structure setup required:

import {defineConfig} from 'sanity'
import {structureTool} from 'sanity/structure'
import {structureHomeLandingPlugin} from 'sanity-plugin-blank-space'

function WelcomePane() {
  return <div style={{padding: 32}}>Welcome to the Studio</div>
}

export default defineConfig({
  // ...
  plugins: [
    structureTool(),
    structureHomeLandingPlugin({component: WelcomePane}),
  ],
})

Custom Component Examples

You can use any React component - style it however you like, including ones that use Sanity hooks.

User Greeting with Sanity hooks

Use useCurrentUser and useProjectId from sanity to personalise the landing pane:

import {useCurrentUser, useProjectId} from 'sanity'
import {Card, Heading, Text, Stack, Flex, Avatar, Inline} from '@sanity/ui'

function UserGreeting() {
  const user = useCurrentUser()
  const projectId = useProjectId()

  return (
    <Flex align="center" justify="center" height="fill">
      <Card padding={5} radius={3} shadow={1}>
        <Stack space={4}>
          <Inline space={3}>
            {user?.profileImage && <Avatar src={user.profileImage} size={1} />}
            <Heading size={3}>Hello, {user?.name || 'there'}!</Heading>
          </Inline>
          <Text muted>
            You are editing project <code>{projectId}</code>. Select a document from the sidebar to
            get started.
          </Text>
        </Stack>
      </Card>
    </Flex>
  )
}

Quick Actions

Use useDocumentTypes to build a "create new" panel - it returns user-defined document types from the schema, filtering out internal sanity.* types. Pair it with IntentLink or useIntentLink from sanity/router for navigation:

import {IntentLink} from 'sanity/router'
import {Card, Stack, Flex, Heading, Button} from '@sanity/ui'
import {useDocumentTypes} from 'sanity-plugin-blank-space'
import {AddIcon} from '@sanity/icons'

function QuickActions() {
  const documentTypes = useDocumentTypes()

  return (
    <Flex align="center" justify="center" height="fill">
      <Card padding={5}>
        <Stack space={4}>
          <Heading size={2}>Create New</Heading>
          {documentTypes.map((docType) => (
            <Button
              key={docType.name}
              as={IntentLink}
              intent="create"
              params={{type: docType.name}}
              icon={docType.icon ?? AddIcon}
              text={docType.title}
              mode="ghost"
            />
          ))}
        </Stack>
      </Card>
    </Flex>
  )
}

Use usePopularDocumentTypes to rank document types by count. Pair with useIntentLink to add a create action per type. The example Studio implementation extends this with a card layout and icons.

import {AddIcon} from '@sanity/icons'
import {Badge, Button, Card, Flex, Heading, Spinner, Stack, Text} from '@sanity/ui'
import {useIntentLink} from 'sanity/router'
import {usePopularDocumentTypes} from 'sanity-plugin-blank-space'

function CreateButton({type}: {type: string}) {
  const {onClick, href} = useIntentLink({intent: 'create', params: {type}})
  return (
    <Button
      as="a"
      href={href}
      onClick={onClick}
      icon={AddIcon}
      mode="ghost"
      padding={2}
      tone="primary"
    />
  )
}

function PopularTypes() {
  const {data, isLoading, error} = usePopularDocumentTypes()

  if (isLoading) return <Spinner />
  if (error) return <Text>Failed to load document counts</Text>

  return (
    <Flex align="center" justify="center" height="fill">
      <Card padding={5}>
        <Stack space={4}>
          <Heading size={2}>Popular Types</Heading>
          {data.map((docType) => (
            <Flex key={docType.name} align="center" gap={3}>
              {docType.icon && (
                <Text>
                  <docType.icon />
                </Text>
              )}
              <Text weight="semibold" flex={1}>
                {docType.title}
              </Text>
              <Badge tone="primary">{docType.documentCount}</Badge>
              <CreateButton type={docType.name} />
            </Flex>
          ))}
        </Stack>
      </Card>
    </Flex>
  )
}

Popular Document Types pane showing document type list with counts and create buttons

API Reference

structureHomeLandingPlugin(options)

OptionTypeRequiredDefaultDescription
componentComponentTypeYes-React component to render in the empty pane
titlestringNo"Welcome"Label shown in the pane header
paneIdstringNo"home"Internal pane ID used in the Studio URL routing

useDocumentTypes()

React hook that returns user-defined document types from the Studio schema.

Returns DocumentTypeInfo[]:

PropertyTypeDescription
namestringThe document type name
titlestringDisplay title (falls back to name)
iconComponentTypeOptional icon component from the schema

usePopularDocumentTypes(limit?)

React hook that returns document types sorted by document count, most popular first. Pairs useDocumentTypes() with a GROQ count query.

ParameterTypeRequiredDefaultDescription
limitnumberNo-Cap the number of returned types

Returns UsePopularDocumentTypesResult:

PropertyTypeDescription
dataDocumentTypeWithCount[]Types sorted by count, descending
isLoadingbooleantrue while counts are loading
errorError | nullFetch error, if any

DocumentTypeWithCount

Extends DocumentTypeInfo with:

PropertyTypeDescription
documentCountnumberDocuments of this type in the dataset

UsePopularDocumentTypesResult

TypeScript interface for the usePopularDocumentTypes() return value.

StructureHomeLandingPluginOptions

TypeScript interface for the plugin options, exported for use in typed configurations.

Development

The example Studio has four workspaces, one for each component example above.

pnpm install
pnpm dev:studio    # starts Sanity Studio at http://localhost:3333

See Local Development for the full dev workflow.

License

MIT

Related contributions