拖拽组件封装
This commit is contained in:
133
src/components/devices/DraggablePanel.jsx
Normal file
133
src/components/devices/DraggablePanel.jsx
Normal file
@@ -0,0 +1,133 @@
|
||||
import React, { useRef, useCallback, useEffect, memo } from 'react';
|
||||
|
||||
// 独立的可拖拽面板组件
|
||||
export const DraggablePanel = memo(({
|
||||
initialX = 0,
|
||||
initialY = 0,
|
||||
width = 200,
|
||||
height = 150,
|
||||
bounds,
|
||||
children,
|
||||
handleContent,
|
||||
onPositionChange
|
||||
}) => {
|
||||
const panelRef = useRef(null);
|
||||
const dragRef = useRef({
|
||||
isDragging: false,
|
||||
startX: 0,
|
||||
startY: 0,
|
||||
originX: initialX,
|
||||
originY: initialY
|
||||
});
|
||||
const positionRef = useRef({ x: initialX, y: initialY });
|
||||
|
||||
// 初始化位置
|
||||
useEffect(() => {
|
||||
if (panelRef.current) {
|
||||
panelRef.current.style.transform = `translate(${initialX}px, ${initialY}px)`;
|
||||
}
|
||||
positionRef.current = { x: initialX, y: initialY };
|
||||
}, [initialX, initialY]);
|
||||
|
||||
// 鼠标移动处理
|
||||
const handleMouseMove = useCallback((e) => {
|
||||
const drag = dragRef.current;
|
||||
if (!drag.isDragging || !panelRef.current) return;
|
||||
|
||||
let newX = drag.originX + (e.clientX - drag.startX);
|
||||
let newY = drag.originY + (e.clientY - drag.startY);
|
||||
|
||||
// 边界限制
|
||||
if (bounds) {
|
||||
newX = Math.max(0, Math.min(newX, bounds.width - width));
|
||||
newY = Math.max(0, Math.min(newY, bounds.height - height));
|
||||
}
|
||||
|
||||
// 直接更新 DOM,性能最优
|
||||
panelRef.current.style.transform = `translate(${newX}px, ${newY}px)`;
|
||||
positionRef.current = { x: newX, y: newY };
|
||||
|
||||
// 可选的位置变化回调
|
||||
if (onPositionChange) {
|
||||
onPositionChange({ x: newX, y: newY });
|
||||
}
|
||||
}, [bounds, width, height, onPositionChange]);
|
||||
|
||||
const handleMouseUp = useCallback(() => {
|
||||
dragRef.current.isDragging = false;
|
||||
}, []);
|
||||
|
||||
// 全局鼠标事件
|
||||
useEffect(() => {
|
||||
window.addEventListener('mousemove', handleMouseMove);
|
||||
window.addEventListener('mouseup', handleMouseUp);
|
||||
return () => {
|
||||
window.removeEventListener('mousemove', handleMouseMove);
|
||||
window.removeEventListener('mouseup', handleMouseUp);
|
||||
};
|
||||
}, [handleMouseMove, handleMouseUp]);
|
||||
|
||||
// 开始拖拽
|
||||
const handleMouseDown = useCallback((e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
dragRef.current = {
|
||||
isDragging: true,
|
||||
startX: e.clientX,
|
||||
startY: e.clientY,
|
||||
originX: positionRef.current.x,
|
||||
originY: positionRef.current.y
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={panelRef}
|
||||
style={{
|
||||
position: 'absolute',
|
||||
width,
|
||||
height,
|
||||
zIndex: 10,
|
||||
willChange: 'transform',
|
||||
pointerEvents: 'auto'
|
||||
}}
|
||||
>
|
||||
{/* 拖拽手柄 */}
|
||||
{handleContent && (
|
||||
<div
|
||||
style={{
|
||||
position: 'absolute',
|
||||
top: 30,
|
||||
left: 0,
|
||||
right: 0,
|
||||
height: 28,
|
||||
borderRadius: 8,
|
||||
//background: 'linear-gradient(to bottom, rgba(0,0,0,0.8), rgba(0,0,0,0.6))',
|
||||
zIndex: 999,
|
||||
cursor: dragRef.current.isDragging ? 'grabbing' : 'grab',
|
||||
display: 'flex',
|
||||
alignItems: 'start',
|
||||
justifyContent: 'start',
|
||||
userSelect: 'none'
|
||||
}}
|
||||
onMouseDown={handleMouseDown}
|
||||
>
|
||||
{handleContent}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 子内容 */}
|
||||
<div style={{
|
||||
position: 'absolute',
|
||||
top: handleContent ? 28 : 0,
|
||||
left: 0,
|
||||
right: 0,
|
||||
bottom: 0
|
||||
}}>
|
||||
{children}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
|
||||
export default DraggablePanel;
|
||||
Reference in New Issue
Block a user