First of all, what is a widget? A widget is any application that is a miniature version of the original application. You can make a widget out of any application you want.
You might have seen them in mobile and computers as small floating windows. For example, you have an application, a small floating widget of your favorite music application. This widget will not only make space for other widgets, but also give you access to a minimal version of the full blown application.
Widgets basically reduce the effort in interacting with the application. One of the use cases could be a “view only widget”, where all the “readonly” data is displayed on the widget and the modify or write actions are performed on the application. This way, you can provide your user a slimmed down version which is easier to use.
Let us create a simple widget app with 3 pages with Create, List and Update operations. We will be using the following
- React as the UI Framework.
- Typescript as the coding language.
- Bootstrap for design.
- Webpack to configure and build the app.
- Local Storage of the browser for data storage.
First let us create a React app. For this tutorial we will be using this template code. To know how this template was created, be sure to check this out.
We will just clone this template and modify the code for the widget. Since our widget will be running inside an iframe, we will not be able to make use of react-routers. So in our case we will use conditional rendering using switch cases to render the components based on a page variable.
After cloning the template and installing the packages, let us start creating an entry point for our widget to initialize. Let us create a file called widget.ts under the src folder. This file will contain all the configuration for setting up and rendering the iframe.
So, it is just basically 2 things combined. You have your normal react app that will be run by the widget.ts inside an iframe and be pluggable anywhere. Since we know that we cannot communicate props directly between the window and an iframe, we need to use the postMessage function to talk between the iframe and window and exchange props or values.
All of these might sound confusing in the start, but will become easier once we go step by step.
Now we can start adding code to the widget.ts file. We will first create our widget object that will be used to configure and initialize from the webpage that is going to use the widget. Let’s do something simple.
widget.ts
The init function will be used in the script tag and the rest is used to build and set up the widget. The handleMessage function will be used to communicate with the React application to pass data across both the iframe and the parent. So here we will just get the configuration that is passed at the script tag in the webpage that uses the widget and pass it in the config variable to the React app. Here we see that the iframe src is http://localhost:9000 . This will be our React app server. Now in order to load the widget onto a page, we need to first configure our webpack file in a different way.
webpack.config.js
We will change the entry of our app to load the react app as app and the widget.ts as Widget. And in our HTMLPlugin we will tell webpack to exclude the widget from the chunks.
We are now ready to set up our server. We will run,
Now if you go to http://localhost:9000/Widget.js , we will see our widget.ts compiled code there. If it does not show any error, then we are good to go. We are ready to move to the React App set up now.
Since we need to only load the widget if we receive the config value, we will need to listen for the postMessage.
index.tsx
Once the DOM is loaded, we will send a message to the iframe with the action init to tell the iframe that the react app has been loaded onto the DOM. The iframe checks the action in the handleMessage function used in widget.ts and sends back a message with the config data. The React app will listen to this message and call the render method if the config exists. This will ensure that the widget always loads only after the config is present.
Now that our React app is loaded, we will create our conditional routing in the App.tsx.
App.tsx
Here I have just made a simple Todo List App. For the full code please refer here . The current page is a state variable and is changed whenever the link is clicked. And the components for the respective pages are loaded based on a switch statement. After setting up all the pages, we will now call the widget method in our html page.
For the test I have created a file called index.html in the dist folder with the following code.
And we are done setting up. Just run this file using the,
and open http://localhost:5000 . Now we have the entire React app that you built rendered in an iframe and can be plugged into any site with the script above.
Here is a demo of the React App that was made as a widget.