Define how your widget appears in the Scandi theme
Now that a <widget> element can appear in the CMS content, we need to implement parsing it and rendering a custom React element in its place. Luckily, as outlined in the intro section, Scandi already parses all CMS content β all we need to do is define a new widget type!
Create a Component
We need to create a Component for the widget. This will just be a regular Scandi React component that takes the widget attributes (only data-title in this case) as props.
To create a new widget, you can just use the ScandiPWA CLI utility:
scandipwa create component NewsletterWidget
Our widget needs to let the user subscribe to the newsletter. Thankfully, this functionality is already implemented in the NewsletterSubscription component, so we can re-use that. All we need to do is add the widget title. Hence, the implementation is pretty simple:
Now we have a component that can render out the Newsletter widget. However, it is not integrated into the Scandi widget system yet. Let's do that next!
Configure the Newsletter Widget
As mentioned in the intro, the WidgetFactory is responsible for determining the widget type and rendering an appropriate component. This mapping is configured in the renderMap:
Note that each key is a string constant that corresponds to the widget type. This means that slider-type widgets will use the HomeSlider component, for example.
We want to add our own configuration entry for the newsletter widget. To do this, let's override the WidgetFactory component:
scandipwa override component WidgetFactory
Only select the WidgetFactory class to override it, and leave the other options blank - we don't need to override anything else!
? Choose things to extend in WidgetFactory.component.js WidgetFactory
? What would you like to do with styles? Keep
? Choose things to extend in WidgetFactory.config.js
NOTE!
The following files have been created:
src/component/WidgetFactory/WidgetFactory.component.js