1717 * under the License.
1818 */
1919import { Box } from "@chakra-ui/react" ;
20- import { forwardRef } from "react" ;
2120import type { PropsWithChildren } from "react" ;
22- import { ResizableBox } from "react-resizable" ;
23- import "react-resizable/css/styles.css" ;
24-
25- import { usePersistentResizableState } from "src/utils/usePersistentResizableState" ;
26-
27- const ResizeHandle = forwardRef < HTMLDivElement > ( ( props , ref ) => (
28- < Box
29- background = "linear-gradient(-45deg, transparent 6px, #ccc 6px, #ccc 8px, transparent 8px, transparent 12px, #ccc 12px, #ccc 14px, transparent 14px)"
30- bottom = { 0 }
31- cursor = "se-resize"
32- height = { 5 }
33- position = "absolute"
34- ref = { ref }
35- right = { 0 }
36- width = { 5 }
37- { ...props }
38- />
39- ) ) ;
21+ import { useEffect , useRef } from "react" ;
22+ import { useLocalStorage } from "usehooks-ts" ;
4023
4124type ResizableWrapperProps = {
4225 readonly defaultSize ?: { height : number ; width : number } ;
@@ -55,28 +38,56 @@ export const ResizableWrapper = ({
5538 maxConstraints = MAX_SIZE ,
5639 storageKey,
5740} : ResizableWrapperProps ) => {
58- const { handleResize, handleResizeStop, size } = usePersistentResizableState ( storageKey , defaultSize ) ;
41+ const ref = useRef < HTMLDivElement > ( null ) ;
42+ const [ storedSize , setStoredSize ] = useLocalStorage ( storageKey , defaultSize ) ;
43+
44+ useEffect ( ( ) => {
45+ const el = ref . current ;
46+
47+ if ( ! el ) {
48+ return undefined ;
49+ }
50+
51+ let timeoutId : ReturnType < typeof setTimeout > ;
52+
53+ const observer = new ResizeObserver ( ( entries ) => {
54+ for ( const entry of entries ) {
55+ const { height, width } = entry . contentRect ;
56+
57+ if ( width > 0 && height > 0 ) {
58+ clearTimeout ( timeoutId ) ;
59+ timeoutId = setTimeout ( ( ) => {
60+ setStoredSize ( { height : Math . round ( height ) , width : Math . round ( width ) } ) ;
61+ } , 300 ) ;
62+ }
63+ }
64+ } ) ;
65+
66+ observer . observe ( el ) ;
67+
68+ return ( ) => {
69+ observer . disconnect ( ) ;
70+ clearTimeout ( timeoutId ) ;
71+ } ;
72+ } , [ setStoredSize ] ) ;
5973
6074 return (
61- < ResizableBox
62- handle = { < ResizeHandle /> }
63- height = { size . height }
64- maxConstraints = { maxConstraints }
65- minConstraints = { [ DEFAULT_SIZE . width , DEFAULT_SIZE . height ] }
66- onResize = { handleResize }
67- onResizeStop = { handleResizeStop }
68- resizeHandles = { [ "se" ] }
69- style = { {
70- backgroundColor : "inherit" ,
71- borderRadius : "inherit" ,
75+ < Box
76+ css = { {
7277 display : "flex" ,
7378 flexDirection : "column" ,
7479 overflow : "hidden" ,
75- position : "relative " ,
80+ resize : "both " ,
7681 } }
77- width = { size . width }
82+ height = { `${ storedSize . height } px` }
83+ maxHeight = { `${ maxConstraints [ 1 ] } px` }
84+ maxWidth = { `${ maxConstraints [ 0 ] } px` }
85+ minHeight = { `${ DEFAULT_SIZE . height } px` }
86+ minWidth = { `${ DEFAULT_SIZE . width } px` }
87+ ref = { ref }
88+ width = { `${ storedSize . width } px` }
7889 >
79- < div > { children } </ div >
80- </ ResizableBox >
90+ { children }
91+ </ Box >
8192 ) ;
8293} ;
0 commit comments