@@ -2,7 +2,8 @@ import { ForwardedRef, forwardRef, useEffect, useMemo, useState } from 'react';
2
2
import { useHover , useMove } from 'react-aria' ;
3
3
4
4
import { BasePropsWithoutChildren , Styles , tasty } from '../../tasty/index' ;
5
- import { mergeProps , useCombinedRefs } from '../../utils/react/index' ;
5
+ import { mergeProps , useCombinedRefs } from '../../utils/react' ;
6
+ import { useEvent } from '../../_internal/hooks' ;
6
7
7
8
import { Panel , CubePanelProps } from './Panel' ;
8
9
@@ -217,19 +218,6 @@ function ResizablePanel(
217
218
) ;
218
219
let [ visualSize , setVisualSize ] = useState < number | null > ( null ) ;
219
220
220
- useEffect ( ( ) => {
221
- if ( ref . current ) {
222
- const offsetProp = isHorizontal ? 'offsetWidth' : 'offsetHeight' ;
223
- const containerSize = ref . current [ offsetProp ] ;
224
-
225
- if ( Math . abs ( containerSize - size ) < 1 && ! isDisabled ) {
226
- setVisualSize ( size ) ;
227
- } else {
228
- setVisualSize ( containerSize ) ;
229
- }
230
- }
231
- } , [ size , isDisabled ] ) ;
232
-
233
221
let { moveProps } = useMove ( {
234
222
onMoveStart ( e ) {
235
223
if ( isDisabled ) {
@@ -258,7 +246,7 @@ function ResizablePanel(
258
246
? e . deltaX * ( direction === 'right' ? 1 : - 1 )
259
247
: e . deltaY * ( direction === 'bottom' ? 1 : - 1 ) ;
260
248
261
- return clamp ( size ) ;
249
+ return size ;
262
250
} ) ;
263
251
} ,
264
252
onMoveEnd ( e ) {
@@ -267,16 +255,44 @@ function ResizablePanel(
267
255
} ,
268
256
} ) ;
269
257
258
+ // Since we sync provided size and the local one in two ways
259
+ // we need a way to prevent infinite loop in some cases.
260
+ // We will run this in setTimeout and make sure it will get the most recent state.
261
+ const notifyChange = useEvent ( ( ) => {
262
+ setSize ( ( size ) => {
263
+ if ( providedSize && Math . abs ( providedSize - size ) > 0.5 ) {
264
+ return providedSize ;
265
+ }
266
+
267
+ return size ;
268
+ } ) ;
269
+ } ) ;
270
+
270
271
useEffect ( ( ) => {
271
- if ( providedSize == null || Math . abs ( providedSize - size ) > 0.5 ) {
272
- onSizeChange ?.( Math . round ( size ) ) ;
272
+ if ( ref . current ) {
273
+ const offsetProp = isHorizontal ? 'offsetWidth' : 'offsetHeight' ;
274
+ const containerSize = ref . current [ offsetProp ] ;
275
+
276
+ if ( Math . abs ( containerSize - size ) < 1 && ! isDisabled ) {
277
+ setVisualSize ( size ) ;
278
+ } else {
279
+ setVisualSize ( containerSize ) ;
280
+ }
273
281
}
274
- } , [ size ] ) ;
282
+ } , [ size , isDisabled ] ) ;
275
283
276
284
useEffect ( ( ) => {
277
- if ( providedSize && Math . abs ( providedSize - size ) > 0.5 ) {
278
- setSize ( clamp ( providedSize ) ) ;
285
+ if (
286
+ ! isDragging &&
287
+ visualSize != null &&
288
+ ( providedSize == null || Math . abs ( providedSize - visualSize ) > 0.5 )
289
+ ) {
290
+ onSizeChange ?.( Math . round ( visualSize ) ) ;
279
291
}
292
+ } , [ visualSize ] ) ;
293
+
294
+ useEffect ( ( ) => {
295
+ setTimeout ( notifyChange ) ;
280
296
} , [ providedSize ] ) ;
281
297
282
298
const mods = useMemo ( ( ) => {
0 commit comments