Skip to main content

How the UI components work

The Expivi UI consists of web components: custom HTML elements whose names start with xpv-. Because they are standard browser elements, they work the same in plain HTML, Vue, React or any other framework. This page explains how they get their data and the rules to follow when you use them.

The component packages​

Each group of components has its own entry point in @expivi/sdk. Register a group by calling its defineCustomElements() once, before you render its elements. Register only what a page uses.

Import fromElementsUse for
@expivi/sdk/ui-kit/componentsOption controls, such as tiles, lists, dropdowns and color pickersAlways: the other components render these
@expivi/sdk/ui-composer/components<xpv-product-visualizer>, <xpv-layout-composer>The product visualization and its options
@expivi/sdk/price-breakdown/components<xpv-price-breakdown>Showing the price
@expivi/sdk/bom-breakdown/components<xpv-bom-breakdown>Showing the bill of materials
@expivi/sdk/offer-inquiry/components<xpv-request-quote>Requesting a quote
@expivi/sdk/form-renderer/components<xpv-form-renderer>Forms set up in Expivi, such as the quote request form
import { defineCustomElements as defineUiKitElements } from '@expivi/sdk/ui-kit/components';
import { defineCustomElements as definePriceBreakdownElements } from '@expivi/sdk/price-breakdown/components';

defineUiKitElements();
definePriceBreakdownElements();

Two kinds of components​

Configurator components take the whole configurator in their configurator property:

  • <xpv-product-visualizer> shows the product. Depending on what is set up for the product in Expivi, that is a 3D model, a 2D visualization or images.
  • <xpv-layout-composer> shows the options. It renders the layout set up for the product in Expivi: the steps, sections and option controls. To change the options panel, change the layout in Expivi; your code stays the same.

Feature components take a controller from one of the SDK's use… functions:

  • <xpv-price-breakdown> takes the controller from usePriceBreakdown(configurator).
  • <xpv-bom-breakdown> takes the controller from useBomBreakdown(configurator).
  • <xpv-request-quote> takes the controller from useRequestQuote(configurator).

The same controllers are what you use to build your own UI. A feature component and your own UI can share one controller, so the data is fetched once. The price tutorial does this.

Pass objects as properties​

The configurator and the controllers are JavaScript objects. HTML attributes can only hold text, so pass them as properties of the element, not as attributes.

Assign the property in your script:

const visualizer = document.createElement('xpv-product-visualizer');
visualizer.configurator = configurator;
document.querySelector('#viewer').append(visualizer);

Render the components once the configurator is ready​

The configurator loads the product asynchronously and reports its progress as a status: INIT, LOADING, LOADED, READY, or ERROR when loading fails. Follow it with useProductConfigurator(configurator).subscribeStatus().

The components read the configurator when they are added to the page. <xpv-layout-composer> throws the error Product configurator is not initialized. when it is added before the product has loaded, and it doesn't pick up a configurator that you set later. So:

  1. Create the configurator and call loadProduct().
  2. Wait for the READY status.
  3. Add the components, with their configurator or controller set before they are added.

The same goes for the use… functions: most of them throw Product configurator is not initialized. when you call them before the product has loaded. Create controllers once the configurator is READY.

Controllers have a lifecycle​

The price and BOM controllers refresh their data in the background, after every change in the configuration. When you no longer need one, call its destroy() method. The feature components don't destroy the controller you give them, because you might share it with your own UI.

Create controllers in the callback that runs when the configurator is READY. On a regular web page, the controllers live as long as the page, so you don't need to destroy them.

useProductConfigurator(configurator).subscribeStatus((status) => {
if (status !== ProductConfiguratorStatus.READY) return;

const priceController = usePriceBreakdown(configurator);
// …render the price with priceController
});

One configurator per product page​

Create one ProductConfigurator for each product you show, and give all components on the page that same instance. Every component and controller then works on the same configuration: when the shopper changes an option in the options panel, the visualization, the price and your own UI update.