Images
note
Images are rendered with an internal renderer. See βRendering page. The content model of images is βblock
, see βElement Models. Also note that this renderer covers the β<img>
tag. β<picture>
tag is not yet supported.
#
Scaling#
Inline StylesThe renderer will automatically scale images down to the available width, even when the provided inline style width is greater than the container width. You need to activate enableExperimentalPercentWidth
for percent-width support in images. See βRenderersProps.img
for reference.
important
You are strongly advised to provide a βcontentWidth
property from βuseWindowDimensions
official hook to help this component handle the scaling.
#
Object FitThis library supports βobject-fit
CSS property. In the below example, which is very similar to the previous one, we set explicitly object-fit:Β contain;
to letterbox the image in the container of width 50%
and height 100px
.
tip
βobject-fit
is a mixed style and thus you can use it in mixed styles declarations such as βtagsStyles
.
#
Automatic SizingThe next image will be sized automatically thanks to the βcontentWidth
and βcomputeEmbeddedMaxWidth
props. The latter allows you to set the maximum width from βcontentWidth
, or disabling scaling by returning βInfinity
.
#
PreloadingSimilarly to browsers, this library will place a print box before fetching image dimensions when both βwidth
and βheight
attributes are provided, or the two dimensions are set in the βstyle
attribute. This is great to avoid layout shifts when images size jumps from 0 to the view box height, and is a hint to good web design.
#
Error Handling#
Custom Renderertip
You are kindly advised to read the βCustom Rendering page before continuing.
#
Via useInternalRendererβuseInternalRenderer
has a great advantage over using βInternalRenderer
prop: you have access to the internal component props. In this scenario, we are going to display an interactive thumbnail which will show the full resolution image when pressed. To do so, we are going to read the URI from the internal source prop (although we could also do this from the βTNode
src
βattributes
), and mangle it to get our "thumbnail" URI.
#
By Reusing Internal Building BlocksYou can reuse to your advantage some building blocks of the internal renderer thanks to the following exports:
To render the container of the β
<img>
element.To render the fallback view on error state.
To render the fallback view on loading state.
To render the image on success state..
To get the state of the image resource fetching.
In the below example, our custom renderer will display an activity indicator when the state is either "loading" or "success". This is for demonstration purposes, and of course you should handle the "success" state by rendering the βIMGElementContentSuccess
component, or a custom component displaying an image.
#
Configuring#
Providing headersYou can take advantage of the βprovideEmbeddedHeaders
prop to pass headers to the image fetching conditionnaly. For example:
tip
In this example, we are using a Bearer token to access a restricted resource. We could also use headers to take advantage of the new Responsive Image Client Hints standard.
#
Advanced configurationWe can take advantage of the βrenderersProps
to customize images behavior (see βRenderersProps.img
).