2022-05-19 16:16:46 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2024-05-31 03:49:36 +00:00
|
|
|
import clsx from 'clsx';
|
2023-03-02 14:26:00 +00:00
|
|
|
import { useState } from '@wordpress/element';
|
2022-05-19 16:16:46 +00:00
|
|
|
import { ResizableBox } from '@wordpress/components';
|
2023-04-28 10:29:45 +00:00
|
|
|
import { useThrottledCallback } from 'use-debounce';
|
2022-05-19 16:16:46 +00:00
|
|
|
|
2022-05-30 14:38:52 +00:00
|
|
|
type ResizeCallback = Exclude< ResizableBox.Props[ 'onResize' ], undefined >;
|
|
|
|
|
2022-05-19 16:16:46 +00:00
|
|
|
export const ConstrainedResizable = ( {
|
|
|
|
className = '',
|
|
|
|
onResize,
|
|
|
|
...props
|
2022-05-30 14:38:52 +00:00
|
|
|
}: ResizableBox.Props ): JSX.Element => {
|
2022-05-19 16:16:46 +00:00
|
|
|
const [ isResizing, setIsResizing ] = useState( false );
|
|
|
|
|
2024-05-31 03:49:36 +00:00
|
|
|
const classNames = clsx( className, {
|
2022-05-19 16:16:46 +00:00
|
|
|
'is-resizing': isResizing,
|
|
|
|
} );
|
2023-04-28 10:29:45 +00:00
|
|
|
const throttledResize = useThrottledCallback< ResizeCallback >(
|
2022-05-30 14:38:52 +00:00
|
|
|
( event, direction, elt, _delta ) => {
|
2022-05-19 16:16:46 +00:00
|
|
|
if ( ! isResizing ) setIsResizing( true );
|
2022-05-30 14:38:52 +00:00
|
|
|
onResize?.( event, direction, elt, _delta );
|
2022-05-19 16:16:46 +00:00
|
|
|
},
|
|
|
|
50,
|
|
|
|
{ leading: true }
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ResizableBox
|
|
|
|
className={ classNames }
|
|
|
|
enable={ { bottom: true } }
|
|
|
|
onResize={ throttledResize }
|
|
|
|
onResizeStop={ ( ...args ) => {
|
2022-05-30 14:38:52 +00:00
|
|
|
onResize?.( ...args );
|
2022-05-19 16:16:46 +00:00
|
|
|
setIsResizing( false );
|
|
|
|
} }
|
|
|
|
{ ...props }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|