Enhance your Blocks widgets with your own design features and CSS capabilities, use any web technology or third party libraries, with Custom Elements. You can add any Javascript component by linking a custom element to a server URL or a Velo file. If needed, you can set more attributes directly from Blocks.
Custom elements let you use your own HTML tags in Blocks widgets. These elements work across all supported browsers, and can be used with any JavaScript ES6 library or framework that works with HTML 5.
With custom elements, you can:
Code requirements:
Note: A site does not need a premium account or a domain to use a Blocks app with a custom element.
Add as many custom elements as you need to your widget. After dragging a custom element to your widget, define its source (server URL or a Velo file) and give it a tag name to appear in the page registry. The tag name should be the exact name used in the customElement.define()
function.
customElements.define('weather-widget', WeatherWidget)
.Tips
The custom element HTML attributes let you adjust your element according to parameters that you control. For example, you can set dynamic texts or change colors on the go, or change the custom element inner logic according to the widget’s instance on a specific site (using getDecodedAppInstance()
).
Make sure that the script of your custom element acknowledges and handles these attributes. Otherwise, they won't function properly.
There are two ways to initiate your custom element's attributes in the Blocks app:
A. Click Set Attributes: Set and initiate your element’s attributes by adding them to this designated panel. You can also edit or remove the attributes later.
title
.B. Use Code: Set your element’s attributes in the widget code with the setAttribute()
function.
For example, let's say that you need to use data from your widget's instance on a specific site, inside your the custom element’s logic. This data is returned by the getDecodedAppInstance() function. Your widget code will look like this:
In most cases, you’ll want to use the custom element logic from one or more of these:
To do this, define properties in the widget API. These properties should represent the Custom Element attributes. The following steps explain how.
Tip:
If you only need to refer to the custom element's attributes inside this widget, there is no need to connect it to the widget API or add a panel.
Define your widget's onPropsChanged() function and onReady()
functions to handle a change in this property. For example, let's say we added a property named title
, which enables the user to change the title of the custom element. The code below sets the custom element attribute title
to be the same as the widget's property title
. Creating a function to apply props and using it in both the onPropsChange()
and onReady()
events, helps you avoid problems when your props aren't applied in preview mode, or other similar situations.
Create a panel that allows a site creator who installed your app to be able to change the widget properties (and therefore, the custom element attributes).
To summarize this last step: the site-creator changed the title through the panel. The new title was passed to the widget through the title
property. When the property changed, the new title was sent to the custom element as an attribute.
You can also use the Color Input
element (in our case its name is panelColorPicker1
) to set some style properties. For example:
Note: When creating a custom-element-only widget, it’s best practice to make it 100% of a widget and define it as non-selectable in the Configuration tab, so that when a site builder clicks on the widget to configure its settings, they will not click on the custom element.