2021-10-03 13:30:50 +08:00
|
|
|
import PropTypes from './vue-types';
|
2023-09-15 14:21:22 +08:00
|
|
|
import {
|
|
|
|
defineComponent,
|
|
|
|
nextTick,
|
|
|
|
onBeforeMount,
|
|
|
|
onMounted,
|
|
|
|
onUpdated,
|
|
|
|
Teleport,
|
|
|
|
watch,
|
|
|
|
} from 'vue';
|
2022-01-07 16:16:11 +08:00
|
|
|
import { useInjectPortal } from '../vc-trigger/context';
|
2021-10-03 13:30:50 +08:00
|
|
|
|
|
|
|
export default defineComponent({
|
2022-09-26 21:33:41 +08:00
|
|
|
compatConfig: { MODE: 3 },
|
2021-10-03 13:30:50 +08:00
|
|
|
name: 'Portal',
|
|
|
|
inheritAttrs: false,
|
|
|
|
props: {
|
|
|
|
getContainer: PropTypes.func.isRequired,
|
2022-03-26 22:52:54 +08:00
|
|
|
didUpdate: Function,
|
2021-10-03 13:30:50 +08:00
|
|
|
},
|
|
|
|
setup(props, { slots }) {
|
2021-12-12 16:47:30 +08:00
|
|
|
let isSSR = true;
|
2021-10-03 13:30:50 +08:00
|
|
|
// getContainer 不会改变,不用响应式
|
2021-12-12 16:47:30 +08:00
|
|
|
let container: HTMLElement;
|
2022-01-07 16:16:11 +08:00
|
|
|
const { shouldRender } = useInjectPortal();
|
2023-10-16 15:56:28 +08:00
|
|
|
|
|
|
|
function setContainer() {
|
|
|
|
if (shouldRender.value) {
|
|
|
|
container = props.getContainer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-12 16:47:30 +08:00
|
|
|
onBeforeMount(() => {
|
|
|
|
isSSR = false;
|
2023-10-16 15:56:28 +08:00
|
|
|
// drawer
|
|
|
|
setContainer();
|
2023-09-15 14:21:22 +08:00
|
|
|
});
|
|
|
|
onMounted(() => {
|
2023-10-16 15:56:28 +08:00
|
|
|
if (container) return;
|
|
|
|
// https://github.com/vueComponent/ant-design-vue/issues/6937
|
|
|
|
setContainer();
|
2022-01-07 16:16:11 +08:00
|
|
|
});
|
2023-10-16 15:56:28 +08:00
|
|
|
|
2022-01-07 16:16:11 +08:00
|
|
|
const stopWatch = watch(shouldRender, () => {
|
|
|
|
if (shouldRender.value && !container) {
|
|
|
|
container = props.getContainer();
|
|
|
|
}
|
|
|
|
if (container) {
|
|
|
|
stopWatch();
|
|
|
|
}
|
2021-12-12 16:47:30 +08:00
|
|
|
});
|
2021-10-03 13:30:50 +08:00
|
|
|
onUpdated(() => {
|
|
|
|
nextTick(() => {
|
2022-01-07 16:16:11 +08:00
|
|
|
if (shouldRender.value) {
|
|
|
|
props.didUpdate?.(props);
|
|
|
|
}
|
2021-10-03 13:30:50 +08:00
|
|
|
});
|
|
|
|
});
|
2023-08-30 15:07:27 +08:00
|
|
|
// onBeforeUnmount(() => {
|
|
|
|
// if (container && container.parentNode) {
|
|
|
|
// container.parentNode.removeChild(container);
|
|
|
|
// }
|
|
|
|
// });
|
2021-10-03 13:30:50 +08:00
|
|
|
return () => {
|
2022-01-07 16:16:11 +08:00
|
|
|
if (!shouldRender.value) return null;
|
2021-12-12 16:47:30 +08:00
|
|
|
if (isSSR) {
|
|
|
|
return slots.default?.();
|
|
|
|
}
|
2021-10-03 13:30:50 +08:00
|
|
|
return container ? <Teleport to={container} v-slots={slots}></Teleport> : null;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|