2022-05-19 16:16:46 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import { useState } from 'react';
|
|
|
|
import { ResizableBox } from '@wordpress/components';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { useThrottle } from '../../utils/useThrottle';
|
|
|
|
|
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 );
|
|
|
|
|
|
|
|
const classNames = classnames( className, {
|
|
|
|
'is-resizing': isResizing,
|
|
|
|
} );
|
2022-05-30 14:38:52 +00:00
|
|
|
const throttledResize = useThrottle< ResizeCallback >(
|
|
|
|
( 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 }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|