1
This commit is contained in:
+23
@@ -0,0 +1,23 @@
|
||||
import * as React from 'react';
|
||||
import type { Placement } from '../Drawer';
|
||||
export interface UseDragOptions {
|
||||
prefixCls: string;
|
||||
direction: Placement;
|
||||
className?: string;
|
||||
style?: React.CSSProperties;
|
||||
maxSize?: number;
|
||||
containerRef?: React.RefObject<HTMLElement>;
|
||||
currentSize?: number | string;
|
||||
onResize?: (size: number) => void;
|
||||
onResizeEnd?: (size: number) => void;
|
||||
onResizeStart?: (size: number) => void;
|
||||
}
|
||||
export interface UseDragReturn {
|
||||
dragElementProps: {
|
||||
className: string;
|
||||
style: React.CSSProperties;
|
||||
onMouseDown: (e: React.MouseEvent) => void;
|
||||
};
|
||||
isDragging: boolean;
|
||||
}
|
||||
export default function useDrag(options: UseDragOptions): UseDragReturn;
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
import * as React from 'react';
|
||||
import { clsx } from 'clsx';
|
||||
import { useEvent } from '@rc-component/util';
|
||||
export default function useDrag(options) {
|
||||
const {
|
||||
prefixCls,
|
||||
direction,
|
||||
className,
|
||||
style,
|
||||
maxSize,
|
||||
containerRef,
|
||||
currentSize,
|
||||
onResize,
|
||||
onResizeEnd,
|
||||
onResizeStart
|
||||
} = options;
|
||||
const [isDragging, setIsDragging] = React.useState(false);
|
||||
const [startPos, setStartPos] = React.useState(0);
|
||||
const [startSize, setStartSize] = React.useState(0);
|
||||
const isHorizontal = direction === 'left' || direction === 'right';
|
||||
const handleMouseDown = useEvent(e => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
setIsDragging(true);
|
||||
if (isHorizontal) {
|
||||
setStartPos(e.clientX);
|
||||
} else {
|
||||
setStartPos(e.clientY);
|
||||
}
|
||||
|
||||
// Use provided currentSize, or fallback to container size
|
||||
let startSize;
|
||||
if (typeof currentSize === 'number') {
|
||||
startSize = currentSize;
|
||||
} else if (containerRef?.current) {
|
||||
const rect = containerRef.current.getBoundingClientRect();
|
||||
startSize = isHorizontal ? rect.width : rect.height;
|
||||
}
|
||||
setStartSize(startSize);
|
||||
onResizeStart?.(startSize);
|
||||
});
|
||||
const handleMouseMove = useEvent(e => {
|
||||
if (!isDragging) return;
|
||||
const currentPos = isHorizontal ? e.clientX : e.clientY;
|
||||
let delta = currentPos - startPos;
|
||||
|
||||
// Adjust delta direction based on placement
|
||||
if (direction === 'right' || direction === 'bottom') {
|
||||
delta = -delta;
|
||||
}
|
||||
let newSize = startSize + delta;
|
||||
|
||||
// Apply min/max size limits
|
||||
if (newSize < 0) {
|
||||
newSize = 0;
|
||||
}
|
||||
// Only apply maxSize if it's a valid positive number
|
||||
if (maxSize && newSize > maxSize) {
|
||||
newSize = maxSize;
|
||||
}
|
||||
onResize?.(newSize);
|
||||
});
|
||||
const handleMouseUp = React.useCallback(() => {
|
||||
if (isDragging) {
|
||||
setIsDragging(false);
|
||||
|
||||
// Get the final size after resize
|
||||
if (containerRef?.current) {
|
||||
const rect = containerRef.current.getBoundingClientRect();
|
||||
const finalSize = isHorizontal ? rect.width : rect.height;
|
||||
onResizeEnd?.(finalSize);
|
||||
}
|
||||
}
|
||||
}, [isDragging, containerRef, onResizeEnd, isHorizontal]);
|
||||
React.useEffect(() => {
|
||||
if (isDragging) {
|
||||
document.addEventListener('mousemove', handleMouseMove);
|
||||
document.addEventListener('mouseup', handleMouseUp);
|
||||
return () => {
|
||||
document.removeEventListener('mousemove', handleMouseMove);
|
||||
document.removeEventListener('mouseup', handleMouseUp);
|
||||
};
|
||||
}
|
||||
}, [isDragging, handleMouseMove, handleMouseUp]);
|
||||
const dragElementClassName = clsx(`${prefixCls}-dragger`, `${prefixCls}-dragger-${direction}`, {
|
||||
[`${prefixCls}-dragger-dragging`]: isDragging,
|
||||
[`${prefixCls}-dragger-horizontal`]: isHorizontal,
|
||||
[`${prefixCls}-dragger-vertical`]: !isHorizontal
|
||||
}, className);
|
||||
return {
|
||||
dragElementProps: {
|
||||
className: dragElementClassName,
|
||||
style,
|
||||
onMouseDown: handleMouseDown
|
||||
},
|
||||
isDragging
|
||||
};
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export default function useFocusable(getContainer: () => HTMLElement, open: boolean, autoFocus?: boolean, focusTrap?: boolean, mask?: boolean): (ele: HTMLElement) => void;
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
import React from 'react';
|
||||
import { useLockFocus } from "@rc-component/util/es/Dom/focus";
|
||||
export default function useFocusable(getContainer, open, autoFocus, focusTrap, mask) {
|
||||
const mergedFocusTrap = focusTrap ?? mask !== false;
|
||||
|
||||
// Focus lock
|
||||
const [ignoreElement] = useLockFocus(open && mergedFocusTrap, getContainer);
|
||||
|
||||
// Auto Focus
|
||||
React.useEffect(() => {
|
||||
if (open && autoFocus === true) {
|
||||
getContainer()?.focus({
|
||||
preventScroll: true
|
||||
});
|
||||
}
|
||||
}, [open]);
|
||||
return ignoreElement;
|
||||
}
|
||||
Reference in New Issue
Block a user