To get more options for configuring Elements on your dApp you can integrate it using JS and the script tag.
Usually when you include script tags in your HTML, it's in the document's head. But we recommend adding this script tag in the document's body.
Setup Script and Stylesheet
<!DOCTYPEhtml><htmllang="en"> <head> <metacharset="UTF-8" /> <metaname="viewport"content="width=device-width, initial-scale=1.0" /> <title>Your Superb dApp</title> </head> <body><!-- Your dApp's markup --> <div> Your dApp Here </div><!-- Add the leap-ui class to a parent --> <divclass="leap-ui"id="leap-elements-container"></div><!-- Add this stylesheet to load styles --> <linkrel="stylesheet"href="https://unpkg.com/@leapwallet/elements@latest/dist/umd/style.css" /><!-- Add this script to load elements --> <scriptdefersrc="https://unpkg.com/@leapwallet/elements@latest/dist/umd/main.js" ></script> </body></html>
If you want to pin down Elements to a specific version you can replace the latest in the URLs with the version number for instance 1.4.1
Render Elements
The script creates a property called LeapElements on the window object.
Know when window.LeapElements is ready
Since elements served via a CDN is a 3MB JS file (gzipped + minified) it can take a while to load. Due to this the dApp may try to access window.LeapElements before the script has loaded.
To combat this issue, elements fires @leapwallet/elements:load event when it's ready to be used.
Calling the mountElements method
Once Elements has loaded, you can call the LeapElements.mountElements method to render the component of your choice. Let's take a deeper look into the method
The mountElements method
Here's a minimal example of how to render the Fiat On-ramp component, with EUR as the source currency and NTRN as the destination token as defaults
It accepts a single object with the following properties -
element -
required
it's an object with the following two properties
name
It can be one of these - ["multi-view","aggregated-swaps","ibc-swaps","fiat-on-ramp","ibc-transfer"]
props
Pass in props based on the element you are rendering
let isElementsReady = false
// 1. Check if window.LeapElements is defined
if (window.LeapElements !== undefined) {
isElementsReady = true
}
// 2. Add an event listener to get notified when Elements is ready to be used
window.addEventListener('@leapwallet/elements:load', () => {
isElementsReady = true
})