Tips for debugging YouTube thumbnail preview in portable content editor
When debugging form.component.preview for a YouTube thumbnail in Portable Text, the most common issue is that your custom preview component completely replaces the default UI, hiding the input fields and edit controls.
The key solution is to use the renderDefault function that's passed to your preview component. This renders the default preview UI, which includes the edit controls and field labels. Here's how to fix it:
const YouTubePreview = (props) => {
const { renderDefault, title } = props;
const url = title?.split(" ")[1];
const id = getYouTubeID(url);
const opts = {
height: "183",
width: "300",
};
return (
<div>
{renderDefault(props)}
<YTPreview opts={opts} videoId={id} />
</div>
);
};The critical changes are:
- Accept the full
propsobject (which includesrenderDefault) - Call
renderDefault(props)to render the default preview UI with all its controls - Add your custom preview below it
This pattern is documented in the Form Components API section on renderDefault. The renderDefault function preserves all the built-in functionality like the edit button (the "..." menu), field labels, and double-click-to-edit behavior, while still letting you add your custom YouTube preview.
Without renderDefault, your custom component completely replaces the default UI, making it difficult for editors to access the input fields. You might notice they can only edit by making the preview very small and double-clicking, which isn't intuitive. By including renderDefault, you get the best of both worlds: your custom preview plus all the standard editing controls.
Additional debugging tips:
- Make sure to handle cases where
titlemight be undefined or empty - Consider using optional chaining (
title?.split()) to prevent errors - The
renderDefaultpattern works for any custom component in Sanity Studio (field, input, item, or preview components) - You can wrap
renderDefaultin a container to add custom styling around the default UI
This same pattern applies whenever you want to enhance rather than completely replace Sanity's default form components!
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.