Custom Rendering
The renderer API shipped since Foundry (v6) is at the same time more strict and more flexible. To get ready for this new API, you must understand some basics of the transient render tree produced by the βTRE:
During the transient render tree generation, every DOM node is translated to a β
TNode.β
TTextnodes correspond to DOM βTextnodes (anonymous βTTextnodes) or DOM elements which children are DOM βTextnodes (named βTTextnodes). So a βTTextnode cannot have children, and its content is a string accessible with the βdatafield.Thanks to hoisting, β
TPhrasingnodes can only have βTTextand βTPhrasingnodes as children.β
TBlocknodes can have any children.
tip
You are kindly advised to read βTransient Render Engine page before continuing!
Options for Custom Rendering#
You can customize rendering at two steps of the flow:
During βTRT generation. via HTML model definition.
At (React) render time via custom renderers.
These customizations are not exclusive, so you can use both approaches at the same time.
Model-based Custom Rendering#
Example: Registering a New Tag#
Let's say we have defined an advanced, powerful <blue-circle> Web Component for our website and we need to register a custom renderer for this tag. If we don't, the <blue-circle> elements will be translated to βTEmpty and won't be rendered.
important
We must register an element model for this tag because it is non-standard.
important
Custom tags in HTML markup must never be self-closing. The HTML parser will not recognize non-void self-closing tags by default which will lead to unexpected outcomes.
tip
We may register a custom component renderer, but this is not mandatory (see next chapter).
For a more detailed explanation of the allowed parameters for theβfromCustomModelstatic method, see βTransient Render Engine.
- JSX Source
- HTML Snippet
- TRT Snapshot
Example: Displaying Inline Images#
In the below example, we are changing the element model of theβ<img> tag to support inline rendering. For this purpose, we take advantage of the βcustomHTMLElementModels prop!
- JSX Source
- HTML Snippet
- TRT Snapshot
note
We used βextend method to change the content model for the β<img> tag. This method is immutable: it creates a new βHTMLElementModel instance. It is thus safe to use this method to create models for new tags derived from models of existing tags.
warning
You cannot set the contentModel dynamically. This is currently a limitation of the βTRE.
Component-based Custom Rendering#
Minimal Example#
You can register custom renderers components with the βrenderers prop.
Stop talking, let's try it out. We're going to define a renderer for the β<h1> element which supports press interactions:
- JSX Source
- HTML Snippet
- TRT Snapshot
tip
The wrapper component injected when handling onPress for βTBlock nodes is defined by the βGenericPressable prop. You can also customize the highlight color with βpressableHightlightColor. Also note that onPress works with textual nodes, in which case the eponym prop of React Native βText element will be used instead.
tip
βTDefaultRenderer can receive textProps prop which will be used when rendering a React Native βText element, and viewProps for βView elements.
Children Tampering#
Let's continue with a common requirement: injecting ads in the body of an article. More precisely, after the 2d and 4th children. To achieve our goal, we are going to use the βTChildrenRenderer component:
- JSX Source
- HTML Snippet
- TRT Snapshot
The foundry API is powerful in terms of rendering customization. It is very easy to insert child elements, while preserving the internal rendering logic.
tip
βTChildrenRenderer can receive a βrenderChild prop to customize the rendering logic for each child.
Renderer Props Summary#
A custom renderer will receive the below props:
β
tnodeThe β
TNodeto render.The default fallback renderer for this β
TNode.The internal renderer for this
tagName. An internal renderer is like a custom renderer, but registered internally. If there is no internal renderer registered for this tag, βInternalRendererwill be equal to βTDefaultRenderer.β
styleThe flatten style object which should be passed to the root element returned by this component.
β
textPropsTo use when you render a β
Text-based element (e.g. the βtypeprop is"text").β
viewPropsTo use when you render a β
View-based element (e.g. the βtypeprop is"block").β
typeTo check whether a β
Text("text") or βView("block") is expected as the root element returned by this component.Props passed directly from the parent custom renderer via β
TChildrenRenderer. See βpropsForChildrenprop.β
renderIndexThe position relative to the parent React element, starting at 0.
β
renderLengthThe total number of children of this React element parent. e.g. number of React element siblings + 1.
See βCustomRendererProps for a complete reference.