Skip to content

Commit

Permalink
Merge pull request #44 from DaveVodrazka/fix/multiple-root-divs
Browse files Browse the repository at this point in the history
fix: prevent creating multiple root divs
  • Loading branch information
bluecco authored Dec 12, 2023
2 parents 9d9dd2b + 583ba85 commit e16db77
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 e16db77

Please sign in to comment.