learnix / src /app /upload /components /Portal.js
shashidharak99's picture
Upload files
7d51e81 verified
// Portal.jsx
// Renders children directly into document.body, escaping any
// parent overflow / transform / stacking-context restrictions.
"use client";
import { useEffect, useRef } from "react";
import { createPortal } from "react-dom";
export default function Portal({ children }) {
const elRef = useRef(null);
if (!elRef.current && typeof document !== "undefined") {
elRef.current = document.createElement("div");
}
useEffect(() => {
const el = elRef.current;
if (!el) return;
document.body.appendChild(el);
return () => {
document.body.removeChild(el);
};
}, []);
if (!elRef.current) return null;
return createPortal(children, elRef.current);
}