| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- import { getCurrentInstance, ref, computed, watch, nextTick, onMounted } from 'vue';
- import { useTimeoutFn, isClient } from '@vueuse/core';
- import { isUndefined } from 'lodash-unified';
- import { useLockscreen } from '../../../hooks/use-lockscreen/index.mjs';
- import { useZIndex } from '../../../hooks/use-z-index/index.mjs';
- import { useId } from '../../../hooks/use-id/index.mjs';
- import { useGlobalConfig } from '../../config-provider/src/hooks/use-global-config.mjs';
- import { defaultNamespace } from '../../../hooks/use-namespace/index.mjs';
- import { addUnit } from '../../../utils/dom/style.mjs';
- import { UPDATE_MODEL_EVENT } from '../../../constants/event.mjs';
- const useDialog = (props, targetRef) => {
- var _a;
- const instance = getCurrentInstance();
- const emit = instance.emit;
- const { nextZIndex } = useZIndex();
- let lastPosition = "";
- const titleId = useId();
- const bodyId = useId();
- const visible = ref(false);
- const closed = ref(false);
- const rendered = ref(false);
- const zIndex = ref((_a = props.zIndex) != null ? _a : nextZIndex());
- let openTimer = void 0;
- let closeTimer = void 0;
- const namespace = useGlobalConfig("namespace", defaultNamespace);
- const style = computed(() => {
- const style2 = {};
- const varPrefix = `--${namespace.value}-dialog`;
- if (!props.fullscreen) {
- if (props.top) {
- style2[`${varPrefix}-margin-top`] = props.top;
- }
- if (props.width) {
- style2[`${varPrefix}-width`] = addUnit(props.width);
- }
- }
- return style2;
- });
- const overlayDialogStyle = computed(() => {
- if (props.alignCenter) {
- return { display: "flex" };
- }
- return {};
- });
- function afterEnter() {
- emit("opened");
- }
- function afterLeave() {
- emit("closed");
- emit(UPDATE_MODEL_EVENT, false);
- if (props.destroyOnClose) {
- rendered.value = false;
- }
- }
- function beforeLeave() {
- emit("close");
- }
- function open() {
- closeTimer == null ? void 0 : closeTimer();
- openTimer == null ? void 0 : openTimer();
- if (props.openDelay && props.openDelay > 0) {
- ({ stop: openTimer } = useTimeoutFn(() => doOpen(), props.openDelay));
- } else {
- doOpen();
- }
- }
- function close() {
- openTimer == null ? void 0 : openTimer();
- closeTimer == null ? void 0 : closeTimer();
- if (props.closeDelay && props.closeDelay > 0) {
- ({ stop: closeTimer } = useTimeoutFn(() => doClose(), props.closeDelay));
- } else {
- doClose();
- }
- }
- function handleClose() {
- function hide(shouldCancel) {
- if (shouldCancel)
- return;
- closed.value = true;
- visible.value = false;
- }
- if (props.beforeClose) {
- props.beforeClose(hide);
- } else {
- close();
- }
- }
- function onModalClick() {
- if (props.closeOnClickModal) {
- handleClose();
- }
- }
- function doOpen() {
- if (!isClient)
- return;
- visible.value = true;
- }
- function doClose() {
- visible.value = false;
- }
- function onOpenAutoFocus() {
- emit("openAutoFocus");
- }
- function onCloseAutoFocus() {
- emit("closeAutoFocus");
- }
- function onFocusoutPrevented(event) {
- var _a2;
- if (((_a2 = event.detail) == null ? void 0 : _a2.focusReason) === "pointer") {
- event.preventDefault();
- }
- }
- if (props.lockScroll) {
- useLockscreen(visible);
- }
- function onCloseRequested() {
- if (props.closeOnPressEscape) {
- handleClose();
- }
- }
- watch(() => props.modelValue, (val) => {
- if (val) {
- closed.value = false;
- open();
- rendered.value = true;
- zIndex.value = isUndefined(props.zIndex) ? nextZIndex() : zIndex.value++;
- nextTick(() => {
- emit("open");
- if (targetRef.value) {
- targetRef.value.parentElement.scrollTop = 0;
- targetRef.value.parentElement.scrollLeft = 0;
- targetRef.value.scrollTop = 0;
- }
- });
- } else {
- if (visible.value) {
- close();
- }
- }
- });
- watch(() => props.fullscreen, (val) => {
- if (!targetRef.value)
- return;
- if (val) {
- lastPosition = targetRef.value.style.transform;
- targetRef.value.style.transform = "";
- } else {
- targetRef.value.style.transform = lastPosition;
- }
- });
- onMounted(() => {
- if (props.modelValue) {
- visible.value = true;
- rendered.value = true;
- open();
- }
- });
- return {
- afterEnter,
- afterLeave,
- beforeLeave,
- handleClose,
- onModalClick,
- close,
- doClose,
- onOpenAutoFocus,
- onCloseAutoFocus,
- onCloseRequested,
- onFocusoutPrevented,
- titleId,
- bodyId,
- closed,
- style,
- overlayDialogStyle,
- rendered,
- visible,
- zIndex
- };
- };
- export { useDialog };
- //# sourceMappingURL=use-dialog.mjs.map
|