Change document create button behavior to open modal before edit screen
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:
- Opens a modal/dialog when clicked
- Collects user input or selection
- 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 thread2 replies
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.