Walkthrough: Creating widget components manually
This walkthrough is the part three of the Developing the UI walkthrough series.
As part of a Sitecore Discover implementation, you need to create and add widget components to your React application.
This walkthrough describes how to create a widget component manually using the UI components reference site. You need to repeat this walkthrough for every widget in your project.
Alternatively, you can create widget components using the Discover UI Components CLI..
This walkthrough describes how to:
-
Create a widget component using the UI reference site.
-
Edit the widget component code to match design
-
Edit the styles of the widget component.
-
Style widget components with a theme.
Create a widget component using the UI reference site.
Adding widgets to your React application can involve using widget components from the JS SDK package or creating widget components using widget templates in the UI Components package. The templates include the code to query hook UI components. At runtime, the widget component renders when data is received for its rfk_id ID.
For content personalization widgets, use widget components from the JS SDK package.
For merchandising widgets, creating widget components is similar irrespective of widget type. Simply repeat the procedure in this section for every widget. Make sure you choose a template for the widget's type.
To create a query hooked recommendation widget component:
-
On the Discover UI components reference site, in the Widget templates section, open the page for Recommendation widget templates.
-
From the list of templates for recommendation widgets, in the Carousel section, click
index.jsto copy the code block. -
In your React project, if you do not have a folder called
widget_components, create one. -
In the
widget_componentsfolder, create a folder namedcarousel, named after the selected recommendation widget option. -
In the
carouselfolder, create a JavaScript file,index.js, then paste the copied code block, and save the file. -
To avoid compile errors, update the imports section, and save the file.
You can style widget components with theme variables, using styled widget components or by applying custom style in the components.
Edit the widget component code to match design
The widget component in the index.js or index.ts file is based on default product and category entities. They might have more or less attributes than those in your system.
To edit the widget component code to match design:
-
Open the
index.jsorindex.tsfile. -
To match your requirements for the widget and to reflect attributes included in the results fetched by the JS SDK, edit the product and category models in the React code.
-
Save file.
Make sure all attributes used in a widget component have been defined in the Domain settings section of the CEC. The attributes have to be selected to return in the API.
Invalid entity attributes can throw runtime errors.
Edit the styles of a styled widget component
If the CLI did not generate a styled.js or styled.ts file, skip the following procedure.
To edit the styles of a styled widget component to match the design:
-
In the
styled.jsorstyled.tsfile, edit the styles.
Style widget components with a theme
Theming an application with CSS variables is the fastest way to style your components. You can use the default theme or a custom theme.