Learn how to build a Countdown widget on your own
Custom widgets are web components that you can add as specific content blocks to News or Pages on the Staffbase platform. In this tutorial, we take you through how to build a basic Countdown widget on your own and make it available on News or Pages. After completing this tutorial, you’ll be familiar with the widget building process and can start building custom widgets based on your business requirement.
You can find more Staffbase custom widget examples here.
Custom widgets cannot be made available in the public area of your app.
A Countdown widget provides your users a countdown timer to major upcoming company events. This can be created as part of a campaign to build excitement and readiness around an anticipated event. Your editors can add the widget to News posts and Pages like other standard widgets.
Once you build the widget, install it on the Staffbase platform to make the widget available on News and Pages. Your editors can configure the widget with the event date in a News post or a Page.
Users, in turn, will see how much time is left until the event based on the event date entered by the editor.
This tutorial is designed only to help you start building a Countdown widget on your own, a basic version is shown here. Feel free to use this as a base to build more improvements and features on it, and design the Countdown widget suited best for your requirement. For instance, you can improve this further by adding a date picker for the Event Date field, a teaser message for the countdown, or even a check-in button for users attending the event, and much more.
Create a project folder with the name Widget
in your local drive.
In the terminal, run the command to navigate to the directory where you created the project.
For example, cd /Users/.../Documents/Widget
Run the command to create resources and files in your project.
The terminal prompts you to give the widget details.
countdown-widget
.The custom widget name must include at least one hyphen. For example, my-widget.
A new folder named countdown-widget has been created under your project folder. All the required resources and files will be added to this folder.
Now, you have created your custom widget project.
The preview shows how each of these views will look like:
countdown-widget
folder.The developer preview opens.
While this is a convenient mockup preview, it does not represent the final look and feel of the configuration dialog in the Staffbase Studio or the final end-user view.
You have successfully installed and started your custom widget. Now you can define the Countdown widget.
You already have the base project for your custom widget. As the project is currently a simple hello world, you need to remove some properties and add new ones to define functionalities for the Countdown widget.
This file gives you the possibility to define the Configuration dialog for the widget in the Staffbase Studio. The data scheme used in the file follows the JSON Schema standard. Learn more about it here.
Under the src
folder, open the configuration-schema.ts
file.
Replace all the values 'message'
with 'enddate'
as shown in the code snippet. The property enddate
allows you to define the event date for your Countdown widget later.
export const configurationSchema
, modify the following properties:type
: Replace "string"
with "number"
title
: Replace "enddate"
with "Event Date"
This modifies the type value entered in the field label of your widget configuration.
ui:help
value to "Enter a date in Unix date format"
.Since this is a basic tutorial, we will not develop a date picker. You can use this as a base and improve the functionalities.
You can view the changes in the developer view.
The index file contains the main class, which is invoked in the registration call and the block definition for the metadata.
Avoid adding too much business logic into the index file. If you need to, move them into a separate module. This ensures that you don’t unintentionally break the implementation when making future changes.
In the index file, declare the metadata used in the configuration file in your index file.
In your development environment, navigate to the src
folder.
Open index.tsx
file.
Scroll to the code snippet where const widgetAttributes: string[]
is defined.
Replace the value 'message'
with 'enddate'
so that the widget reacts to the changes made to the attribute.
The widget file is the view layer of your widget and will be called by the main class. It gets all of the attributes as properties.
src
folder, open the countdown-widget.tsx
file.If you have named your widget differently the file name will vary accordingly.
'message'
with 'enddate'
and 'string'
with 'number'
in CountdownWidgetProps export
.export function const CountdownWidget
.export const CountdownWidget
.return
value with the following code snippet. This defines how the countdown will be displayed to the end-user.You may have already tested the widget after making changes to each file. Run it in the developer preview by providing an Event Date in Unix date format.
You’ll have to generate a unix timestamp in milliseconds, which must be a current or future date. Use a Unix date converter to generate the date in Unix format.
Ensure that your developer preview looks like this:
You have defined and tested your widget. Now you can build the widget by running the command:
Check the message to see if the build was successful.
Once you get a message that the build was successful, you can host the widget bundle.
Under the dist
folder, the .js
file contains the license information and details to allow you to host the Countdown widget on a web server or a content delivery network (CDN).
After hosting the js bundle on a web server or a CDN, you can install the Countdown widget to your Staffbase platform. In the Staffbase Studio, the admin needs to install the js bundle URL in order to make it available on News and Pages. Learn more about it here.