UNPKG

polaris-glints

Version:

Glints forked polaris react

29 lines (23 loc) 804 B
import React, { useEffect } from 'react'; import { createPortal } from 'react-dom'; import { nanoid } from 'nanoid'; function Portal({ children, idPrefix = '', onPortalCreated = noop }) { const container = document.getElementById('glints-portal-container'); if (!container) { throw new Error('No element with id `glints-portal-container` found, please add it outside where the root app is rendered'); } const uniqueId = `portal-${nanoid()}`; const portalId = idPrefix !== '' ? `${idPrefix}-${uniqueId}` : uniqueId; useEffect(() => { onPortalCreated(); }, [onPortalCreated]); return container ? /*#__PURE__*/createPortal( /*#__PURE__*/React.createElement("div", { "data-portal-id": portalId }, children), container) : null; } function noop() {} export { Portal };