Web Components API
Use Builder web components to display dynamic Builder content on any tech stack.
With the script tag and the builder-component custom element, you can optionally set the targeting attributes for Builder to load content dynamically. For example:
Subresource Integrity (SRI)
Every time the web components script is published to Builder's CDN, Builder generates an integrity hash for that exact file. You can add this hash to the integrity attribute on your script tag:
Pinning to a specific version and setting integrity means the script only runs if the fetched file matches the expected hash exactly, and crossorigin="anonymous" is required for the browser to perform this check.
If you use the unversioned https://cdn.builder.io/js/webcomponents URL, the file can change over time, so omit the integrity attribute. To useintegrity, pin to a fixed version as in the example above.
Attributes
model
Required: Yes
Description: The name of the your page or component model to display
api-key
Required: Yes
Description: Your Builder Content Public API Key
entry
Required: No
Load a specific Builder Content entry by ID, e.g.
reload-on-route
Required: No
If on, the component observes location pushState events and reloads when the browser URL changes client side; for example, if you target different content for this code at different URL paths.
options
Required: No
Full Builder options object as JSON to customize how content is requested.
Events
load
Fires when the Builder content loads and passes you the data loaded. Good for transitioning content or tracking analytics such as which Builder content and A/B tests were viewed to other analytics providers.
error
Fires when Builder content fails to load.
Initializing
If you need to run some logic before Builder Content web components fetch and render, you can declare a window variable called builderWcLoadCallbacks as in the example below:
This code snippet sets up a builderWcLoadCallbacks callback function that runs when the Builder Content web component loads. Inside the callback, the context.builder.setUserAttributes() method is used to set targeting attributes for the Builder component, allowing dynamic loading of content based on these attributes.
This example features:
window.builderWcLoadCallbacks: This is an array that holds callback functions to be executed when the Builder Content web component loads.(context) => ...: This is an arrow function that takes a parametercontext, which represents the context of the Builder Content component. Thecontextparameter provides access to various methods and data related to the Builder Content component.context.builder.setUserAttributes({ ... }): This method is called on thecontext.builderobject to set user targeting attributes. Targeting attributes are used to customize the content displayed by the Builder Content component based on specific conditions.{ locale: navigator.language }: This is an example of a targeting attribute being set. In this case, it sets thelocaleattribute to the value ofnavigator.language, which represents the user's preferred language as detected by the browser.
By setting these attributes, you can dynamically load content into the web component based on specific criteria, such as the user's language in this example. In this way, you can provide personalized content based on the user's context and preferences.
Registering custom elements
Register custom elements with Builder Content to get support for custom blocks in Builder Content for any framework.
For example, suppose you have a web component called my-hero that takes a title and subtitle as in the following example:
And the MyHero class has the following definition:
You can register it in your code, as in the following example:
For more on Builder Content 's supported import types, read Input Types. Note that for web components Builder Content only supports primitive elements–such as text, number, boolean–but not deep objects and arrays like lists and maps.
You can also wrap components in your favorite framework as custom elements; for examples, see Using React in your Web Components in the React documentation and Vue's web component wrapper on GitHub.
Passing data and context for binding
In the Builder Content component, builder-component, you can pass data and functions to your UI. This way you can bind data values to UI elements, such as text values or lists, and define actions triggered by events such as clicking a button.
Any data passed down to is accessible within Builder Content actions and bindings using the state.* syntax. For instance, if you pass down products as data, you can access it within the component using state.products.
The following code snippet demonstrates these guidelines and dynamically sets the text of a button using the Builder Content component with the example "buttonText" as "Click Me."
The code example:
- Sets up
builder-component. - Passes data using
setState(). - Demonstrates how to pass functions and complex data through the
setContext()method. - Loads the Builder Content component from the CDN.