Change document create button behavior to open modal before edit screen

2 replies
Last updated: Feb 29, 2024
Hi all, is there anyway to change behaviour of this button? I want it to open an modal and then based on selection open an edit screen. Thank you
AI Update

Based on your question about customizing the button behavior to open a modal before proceeding to the edit screen, you'll want to look into Document Actions. These are the customizable toolbar actions in Sanity Studio that control what happens when you interact with documents.

You can create a custom document action that:

  1. Opens a modal/dialog when clicked
  2. Collects user input or selection
  3. Proceeds to open the document editor based on that selection

Here's the general approach:

1. Create a custom document action resolver in your Studio configuration. Add this to your sanity.config.ts:

import {defineConfig} from 'sanity'

export default defineConfig({
  // ... other config
  document: {
    actions: (prev, context) => {
      // Add your custom action
      return [...prev, MyCustomAction]
    }
  }
})

2. Build your custom action with a dialog component. Your action can return a dialog property that renders a modal:

import {useState} from 'react'

const MyCustomAction = (props) => {
  return {
    label: 'Custom Edit',
    icon: YourIcon,
    onHandle: () => {
      // This opens your modal
      props.onComplete()
    },
    dialog: {
      type: 'modal',
      content: YourModalComponent
    }
  }
}

3. In your modal component, handle the selection logic and then programmatically navigate to the edit screen or trigger the appropriate action based on the user's choice.

You can also filter or replace existing actions if you want to modify the default behavior of built-in buttons like "Edit". For example, you might want to intercept the default edit action for certain document types:

actions: (prev, context) => {
  if (context.schemaType === 'yourDocType') {
    return prev.map(action => 
      action.name === 'edit' ? YourCustomAction : action
    )
  }
  return prev
}

Check out the full Document Actions API documentation and this guide on custom document actions for detailed examples and implementation patterns.

Show original thread
2 replies
You would need to create a custom component to handle that.
If you do, you can pull in Sanity UI's Dialog component and show it or hide it (open or close, basically) with a simple state-setter.
The interesting and fun thing is that even though it's presentationally "over" the document (and field), it will have full access to the information. I just used it to send people out to a really long virtualized list component that didn't make sense to use/see inline (since it's heavily dependent on scroll and you wouldn't wanna whoosh down the document every time you needed to use it) and their choices get saved while still in the modal.

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?