Adding bold/italics to string field without full block editor
Your current approach is actually pretty solid! Using an array with a single block type and validation to limit it to one line is the standard way to add bold/italic formatting to a title field in Sanity. However, there are a few things you can do to make it feel less like overkill:
Making it more title-like
According to the block type documentation, you can use the oneLine option to restrict the Portable Text input to a single line:
{
name: 'title',
title: 'Title',
type: 'array',
of: [
{
type: 'block',
styles: [], // Remove style dropdown (no H1, H2, etc)
lists: [], // Remove list options
marks: {
decorators: [
{title: 'Strong', value: 'strong'},
{title: 'Emphasis', value: 'em'}
],
annotations: [] // Remove link option
},
options: {
oneLine: true // This prevents hitting return!
}
}
],
validation: Rule => Rule.required().max(1)
}The key improvements here:
oneLine: true- This disables the ability to hit return and keeps everything on a single linestyles: []- Removes the style dropdown (Normal, H1-H6, blockquote)lists: []- Removes bullet/numbered list optionsannotations: []- Removes the link annotation if you don't need it
Styling the editor height
Unfortunately, there's no built-in option to reduce the WYSIWYG height to match a string field exactly. The Portable Text Editor has a minimum height by default. However, you can use custom components to add custom CSS:
{
type: 'block',
// ... other config
components: {
input: (props) => (
<div style={{ minHeight: 'auto' }}>
{props.renderDefault(props)}
</div>
)
}
}This approach gives you inline formatting in what feels much more like a title field, while still being technically correct as Portable Text.
Show original thread3 replies
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.