ant-design-vue/components/_util/Portal.js

39 lines
855 B
JavaScript
Raw Normal View History

2020-05-13 10:36:53 +00:00
import PropTypes from './vue-types';
import {
defineComponent,
nextTick,
onBeforeUnmount,
onMounted,
onUpdated,
ref,
Teleport,
} from 'vue';
2020-05-13 10:36:53 +00:00
2020-10-12 05:27:16 +00:00
export default defineComponent({
2020-05-13 10:36:53 +00:00
name: 'Portal',
inheritAttrs: false,
2020-05-13 10:36:53 +00:00
props: {
2020-10-02 07:44:10 +00:00
getContainer: PropTypes.func.isRequired,
2020-05-13 10:36:53 +00:00
didUpdate: PropTypes.func,
},
setup(props, { slots }) {
const container = ref();
onMounted(() => {
container.value = props.getContainer();
});
onUpdated(() => {
2020-10-28 07:31:45 +00:00
nextTick(() => {
props.nextTick?.(props);
2020-05-13 10:36:53 +00:00
});
});
onBeforeUnmount(() => {
if (container.value && container.value.parentNode) {
container.value.parentNode.removeChild(container.value);
2020-05-13 10:36:53 +00:00
}
});
return () => {
return container.value ? <Teleport to={container.value}>{slots.default?.()}</Teleport> : null;
};
2020-05-13 10:36:53 +00:00
},
2020-10-12 05:27:16 +00:00
});