UNPKG

@carbon/ibm-products

Version:
30 lines (28 loc) 1.21 kB
/** * Copyright IBM Corp. 2020, 2026 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ import { pkg } from "../../../settings.js"; import { useFeatureFlag } from "../../../components/FeatureFlags/index.js"; import { useCallback, useEffect, useState } from "react"; import { createPortal } from "react-dom"; //#region src/global/js/hooks/usePortalTarget.js /** * Copyright IBM Corp. 2023, 2024 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ const usePortalTarget = (portalTargetIn) => { const enablePortalTarget = useFeatureFlag("default-portal-target-body"); const [portalTarget, setPortalTarget] = useState(portalTargetIn ?? null); useEffect(() => { if (portalTargetIn) setPortalTarget(portalTargetIn); else if (pkg.isFeatureEnabled("default-portal-target-body") || enablePortalTarget) setPortalTarget(document.body); }, [portalTargetIn, enablePortalTarget]); return useCallback((children) => portalTarget ? createPortal(children, portalTarget) : children, [portalTarget]); }; //#endregion export { usePortalTarget };