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

34 lines
737 B
TypeScript
Raw Normal View History

import { watch, onUnmounted } from 'vue';
2022-01-08 20:49:21 +08:00
import useBoolean from './useBoolean';
import useBodyScroll from './useBodyScroll';
2022-01-14 14:17:34 +08:00
2022-01-08 20:49:21 +08:00
/**
* 使
2022-01-14 14:17:34 +08:00
* @param hideScroll - html滚动条
2022-01-08 20:49:21 +08:00
*/
export default function useModalVisible(hideScroll = true) {
2022-01-08 20:49:21 +08:00
const { bool: visible, setTrue: openModal, setFalse: closeModal, toggle: toggleModal } = useBoolean();
const { scrollBodyHandler } = useBodyScroll();
2022-01-08 20:49:21 +08:00
function modalVisibleWatcher() {
const stopHandle = watch(visible, async (newValue) => {
scrollBodyHandler(newValue);
2022-01-14 14:17:34 +08:00
});
onUnmounted(() => {
stopHandle();
});
}
if (hideScroll) {
modalVisibleWatcher();
2022-01-14 14:17:34 +08:00
}
2022-01-08 20:49:21 +08:00
return {
visible,
openModal,
closeModal,
toggleModal,
2022-01-08 20:49:21 +08:00
};
}