Your site can interact with a custom element by handling events, updating attributes, and exchanging data.
Custom elements use standard ES6 class syntax, meaning you can't call Velo APIs directly within their code. However, you can use Velo in your page to pass data to the custom element through attributes.
Custom elements can respond to both internal and site events:
connectedCallback()
and attributeChangedCallback()
.$w.onReady()
event notifies the custom element when the page is fully loaded. Then, you can listen for other page-specific events, like scrolling or mouse actions, and pass data to your custom element by updating its attributes.The following example shows how to pass data from your site to the custom element. When the button #myButton
is clicked, it updates the customData
attribute of the custom element #myCustomElement
. This data can then be accessed by the custom element via the attributeChangedCallback()
method.
Custom elements can dispatch custom events using the CustomEvent
API. These events can be handled in your page code by setting up event listeners.ss
The following example shows how to pass data from the custom element to your site.