Skip to content

Commit

Permalink
fix: prevent creating multiple root divs
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveVodrazka authored Dec 8, 2023
1 parent 9d9dd2b commit 583ba85
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<style>${css}</style>`
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 = `<style>${css}</style>`

return target
}

return new Promise((resolve) => {
const modal = new Modal({
target,
target: getTarget(),
props: {
dappName,
callback: async (value: StarknetWindowObject | null) => {
Expand Down

0 comments on commit 583ba85

Please sign in to comment.