// Re-usable component typed against every registered query. Relies on
// `overloadClientMethods` being enabled in `sanity.cli.ts#typegen` (the default).
import {
PortableText,
type InferStrictComponents,
type InferValue,
type SanityQueries,
} from 'next-sanity'
type PortableTextValue = InferValue<SanityQueries[keyof SanityQueries]>
export function CustomPortableText(props: {value: PortableTextValue}) {
const components = {
types: {
// custom types are autocompleted and fully typed
},
} satisfies InferStrictComponents<PortableTextValue>
return <PortableText components={components} value={props.value} />
}
Infer the Portable Text array value type from Sanity TypeGen generated types.
Stega-aware version of
InferValuefrom@portabletext/react: the inferred value type accepts both clean query results (sanityFetchwithstega: false) and stega-branded results (sanityFetchwhen stega may be enabled). Don't clean the value withstegaClean()before rendering it, that strips the hidden characters@sanity/visual-editinguses to make each paragraph clickable. Instead, usestegaCleanon individual strings at the point where they're compared against literals.Useful when building a re-usable wrapper component that only takes
valueas an input prop and sets upcomponentsinternally. Pass a TypeGen- generated query result type that contains Portable Text fields - such as an individual query result likePostQueryResult, or theSanityQueriesinterface from@sanity/client- andInferValue<T>returns an array type containing every Portable Text item shape it can find.Always feed
InferValue<T>query result types, not Sanity schema types. Schema types describe how content is stored, which can differ from how it is queried (e.g. references resolved with->).