UNPKG

hongluan-ui

Version:
193 lines (190 loc) 4.84 kB
import { ref, computed, getCurrentInstance, watch, nextTick, onMounted } from 'vue'; import { isUndefined } from 'lodash-unified'; import { useTimeoutFn, isClient } from '@vueuse/core'; import '../../../utils/index.mjs'; import '../../../constants/index.mjs'; import '../../../hooks/index.mjs'; import { useId } from '../../../hooks/use-id/index.mjs'; import { useZIndex } from '../../../hooks/use-z-index/index.mjs'; import { UPDATE_MODEL_EVENT } from '../../../constants/event.mjs'; import { useLockscreen } from '../../../hooks/use-lockscreen/index.mjs'; const CLOSE_EVENT = "close"; const OPEN_EVENT = "open"; const CLOSED_EVENT = "closed"; const OPENED_EVENT = "opened"; const useDialog = (props, targetRef) => { let lastPosition = ""; const titleId = useId(); const bodyId = useId(); const visible = ref(false); const closed = ref(false); const rendered = ref(false); const { nextZIndex } = useZIndex(); const zIndex = ref(props.zIndex || nextZIndex()); let cleanup; let stopOpenTimer = void 0; let stopCloseTimer = void 0; const animName = computed(() => { let name = props.animationName; if (props.showAs === "drawer") { switch (props.placement) { case "left": name = "slideRightSide"; break; case "right": name = "slideLeftSide"; break; case "bottom": name = "slideTopSide"; break; case "top": name = "slideBottomSide"; break; case "fullscreen": name = "scale"; break; } } return name; }); const instance = getCurrentInstance(); const emit = instance.emit; function afterEnter() { emit(OPENED_EVENT); } function afterLeave() { emit(CLOSED_EVENT); emit(UPDATE_MODEL_EVENT, false); if (props.destroyOnClose) { rendered.value = false; } cleanup && cleanup(); } function beforeLeave() { emit(CLOSE_EVENT); } function open() { stopCloseTimer == null ? void 0 : stopCloseTimer(); stopOpenTimer == null ? void 0 : stopOpenTimer(); if (props.openDelay && props.openDelay > 0) { ({ stop: stopOpenTimer } = useTimeoutFn(() => doOpen(), props.openDelay)); } else { doOpen(); } } function close() { stopOpenTimer == null ? void 0 : stopOpenTimer(); stopCloseTimer == null ? void 0 : stopCloseTimer(); if (props.closeDelay && props.closeDelay > 0) { ({ stop: stopCloseTimer } = useTimeoutFn(() => doClose(), props.closeDelay)); } else { doClose(); } } function hide(shouldCancel) { if (shouldCancel) return; closed.value = true; visible.value = false; } function handleClose() { 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 _a; if (((_a = event.detail) == null ? void 0 : _a.focusReason) === "pointer") { event.preventDefault(); } } if (props.lockScroll) { cleanup = useLockscreen(visible, false); } 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_EVENT); if (targetRef.value) { targetRef.value.scrollTop = 0; } }); } else { if (visible.value) { close(); } } }); watch(() => props.placement, (val) => { if (val === "fullscreen") { 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, rendered, animName, visible, zIndex }; }; export { CLOSED_EVENT, CLOSE_EVENT, OPENED_EVENT, OPEN_EVENT, useDialog }; //# sourceMappingURL=use-dialog.mjs.map