diff --git a/src/main.ts b/src/main.ts
index ddbb659..6d3d51e 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -82,15 +82,33 @@ export const connect = async ({
storeVersion,
})
- const element = document.createElement("div")
- document.body.appendChild(element)
- const target = element.attachShadow({ mode: "open" })
-
- target.innerHTML = ``
+ const getTarget = (): ShadowRoot => {
+ const modalId = "starknetkit-modal"
+ const existingElement = document.getElementById(modalId)
+
+ if (existingElement) {
+ if (existingElement.shadowRoot) {
+ // element already exists, use the existing as target
+ return existingElement.shadowRoot
+ }
+ // element exists but shadowRoot cannot be accessed
+ // delete the element and create new
+ existingElement.remove()
+ }
+
+ const element = document.createElement("div")
+ // set id for future retrieval
+ element.id = modalId
+ document.body.appendChild(element)
+ const target = element.attachShadow({ mode: "open" })
+ target.innerHTML = ``
+
+ return target
+ }
return new Promise((resolve) => {
const modal = new Modal({
- target,
+ target: getTarget(),
props: {
dappName,
callback: async (value: StarknetWindowObject | null) => {