Limit block rows or alternative to block for single decorator?

7 replies
Last updated: Feb 8, 2021
Hi! I have a block with one decorator - it's only used to set part of the text as pink. Is there a way of limiting rows of block same as we can do with type 'text'? It won't ever be a long text and at the moment it takes a few rows. Or maybe there is a better way than using a block if I need only one decorator?
AI Update

Based on the information I found, there is no built-in rows option for Portable Text (block) fields like there is for simple text fields. This is a known limitation that has been requested by the community.

However, there are a couple of approaches you can take:

CSS Workaround

You can limit the height of the Portable Text editor using CSS overrides. According to a community discussion, you can add this to your Studio styling overrides:

:global([class^="PortableTextInput_editorBoxContent"]) {
  height: 8rem;
}

Note: This approach will affect all block content fields in your Studio. To target a specific field, you'd need to find a way to add a custom class or use more specific CSS selectors based on the field's position in the DOM.

Consider Using a Simple Text Field

Since you mentioned:

  • You only need one decorator (for pink text)
  • It will never be a long text

You might want to reconsider whether you actually need a full Portable Text block. If you only need basic inline styling and no other block features (like headings, lists, custom blocks, etc.), you could use:

  1. A text field with rows option - This gives you the height control you want
  2. A string field - For single-line text

The downside is you'd lose the ability to apply that pink decorator inline. However, you could potentially:

  • Use a separate boolean field to indicate if the text should be styled pink
  • Create a custom input component that allows for simple inline styling while maintaining height control

Better Alternative: Custom Input Component

Since you only need one decorator, you might consider creating a custom input component that:

  • Provides a simple text input with the rows attribute
  • Adds a button or keyboard shortcut to wrap selected text in a custom mark
  • Stores the data in a simpler format than full Portable Text

This would give you better control over the UI while keeping the data structure simpler for your specific use case.

Feature Request

The ability to control Portable Text editor height (or have a "single-line" block variant) has been requested multiple times in the community. Consider upvoting or creating a feature request on Sanity's feedback channels if this is important for your use case.

Bottom line: The CSS workaround is currently your best option if you need to stick with Portable Text, but given your simple requirements (one decorator, short text), you might want to explore using a regular text field with rows or creating a custom input component that better fits your specific needs.

Show original thread
7 replies
Hi User. I'm curious what you mean by "limiting rows" and "it takes a few rows." If you don't mind, could you please show what you're getting and describing what you want instead? Thanks!
Sorry for not explaining properly. I mean the size of this field.In type text we have "
rows -Controls the number of rows/lines in the rendered textarea. Default number of rows: 
10
."
I'm using https://www.sanity.io/docs/block-type and wondered if there is a way of controlling the amount of these rows/lines. ?The use case in here is I can have text like this "this is my
pinkword text", that's why I'm using the block type as you can see on the screen. But the text won't ever be very long, as it's mostly used for section titles.
      title: "Text Field",
      name: "text",
      type: "array",
      description: "Use the P to mark part of text as pink",
      of: [
        {
          type: "block",
          lists: [],
          marks: {
            decorators: [pinkDecorator],
            annotations: [],
          },
        },
      ],

It's purely about how much space this text field takes in the sanity.Like in here in case of text type:

    {
      title: "Text",
      name: "text",
      type: "text",
      rows: 2,
    },
Thanks User! I appreciate you helping me understand. I’m afraid I’m not aware of a way to control the number of rows for the Portable Text editor. It may be complicated by the fact that it is expandable (to fullscreen). You could put it in a collapsible object if you want to keep it out of sight.
I see. Thank you for your time! 🙂

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?