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
.β
TText
nodes correspond to DOM βText
nodes (anonymous βTText
nodes) or DOM elements which children are DOM βText
nodes (named βTText
nodes). So a βTText
node cannot have children, and its content is a string accessible with the βdata
field.Thanks to hoisting, β
TPhrasing
nodes can only have βTText
and βTPhrasing
nodes as children.β
TBlock
nodes can have any children.
tip
You are kindly advised to read βTransient Render Engine page before continuing!
#
Options for Custom RenderingYou 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 TagLet'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βfromCustomModel
static method, see βTransient Render Engine.
#
Example: Displaying Inline ImagesIn 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!
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 ExampleYou 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:
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 TamperingLet'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:
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 SummaryA custom renderer will receive the below props:
β
tnode
The β
TNode
to 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, βInternalRenderer
will be equal to βTDefaultRenderer
.β
style
The flatten style object which should be passed to the root element returned by this component.
β
textProps
To use when you render a β
Text
-based element (e.g. the βtype
prop is"text"
).β
viewProps
To use when you render a β
View
-based element (e.g. the βtype
prop is"block"
).β
type
To 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 βpropsForChildren
prop.β
renderIndex
The position relative to the parent React element, starting at 0.
β
renderLength
The total number of children of this React element parent. e.g. number of React element siblings + 1.
See βCustomRendererProps
for a complete reference.