🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

How to reuse schema sections in Sanity.io

6 replies
Last updated: Oct 6, 2023
Can someone help me find documentation on how to reuse schema sections? Example, I have a color picker I want to use in multiple places and schemas. Thanks.
Oct 6, 2023, 3:07 PM
I saw a conversation recently regarding this link: https://www.sanity.io/docs/schema-field-types#81e2f304a9a8 which itself harkens back to this one: https://www.sanity.io/docs/schemas-and-forms
The basic idea is that you can write documents or objects, and while setting up the Desk Tool only shows
documents in each pane, you can add any custom objects you want as well to the schema by simply setting up your config to let it know they exist.
Basically, if they're in the schema, they're officially usable.

A great guide came out too where this happens twice in a kind of nested way, and it demonstrates the flexibility of the concept:
https://www.sanity.io/guides/create-a-time-duration-object-field
Oct 6, 2023, 3:25 PM
Thank you. I will poke around at these links.
Oct 6, 2023, 3:29 PM
So what I ended up doing is this:

Make a file for the schema:
export default defineType({
  name: "colorPicker",
  title: "Color Picker",
  description:
    "Changes the background color of this section. Default is 'water'.",
  type: "string",
  options: {
    list: [
      { title: "Water",         value: "water", },
      { title: "Sunshine",         value: "sunshine",  },
      { title: "Ocean",         value: "ocean",   },
      {  title: "Forest",         value: "forest",  },
      { title: "Grey",         value: "grey", },
    ],
  },
});
In the schema I want to use it, I just do this:

defineField({
   name: "colorPicker",
   title: "Color Picker",
   type: "colorPicker",
 }),
Then import into sanity.config.ts
Oct 6, 2023, 5:05 PM
Is it working okay for you?
Oct 6, 2023, 5:08 PM
Yeah so that is working as I wanted now.
Oct 6, 2023, 5:22 PM
Sweet!
Oct 6, 2023, 5:28 PM

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?