Using onChange and PatchEvent with custom-asset-sources in Sanity
The short answer is that custom asset sources don't receive onChange or PatchEvent props - they work differently than custom input components.
How Custom Asset Sources Work
According to the Asset Source API documentation, custom asset sources are designed specifically for selecting and uploading assets. They receive a different set of props focused on asset selection:
Asset source selection component props:
onSelect(Asset[])- Called when assets are selected. The Studio automatically handles uploading the asset(s)onClose()- Called when the selection dialog is closedselectionType- Either 'single' or 'multiple'selectedAssets- Array of currently selected Sanity asset documentsassetType- Either 'file' or 'image'dialogHeaderTitle- Optional header component
Why You Can't Update Other Document Fields
Custom asset sources are scoped to asset selection only. They don't have access to the document's form context or onChange handlers because they operate at a different level - they're specifically for browsing and selecting assets from external sources, not for modifying document fields.
Alternative Approaches
If you need to capture additional metadata (like a caption) when selecting an asset, you have a few options:
Use the asset's built-in metadata fields - Sanity assets support fields like
title,description,source,originalFilename, and custom metadata that can be set on the asset document itself.Custom input component instead - If you need to update multiple fields simultaneously, you should create a custom input component rather than a custom asset source. Custom input components do receive
onChangeand can usePatchEventto update multiple fields, including using real-time safe patches with absolute paths.Post-selection handling - Handle the additional data collection after the asset is selected, perhaps through document actions or a custom form component that wraps the image field.
Example Reference
The community thread you found mentioned looking at the OG Image creator plugin as a reference. While this plugin shows asset source patterns, it's also limited to the onSelect interface for returning assets.
If you need to modify other parts of the document when an asset is selected, you'll likely need to rethink your approach - either by using custom input components that have full form access, or by handling the additional data separately from the asset selection flow.
Show original thread4 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.