How to add and access options in Sanity custom type definitions?
When creating custom types in Sanity, you can add options to your type definition and access them within your schema. Here's how it works:
Adding Options to Custom Types
Options are defined within an options object in your schema definition. For example:
{
type: 'object',
name: 'localeBlock',
title: 'Localized Block',
options: {
collapsible: true,
collapsed: false,
columns: 2,
modal: {
type: 'dialog',
width: 'medium'
}
},
fields: [
// your fields here
]
}Common Options for Objects
According to the object type documentation, you can use these options:
collapsible: Set totrueto make fields collapsible (default at depth 3)collapsed: Set totrueto start with fields collapsed (requirescollapsible: true)columns: Number of columns in a grid layout for inputsmodal: Controls modal rendering withtype('dialog'or'popover') andwidthproperties
Accessing Options in Your Definition
If you need to access options programmatically within custom components or input components, you can access them through the schema type context:
// In a custom input component
const MyCustomInput = (props) => {
const options = props.schemaType.options
// Access specific options
const isCollapsible = options?.collapsible
const columnCount = options?.columns
// Use them in your component logic
return (
// your component JSX
)
}Field-Level vs Type-Level Options
Remember that options can be defined at both the type level (affecting all instances) and when you use the type in a field:
// In your schema
fields: [
{
name: 'myLocaleBlock',
type: 'localeBlock',
options: {
// These override the type-level options for this specific field
collapsed: true
}
}
]The field-level options will override type-level options when both are present. This gives you flexibility to set defaults at the type level while customizing behavior for specific field instances.
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.