Update custom document badge with custom document action

2 replies
Last updated: Feb 5, 2024
Is it possible to update a custom document badge with a custom document action?I can’t seem to query for the badges on a document…
AI Update

No, you cannot directly update or query custom document badges from a custom document action. This is an important architectural limitation to understand.

Why This Limitation Exists

Document badges exist purely in the Studio UI layer and are not persisted as part of the actual document data. According to the Document Badges documentation, badge values are not written to the document itself - they're computed and displayed on-the-fly based on the document's current state.

What Document Badges Can Access

When you create a custom badge, it receives these properties:

  • id - The document ID
  • type - The schema type
  • draft - The draft document (if any)
  • published - The published document (if any)

Badges read from the document state and display information, but they don't store queryable data themselves.

Alternative Approaches

If you need to track status or metadata that both document actions and badges can access, consider these patterns:

  1. Store status in the document itself - Add a field to your schema (like status or workflowState) that both your custom action and badge can read from
  2. Use document actions to update document fields - Your custom action can modify document fields using the useDocumentOperation hook
  3. Have badges display document field values - Your badge can read from those fields and display them visually

Here's a simple example pattern:

// In your schema
{
  name: 'myDocument',
  type: 'document',
  fields: [
    {
      name: 'status',
      type: 'string',
      options: {
        list: ['draft', 'review', 'approved']
      }
    }
  ]
}

// Custom badge reads the status
export function StatusBadge(props) {
  const status = props.published?.status || props.draft?.status
  return {
    label: status?.toUpperCase(),
    color: status === 'approved' ? 'success' : 'default'
  }
}

// Custom action updates the status
export function ApproveAction(props) {
  // Use useDocumentOperation to patch the document
  // and update the status field
}

This way, your document action modifies the actual document data, and your badge displays that data - creating a workflow where both features work together through the document's content rather than trying to communicate directly.

Show original thread
2 replies
The value of a badge doesn’t get written to your actual data. It exists only in the Studio UI.
Yeahhh this is what I ended up assuming here, thank you for validating

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?