Warning
Deprecation notice
This repository has been archived and is no longer maintained. Module superseded by @kontent-ai/rich-text-resolver.
The package containing React components useful when processing Kontent.ai's data to the site.
npm install @kontent-ai/react-componentsComponents exports their typescript definitions so that you know what data format you need to provide via props and what data format expect from function prop callback arguments.
Rich text elements from Kontent.ai could be resolved to React components using html-react-parser (based on this article)
This package should make the usage easier. Basically by loading the rich text data and use these components to provide this data and resolver functions.
More showcases could be found in RichTextElement.spec.tsx.
import{createDeliveryClient,Elements}from'@kontent-ai/delivery-sdk';import{isComponent,isLinkedItem,RichTextElement}from'@kontent-ai/react-components';import{ElementasDomHandlerElement}from'domhandler';// ...constclient=createDeliveryClient({environmentId: '<YOUR ENVIRONMENT ID>'});constresponse=awaitclient.item("<YOUR ITEM CODENAME>")).toPromise();// ...<RichTextElementrichTextElement={response.item.elements["bio"]asElements.RichTextElement}resolvers={{resolveLinkedItem: (linkedItem,{ domElement, domToReact })=>{if(isComponent(domElement)){return(<><h1>Component</h1><pre>{JSON.stringify(linkedItem,undefined,2)}</pre>;
</>);}if(isLinkedItem(domElement)){return(<><h1>Linked item</h1><pre>{JSON.stringify(linkedItem,undefined,2)}</pre>;
</>);}thrownewError("Unknown type of the linked item's dom node");},resolveImage: (image,{ domElement, domToReact }): JSX.Element=>(<imgsrc={image.url}alt={image.description ? image.description : image.imageId}width="200"/>),resolveLink: (link,{ domElement, domToReact }): JSX.Element=>(<ahref={`/${link.type}/${link.urlSlug}`}>{domToReact(domElement.children)}</a>),resolveDomNode: ({ domNode, domToReact })=>{if(domNodeinstanceofDomHandlerElement&&domNode.name==='table'){return<divclassName="table-wrapper">{domToReact([domNode])}</div>;}}}}/>If you want to resolve multiple levels of components and linked items in rich text, it is possible to use the component recursively and reuse the resolving logic.
There is an example when rich text can have row components, and these can contains column components with html.
// resolving functionalityconstresolveLinkedItemsRecursively: ResolveLinkedItemType=(linkedItem,_domNode)=>{switch(linkedItem?.system.type){case"row":
return(<divclassName="row"><RichTextElementrichTextElement={linkedItem?.elements["columns"]asElements.RichTextElement}// Recursively resolve items in the rich textresolvers={{resolveLinkedItem: resolveLinkedItemsRecursively,}}/></div>);case"column":
return(<divclassName="column"><RichTextElementrichTextElement={linkedItem?.elements["content"]asElements.RichTextElement}resolvers={{resolveLinkedItem: resolveLinkedItemsRecursively,}}/></div>);}};// SO the top level rich text would define<RichTextElementrichTextElement={multiLevelComponentsRichTextItem.item.elements["content"]asElements.RichTextElement}resolvers={{resolveLinkedItem: resolveLinkedItemsRecursively,}}/>;⚠ Recursive resolution could lead to infinite loops, if you have a circular dependency. To avoid that, you can store the codenames of already processed items and if you hit the same one during resolution, break the resolution chain - this could happen only if you use linked items, not components in rich text.
By returning the react components in any of the resolvers functions, you stop traversing the DOM tree under the current DOM node (its children). If you just want to avoid that behavior, you can mutate the provided DOM node and return undefined.
In this showcase a simple html is being resolved and for <p> tags and all <strong> tags a proper class is being set without stopping and traversing.
<RichTextElementrichTextElement={{
...emptyRichText,value: "<p>Lorem ipsum with <strong>bold text</strong></p>",}}resolvers={{resolveDomNode: ({ domNode, domToReact })=>{if(domNodeinstanceofDomHandlerElement){if(domNode.name==="strong"){domNode.attribs.class=domNode.attribs.class
? domNode.attribs.class+" strongClass"
: "strongClass";returnundefined;}elseif(domNode.name==="p"){domNode.attribs.class=domNode.attribs.class
? domNode.attribs.class+" pClass"
: "pClass";returnundefined;}}},}}/>The outcome is
<pclassName="pClass">
Lorem ipsum with
<strongclassName="strongClass"> bold text </strong></p>If you have any feedback, feel free to submit an issue or open a PR!