1- import classNames from 'classnames' ;
21import * as React from 'react' ;
3- import type { Placement } from './Drawer' ;
2+ import classNames from 'classnames' ;
3+ import type { Placement } from '../Drawer' ;
44
5- export interface ResizableLineProps {
5+ export interface UseDragOptions {
66 prefixCls ?: string ;
77 direction : Placement ;
88 className ?: string ;
99 style ?: React . CSSProperties ;
1010 minSize ?: number ;
1111 maxSize ?: number ;
12- isDragging ?: boolean ;
12+ disabled ?: boolean ;
13+ container ?: HTMLElement | null ;
1314 onResize ?: ( size : number ) => void ;
1415 onResizeEnd ?: ( size : number ) => void ;
1516 onResizeStart ?: ( size : number ) => void ;
16- onDraggingChange ?: ( dragging : boolean ) => void ;
1717}
1818
19- const ResizableLine : React . FC < ResizableLineProps > = ( {
20- prefixCls = 'resizable' ,
21- direction,
22- className,
23- style,
24- minSize = 100 ,
25- maxSize,
26- isDragging = false ,
27- onResize,
28- onResizeEnd,
29- onResizeStart,
30- onDraggingChange,
31- } ) => {
32- const lineRef = React . useRef < HTMLDivElement > ( null ) ;
19+ export interface UseDragReturn {
20+ dragElementProps : {
21+ className : string ;
22+ style : React . CSSProperties ;
23+ onMouseDown : ( e : React . MouseEvent ) => void ;
24+ } ;
25+ isDragging : boolean ;
26+ }
27+
28+ export default function useDrag ( options : UseDragOptions ) : UseDragReturn {
29+ const {
30+ prefixCls = 'resizable' ,
31+ direction,
32+ className,
33+ style,
34+ minSize = 100 ,
35+ maxSize,
36+ disabled = false ,
37+ container,
38+ onResize,
39+ onResizeEnd,
40+ onResizeStart,
41+ } = options ;
42+
43+ const [ isDragging , setIsDragging ] = React . useState ( false ) ;
3344 const [ startPos , setStartPos ] = React . useState ( 0 ) ;
3445 const [ startSize , setStartSize ] = React . useState ( 0 ) ;
3546
3647 const isHorizontal = direction === 'left' || direction === 'right' ;
3748
3849 const handleMouseDown = React . useCallback (
3950 ( e : React . MouseEvent ) => {
51+ if ( disabled ) return ;
52+
4053 e . preventDefault ( ) ;
4154 e . stopPropagation ( ) ;
4255
43- onDraggingChange ?. ( true ) ;
56+ setIsDragging ( true ) ;
4457
4558 if ( isHorizontal ) {
4659 setStartPos ( e . clientX ) ;
4760 } else {
4861 setStartPos ( e . clientY ) ;
4962 }
5063
51- // Get the current size of the parent container
52- const parentElement = lineRef . current ?. parentElement ;
53- if ( parentElement ) {
54- const rect = parentElement . getBoundingClientRect ( ) ;
64+ // Get the current size of the container
65+ if ( container ) {
66+ const rect = container . getBoundingClientRect ( ) ;
5567 const currentSize = isHorizontal ? rect . width : rect . height ;
5668 setStartSize ( currentSize ) ;
5769 onResizeStart ?.( currentSize ) ;
5870 }
5971 } ,
60- [ isHorizontal , onResizeStart , onDraggingChange ] ,
72+ [ disabled , isHorizontal , container , onResizeStart ] ,
6173 ) ;
6274
6375 const handleMouseMove = React . useCallback (
6476 ( e : MouseEvent ) => {
65- if ( ! isDragging ) return ;
77+ if ( ! isDragging || disabled ) return ;
6678
6779 const currentPos = isHorizontal ? e . clientX : e . clientY ;
6880 let delta = currentPos - startPos ;
@@ -87,6 +99,7 @@ const ResizableLine: React.FC<ResizableLineProps> = ({
8799 } ,
88100 [
89101 isDragging ,
102+ disabled ,
90103 startPos ,
91104 startSize ,
92105 direction ,
@@ -98,18 +111,17 @@ const ResizableLine: React.FC<ResizableLineProps> = ({
98111 ) ;
99112
100113 const handleMouseUp = React . useCallback ( ( ) => {
101- if ( isDragging ) {
102- onDraggingChange ?. ( false ) ;
114+ if ( isDragging && ! disabled ) {
115+ setIsDragging ( false ) ;
103116
104117 // Get the final size after resize
105- const parentElement = lineRef . current ?. parentElement ;
106- if ( parentElement ) {
107- const rect = parentElement . getBoundingClientRect ( ) ;
118+ if ( container ) {
119+ const rect = container . getBoundingClientRect ( ) ;
108120 const finalSize = isHorizontal ? rect . width : rect . height ;
109121 onResizeEnd ?.( finalSize ) ;
110122 }
111123 }
112- } , [ isDragging , onResizeEnd , isHorizontal , onDraggingChange ] ) ;
124+ } , [ isDragging , disabled , container , onResizeEnd , isHorizontal ] ) ;
113125
114126 React . useEffect ( ( ) => {
115127 if ( isDragging ) {
@@ -123,30 +135,27 @@ const ResizableLine: React.FC<ResizableLineProps> = ({
123135 }
124136 } , [ isDragging , handleMouseMove , handleMouseUp ] ) ;
125137
126- const resizeLineClassName = classNames (
127- `${ prefixCls } -line ` ,
128- `${ prefixCls } -line -${ direction } ` ,
138+ const dragElementClassName = classNames (
139+ `${ prefixCls } -dragger ` ,
140+ `${ prefixCls } -dragger -${ direction } ` ,
129141 {
130- [ `${ prefixCls } -line-dragging` ] : isDragging ,
142+ [ `${ prefixCls } -dragger-dragging` ] : isDragging ,
143+ [ `${ prefixCls } -dragger-horizontal` ] : isHorizontal ,
144+ [ `${ prefixCls } -dragger-vertical` ] : ! isHorizontal ,
131145 } ,
132146 className ,
133147 ) ;
134148
135- const resizeLineStyle : React . CSSProperties = {
136- position : 'absolute' ,
137- zIndex : 2 ,
138- cursor : isHorizontal ? 'col-resize' : 'row-resize' ,
149+ const dragElementStyle : React . CSSProperties = {
139150 ...style ,
140151 } ;
141152
142- return (
143- < div
144- ref = { lineRef }
145- className = { resizeLineClassName }
146- style = { resizeLineStyle }
147- onMouseDown = { handleMouseDown }
148- />
149- ) ;
150- } ;
151-
152- export default ResizableLine ;
153+ return {
154+ dragElementProps : {
155+ className : dragElementClassName ,
156+ style : dragElementStyle ,
157+ onMouseDown : handleMouseDown ,
158+ } ,
159+ isDragging,
160+ } ;
161+ }
0 commit comments