11/**
22 * External dependencies
33 */
4- import { FC } from 'react' ;
4+ import { FC , useEffect , useState } from 'react' ;
55import classnames from 'classnames' ;
66
77/**
88 * WordPress dependencies
99 */
1010import { __ } from '@wordpress/i18n' ;
11- import { InspectorControls } from '@wordpress/block-editor' ;
11+ import { InspectorControls , useBlockProps } from '@wordpress/block-editor' ;
1212import {
1313 TextControl ,
1414 PanelBody ,
1515 RangeControl ,
1616 ResizableBox ,
1717 ToggleControl ,
1818} from '@wordpress/components' ;
19+ import { View } from '@wordpress/primitives' ;
1920
2021/**
2122 * Internal dependencies
@@ -31,6 +32,13 @@ const Edit: FC<BlockEditProps> = ({
3132 isSelected,
3233 toggleSelection,
3334} ) => {
35+ const [ isResizing , setIsResizing ] = useState ( false ) ;
36+ const [ _height , setHeight ] = useState ( height ) ;
37+
38+ useEffect ( ( ) => {
39+ setHeight ( height ) ;
40+ } , [ height ] ) ;
41+
3442 const updateTabletHeight = ( value : number | undefined ) =>
3543 setAttributes ( { tabletHeight : value } ) ;
3644
@@ -54,44 +62,60 @@ const Edit: FC<BlockEditProps> = ({
5462
5563 return (
5664 < >
57- < ResizableBox
58- className = { classnames (
59- 'block-library-spacer__resize-container' ,
60- className ,
61- {
62- 'is-selected' : isSelected ,
63- }
64- ) }
65- size = { {
66- height,
67- width : '100%' ,
68- } }
69- minHeight = { MIN_SPACER_HEIGHT }
70- enable = { {
71- top : false ,
72- right : false ,
73- bottom : true ,
74- left : false ,
75- topRight : false ,
76- bottomRight : false ,
77- bottomLeft : false ,
78- topLeft : false ,
79- } }
80- onResizeStart = { ( ) => toggleSelection && toggleSelection ( false ) }
81- onResizeStop = { ( event , direction , elt , delta ) => {
82- if ( toggleSelection ) {
83- toggleSelection ( true ) ;
84- }
65+ < View
66+ { ...useBlockProps ( {
67+ style : {
68+ height : _height ,
69+ } ,
70+ } ) }
71+ >
72+ < ResizableBox
73+ className = { classnames (
74+ 'block-library-spacer__resize-container' ,
75+ className ,
76+ {
77+ 'is-resizing' : isResizing ,
78+ 'is-selected' : isSelected ,
79+ }
80+ ) }
81+ minHeight = { MIN_SPACER_HEIGHT }
82+ enable = { {
83+ top : false ,
84+ right : false ,
85+ bottom : true ,
86+ left : false ,
87+ topRight : false ,
88+ bottomRight : false ,
89+ bottomLeft : false ,
90+ topLeft : false ,
91+ } }
92+ onResizeStart = { ( ) => {
93+ if ( toggleSelection ) {
94+ toggleSelection ( false ) ;
95+ }
96+
97+ setIsResizing ( true ) ;
98+ } }
99+ onResizeStop = { ( event , direction , elt ) => {
100+ if ( toggleSelection ) {
101+ toggleSelection ( true ) ;
102+ }
103+
104+ updateHeight ( elt . clientHeight ) ;
105+
106+ setIsResizing ( false ) ;
107+ } }
108+ onResize = { ( event , direction , elt ) => {
109+ if ( ! isResizing ) {
110+ setIsResizing ( true ) ;
111+ }
85112
86- const spacerHeight = Math . min (
87- height + delta . height ,
88- MAX_SPACER_HEIGHT
89- ) ;
90- updateHeight ( spacerHeight ) ;
91- } }
92- showHandle = { isSelected }
93- children = { undefined } // Fix TypeScript error due to invalid types
94- />
113+ setHeight ( elt . clientHeight ) ;
114+ } }
115+ showHandle = { isSelected }
116+ children = { undefined } // Fix TypeScript error due to invalid types
117+ />
118+ </ View >
95119 < InspectorControls >
96120 < PanelBody title = { __ ( 'Size settings' ) } >
97121 < RangeControl
0 commit comments