Go Behind the Experience to see how Tecovas brings the West to life with Sanity 🤠 July 18th

How to hide/remove specific document types from the "Create a new document" button in Sanity Studio.

25 replies
Last updated: Feb 23, 2022
Hello. Is there anyway to hide/remove/filter specific document types from this main “Create a new document” button at the top left of the studio?
Feb 13, 2022, 1:00 PM
I’m wondering about the same,
user F
. Here’re some docs about configuring the dashboard. I can see how to add new widgets but can not find how to remove some.
Feb 13, 2022, 1:13 PM
seems like it’s a default config that can not be changed
Feb 13, 2022, 1:20 PM
Can the button be removed/hidden?
Feb 13, 2022, 1:20 PM
As far as I can see the default config is coming from this library: https://github.com/sanity-io/sanity/tree/next/packages/%40sanity/default-layout . And it’s a required one to run a sanity studio. So I assume that to change the default dashboard, you need to make some changes to the library. BUT these are just mu assumptions ☺️
Feb 13, 2022, 1:26 PM
Good to know. I guess I would need to resort to using CSS to hide that button if possible.
Feb 13, 2022, 1:27 PM
Do you or anyone else know how this can be even targeted. Following this Sanity doc about “Styling the Studio ” and it triggers errors without even adding / customizing from what the docs day.
Feb 13, 2022, 1:54 PM
Also it seems like it’s just for overriding variables. Can any css be added to just include a line like
.button-class { display: none; }
work?
Feb 13, 2022, 1:55 PM
I do not think you can reach your goal with overwriting a css class in the studio. It’s the CreateDomunentDialog that has to be removed from I understand. But to do that you need to add changes to the widget itself.
Feb 13, 2022, 2:26 PM
OK…I see what you are saying. So I would need to possibly “extend” it and override it? Just not sure how this is possible
Feb 13, 2022, 2:27 PM
I think so. Maybe there’re easier ways to access this one but that my initial thoughts.
Feb 13, 2022, 2:29 PM
The easiest way to remove documents from that button, for now, is still using this soon-to-be deprecated feature:
https://www.sanity.io/docs/ui-affordances-for-actions
There are no clear ways to achieve this with other features mentioned in that doc (Initial value templates or custom document actions)
Feb 13, 2022, 2:40 PM
Is there anyway to hide/remove/filter specific document types from this main “Create a new document” button at the top left of the studio?
You can remove documents from that list using the following, though this will also remove the
Create
icon from the document list. Technically, you’ll still be able to create them at
/intent/create/template=<TYPE>;type=<TYPE>/
.
In sanity.json (path can be any file or name you want):

{
  "name": "part:@sanity/base/initial-value-templates",
  "path": "./initial-value-templates/initialValueTemplates.js"
},
In initialValueTemplates.js:


import T from '@sanity/base/initial-value-template-builder' // Edited to add. Good catch, Derek!

export default [
  ...T.defaults().filter(item => item.spec.schemaType !== 'schemaTypeToRemove'),
]
Feb 13, 2022, 5:20 PM
TIL, thanks Geoff! Perhaps should add that to the docs, in the experiment actions section and also the structure builder example where the experiment actions are still recommended
Feb 13, 2022, 5:24 PM
Thanks
user A
, this is exactly what I was looking for. Thank you!
Feb 13, 2022, 5:26 PM
Hi
user A
, I tried this for a document type
event
and it is still showing…can you please check this out and let me know what I am doing wrong?
Feb 13, 2022, 5:44 PM
I've just given this a try, perhaps you need to import the builder?

import T from '@sanity/base/initial-value-template-builder'
But it's odd if you didn't see any errors
Feb 13, 2022, 5:50 PM
Whoops! Exactly what Derek said. Yes, you’ll need to add that to
initialValueTemplates.js
.
Feb 13, 2022, 5:52 PM
I've just given this a try, perhaps you need to import the builder?

import T from '@sanity/base/initial-value-template-builder'
But it's odd if you didn't see any errors
Feb 13, 2022, 5:50 PM
Whoops! Exactly what Derek said. Yes, you’ll need to add that to
initialValueTemplates.js
.
Feb 13, 2022, 5:52 PM
Awesome! That worked. Thanks for all your help
user A
,
user G
and
user M
!
Feb 13, 2022, 6:06 PM
Perhaps should add that to the docs
user G
Will do. There are some differences between the two APIs that need to be confirmed, but I agree that it should be there. Thanks! 🙌
Feb 13, 2022, 6:19 PM
Thank you for the help,
user A
user G
!! it turned out to be easier then going into the default-layout library and expanding it as I thought. ☺️🙏 Hope you got it working,
user F
💪
Feb 14, 2022, 7:46 AM
Circle back to add this bit; the code
user A
shared removes the doc from the menu but doesn't prevent editors from (hopfully) accidentally removing / unpublishing the doc. In order to do that we'd need to add a custom document action as well:

// sanity.json 
//... 
"parts": [ 
  //... 
  { 
    "implements": "part:@sanity/base/document-actions/resolver", 
    "path": "resolveDocumentActions.js" 
  } 
]

// resolveDocumentActions.js
import defaultResolve, {
  PublishAction,
  DiscardChangesAction,
} from 'part:@sanity/base/document-actions'

export default function resolveDocumentActions(props) {
  if (['dont', 'delete', 'me'].includes(props.type)) {
    return defaultResolve(props).filter((action) =>
      [PublishAction, DiscardChangesAction].includes(action)
    )
  } else {
    return defaultResolve(props)
  }
}
Feb 23, 2022, 9:32 AM
Circle back to add this bit; the code
user A
shared removes the doc from the menu but doesn't prevent editors from (hopfully) accidentally removing / unpublishing the doc. In order to do that we'd need to add a custom document action as well:

// sanity.json 
//... 
"parts": [ 
  //... 
  { 
    "implements": "part:@sanity/base/document-actions/resolver", 
    "path": "resolveDocumentActions.js" 
  } 
]

// resolveDocumentActions.js
import defaultResolve, {
  PublishAction,
  DiscardChangesAction,
} from 'part:@sanity/base/document-actions'

export default function resolveDocumentActions(props) {
  if (['dont', 'delete', 'me'].includes(props.type)) {
    return defaultResolve(props).filter((action) =>
      [PublishAction, DiscardChangesAction].includes(action)
    )
  } else {
    return defaultResolve(props)
  }
}
Feb 23, 2022, 9:32 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?