@tinloof/sanity-document-options
v3.1.0
Published
Readme
@tinloof/sanity-document-options
Configure document actions, badges, templates, and structure directly in your schema definitions.
Installation
npm install @tinloof/sanity-document-optionsBasic Setup
Add the plugin to your sanity.config.ts:
import {defineConfig} from "sanity";
import {documentOptions} from "@tinloof/sanity-document-options";
export default defineConfig({
plugins: [documentOptions()],
});Disable Structure Tool
If you want to use only document options (actions, badges, templates) without the structure tool:
export default defineConfig({
plugins: [documentOptions({structure: false})],
});Features
✅ Zero Configuration
Works out of the box - automatically creates structure from your schemas.
✅ Document Actions & Badges
Configure actions and badges directly in schema options.
✅ Document Templates
Define initial value templates in schemas.
✅ Structure Options
Control how documents appear in the studio structure.
✅ Grouping & Organization
Organize documents into collapsible groups.
✅ Singletons & Ordering
Create single-instance documents or enable drag-and-drop ordering.
✅ Localization Support
Multi-language content with locale-specific views.
Usage Examples
Document Actions
defineType({
name: "post",
type: "document",
options: {
document: {
actions: (prev, context) => {
// Remove delete for published posts
if (context.published) {
return prev.filter((action) => action.action !== "delete");
}
return prev;
},
},
},
});Document Badges
defineType({
name: "article",
type: "document",
options: {
document: {
badges: (prev, context) =>
[
...prev,
context.published && {
label: "Published",
color: "success",
},
].filter(Boolean),
},
},
});Document Templates
defineType({
name: "page",
type: "document",
options: {
schema: {
templates: [
{
id: "page-blog",
title: "Blog Post",
schemaType: "page",
value: {category: "blog"},
},
],
},
},
});Structure Groups
defineType({
name: "settings",
type: "document",
options: {
structureGroup: "configuration",
},
});Singleton Documents
defineType({
name: "homePage",
type: "document",
options: {
structureGroup: "pages",
structureOptions: {
singleton: true,
icon: HomeIcon,
title: "Home Page",
},
},
});Singleton Documents with Custom ID
By default, singleton documents use the schema type name as their document ID. You can specify a custom ID for more control:
defineType({
name: "homePage",
type: "document",
options: {
structureGroup: "pages",
structureOptions: {
singleton: {
id: "site-home-page",
},
icon: HomeIcon,
title: "Home Page",
},
},
});This is useful when:
- You need predictable document IDs for API queries
- You're migrating from another system with existing IDs
- You want human-readable IDs in URLs
Orderable Documents
defineType({
name: "menuItem",
type: "document",
options: {
structureGroup: "navigation",
structureOptions: {
orderable: true,
icon: MenuIcon,
},
},
});Localized Documents
// Plugin configuration
documentOptions({
structure: {
locales: [
{id: "en", title: "English"},
{id: "fr", title: "French"},
],
},
});
// Schema configuration
defineType({
name: "article",
type: "document",
options: {
localized: true,
},
fields: [
{name: "locale", type: "string", hidden: true},
// ... other fields
],
});Custom Structure Builder
defineType({
name: "product",
type: "document",
options: {
structureGroup: "commerce",
structureOptions: (S, context) =>
S.listItem()
.title("Products by Category")
.child(
S.list()
.title("Categories")
.items([
S.listItem()
.title("Electronics")
.child(
S.documentTypeList("product").filter(
'category == "electronics"',
),
),
]),
),
},
});Plugin Options
import {
documentOptions,
type DocumentOptionsPluginOptions,
} from "@tinloof/sanity-document-options";
const options: DocumentOptionsPluginOptions = {
structure: {
locales: [{id: "en", title: "English"}],
hide: ["internalNote"],
toolTitle: "Content",
localeFieldName: "locale",
keepPanesOnCreate: true,
},
abstracts: {singleton: true, sync: true, orderable: true},
};
documentOptions(options);Call documentOptions() to use the defaults, or pass an options object as above. DocumentOptionsStructureOptions is the object accepted by structure. The existing DocumentOptionsProps and InlineStructureProps names remain supported as compatibility aliases; no migration is required.
Schema Options
interface DocumentOptions {
// Structure configuration
structureGroup?: string | string[];
structureOptions?:
| false
| StructureBuiltinOptions
| ((
S: StructureBuilder,
context: StructureResolverContext,
) => ListItemBuilder);
localized?: boolean;
// Document options
document?: {
actions?: (prev, context) => DocumentActionComponent[];
badges?: (prev, context) => DocumentBadgeComponent[];
newDocumentOptions?: (prev, context) => TemplateItem[];
};
// Schema options
schema?: {
templates?: Template[] | ((prev, context) => Template[]);
};
}Built-in Structure Options
type StructureBuiltinOptions = {
singleton?: boolean | { id?: string };
orderable?: boolean;
icon?: React.ComponentType | React.ReactNode;
title?: string;
views?: (S: StructureBuilder) => (View | ViewBuilder)[];
};Contextual Lists Without Duplicate Entries
Let one document own a custom hierarchy and omit child types from the autogenerated navigation:
import {defineConfig, defineType} from "sanity";
import {documentOptions} from "@tinloof/sanity-document-options";
import {withExtends} from "@tinloof/sanity-extends";
const framework = defineType({
name: "framework",
type: "document",
fields: [{name: "title", type: "string"}],
options: {
structureOptions: (S) =>
S.listItem()
.title("Collections")
.child(
S.documentTypeList("framework").child((frameworkId) =>
S.documentTypeList("article")
.title("Articles")
.filter('_type == "article" && framework._ref == $frameworkId')
.params({frameworkId})
.initialValueTemplates([
S.initialValueTemplateItem("article-in-framework", {frameworkId}),
]),
),
),
},
});
const article = defineType({
name: "article",
type: "document",
fields: [
{name: "title", type: "string"},
{name: "framework", type: "reference", to: [{type: "framework"}]},
],
options: {
structureOptions: false,
schema: {
templates: [{
id: "article-in-framework",
title: "Article in framework",
schemaType: "article",
parameters: [{name: "frameworkId", type: "string"}],
value: ({frameworkId}: {frameworkId: string}) => ({
framework: {_type: "reference", _ref: frameworkId},
}),
}],
},
},
});
export default defineConfig({
projectId: "your-project-id",
dataset: "production",
schema: {types: withExtends([framework, article])},
plugins: [documentOptions({structure: {keepPanesOnCreate: true}})],
});structureOptions: false hides only the autogenerated entry. Articles remain available in the filtered list, references, templates, and creation menus. Hidden-only groups disappear. Existing singleton restrictions still apply. Use structure.hide when you need to control visibility centrally.
keepPanesOnCreate is forwarded to Sanity without a default override. Explicit false is preserved; omission uses Sanity's default. Your custom pane must offer the matching create action or initial value template for Sanity to retain its hierarchy.
Use an array for nested groups, for example structureGroup: ["content", "articles"]. Hidden-only groups and their empty ancestors are omitted.
Choose the visibility control that matches its scope:
documentOptions({structure: false})disables the generated structure tool but keeps document actions, badges, templates, and abstracts.documentOptions({structure: {hide: [...]}})centrally omits named types from the generated structure.- A schema's
options.structureOptions: falselocally omits only that type's generated entry, which is useful when another schema callback owns its navigation.
License
MIT © Tinloof
