Discussion on rendering PortableText with images and creating custom annotations for color in a React app using Sanity.
7 replies
Last updated: Apr 19, 2023
E
So, I'm trying to render a PortableText with images in a React application (using @portabletext/react and Sanity v3). I have followed the ReadMe on how to create a custom react component to display images, but the BlockContent returned from my query contains objects of images with a reference to the
In other queries I can do
This is the blog template without any big modifications.
image->asset->_refinstead of the expanded image url. How can I get the expanded image references directly from my GROQ query?
In other queries I can do
*[_type == "post"] { "image": image.asset->url }but I'm not sure how to do this for BlockContent.
This is the blog template without any big modifications.
Jan 21, 2023, 3:22 PM
E
My query
Jan 21, 2023, 3:26 PM
E
The data returned
Jan 21, 2023, 3:27 PM
E
Of course I find the answer immediately after asking the question π https://www.sanity.io/help/block-content-image-materializing
But then I have a follow-up question: The query suggested in the article includes the
But then I have a follow-up question: The query suggested in the article includes the
assetproperty for all blocks with
nullas value if the source block does not contain the
assetproperty. Is there a way to only include the "asset"-property in the result for blocks that definitely contains an
assetproperty?
Jan 21, 2023, 3:43 PM
E
Never mind, figured out that as well... Oh, the wonders of rubber ducking oneself.... π€£
Jan 21, 2023, 3:51 PM
S
This is delightful to read π If you want some π
for your portable text, you should look at these:β’
inline blocks β’
custom annotations and more
inline blocks β’
custom annotations and more
Jan 23, 2023, 3:44 PM
E
user J
Thanks for those links! π I already made a youtube embed following one of the guides on the website. Pretty cool! Didn't know you could have those kind of inline blocks as wellJan 25, 2023, 8:47 AM
A
Nice ones, I had really hoped for an complete example of how to create an annotation component for color because I can not get it to work render the selected color π
the custom color picker stores the color in correct way, like
My custom TextColor component set to components: { annotation: TextColor } in Block schema
console.log, where we see that it actually pickup selected color.
renders in portableText UI
But the .erPoJB class comes from built in <Annotation> with below css and I have no idea how to solve this. , to not render dashed bottom border nor setting the color
π
for reference, βmyβ .hSVqgX got below css:
The portableBlock component in the studio looks like first screen shot
Could you please give me a hint how to render the texts colors in the βRTEβ?
(In V2 we just used the blockedEditor in the block content schema, very simple and just worked
π
)
the custom color picker stores the color in correct way, like
"_type": "block", "children": [ { "_key": "6dce5ea4c2d4", "_type": "span", "marks": [ "021bc167e2d2" ], "text": "I want to set color on this text!" } ], "markDefs": [ { "_key": "021bc167e2d2", "_type": "color", "color": "rgb(0, 113, 119)" } ],
const InlineAnnotation = styled.span` border: none; ${(props) => props.textColor}; ` const TextColor = (props) => { const { value, renderDefault } = props const { color } = value console.log(value, color) return ( <InlineAnnotation className="MyCustomAnnotatianWhereColorIsSet" textColor={{ color }} > {renderDefault({ ...props })} </InlineAnnotation> ) }
{_key: '021bc167e2d2', _type: 'color', color: 'rgb(0, 113, 119)'} 'rgb(0, 113, 119)' {_key: 'a0f14ee6a1c6', _type: 'color', color: 'rgb(249, 93, 122)'} 'rgb(249, 93, 122)'
<span class="sc-dOrDzm hSVqgX MyCustomAnnotatianWhereColorIsSet"> <span class="sc-eyLAWx erPojB"> <span data-annotation=""> <span data-slate-string="true"> here is the next text for color! </span> </span> </span> </span>
π
{ -webkit-text-decoration: none; text-decoration: none; display: inline; background-color: #fff; border-bottom: 1px dashed #565d67; color: #565d67; }
border: none; color: rgb(249,93,122);
Could you please give me a hint how to render the texts colors in the βRTEβ?
user J
π(In V2 we just used the blockedEditor in the block content schema, very simple and just worked
π
blockEditor: { render: ({ color, children }) => { return ( <span style={{ color }}>{children}</span> ) }, },
Apr 19, 2023, 1:47 PM
Sanityβ build remarkable experiences at scale
The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.