mirror of
https://gitee.com/dapppp/ruoyi-plus-vben5.git
synced 2026-03-22 07:58:58 +08:00
feat: 侧边栏宽度拖拽改为composable实现,同时修复tabbar.ts文件lint报错
This commit is contained in:
@@ -2,6 +2,7 @@ export * from './use-is-mobile';
|
||||
export * from './use-layout-style';
|
||||
export * from './use-namespace';
|
||||
export * from './use-priority-value';
|
||||
export * from './use-resizable';
|
||||
export * from './use-scroll-lock';
|
||||
export * from './use-simple-locale';
|
||||
export * from './use-sortable';
|
||||
|
||||
72
packages/@core/composables/src/use-resizable.ts
Normal file
72
packages/@core/composables/src/use-resizable.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { onUnmounted, ref } from 'vue';
|
||||
|
||||
interface ResizableOptions {
|
||||
max?: number;
|
||||
min?: number;
|
||||
onChange?: (newWidth: number) => void;
|
||||
}
|
||||
|
||||
export function useResizable(options: ResizableOptions = {}) {
|
||||
const { min = 0, max = 999, onChange } = options;
|
||||
|
||||
const isDragging = ref(false);
|
||||
|
||||
let cleanup: (() => void) | null = null;
|
||||
|
||||
let userSelect = '';
|
||||
let cursor = '';
|
||||
|
||||
const startDrag = (e: MouseEvent, width: number) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
isDragging.value = true;
|
||||
const startX = e.clientX;
|
||||
const startWidth = width;
|
||||
|
||||
userSelect = document.body.style.userSelect;
|
||||
cursor = document.body.style.cursor;
|
||||
|
||||
document.body.style.userSelect = 'none';
|
||||
document.body.style.cursor = 'col-resize';
|
||||
|
||||
const onMouseMove = (moveEvent: MouseEvent) => {
|
||||
if (!isDragging.value) return;
|
||||
|
||||
const deltaX = moveEvent.clientX - startX;
|
||||
let newWidth = startWidth + deltaX;
|
||||
|
||||
newWidth = Math.min(max, Math.max(min, newWidth));
|
||||
|
||||
onChange?.(newWidth);
|
||||
};
|
||||
|
||||
const onMouseUp = () => {
|
||||
if (!isDragging.value) return;
|
||||
cleanup?.();
|
||||
};
|
||||
|
||||
document.addEventListener('mousemove', onMouseMove, { passive: true });
|
||||
document.addEventListener('mouseup', onMouseUp);
|
||||
|
||||
cleanup = () => {
|
||||
document.removeEventListener('mousemove', onMouseMove);
|
||||
document.removeEventListener('mouseup', onMouseUp);
|
||||
|
||||
document.body.style.userSelect = userSelect;
|
||||
document.body.style.cursor = cursor;
|
||||
|
||||
isDragging.value = false;
|
||||
cleanup = null;
|
||||
};
|
||||
};
|
||||
|
||||
onUnmounted(() => {
|
||||
cleanup?.();
|
||||
});
|
||||
|
||||
return {
|
||||
isDragging,
|
||||
startDrag,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user