How to add table headers in sanity-table plugin?

12 replies
Last updated: Oct 5, 2023
Is there a way to have this table plugin (https://www.sanity.io/plugins/sanity-table ) have table headers? Or are we supposed to on our end just assume the first row is the table headers?
AI Update

The @sanity/table plugin doesn't have built-in support for designated table headers with <thead> and <th> elements. By default, it stores table data as rows and cells without distinguishing headers from regular content.

The common approach is to treat the first row as headers when rendering the table on your frontend. This is exactly what you'd need to handle yourself.

Here's how you can render the table with proper <thead> and <th> support for accessibility when using the @sanity/table plugin with @portabletext/react:

function BlockTable(props) {
  const [head, ...rows] = props.value.rows
  const isBiDirectional = head.cells[0].length === 0

  return (
    <table>
      <thead>
        <tr>
          {head.cells.map(cell => (
            <th key={cell}>{cell}</th>
          ))}
        </tr>
      </thead>
      <tbody>
        {rows.map((row, index) => (
          <tr key={index}>
            {row.cells.map((cell, index) => {
              const Component = isBiDirectional && index === 0 ? 'th' : 'td'
              return <Component key={index}>{cell}</Component>
            })}
          </tr>
        ))}
      </tbody>
    </table>
  )
}

Then add this to your PortableText components:

const components = {
  types: {
    table: BlockTable,
    // other types...
  },
}

<PortableText components={components} value={_rawField} />

This approach:

  • Treats the first row as headers (<thead> with <th> elements)
  • Handles bi-directional tables (where the first column might also be headers)
  • Makes the table accessible for screen readers

As noted in this community discussion, proper header support is important for accessibility, so make sure you're using <th> elements rather than just <td> for all cells.

Show original thread
12 replies
I don’t believe you can set up headers with that bad boy.
😭 dang yeah I’ve been looking for a table that allows that and allows images in the cells
Trying to recreate this
If there was something that did that, plus rich text inside of the cells it would be :chefs-kiss:. It could probably end up being an attribute nightmare, though.
For what you shared, you probably just have to assume that the first row contains the headers
😞
Yeah I’m also wondering if it would be better to just have like an array of voters and then just render accordingly?
That’s what I tend to go with. I don’t use the table plugin, instead I create an array of objects then handle rendering it as a table on my FE if I need to. Some editors can find that confusing, though, and need the table visual when they’re in the PTE.
But I have the preview tab in my sanity studio so they should be able to just go to that and see it if they need to
Nice. You’re all set then 😎
So I ended up figuring it all out
This is what it looks like
No problem!
Dope. Thanks for sharing!

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?