How to reuse schema sections across multiple schemas in Sanity?
I can help you with reusing schema sections in Sanity! The key is to define reusable field definitions or object types that you can reference throughout your schemas.
Creating Reusable Fields
For a color picker you want to use in multiple places, you have a few approaches:
1. Define a reusable field configuration:
Create a shared field definition that you can spread into multiple schemas:
// In a shared file like schemas/fields/colorPicker.ts
import { defineField } from 'sanity'
export const colorPickerField = defineField({
name: 'color',
title: 'Color',
type: 'color',
options: {
disableAlpha: true
}
})
// Then use it in your document schemas
import { defineType } from 'sanity'
import { colorPickerField } from './fields/colorPicker'
export const myDocumentType = defineType({
name: 'myDocument',
type: 'document',
fields: [
colorPickerField,
// ... other fields
]
})2. Create a reusable object type:
If you want a more complex reusable structure, define a custom object type using defineType:
// In schemas/objects/colorSettings.ts
import { defineType } from 'sanity'
export const colorSettings = defineType({
name: 'colorSettings',
title: 'Color Settings',
type: 'object',
fields: [
{
name: 'backgroundColor',
title: 'Background Color',
type: 'color'
},
{
name: 'textColor',
title: 'Text Color',
type: 'color'
}
]
})
// Use it in your schemas
export const pageType = defineType({
name: 'page',
type: 'document',
fields: [
{
name: 'colorScheme',
type: 'colorSettings' // Reference your reusable object type
}
]
})3. Factory function for variations:
If you need the same field with different names across schemas, create a factory function:
// schemas/fields/colorPickerFactory.ts
import { defineField } from 'sanity'
export const createColorField = (name: string, title: string) =>
defineField({
name,
title,
type: 'color',
options: {
disableAlpha: true
}
})
// Use it
const brandColor = createColorField('brandColor', 'Brand Color')
const accentColor = createColorField('accentColor', 'Accent Color')The defineField helper provides TypeScript support and autocomplete, making it the recommended approach for defining any field in Sanity. By extracting common field definitions into shared modules, you maintain consistency across your schema and make updates easier—change the color picker configuration once, and it updates everywhere you've used it.
Show original thread6 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.