Can Form Component access parent component props/data in Sanity?
Yes! You can access parent field data in your Form Component using the useFormValue hook. Since your colorPalette field is nested inside the image field, you can access the parent image data (including its palette metadata) by using the appropriate path.
Here's how to do it:
import { useFormValue } from 'sanity'
import { StringInputProps } from 'sanity'
function ColorPaletteInput(props: StringInputProps) {
const { renderDefault } = props
// Get the parent image object (including asset with palette metadata)
const imageValue = useFormValue(['image'])
// Access the currently selected color palette option
const selectedPalette = props.value
// Extract the color from the image's palette metadata
const paletteColor = imageValue?.asset?.metadata?.palette?.[selectedPalette]?.background
return (
<div style={{ display: 'flex', alignItems: 'center', gap: '8px' }}>
{paletteColor && (
<div
style={{
width: '24px',
height: '24px',
borderRadius: '4px',
backgroundColor: paletteColor,
border: '1px solid #ccc'
}}
/>
)}
{renderDefault(props)}
</div>
)
}
// In your schema:
defineField({
title: "Colour palette",
name: "colorPalette",
type: "string",
components: {
input: ColorPaletteInput
},
options: {
list: [
{ title: "Dominant", value: "dominant" },
{ title: "Vibrant", value: "vibrant" },
// ... rest of your options
]
}
})The key insight is that useFormValue accepts a path array to access any field in the current document. Since you're inside a nested field, using ['image'] will give you access to the entire parent image object, including its asset.metadata.palette data.
The image palette metadata contains properties like background, foreground, population, and title for each color swatch (dominant, vibrant, lightVibrant, darkVibrant, muted, lightMuted, darkMuted).
Note that in Studio v3, accessing parent values requires useFormValue rather than the old v2 pattern of using props.parent or props.document.
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.