UNPKG

@arco-design/web-vue

Version:

Arco Design Vue 2.0: A Vue.js 3 UI Library

34 lines (33 loc) 1.09 kB
import { ref, onMounted, watch } from "vue"; import { getElement } from "../_utils/dom.js"; const useTeleportContainer = ({ popupContainer, visible, defaultContainer = "body", documentContainer }) => { const teleportContainer = ref(popupContainer.value); const containerRef = ref(); const getContainer = () => { const element = getElement(popupContainer.value); const _teleportContainer = element ? popupContainer.value : defaultContainer; const _containerElement = element != null ? element : documentContainer ? document.documentElement : getElement(defaultContainer); if (_teleportContainer !== teleportContainer.value) { teleportContainer.value = _teleportContainer; } if (_containerElement !== containerRef.value) { containerRef.value = _containerElement; } }; onMounted(() => getContainer()); watch(visible, (visible2) => { if (teleportContainer.value !== popupContainer.value && visible2) { getContainer(); } }); return { teleportContainer, containerRef }; }; export { useTeleportContainer };