The HtImage
component lives alongside your post body. Instead of just rendering a plain old image it comes with some additional functionality out-of-the-box:
Unlike the HtHero
or HtAuthor
components, this component is meant to live alongside your
article content. It is typically rendered by your headless CMS's "resolver" aka
"renderer" aka "serializer" (depending on which one you use).
This differs based on your headless CMS of choice, but the basic patterns are the same. For example, if you are using Contentful's @contentful/rich-text-react-renderer
library, you would need to
pass custom components to the second argument of documentToReactComponents
:
1<article>
2 {documentToReactComponents(myPostContent, RICH_TEXT_OPTIONS}
3</article>
To use our custom image in this instance you would simply construct RICH_TEXT_OPTIONS
like so:
const RICH_TEXT_OPTIONS = {
renderNode: {
'embedded-asset-block': ({ data }) => {
const { fields } = data.target
if (fields.file.contentType.includes('image')) {
return (
<HtImage
src={fields.file.url}
alt={fields.description}
caption={fields.description}
/>
)
} else if (fields.file.contentType === 'video/mp4') {
// return video element
// etc. etc.
}
}
}
}
Name | Type | Default | Description |
src | String | — | The source of the image, either as a relative or absolute path depending on your setup. |
alt | String | — | Alt text is helpful for both accessibility and SEO. It will be displayed if the image cannot load. |
caption | String | — | A caption for the photo which will be shown below the image. This could also be a credit for the photo, description, etc. |
isLazy | Boolean | true | Whether or not to utilize the browser's default lazy attribute on the img tag |