-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
143 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "./overlays-manager.lite"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { useRef, onMount, onUnMount, useMetadata } from "@builder.io/mitosis"; | ||
import { overlays } from "./overlays"; | ||
import { OverlaysManagerProps } from "./overlays-manager.types"; | ||
|
||
useMetadata({ | ||
rsc: { | ||
componentType: "client", | ||
}, | ||
}); | ||
|
||
export default function OverlaysManager(props: OverlaysManagerProps) { | ||
const containerRef = useRef<HTMLDivElement>(null); | ||
let cleanupRef = useRef<(() => void) | null>(null); | ||
|
||
onMount(() => { | ||
if (containerRef) { | ||
const ownerDocument = containerRef.ownerDocument; | ||
const overlayRoot = overlays.getOrCreateOverlayRoot(ownerDocument); | ||
|
||
// Append children to the overlay root | ||
while (containerRef.firstChild) { | ||
overlayRoot.appendChild(containerRef.firstChild); | ||
} | ||
|
||
let zIndexCounter = 1; | ||
|
||
// Function to apply styles to direct children | ||
const applyStylesToChildren = () => { | ||
Array.from(overlayRoot.children).forEach((child, index) => { | ||
if (child instanceof HTMLElement) { | ||
child.style.position = "relative"; | ||
child.style.zIndex = (index + 1).toString(); | ||
} | ||
}); | ||
zIndexCounter = overlayRoot.children.length + 1; | ||
}; | ||
|
||
// Apply initial styles | ||
applyStylesToChildren(); | ||
|
||
// Set up MutationObserver to watch for changes in children | ||
const observer = new MutationObserver((mutations) => { | ||
mutations.forEach((mutation) => { | ||
if (mutation.type === "childList") { | ||
applyStylesToChildren(); | ||
} | ||
}); | ||
}); | ||
|
||
observer.observe(overlayRoot, { childList: true }); | ||
|
||
// Cleanup function | ||
cleanupRef = () => { | ||
observer.disconnect(); | ||
while (containerRef.firstChild) { | ||
containerRef.removeChild(containerRef.firstChild); | ||
} | ||
}; | ||
} | ||
}); | ||
|
||
onUnMount(() => { | ||
if (typeof cleanupRef === "function") cleanupRef(); | ||
}); | ||
|
||
return <div ref={containerRef} style={{ display: "none" }} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export interface OverlaysManagerProps {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
export const OVERLAYS_MANAGER_ID = "interchain-ui-overlays-manager"; | ||
|
||
class Overlays { | ||
private static instance: Overlays; | ||
private overlayRoots: Map<Document, HTMLElement> = new Map(); | ||
|
||
private constructor() {} | ||
|
||
public static getInstance(): Overlays { | ||
if (!Overlays.instance) { | ||
Overlays.instance = new Overlays(); | ||
} | ||
return Overlays.instance; | ||
} | ||
|
||
public getOrCreateOverlayRoot(ownerDocument: Document): HTMLElement { | ||
if (!this.overlayRoots.has(ownerDocument)) { | ||
const root = ownerDocument.createElement("div"); | ||
root.id = OVERLAYS_MANAGER_ID; | ||
// root.style.position = "fixed"; | ||
// root.style.top = "0"; | ||
// root.style.left = "0"; | ||
// root.style.width = "100%"; | ||
// root.style.height = "100%"; | ||
// root.style.pointerEvents = "none"; | ||
// root.style.zIndex = "1000"; | ||
ownerDocument.body.appendChild(root); | ||
this.overlayRoots.set(ownerDocument, root); | ||
} | ||
return this.overlayRoots.get(ownerDocument)!; | ||
} | ||
|
||
public cleanup() { | ||
this.overlayRoots.forEach((root, doc) => { | ||
doc.body.removeChild(root); | ||
}); | ||
this.overlayRoots.clear(); | ||
} | ||
} | ||
|
||
export const overlays = Overlays.getInstance(); |