Skip to main content
Version: next

Display WYSIWYG content

Since version 3.4

Let your users write their content without needing any HTML or React knowledge while still being able to deliver a qualitative UI (performant, responsive, accessible…) to customers.

WYSIWYG allows your back office users write their content without needing any HTML or React knowledge. This is the case in most CMS tools nowadays. The output though is usually HTML which does not necessarily match the React components you've built in your Front-Commerce application.

This is why we've built a theme/modules/Wysiwyg component in Front-Commerce. In this guide you will learn how to use it and how to customize the displayed components.

<Wysiwyg /> usage

Here is an example of using this component:

import React from "react";
import Wysiwyg from "theme/modules/Wysiwyg";

export default function BlogContent(props) {
return <Wysiwyg content={props.cms.contentWysiwyg} />;
}

The contentWysiwyg property originates from a GraphQL field Wysiwyg. To see the required fields, see the WysiwygFragment fragment reference available at theme/modules/Wysiwyg/WysiwygFragment.gql, and using in CmsPageFragment available at theme/pages/CmsPage/CmsPageFragment.gql.

WYSIWYG customization

Prerequisite: Please note that the following sections use advanced techniques in Front-Commerce. You especially need to be familiar with the GraphQL extension mechanisms before being able to fully apprehend what is explained. If some areas are still unclear, please feel free to reach out!

Create a new WYSIWYG Type

Each service is likely to have their own set of rules and constraints when it comes to WYSIWYG. For instance, in Magento you have shortcodes that are defined by using the {% raw %}{{ }}{% endraw %} syntax. In WordPress, you will have [] instead. Despite this syntax difference you will have also some functionality difference. For instance, Magento allows you to display a link to a product while WordPress have media galleries. This means that each service will have its own set of transformations for WYSIWYG data.

We are solving this by creating a new WYSIWYG Type for each service using the Content Composition API.

Register your WYSIWYG type

Fetching data to render WYSIWYG components

To fetch additional data for your WYSIWYG components, you'll need to:

Nested Wysiwyg components

In some cases, you will need to render a WYSIWYG component inside another WYSIWYG component. For instance, this can be the case if you implement a Product Preview feature in your WYSIWYG and want to display the description of the product. It is likely to have formatted text that can be defined directly in the administration panel.

Please be advised that we won't support infinite nested WYSIWYG component since it represents a huge performance risk for both you and your users. The goal would be instead to use only simplified WYSIWYG for nested data. In our Product Preview case, this means that instead of rendering a MagentoWysiwyg you would only render a DefaultWysiwyg that won't fetch additional data but only transform the basic HTML.

If you need more information about implementing this, please contact us.

Dynamic styles

You may want to render inline styles dynamically when displaying WYSIWYG components. It can be required if content managers customizes how a given element is displayed for instance (e.g: alignment, border color etc…).

Each <Wysiwyg> component is wrapped into a unique id. The Front-Commerce WYSIWYG mechanism provides a <Style> component that allows you to render styles that could be restricted to the current WYSIWYG context only (and prevent side effects on other parts of the page)

Here is how you could use it from a WYSIWYG component:

import React from "react";
import Style from "theme/modules/Wysiwyg/Style";

const MyComponent = ({ children, data }) => {
const { id, align, content } = data;
return (
<>
<Style rootSelector="#html-body">
{`#html-body [data-my-component-id=${id}] .my-component__content {
text-align: "${align}";
}`}
</Style>
<div class="my-component" data-my-component-id={id}>
<p class="my-component__introduction">Lorem ipsum…</p>
<p class="my-component__content">{content}</p>
</div>
</>
);
};

export default MyComponent;

It will generate an inline <style> HTML tag with the following content (#html-body being replaced with the automatically generated WYSIWYG element id):

<style>
#wysiwyg-styles-xxxxxxxx-yyyy
[data-my-component-id="42"]
.my-component__content {
text-align: "center";
}
</style>

That's it! 🎉 Everything explained previously is the core behavior of the WYSIWYG implemented in Front-Commerce. It is very flexible as it was implemented over a lot of iterations and feedbacks from our integrators. However, with this flexibility comes a bit of a complexity. Please keep in mind that you don't need to fully understand all of it to get started. However, if you've understood most of it, you will be able to dive into our code and understand how we have implemented platform specific WYSIWYG components. You could even use those components as an inspiration to develop your own specific behaviors.