33import React , { useState } from 'react' ;
44
55import { useResize } from '~components/app-layout/visual-refresh-toolbar/drawer/use-resize' ;
6+ import Container from '~components/container' ;
7+ import FormField from '~components/form-field' ;
8+ import Input from '~components/input' ;
69import PanelResizeHandle from '~components/internal/components/panel-resize-handle' ;
10+ import SpaceBetween from '~components/space-between' ;
11+ import { getLimitedValue } from '~components/split-panel/utils/size-utils' ;
712
813import styles from './styles.scss' ;
914
10- export default function ResizablePanelsPage ( ) {
11- const panel1SliderRef = React . createRef < HTMLDivElement > ( ) ;
12- const panel1Ref = React . createRef < HTMLDivElement > ( ) ;
13- const [ panel1Size , setPanel1Size ] = useState ( 200 ) ;
15+ interface PanelProps {
16+ defaultWidth ?: number ;
17+ minWidth : number ;
18+ maxWidth : number ;
19+ onResize ?: ( value : number ) => void ;
20+ }
21+
22+ const Panel : React . FC < PanelProps > = ( { children, maxWidth, minWidth, onResize, defaultWidth = 300 } ) => {
23+ const sliderRef = React . useRef < HTMLDivElement > ( null ) ;
24+ const panelRef = React . useRef < HTMLDivElement > ( null ) ;
25+ const [ panelSize , setPanelSize ] = useState ( defaultWidth ) ;
1426 const resizeProps = useResize ( {
15- currentWidth : panel1Size ,
16- minWidth : 200 ,
17- maxWidth : 700 ,
18- panelRef : panel1Ref ,
19- handleRef : panel1SliderRef ,
27+ currentWidth : panelSize ,
28+ minWidth,
29+ maxWidth,
30+ panelRef : panelRef ,
31+ handleRef : sliderRef ,
2032 position : 'side-start' ,
2133 onResize : size => {
22- console . log ( size ) ;
23- setPanel1Size ( size ) ;
34+ onResize ?. ( size ) ;
35+ setPanelSize ( size ) ;
2436 } ,
2537 } ) ;
38+ const size = getLimitedValue ( minWidth , panelSize , maxWidth ) ;
39+
2640 return (
27- < div className = { styles [ 'panels-container' ] } >
28- < div className = { styles . panel } ref = { panel1Ref } style = { { width : ` ${ panel1Size } px` } } >
29- panel 1
41+ < div className = { styles . panel } ref = { panelRef } style = { { inlineSize : ` ${ size } px` } } >
42+ < div > { children } </ div >
43+ < div className = { styles [ 'panels-slider' ] } >
3044 < PanelResizeHandle
31- ref = { panel1SliderRef }
32- position = "side"
33- className = { styles [ 'panels-slider' ] }
45+ ref = { sliderRef }
46+ position = "side-start"
3447 ariaLabel = "Resize handle"
3548 tooltipText = "Resize handle"
3649 ariaValuenow = { resizeProps . relativeSize }
@@ -39,7 +52,46 @@ export default function ResizablePanelsPage() {
3952 onPointerDown = { resizeProps . onPointerDown }
4053 />
4154 </ div >
42- < div > panel 2</ div >
4355 </ div >
4456 ) ;
57+ } ;
58+
59+ export default function ResizablePanelsPage ( ) {
60+ const [ minWidth , setMinWidth ] = useState ( 200 ) ;
61+ const [ maxWidth , setMaxWidth ] = useState ( 800 ) ;
62+ // window.innerWidth - 300
63+ return (
64+ < >
65+ < div style = { { maxWidth : 400 , padding : 10 } } >
66+ < Container >
67+ < SpaceBetween size = "xs" >
68+ < FormField label = "Min width" >
69+ < Input
70+ type = "number"
71+ ariaLabel = "Min width"
72+ placeholder = "Min width"
73+ value = { minWidth + '' }
74+ onChange = { event => setMinWidth ( parseInt ( event . detail . value ) ) }
75+ />
76+ </ FormField >
77+ < FormField label = "Max width" >
78+ < Input
79+ type = "number"
80+ ariaLabel = "Max width"
81+ placeholder = "Max width"
82+ value = { maxWidth + '' }
83+ onChange = { event => setMaxWidth ( parseInt ( event . detail . value ) ) }
84+ />
85+ </ FormField >
86+ </ SpaceBetween >
87+ </ Container >
88+ </ div >
89+ < div className = { styles [ 'panels-container' ] } >
90+ < Panel minWidth = { minWidth } defaultWidth = { 350 } maxWidth = { maxWidth } >
91+ < div > panel 1</ div >
92+ </ Panel >
93+ < div > panel 2</ div >
94+ </ div >
95+ </ >
96+ ) ;
4597}
0 commit comments