<script>
import { createComponentName, componentMixin } from '../_utils/component';

export default {
    name: createComponentName('portal'),
    mixins: [componentMixin],
    props: {
        // 挂载的节点 默认body
        target: {//eslint-disable-line
            default: 'body',
        },
        // 外层class

        name: {
            type: String,
            default: '',
        }
    },
    computed: {
        targetEl() {
            if (this.$isServer || !this.target) return;
            if (typeof this.target === 'string') return document.querySelector(this.target);
            return this.target;
        },
    },
    watch: {
        targetEl(el) {
            el.appendChild(this.$el);
        },
    },
    mounted() {
        if (this.targetEl) this.targetEl.appendChild(this.$el);
    },
    destroyed() {
        if (!this.levaeing || !this.name) {
            if (this.$el && this.$el.parentNode) this.$el.parentNode.removeChild(this.$el);
        }
    },
    render() {
        if (this.$slots.default) return this.$slots.default[0];
        return null;
    }
};

</script>
