Skip to content

Commit

Permalink
[components] Add default value for PortalContext
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuslundgard authored and rexxars committed Oct 6, 2020
1 parent 748ac7b commit 6cc81ab
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion packages/@sanity/components/src/portal/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,16 @@ export interface PortalContextInterface {
element: HTMLElement
}

export const PortalContext = createContext<PortalContextInterface | null>(null)
let globalElement: HTMLDivElement | null = null

const defaultContextValue = {
get element() {
if (globalElement) return globalElement
globalElement = document.createElement('div')
globalElement.setAttribute('data-portal', 'default')
document.body.appendChild(globalElement)
return globalElement
}
}

export const PortalContext = createContext<PortalContextInterface>(defaultContextValue)

0 comments on commit 6cc81ab

Please sign in to comment.