Block
Schema type for block which provides a rich text editor for block content.
The block type is the basis for Sanity's Portable Text editor. See the BlockDefinition reference for the full type definition.
To activate the Portable Text editor, define an array of blocks. In the schema, it looks like this in its simplest form:
{
title: 'Content',
name: 'content',
type: 'array',
of: [{type: 'block'}]
}In other words, rich text is modeled as an array of content following the specification for Portable Text. What is stored in the database is an array of JSON objects describing the rich text content. This JSON data can later be used to produce HTML, React components, or other formats depending on the target requirements. This provides a lot of flexibility if you should later want to re-use your content across the web, apps, print, set-top-boxes, consoles, etc.
The block type supports block styles, lists, decorators (bold, italic, and so on), custom content types (embedded objects), inline objects, and marking up text with arbitrary object data (annotations). Learn more about how to configure the Portable Text editor.
Gotcha
You can't currently use block as a standalone field outside of an array.
Properties
A block declaration accepts only the properties in this table. Any other key makes the schema invalid with the error Found unknown properties for block declaration. Notably, icon, description, hidden, readOnly, deprecated and initialValue are not supported on a block, even though TypeScript accepts several of them. Set those on the enclosing array field instead.
Declaring styles, lists, marks.decorators or marks.annotations replaces that default set rather than extending it, so any built-in you leave out is gone. Each defaults independently: if you declare marks.decorators and omit marks.annotations, the default link annotation is still there, and the reverse is also true.
Requiredtype
Value must be set to
block. Also, blocks only make sense as member of an array, see examples below.Requiredname
Required. The field name. This will be the key in the data record.
Human readable label for the field.
styles
array
The block styles available in the style dropdown. Each style is an object with a
titleand avalue, for examplestyles: [{title: 'Quote', value: 'blockquote'}]. The defaults are Normal, Heading 1 through Heading 6, and Quote, with the valuesnormal,h1throughh6andblockquote. Thenormalstyle is always included: it is prepended if you leave it out, sostyles: []gives you Normal on its own rather than no styles at all.lists
array
The list types that can be applied to blocks. Like
styles, each entry is an object with atitleand avalue, for example{title: 'Bullet', value: 'bullet'}. Thevalueis required. The defaults arebulletandnumber. Settinglists: []removes list support entirely.marks
object
An object defining which
decoratorsandannotationsare allowed. Decorators are the simple on/off marks, and default to Strong, Italic, Code, Underline and Strike. Annotations are object types that carry data, and default to a singlelinkannotation whosehreffield is of typeurl. Decorator values are stored as plain strings on a span, while annotations are stored in the block’smarkDefsarray and referenced from the span by key. See Span for the stored shape.An array of inline content types that you can place in running text from the Insert menu.
options
object
Editor options for this block. See
Optionsbelow.Lets you provide custom components to override the studio defaults in various contexts. This type takes a single key,
block, which replaces the component that renders a text block. It accepts none of the other component keys, and it applies to the block text type only, not to block object types such as images.validation
array
Validation rules for this block. See
Validationbelow.
Options
Set these under the block’s options key. See BlockOptions for the full type definition.
oneLine
boolean
Restricts the Portable Text input to a single line when set to
true. Line breaks and soft breaks are blocked, pasted multi-line content is normalized to one line where possible, and the input is styled to match. Defaults tofalse.spellCheck
boolean
Enables or disables the browser’s built-in spellchecking in the Portable Text editor. Defaults to
true.
Validation
Build these with the rule builder passed to validation. See BlockRule for the full type definition.
Ensures that this field exists.
Discards the validation rules set before it in the chain and makes the field optional. Rules chained after it still apply.
Creates a custom validation rule.
Sets a custom error message for the preceding validation rule.
Sets a custom warning message for the preceding validation rule. Warnings do not prevent publishing.
Sets a custom info message for the preceding validation rule. Info messages are purely informational and do not prevent publishing.
Gets the value of a sibling field to use in validation. Useful for creating validation rules that depend on the value of another field.
Gotcha
A block represents a single paragraph. To make sense, your blocks must live inside an array.
Example: default block array
With no custom configuration, the Portable Text editor supports:
- Block styles: Normal, Heading 1 to Heading 6, and blockquotes
Decorators: Strong, Italic, Code, Underline and Strike, with the values strong, em, code, underline and strike-through
- Lists: bullet list and ordered list
- Link: An annotation that is an object with a
hrefwith typeurl
Input
{
title: 'Rich text example',
name: 'myRichTextExample',
type: 'array',
of: [{type: 'block'}]
}Response
{
"myRichTextExample": [
{
"_key": "9d2d1ed68d84",
"_type": "block",
"style": "normal",
"children": [
{
"_key": "a1f4c2d90b31",
"_type": "span",
"marks": [],
"text": "I am "
},
{
"_key": "b7e5d3a81c42",
"_type": "span",
"marks": [
"strong"
],
"text": "strong and "
},
{
"_key": "c8f6e4b92d53",
"_type": "span",
"marks": [
"strong",
"cbe9d12c6af9"
],
"text": "annotated"
}
],
"markDefs": [
{
"_key": "cbe9d12c6af9",
"_type": "link",
"href": "https://example.com/annotations"
}
]
},
{
"_key": "4b0e7a2c8d15",
"_type": "block",
"style": "normal",
"markDefs": [],
"children": [
{
"_key": "e2c9f1a63b74",
"_type": "span",
"marks": [],
"text": "Amazing, actually."
}
]
}
]
}Example: block array with custom types
This defines a block array that can include both text, actors, and (inline) images.
{
title: 'Rich text',
type: 'array',
of: [
{type: 'block'},
{type: 'actor'},
{type: 'image', icon: myIcon}
]
}The editor will now get an insertion (+) icon in the text editor that can be used to insert actors or images as content blocks in the text. The data stored in the array for these objects are exactly as if they were in a regular array of objects, because they are.
These objects are embedded on the block level, but you may also need objects that appear inline with text useful for stuff like footnotes, ticker-symbols or sparklines. Add these to an array under the of key in the block type object:
{
title: 'Rich text',
type: 'array',
of: [
{
type: 'block',
of: [
{type: 'footnote'}
]
}
]
}Customize styles, decorators, and annotations
Almost every aspect of the Portable Text editor and the content it produces is configurable. You may want to restrict certain decorators or add your own, use your own list styles, annotate text with custom data such as a citation or reference, or support highlighted text.
A style, decorator, list or annotation can take a component property, a callback that controls how that mark is rendered in the studio, and an icon property for its button in the editor toolbar. On the block itself, neither key is valid: use components (plural) for the block’s own render overrides.
Gotcha
Note that customizations made in the studio will not affect how content is rendered elsewhere, such as your front end. That gets handled via portable text serialization.
{
name: 'customized',
title: 'Customized block type',
type: 'array',
of: [
{
type: 'block',
// ...
marks: {
decorators: [
{ title: "Strong", value: "strong" },
{ title: "Emphasis", value: "em" },
{
title: "Sup",
value: "sup",
icon: () => <div>x<sup>2</sup></div>,
component: ({ children }) => <sup>{children}</sup>
},
],
},
// ...
}
]
}This declares decorators only, so it replaces the five default decorators with these three and drops Code, Underline and Strike. Because annotations is not declared, the default link annotation is untouched and still available in the editor.
Example: restricting styles, lists, and marks
{
name: 'customized',
title: 'Customized block type',
type: 'array',
of: [
{
type: 'block',
// Only allow these block styles
styles: [
{title: 'Normal', value: 'normal'},
{title: 'H1', value: 'h1'},
{title: 'H2', value: 'h2'}
],
// Only allow numbered lists
lists: [
{title: 'Numbered', value: 'number'}
],
marks: {
// Only allow these decorators
decorators: [
{title: 'Strong', value: 'strong'},
{title: 'Emphasis', value: 'em'}
],
// Support annotating text with a reference to an author
annotations: [
{name: 'author', title: 'Author', type: 'reference', to: {type: 'author'}}
]
}
}
]
}Protip
Looking to query for the occurence of a string in an array of blocks? Try *[pt::text(body) match "aliens"] (where body is the name of your array).
Block Content
Block content allows you to create a rich text experience tailored to the needs of your content.
Configure the Portable Text Editor
Configure the Portable Text Editor: styles, lists, decorators, annotations, custom blocks, tables, and the built-in Markdown and typography behaviors.
Array
Schema type for arrays of other types.
Span
Schema type reference for the Span type.
Schema
A schema describes the types of documents and fields editors may author in a Sanity Studio workspace.
