Wix Blocks Tutorial: Creating a Counter Widget
12 min read
Wix Blocks is currently open to a limited number of users.
This tutorial shows you how to build a counter widget, where you can increment and decrement a number. Through this simple widget we will walk through everything that you can do with Wix Blocks.

We recommend that you build this widget from scratch, but if you get stuck, you can view a template of the widget in Blocks and edit it under your Wix account.
Step 1 | Create a new Widget in Blocks
- Create an empty Blocks app.
- If the app is not created with a default widget, add a widget to your app.
Show screenshot
Step 2 | Add a Layouter
A layouter is a container that allows responsiveness. It will keep the other elements organized. To add a layouter:
- Click the Add Elements
button in the top menu.
- Click LAYOUT. The Layout Tools appear.
- Go to LAYOUTERS.
- Click the vertical 3-section layouter.
Show screenshot
Step 3 | Add Widget Elements
Adding the buttons
- Click the Add Elements
button on the top menu.
- Click BUTTON.
- Drag and drop a square button onto the right section of the layouter.
Show screenshot
4. Click Change Text & Icon.
5. Select Text only from the drop down list.
6. Change the text to "+".
7. Click the Design
icon in the Inspector panel
and select Text.


8. Click Themes and select Heading 6 from the drop down list.
9. Drag and drop another square button to the left section.
10. Change the text to "-".
Show screenshot
Adding the middle number
- Click the Add Elements
button and select TEXT.
- Drag and drop an 32px Title element to the central section of the layouter.
- Edit the text to "0" and align it to the center.
Show screenshot
Changing the IDs
- Click the Properties and Events
icon in the bottom right of the screen.
- Change the ID of your increment button to incBtn.
- Change the ID of your decrement button to decBtn.
- Change the ID of your text element to countTxt.
Show screenshot
Step 4 | Add Code to Your Elements
Now it's time to make the widget do what it's supposed to do: count up and down when the buttons are clicked.
1. Click the Code tab or go to the bottom part of your screen to add code to your widget.
Show screenshot
2. Add the following code before your onReady() function.
1//Set the count to 0
2let count = 0;
3
4//Assign the count to your text element
5function render() {
6 $w('#countTxt').text = count.toString();
7}
8
9//Add a certain amount to the count and fire an event in the widget API
10function addToCount(amount) {
11 count += amount;
12 render();
13}
3. Add the following code in your onReady() function.
1 $w('#incBtn').onClick(() => {
2 addToCount(1);
3 });
4
5 $w('#decBtn').onClick(() => {
6 addToCount(-1);
7 });
8
9 render();
4. Click the Preview button to see your widget in action.
Step 5 | Make Your Widget Customizable with Widget API
You can define an API for your widget, so that the user (a site owner who installs the widget) can customize it to their needs. The API can contain properties, events and exported functions.
Let's say that you want the site owner to be able to decide what the "step" of the count is. Perhaps they want the step to be 2, so that the count is 0,2,4,6 etc.
You can define a property in the widget API named "step" and allow the user to customize it.
Define the "step" Property
- Click the Widget API
icon to open the Widget API panel.
- Click Add New Property in the Properties section (or hover over the section and click the
icon that appears).
Show screenshot
3. Name your property "step", give it the type Number and a default value of 1.
Show screenshot
Change Your Code to Consider Your New Property
Change your code so that when the buttons are clicked, addToCount is called with the step from the properties rather than with the default value of 1.
Use $widget.props to access your properties (notice the autocomplete Velo suggestions).
1$w.onReady(function () {
2 $w('#incBtn').onClick(() => {
3 addToCount($widget.props.step);
4 });
5 $w('#decBtn').onClick(() => {
6 addToCount(-$widget.props.step);
7 });
8
9 render();
10});
Test Your Property in Preview Mode
- Click Preview to move to preview mode.
- Click Test API Properties.
Show screenshot
3. Change the step to a different number and see how your widget works.
Show screenshot
Add a Public Event to Your Widget API
The widget API allows you to add an event that is fired whenever you decide.
Add an event that is fired when the "count" variable changes:
- Click the Widget API
icon to open the Widget API panel.
- Click Add New Public Event in the Events section (or hover over the section and click the
icon that appears).
- Name your event and describe it
Show screenshot
4. Add this line to your addToCount() function, to fire the event when the count changes.
1$widget.fireEvent('change', count);
5. Now, your addToCount() function should look like this:
1function addToCount(amount) {
2 count += amount;
3 render();
4 $widget.fireEvent('change', count);
5}
Catch your event in the editor
You'll need to catch your event in the Editor once you have installed your widget in your website (this will be describe in Step 8).
Add a Public Function to Your Widget API
Create and export a reset() function, so that site owners can easily set the count to 0.
- Click the Widget API
icon to open the Widget API panel.
- Click Add New Public Function in the Functions section (or hover over the section and click the
icon that appears).
- A new empty function by the default name foo() is created, with a designated JSDoc (the comments block) that can be read by anyone who will use your function on a site.
Show screenshot
4. Paste the following code instead of the function default code:
1export function reset(){
2 //This function is part of my public API
3 count = 0;
4 render();
5}
5. Update the JSDoc. Note: you must keep the function's annotation in this format.
1/**
2*@function
3*@description Sets count to 0 and renders.
4*@returns nothing
5*/
Tip
You can also go the other way around. Any exported function with the proper JSDoc above it, will appear in the Widget API panel on the right. So, you can write your code directly, instead of receiving the template through clicking Add New Public Function.
Step 6 | (Optional) Configure Your Widget's Edit-Time Behavior
The Configuration tab allows you to determine the behavior of the widget and its elements when a site owner edits it in the editor. Try out a few options in the Configuration tab.
Show screenshot
Set a display name for your widget elements:
- Select the decrement button.
- Change its display name under Component Name to "My Decrement" in the Inspector
panel.
Show screenshot
Prevent the text element from being selectable in the editor:
- Select the text element that represents your count.
- Click the Can be selected option in the Behavior section of the Inspector
panel to remove the blue checkmark.
Show screenshot
Step 7 | (Optional) Create More Design Presets
Blocks allows you to create various variations for your widget's design and layout, through design presets. To create another design preset:
- Click the Design tab in the top menu.
- Click + Add Design Preset + in the Design Presets section of the Design pane under App Structure
.
- Click the ֿMore Actions icon
to rename your preset (you can also duplicate or delete it).
Show screenshot
4. Make some visible changes in your widget, like changing the color of the middle section.
Show screenshot
Move back and forth between presets to see the differences.
Changes per preset versus global changes
Note that some design and layout changes impact only your current preset, while others impact all (learn more).
Step 8 | Catch Your Event in the Editor
If you have added an event to your app you need to catch it after you have installed it in your website. First you need to register an Event Handler for the change event:
- Install your widget app.
- Click the Properties and Events
icon
- Change the ID of your widget, for example, to "counter1".
- Click onChange( ) under Event Handlers.
- Select counter1_change in the box.
Show screenshot
Now write the function. It gets the `count` as a parameter and resets the count when it gets to whatever number you decide (in our example: 30). Your function should look like this:
1export function counter1_change({data: count}) {
2 // This function was added from the Properties & Events panel. To learn more, visit http://wix.to/UcBnC-4
3 // Add your code for this event here:
4 if (count > 30) {
5 $w('#counter1').reset();}
6}
Preview or publish to see your site in action.
Did this help?
|