<template>
  <div v-if="isOpen" ref="container" :class="[ui.container, ui.width]">
    <transition appear v-bind="ui.transition">
      <div :class="[ui.base, ui.ring, ui.rounded, ui.shadow, ui.background]">
        <slot />
      </div>
    </transition>
  </div>
</template>

<script>
import { computed, toRef, defineComponent } from "vue";
import { defu } from "defu";
import { onClickOutside } from "@vueuse/core";
import { usePopper } from "../../composables/usePopper";
import { useAppConfig } from "#imports";
import appConfig from "#build/app.config";
export default defineComponent({
  props: {
    modelValue: {
      type: Boolean,
      default: false
    },
    virtualElement: {
      type: Object,
      required: true
    },
    popper: {
      type: Object,
      default: () => ({})
    },
    ui: {
      type: Object,
      default: () => appConfig.ui.contextMenu
    }
  },
  emits: ["update:modelValue", "close"],
  setup(props, { emit }) {
    const appConfig2 = useAppConfig();
    const ui = computed(() => defu({}, props.ui, appConfig2.ui.contextMenu));
    const popper = computed(() => defu({}, props.popper, ui.value.popper));
    const isOpen = computed({
      get() {
        return props.modelValue;
      },
      set(value) {
        emit("update:modelValue", value);
      }
    });
    const virtualElement = toRef(props, "virtualElement");
    const [, container] = usePopper(popper.value, virtualElement);
    onClickOutside(container, () => {
      isOpen.value = false;
    });
    return {
      // eslint-disable-next-line vue/no-dupe-keys
      ui,
      isOpen,
      container
    };
  }
});
</script>
