Skip to content

Commit

Permalink
fix(overlays): guard against SSR env
Browse files Browse the repository at this point in the history
  • Loading branch information
yyyyaaa committed Jul 23, 2024
1 parent 6adc68d commit d72a6cf
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
3 changes: 2 additions & 1 deletion packages/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ Then in your root route/layout, import `ThemeProvider` and CSS

```TSX
// layout.tsx
import { ThemeProvider } from '@interchain-ui/react';
import { ThemeProvider, OverlaysManager } from '@interchain-ui/react';
import '@interchain-ui/react/styles';

export function RootLayout(props: LayoutProps) {
return (
<ThemeProvider>
{props.children}
<OverlaysManager />
</ThemeProvider>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type { ChainListItemProps } from "@/ui/chain-list-item/chain-list-item.ty
import type { Sprinkles } from "@/styles/rainbow-sprinkles.css";
import { overlays } from "@/ui/overlays-manager/overlays";
import useTheme from "@/ui/hooks/use-theme";
import { getOwnerDocument } from "@/helpers/platform";

interface ItemProps {
isActive: boolean;
Expand Down Expand Up @@ -211,8 +212,12 @@ export default function ChainSwapCombobox(props: ChainSwapComboboxProps) {
if (props.rootNode) {
return setMountRoot(props.rootNode);
}
if (!containerRef.current) return;

setMountRoot(overlays.getOrCreateOverlayRoot(containerRef.current));
const ownerDocument = getOwnerDocument(containerRef.current);
if (!ownerDocument) return;

setMountRoot(overlays.getOrCreateOverlayRoot(ownerDocument));
}, []);

return (
Expand Down
4 changes: 4 additions & 0 deletions src/helpers/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ export function closestBodyElement(node: HTMLElement) {
}
return null;
}

export function getOwnerDocument(node: HTMLElement) {
return node.ownerDocument;
}
25 changes: 15 additions & 10 deletions src/ui/overlays-manager/overlays.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,36 @@ class Overlays {

private constructor() {}

private static isBrowser(): boolean {
return typeof window !== "undefined";
}

public static getInstance(): Overlays {
if (!Overlays.instance) {
Overlays.instance = new Overlays();
}
return Overlays.instance;
}

public getOrCreateOverlayRoot(ownerDocument: Document): HTMLElement {
public getOrCreateOverlayRoot(ownerDocument: Document): HTMLElement | null {
if (!Overlays.isBrowser()) {
return null;
}

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)!;
return this.overlayRoots.get(ownerDocument) || null;
}

public cleanup() {
public cleanup(): void {
if (!Overlays.isBrowser()) {
return;
}

this.overlayRoots.forEach((root, doc) => {
doc.body.removeChild(root);
});
Expand Down

0 comments on commit d72a6cf

Please sign in to comment.