|
| 1 | +"use strict"; |
| 2 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 3 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 4 | +}; |
| 5 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 6 | +exports.BodyPortal = void 0; |
| 7 | +const react_1 = __importDefault(require("react")); |
| 8 | +const react_dom_1 = require("react-dom"); |
| 9 | +const BodyPortalSlotsContext_1 = require("./BodyPortalSlotsContext"); |
| 10 | +const getInsertBeforeTarget = (bodyPortalSlots, slot) => { |
| 11 | + // Note: If the slot is not found in bodyPortalSlots, this code will append the tag instead, |
| 12 | + // meaning the ordering will then depend on the rendering order and may change |
| 13 | + const slotIndex = bodyPortalSlots.findIndex((sl) => sl === slot); |
| 14 | + if (slotIndex === -1) { |
| 15 | + return null; |
| 16 | + } |
| 17 | + // Find the next slot that is present in the DOM and return it |
| 18 | + for (let index = slotIndex + 1; index < bodyPortalSlots.length; index++) { |
| 19 | + const sl = bodyPortalSlots[index]; |
| 20 | + const tag = sl === 'root' |
| 21 | + ? document.body.querySelector('#root') |
| 22 | + : document.body.querySelector(`[data-portal-slot="${sl}"]`); |
| 23 | + if (tag) { |
| 24 | + return tag; |
| 25 | + } |
| 26 | + } |
| 27 | + // None of the slots after this one are present in the DOM, so just append it instead |
| 28 | + return null; |
| 29 | +}; |
| 30 | +exports.BodyPortal = react_1.default.forwardRef(({ children, className, role, slot, tagName, id, ariaLabel, ...props }, ref) => { |
| 31 | + var _a; |
| 32 | + const tag = (_a = tagName === null || tagName === void 0 ? void 0 : tagName.toUpperCase()) !== null && _a !== void 0 ? _a : 'DIV'; |
| 33 | + const internalRef = react_1.default.useRef(typeof document !== 'undefined' ? document.createElement(tag) : null); |
| 34 | + if (typeof document !== 'undefined' && (!internalRef.current || internalRef.current.tagName !== tag)) { |
| 35 | + internalRef.current = document.createElement(tag); |
| 36 | + } |
| 37 | + if (ref && internalRef.current) { |
| 38 | + if (typeof ref === 'function') { |
| 39 | + ref(internalRef.current); |
| 40 | + } |
| 41 | + else { |
| 42 | + ref.current = internalRef.current; |
| 43 | + } |
| 44 | + } |
| 45 | + const bodyPortalOrderedRefs = react_1.default.useContext(BodyPortalSlotsContext_1.BodyPortalSlotsContext); |
| 46 | + const testId = props['data-testid']; |
| 47 | + react_1.default.useLayoutEffect(() => { |
| 48 | + const element = internalRef.current; |
| 49 | + if (!element) { |
| 50 | + return; |
| 51 | + } |
| 52 | + if (className) { |
| 53 | + element.classList.add(...className.split(' ')); |
| 54 | + } |
| 55 | + if (id) { |
| 56 | + element.id = id; |
| 57 | + } |
| 58 | + if (testId) { |
| 59 | + element.dataset.testid = testId; |
| 60 | + } |
| 61 | + if (role) { |
| 62 | + element.setAttribute('role', role); |
| 63 | + } |
| 64 | + if (slot) { |
| 65 | + element.dataset.portalSlot = slot; |
| 66 | + } |
| 67 | + if (ariaLabel) |
| 68 | + element.setAttribute('aria-label', ariaLabel); |
| 69 | + document.body.insertBefore(element, getInsertBeforeTarget(bodyPortalOrderedRefs, slot)); |
| 70 | + return () => { |
| 71 | + if (element.parentNode) { |
| 72 | + element.parentNode.removeChild(element); |
| 73 | + } |
| 74 | + if (slot) { |
| 75 | + delete element.dataset.portalSlot; |
| 76 | + } |
| 77 | + if (role) { |
| 78 | + element.removeAttribute('role'); |
| 79 | + } |
| 80 | + if (ariaLabel) { |
| 81 | + element.removeAttribute('aria-label'); |
| 82 | + } |
| 83 | + if (className) { |
| 84 | + element.classList.remove(...className.split(' ')); |
| 85 | + } |
| 86 | + if (id) { |
| 87 | + element.id = ''; |
| 88 | + } |
| 89 | + if (testId) { |
| 90 | + delete element.dataset.testid; |
| 91 | + } |
| 92 | + }; |
| 93 | + }, [bodyPortalOrderedRefs, className, id, role, slot, ariaLabel, tag, testId]); |
| 94 | + if (!internalRef.current) { |
| 95 | + return null; |
| 96 | + } |
| 97 | + return (0, react_dom_1.createPortal)(children, internalRef.current); |
| 98 | +}); |
0 commit comments