<script setup>
import { onClickOutside, useVModel } from "@vueuse/core";
import { useFocusTrap } from "@vueuse/integrations/useFocusTrap";
import { computed, nextTick, ref, watchEffect } from "vue";
const props = defineProps({
  modelValue: { type: Boolean, required: false, default: false },
  dim: { type: Boolean, required: false, default: true },
  autoClose: { type: Boolean, required: false, default: true }
});
const emit = defineEmits(["close", "update:modelValue"]);
const show = useVModel(props, "modelValue", emit, { passive: true });
const card = ref(null);
const shown = ref(false);
const { activate, deactivate } = useFocusTrap(computed(() => card.value || document.body), { immediate: false });
watchEffect(
  () => {
    if (!shown.value && show.value)
      shown.value = true;
    if (show.value && card.value)
      nextTick(activate);
    else
      deactivate();
  }
);
onClickOutside(card, () => {
  if (props.modelValue && props.autoClose) {
    show.value = false;
    emit("close");
  }
}, {
  ignore: ["a", "button", "summary"]
});
</script>

<script>
export default {
  inheritAttrs: false
};
</script>

<template>
  <Teleport v-if="shown" to="body">
    <div
      v-show="show"
      class="fixed inset-0 z-100 flex items-center justify-center n-glass-effect n-transition"
      role="dialog"
      aria-modal="true"
      :class="[
  show ? '' : 'op0 pointer-events-none visibility-none'
]"
    >
      <div
        class="absolute inset-0 -z-1"
        :class="[
  dim ? 'glass-effect' : ''
]"
      />
      <NCard v-bind="$attrs" ref="card" class="max-h-screen of-auto">
        <slot />
      </NCard>
    </div>
  </Teleport>
</template>
