How to create a widget
What is widget?
When you open Experro Visual Builder, you’ll notice a side panel on the right sidebar labeled “Basic Components”, “Theme Components”, etc. Within each section, you’ll find various icons representing widgets. These widgets can be dragged and dropped onto the page. Once a widget is dropped, the associated component will be rendered on the page.
How can you create a Widget?
To create a widegt you can follow the below steps, Follow the bellow directory structure inbase-theme.
Step 1:
Create a component folder incms-library.e.g, The folder named
exp-test-component contains three files:
index.ts: This file serves as an entry point for the component.exp-test-component.tsx: Responsible for returning the component’s JSX.exp-test-component-controller.tsx: Implements all the business logic of the component. This includes tasks such as fetching data through API calls and manipulating the data that will be rendered by the component.
Step 2:
Add an entry for the newly created component to theindex.ts file located in the components folder. You will understand how to do this by examining the contents of the index.ts file.
Step 3:
Create a new file in the Widget folder that will be responsible for displaying thewidget on the Visual Builder’s side panel.
Step 4:
- To view the widget, you need to register the newly created widget file in the
index.tsfile of the widget folder. - In the
index.tsfile within the widget folder, you will notice that all existing widgets are imported, and their files are added to awidgetnamed array. This process ensures that your newly created widget becomes visible on the side panel of the Visual Builder.
Let’s take a look at the content of newly created component,
exp-test-component.tsx
index.ts
components > index.ts
Now let’s checkout the widget file,
exp-test-component.tsx
Changes to Be Verified
To associate a component with the widget, follow the steps outlined in the code snippet above, where the component is imported asExpTestComponent. Ensure that you pass the same component, unchanged, to theWidget.createWidget()object. For thecomponentkey of that object, simply assign the component. In thecontentkey, provide the value as<ExpTestComponent data-cms-widget="true"/>, but be sure to update the line according to the name of your new component. Similarly, provide the name of the component to thewidgetNamekey. Review all the changes in the code snippet provided above.
widgets > index.ts