Extending drop-in components
Drop-in components are designed to be flexible and extensible. This guide provides an overview of how to extend drop-in components to add new features, integrate with third-party services, and customize the user experience.
Extend drop-ins with Commerce APIs
The following steps describe how to add existing Commerce API services to a drop-in. For example, the Commerce API provides the necessary endpoints to fetch and update gift messages through GraphQL, but the checkout drop-in doesn’t provide this feature out of the box. We will extend the checkout drop-in by adding a UI for gift messages, use the Commerce GraphQL API to update the message data on the cart, and extend the cart drop-in to include the message data when it fetches the cart.
Step-by-step
Add your UI to the drop-in
The first step is to create a UI for the feature and add it to the checkout drop-in. You can implement the UI however you want, as long as it can be added to the HTML DOM. For this example, we’ll implement a web component (GiftOptionsField
) that provides the form fields needed to enter a gift message. Here’s an example implementation of the UI component:
Render the UI into a checkout container
Next, we need to render the GiftOptionsField
component into the checkout page by creating the gift-options-field
custom element.
Then, insert the custom element into the layouts defined on the checkout page. The following example updates the render function for mobile and desktop to insert the giftOptionsField
element into the layouts.
Add handler for gift message submission
Now that we have the UI in place, we need to add a handler to save the gift message data. We’ll use the fetchGraphl()
function from the API to send a GraphQL mutation to set the gift message on the cart.
Extend the data payload for the drop-in
To extend the data payload of a drop-in, first you need to update the GraphQL fragment used by the cart drop-in to request the additional field. This is done by modifying the build.mjs
script at the root of your storefront project. In the following example, the CART_FRAGMENT
fragment is extended to include the gift message data whenever the cart drop-in requests the cart data from GraphQL:
When you run the install command, the build.mjs
script generates a new GraphQL query for the cart drop-in that includes the gift_message
data.
Add new data to the payload
Map the new GraphQL data to the payload data that the cart events provide to listeners so they can access the gift message values.
Configure the cart drop-in’s initializer to add the new cart data to the existing cart payload. This is done by defining a transformer function on the CartModel. This function receives the GraphQL data and returns an object that gets merged with the rest of the cart payload. As an example, here is how it might be configured:
Now when the cart emits an event with cart data, the giftMessage
data is included.
Retrieve the data and render it
Get the data from the cart event and use it to populate the gift message fields on the checkout page. Here’s an example of how you might do this:
Summary
After just a few changes, we were able to add a new feature to the checkout drop-in that allows users to add a gift message to their order. We added a new UI component, integrated the Commerce API to fetch and update gift messages, and extended the data payload for the drop-in to include the gift message data. You can apply these same concepts to any drop-in.
Extend drop-ins with third-party components
The following steps guide you through adding a third-party component to a drop-in. We’ll add a fictitious ratings & reviews component to the product details drop-in as an example.
Prerequisites
- Third-party component API key. You typically need an API key to fetch data for the component.
- Familiarity with project configurations.
What you’ll learn
- How to configure third-party API keys for use in drop-ins.
- How to use the
EventBus
to emit events and listen for events from the third-party component. - How to delay loading large data sets from third-party components to improve page performance.
Step-by-step
Add your third-party API key
The boilerplate starter code contains three config files, one for each environment: configs.xlsx
(prod), configs-stage.xlsx
and configs-dev.xlsx
(or the equivalent Google sheets files). Add your third-party API key to the config environments you want to test or develop against. For example, if you’re working in the dev environment, add the key to configs-dev.xlsx
(or the equivalent Google sheet).
Key | Value |
---|---|
third-party-api-key | third-party-api-value |
Replace third-party-api-key
and third-party-api-value
with the actual API key name and value.
Fetch the API key
To fetch the API key, you need to import the getConfigValue
function from the configs.js
file. This function reads the API key from the config file and returns the value. You can then use this value to fetch data from the third-party service.
Fetch the component data
After the page loads, your third-party component likely needs to fetch some data. In our case, our ratings & reviews component needs to fetch data from its rating service to display the star-rating for the product. After your API key is fetched (thirdPartyApiKey
), you can trigger a call to the service’s endpoint and use the EventBus to emit an event when the data is received.
Render the component
To ensure the least amount of CLS, we’ll make sure we don’t render the component until after its data is returned. To do this, we need to add an event listener for the third-party component’s event. This strategy, along with reserving a predefined space for the component, will minimize CLS. Here’s an example implementation for our third-party ratings component:
Delay loading large data sets
Components like ratings & reviews typically load large blocks of text to display a product’s reviews. In such cases, we need to ensure that those reviews are not loaded until the user scrolls near the reviews section or clicks a “View All Reviews” button. This strategy keeps the First Contentful Paint (FCP) and Cumulative Layout Shift (CLS) scores low.
The following example uses an Intersection Observer to load reviews only when a user scrolls near the reviews section or clicks “View All Reviews”.
Summary
Throughout this tutorial, we examined the key steps of integrating a fictitious third-party component. We learned how to configure API keys, fetch data, and delay loading data sets to improve page performance. You can apply these same concepts to any drop-in.