ruoyi-plus-soybean/src/hooks/common/useModalVisible.ts

70 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-01-14 14:17:34 +08:00
import { computed, watch, onUnmounted } from 'vue';
import type { ComputedRef } from 'vue';
2022-01-08 20:49:21 +08:00
import useBoolean from './useBoolean';
2022-01-14 14:17:34 +08:00
interface ScrollBodyStyle {
overflow: string;
paddingRight: string;
}
2022-01-08 20:49:21 +08:00
/**
* 使
2022-01-14 14:17:34 +08:00
* @param hideScroll - html滚动条
* @param duration -
2022-01-08 20:49:21 +08:00
*/
2022-01-14 14:17:34 +08:00
export default function useModalVisible(hideScroll = true, duration = 300) {
2022-01-08 20:49:21 +08:00
const { bool: visible, setTrue: openModal, setFalse: closeModal, toggle: toggleModal } = useBoolean();
2022-01-14 14:17:34 +08:00
const defaultStyle: ScrollBodyStyle = {
overflow: '',
paddingRight: ''
};
function getInitBodyStyle() {
2022-01-08 20:49:21 +08:00
if (hideScroll) {
2022-01-14 14:17:34 +08:00
const { overflow, paddingRight } = document.body.style;
Object.assign(defaultStyle, { overflow, paddingRight });
2022-01-08 20:49:21 +08:00
}
2022-01-14 14:17:34 +08:00
}
function setScrollBodyStyle() {
document.body.style.paddingRight = `${window.innerWidth - document.body.clientWidth}px`;
document.body.style.overflow = 'hidden';
}
function resetScrollBodyStyle() {
document.body.style.overflow = defaultStyle.overflow;
document.body.style.paddingRight = defaultStyle.paddingRight;
}
function modalVisibleWatcher(visible: ComputedRef<boolean>) {
const stopHandle = watch(visible, async newValue => {
if (hideScroll) {
if (newValue) {
setScrollBodyStyle();
} else {
setTimeout(() => {
resetScrollBodyStyle();
}, duration);
}
}
});
onUnmounted(() => {
stopHandle();
});
}
function init() {
getInitBodyStyle();
modalVisibleWatcher(computed(() => visible.value));
}
2022-01-08 20:49:21 +08:00
2022-01-14 14:17:34 +08:00
init();
2022-01-08 20:49:21 +08:00
return {
visible,
openModal,
closeModal,
2022-01-14 14:17:34 +08:00
toggleModal,
modalVisibleWatcher
2022-01-08 20:49:21 +08:00
};
}