Using rich text widgets with FastStore
Learn how to configure rich text fields and render CMS images in a FastStore storefront.
The CMS provides rich text widgets for formatted storefront content. To let editors insert images, set the field widget to draftjs-rich-text-v2. Render that field with RichText from @faststore/core.
Before you begin
Make sure that:
- The store uses the CMS. In
discovery.config.js, setcontentSource.typetoCP. - Your project uses the latest FastStore version so the storefront can render images added with
draftjs-rich-text-v2. - You can edit and upload the store's CMS schema. See Content plugin.
Instructions
Step 1 - Configuring an image-enabled rich text field
-
In
cms/faststore/components/, define the rich text field as a string and setwidget.ui:widgettodraftjs-rich-text-v2. For example,cms_component__richtextsection.jsonc:_17{_17"$extends": ["#/$defs/base-component"],_17"$componentKey": "RichTextSection",_17"$componentTitle": "Rich text section",_17"title": "Rich text section",_17"type": "object",_17"required": ["content"],_17"properties": {_17"content": {_17"title": "Content",_17"type": "string",_17"widget": {_17"ui:widget": "draftjs-rich-text-v2"_17}_17}_17}_17} -
After changing the schema, open a terminal and run the following command to generate the consolidated schema:
_10vtex content generate-schema -
Upload the schema to the CMS:
_10vtex content upload-schema
In a FastStore project, you can run both steps with a single command from the store root:
_10yarn cms-sync
For more information about these commands, see Content plugin.
Step 2 - Rendering the field
-
In
src/components, createRichTextSection.tsxand pass the CMS field toRichTextfrom@faststore/core. Set$componentKeyto the same value used in the schema:_11import { RichText } from '@faststore/core'_11_11export interface RichTextSectionProps {_11content: string_11}_11_11export default function RichTextSection({ content }: RichTextSectionProps) {_11return <RichText content={content} />_11}_11_11RichTextSection.$componentKey = 'RichTextSection'RichTextfrom@faststore/coreis the ready-to-use way to render this field, including images. You can also render the field with your own component, but it must handle the image content stored by the CMS. -
Export the section in
src/components/index.tsx:_10import RichTextSection from './RichTextSection'_10_10export default {_10RichTextSection,_10}
For more information about creating and registering sections, see Creating a new section in the CMS.
Step 3 - Adding and formatting rich text images
- In the VTEX Admin, go to Storefront > All Content.
- Open an entry containing the image-enabled rich text field.
- In the rich text toolbar, click the image action.
- Select an existing image from the Media Gallery or upload a new one. Supported formats are
PNG,JPG,JPEG,GIF,WebP, andSVG. - Select the inserted image to display its controls. You can:
- Align the image to the left, center, or right.
- Resize it by dragging a corner handle.
- Expand it to the full width of the rich text field.
- Delete it.
- Drag it to another position among the text paragraphs.
- Save the entry and use the branch preview to verify the content on the storefront.
- When the content is ready, merge the branch to publish it.
The CMS derives the image alternative text from its filename. Use a descriptive filename before uploading the image.