Disable normal text or set heading 2 as default in Sanity rich text editor
You can't remove the Normal style. Studio always includes normal in a block's styles, and if you leave it out of styles the editor adds it back at the front. (styles: [] gives you Normal only.) New blocks therefore always start as normal, and you can't make H2 the default style through config. What you can do:
- Make Normal look and act like your heading in Studio.
- Render
normalas an<h2>on the frontend. - Optionally seed the first block or validate the style.
1. Restyle Normal in Studio
Style entries take a component that controls how that style looks in the editor (docs). Relabel Normal and give it heading styling. With only one style, editors never need to pick one:
import {defineType} from 'sanity'
const HeadingStyle = (props: any) => (
<span style={{fontSize: '1.5em', fontWeight: 'bold'}}>{props.children}</span>
)
export default defineType({
name: 'headingText',
title: 'Heading',
type: 'array',
of: [
{
type: 'block',
styles: [{title: 'Heading 2', value: 'normal', component: HeadingStyle}],
lists: [],
marks: {decorators: [], annotations: []},
},
],
})Setting styles, lists or marks replaces that default set rather than adding to it (configuration docs), and lists: [] turns lists off. In Studio v2 this was done with blockEditor: {render: ...} on the style entry. Current Studio uses component.
2. Render normal as an h2 on the frontend
Studio customization doesn't change how the content renders on your site, so map the style in your renderer. @sanity/block-content-to-react and its serializers are deprecated in favour of @portabletext/react. Use its PortableText component, and pass a components object that renders blocks with the normal style as <h2>. The @portabletext/react README (linked from that page) has the exact key for overriding block styles.
3. Optional: seed or enforce a style
If you need real h2 values stored, include h2 in styles and do one or both of these:
Seed the first block. initialValue isn't supported on the block member, so set it on the enclosing array (block type reference). This only sets the first block of a new document. Blocks added after it still start as normal.
defineType({
name: 'content',
type: 'array',
of: [{type: 'block', styles: [{title: 'Heading 2', value: 'h2'}]}],
initialValue: [
{_type: 'block', style: 'h2', children: [{_type: 'span', text: ''}]},
],
})Flag blocks left as Normal. Add custom validation on the block member (validation pattern):
{
type: 'block',
styles: [{title: 'Heading 2', value: 'h2'}],
validation: (Rule) =>
Rule.custom((value: any) =>
value?.style === 'normal' ? 'Choose a heading style for this block' : true,
),
}Use .warning() or .error() on the rule, depending on whether a block left as Normal should block publishing.
If the field only ever holds one line of heading text, a string or text field styled on the frontend may be simpler than a block array.
Show original thread8 replies
Was this answer helpful?
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.