Creating Sanity navbar button plugin that opens modal instead of routing
Yes, there is a way to create a plugin with a button in the navbar that opens a modal without navigating to a new route! The Studio Components API allows you to customize various parts of the Studio UI, including the navbar.
According to the Studio Components Reference, you can override the navbar component to add custom elements. Here's how you can approach this:
Custom Navbar Button with Modal
You can use the studio.components.navbar configuration to render custom components alongside the default navbar:
// ./sanity.config.tsx
import {defineConfig, NavbarProps} from 'sanity'
import {Button, Dialog} from '@sanity/ui'
import {useState} from 'react'
function CustomNavbar(props: NavbarProps) {
const [isOpen, setIsOpen] = useState(false)
return (
<>
{props.renderDefault(props)} {/* Render the default navbar */}
{/* Your custom button */}
<Button
onClick={() => setIsOpen(true)}
text="Open Modal"
tone="primary"
/>
{/* Your modal */}
{isOpen && (
<Dialog
header="My Custom Modal"
id="custom-modal"
onClose={() => setIsOpen(false)}
width={1}
>
{/* Your modal content here */}
<div>Modal content goes here!</div>
</Dialog>
)}
</>
)
}
export default defineConfig({
// rest of config ...
studio: {
components: {
navbar: CustomNavbar,
}
}
})Key Points
renderDefault(props)renders the standard navbar, so you're extending rather than replacing it- You can add your custom button alongside the default navbar elements
- Use Sanity UI components like
Dialog,Button, etc. for consistency with the Studio's design - The modal/dialog stays on the current page - no routing needed
The {/* Insert plugins here */} comment you found in the source is likely a placeholder for future extension points, but the navbar component override is the official way to achieve what you're looking for. This approach gives you full control over adding interactive elements to the navbar without creating a separate tool route.
Show original thread3 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.