Studio

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.

Loading...
An example of a block field with a rich block text editor.

To activate the Portable Text editor, define an array of blocks. In the schema, it looks like this in its simplest form:

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

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 title and a value, for example styles: [{title: 'Quote', value: 'blockquote'}]. The defaults are Normal, Heading 1 through Heading 6, and Quote, with the values normal, h1 through h6 and blockquote. The normal style is always included: it is prepended if you leave it out, so styles: [] 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 a title and a value, for example {title: 'Bullet', value: 'bullet'}. The value is required. The defaults are bullet and number. Setting lists: [] removes list support entirely.

  • marks

    object

    An object defining which decorators and annotations are 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 single link annotation whose href field is of type url. Decorator values are stored as plain strings on a span, while annotations are stored in the block’s markDefs array 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 Options below.

  • 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 rules for this block. See Validation below.

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 to false.

  • 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

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 href with type url

Input

Response

Example: block array with custom types

This defines a block array that can include both text, actors, and (inline) images.

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:

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

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

Protip

Was this page helpful?