Helpers
The Sanity Structure Tool provides a set of helper functions that are strictly typed to your project's specific configuration.
These helpers are generated by a factory function (structureToolPlugin), ensuring that your custom roles and workspaces are fully available via IntelliSense and validated at compile-time.
The Factory Pattern
When you initialize the plugin, you receive a helpers object that is pre-bound to your types, which you then use to define your structure list items:
import { structureToolPlugin } from 'sanity-plugin-structure-tool';
export const { helpers } = structureToolPlugin({
// 1. Define your project-specific roles and workspaces
roles: ['administrator', 'editor', 'viewer'],
workspaces: ['production', 'staging'],
});helpers.listing('post', {
// Roles and Workspaces are now strictly typed!
roles: ['administrator'],
workspaces: ['production'],
});helpers.children
- Shorthand Signature:
(title: string, children: ListItem[], params?: CoreParams) => ChildrenOutput - Object Signature:
(params: ChildrenParams) => ChildrenOutput - Examples: See Examples
Creates a nested list structure (folder).
{
title: 'Settings',
children: [
{
schemaType: 'generalSettings',
singleton: true,
},
{
schemaType: 'apiSettings',
singleton: true,
},
],
}helpers.children('Settings', [
helpers.singleton('generalSettings'),
helpers.singleton('apiSettings'),
]);helpers.children({
title: 'Settings',
children: [helpers.singleton('generalSettings'), helpers.singleton('apiSettings')],
});helpers.component
- Shorthand Signature:
(title: string, component: ComponentType, params?: CoreParams) => ComponentOutput - Object Signature:
(params: ComponentParams) => ComponentOutput - Examples: See Examples
Renders a custom React component as the pane content.
{
title: 'Dashboard',
component: MyDashboard,
}helpers.component('Dashboard', MyDashboard);helpers.component({
title: 'Dashboard',
component: MyDashboard,
});helpers.divider
- Shorthand Signature:
(title?: string, params?: CoreParams) => DividerOutput - Object Signature:
(params?: DividerParams) => DividerOutput - Examples: See Examples
Renders a visual separator in the desk list.
{
title: 'Content Section',
isDivider: true,
}helpers.divider('Content Section');helpers.divider({
title: 'Content Section',
});helpers.filters
- Type:
(params: FiltersParams) => FiltersOutput - Examples: See Examples
Creates a filtered list (e.g., "Drafts", "Published").
{
title: 'Published Posts',
filter: '_type == "post" && !(_id in path("drafts.**"))',
}helpers.filters({
title: 'Published Posts',
filter: '_type == "post" && !(_id in path("drafts.**"))',
});helpers.listing
- Shorthand Signature:
(schemaType: string, params?: CoreParams) => ListingOutput - Object Signature:
(params: ListingParams) => ListingOutput - Examples: See Examples
Used to define a standard document list.
{
title: 'All Authors',
schemaType: 'author',
icon: UserIcon,
}helpers.listing('author', {
title: 'All Authors',
});helpers.listing({
title: 'All Authors',
schemaType: 'author',
icon: UserIcon,
});helpers.raw
- Shorthand Signature:
(raw: (S: StructureBuilder, context: any) => any, params?: CoreParams) => RawOutput - Object Signature:
(params: RawParams) => RawOutput - Examples: See Examples
The "Escape Hatch" to use the native Sanity Structure Builder API.
{
raw: (S) => S.listItem().title('Advanced').child(...),
}helpers.raw((S) => S.listItem().title('Advanced').child(...))helpers.raw({
raw: (S) => S.listItem().title('Advanced').child(...)
})helpers.singleton
- Shorthand Signature:
(schemaType: string, params?: CoreParams) => SingletonOutput - Object Signature:
(params: SingletonParams) => SingletonOutput - Examples: See Examples
Used for singleton documents (documents that only have one instance).
{
title: 'Global Settings',
schemaType: 'settings',
singleton: true,
}helpers.singleton('settings', {
title: 'Global Settings',
});helpers.singleton({
title: 'Global Settings',
schemaType: 'settings',
});Why a Factory Pattern?
By using a factory function (structureToolPlugin) to generate your helpers, we ensure that:
- Context is preserved: Helpers are pre-bound and know the specific "shape" of your structure.
- No generic "string" types: Roles and Workspaces are treated as literal unions, not just broad strings.
- Refactoring is easy: If you change a role name in your configuration, TypeScript will immediately highlight all the helpers that need to be updated.
Usage via Callbacks (No Imports)
If you do not want to import the generated helpers object in every structure file, both defineListItems and defineListItem support a callback function that passes the bound helpers as an argument. This is especially useful for modular structures:
import { defineListItems } from './index';
export default defineListItems(({ helpers }) => [
helpers.divider('General'),
helpers.listing('author'),
helpers.singleton('settings'),
]);Advantages of Helpers
Using the built-in helpers offers several key advantages:
- Readability: The syntax is often more expressive and closer to natural language.
- Type Safety: Every helper is strictly typed to ensure you provide the correct parameters for each item type.
- Type Intelligence: If you configured
rolesorworkspacesin your setup, they will be available as autocomplete options within the helper parameters. - Shorthand Syntax: Many helpers allow you to pass the
schemaTypeortitleas the first argument, reducing boilerplate.
