Developer guides

Change the height of a Portable Text Editor (PTE) using a custom input component

Reduce the height of a Portable Text Editor and add a character counter with a custom input component.

Learn how to reduce the height and add a character counter based on a maximum character length set in the schema.

Loading...
A Portable Text Editor with a reduced height

Sometimes, you need a Portable Text Editor (PTE) but want it to take up less space in your document form.

In this example, the PTE can only use decorators, and you need to keep track of the character count. Custom input components make this possible.

Create the schema type

The overview type uses the CharacterCountInputPTE input component, which you create in "Create the custom input component."

Use the type in a field

// * * * Title * * *
    defineField({
      name: 'title',
      type: 'overview',
      description: 'This will be used as the H2 of the Sections. Short and on point – max. 200',
      // overrides the 280-character limit set on the overview type.
      // Don't set `validation` here: it replaces the type's character-limit rule.
      options: { maxLength: 200 },
    }, { strict: false }),

Create the custom input component

The input component reads the maxLength value from the field's options and shows editors how many characters they've used.

The component also wraps the output of renderDefault in a container that sets the height of this specific PTE while keeping it resizable, using styled-components. The selector targets the editor's data-testid attribute, which isn't a public API and can change between Studio releases.

initialActive set to true lets editors focus the PTE and start writing without activating it first. In Sanity Studio v3.83.0 and later, this is the default behavior, so the setting only matters on earlier versions.

You now have a compact PTE that shows a character count and enforces a character limit. To customize the editor further, see Customize the Portable Text Editor and Form components.

Was this page helpful?