How to reduce PortableText editor size in Sanity?

4 replies
Last updated: Nov 13, 2024
Is there any way to reduce the size of a Block content field? For a
text
field there's
rows
but there appears to be no such thing for a PortableText editor. It would help to free up some screen space, as well as provide a proper visual cue - an
intro
field is likely to be smaller than
body
in this case:
AI Update

Yes! There's absolutely a way to reduce the size of a Portable Text editor field. While there isn't a built-in rows option like there is for regular text fields, you can control the editor height using a custom input component.

The Solution: Custom Input Component

The approach involves creating a custom input component that wraps the default Portable Text editor and applies custom styling to reduce its height. Here's how to do it:

1. Create Your Custom Input Component

You'll need to create a custom input component that:

  • Uses styled-components (or your preferred CSS solution) to control the editor height
  • Wraps the default renderDefault output
  • Optionally sets initialActive: true so users can start typing immediately without clicking to activate

Here's a complete example from Sanity's documentation on reducing PTE height:

// CharacterCountInputPTE.tsx
import { Stack, Text } from '@sanity/ui'
import { PortableTextInputProps } from 'sanity'
import styled from 'styled-components'

export function CustomPTEInput(props: PortableTextInputProps) {
  return (
    <Container>
      {props.renderDefault({
        ...props,
        initialActive: true, // Removes need to activate the editor
      })}
    </Container>
  )
}

// Target the editor when not in fullscreen mode
const Container = styled.div`
  [data-testid='pt-editor'][data-fullscreen='false'] {
    height: 100px; /* Adjust to your needs */
  }
`

2. Apply It to Your Schema

Then reference this custom component in your schema field definition:

import { CustomPTEInput } from './path-to-your-component'

defineField({
  name: 'intro',
  type: 'array',
  title: 'Introduction',
  components: {
    input: CustomPTEInput,
  },
  of: [
    defineArrayMember({
      type: 'block',
      // ... your block configuration
    }),
  ],
})

Benefits of This Approach

  1. Visual hierarchy: Smaller fields like intro can be visually distinguished from larger fields like body
  2. Screen space: Saves valuable vertical space in your Studio forms
  3. Still resizable: The editor remains resizable by users if they need more space
  4. Better UX: Setting initialActive: true removes the extra click to start editing

Bonus: Add Character Counter

You can enhance this further by adding a character counter based on validation rules, which is particularly useful for meta descriptions or other length-constrained fields. The full guide shows how to extract the max value from your field validation and display it alongside a live character count.

This is a great example of how custom input components give you precise control over the editing experience in Sanity Studio!

Show original thread
4 replies
Hi
user P
, great points on screen real estate and visual cues. Unfortunately, it's not possible today to control the height of a block content field, but it's an important feature request. I've also seen it come up for a single-line block content field, e.g. to allow a few styling options on a short label text.
Here's a workaround, although it's not the prettiest. This would decrease the height on
all block content fields, but it might be possible to target one of them specifically [will have to look into that]:
:global([class^="PortableTextInput_editorBoxContent"]) {
  height: 8rem;
}
Thanks for your input! Would be great if we could have something like
className
as an option for any of the fields. That way we could target specific fields quite easily.
PS I presume I would have to set up the css like this then?
https://www.sanity.io/docs/styling#d7f9b5cc2adb
That's correct, it should be in the overrides. I should have specified 🙂 Will forward the
className
suggestion to the components team, thanks!
Has there been any action on this very popular request? (For simple rich title field for ex)

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?