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.
A layouter is a container that allows responsiveness. It will keep the other elements organized. To add a layouter:
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 "-".
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. Go to the bottom part of your screen to add code to your widget.
2. Add the following code before your onReady() function.
1
3. Add the following code in your onReady() function.
1
4. Click the Preview button to see your widget in action.
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.
3. Name your property "step", give it the type Number and a default value of 1.
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
3. Change the step to a different number and see how your widget works.
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:
4. Add this line to your addToCount() function, to fire the event when the count changes.
1
5. Now, your addToCount() function should look like this:
1
Catch your event in the site editor You'll need to catch your event in the site editor once you have installed your widget in your website (this will be describe in Step 8).
Create and export a reset() function, so that site owners can easily set the count to 0.
4. Paste the following code instead of the function default code:
1
5. Update the JSDoc. Note: you must keep the function's annotation in this format.
1
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.
The Configuration tab within the Editor Experience Panel 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.
Set a display name for your widget elements:
Prevent the text element from being selectable in the editor:
Blocks allows you to create various variations for your widget's design and layout, through design presets. To create another design preset:
4. Make some visible changes in your widget, like changing the color of the middle section.
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).
Important This step takes place in the Wix Editor or Studio Editor, not in Blocks.
Go to your site editor to catch the event from your app. First you need to register an Event Handler for the change event:
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:
1
Preview or publish to see your site in action.