@@ -12,48 +12,30 @@ import { getPanelGroupElement, getResizeHandleElementsForGroup } from 'react-res
12
12
13
13
type Direction = 'horizontal' | 'vertical' ;
14
14
15
- export type UsePanelOptions =
16
- | {
17
- id : string ;
18
- /**
19
- * The minimum size of the panel as a percentage.
20
- */
21
- minSize : number ;
22
- /**
23
- * The default size of the panel as a percentage.
24
- */
25
- defaultSize ?: number ;
26
- /**
27
- * The unit of the minSize
28
- */
29
- unit : 'percentages' ;
30
- onCollapse ?: ( isCollapsed : boolean ) => void ;
31
- }
32
- | {
33
- id : string ;
34
- /**
35
- * The minimum size of the panel in pixels.
36
- */
37
- minSize : number ;
38
- /**
39
- * The default size of the panel in pixels.
40
- */
41
- defaultSize ?: number ;
42
- /**
43
- * The unit of the minSize.
44
- */
45
- unit : 'pixels' ;
46
- /**
47
- * The direction of the panel group.
48
- * This is required to accurately calculate the available space for the panel, minus the space taken by the handles.
49
- */
50
- panelGroupDirection : Direction ;
51
- /**
52
- * A ref to the panel group.
53
- */
54
- panelGroupRef : RefObject < ImperativePanelGroupHandle > ;
55
- onCollapse ?: ( isCollapsed : boolean ) => void ;
56
- } ;
15
+ export type UsePanelOptions = {
16
+ id : string ;
17
+ /**
18
+ * The minimum size of the panel in pixels.
19
+ */
20
+ minSize : number ;
21
+ /**
22
+ * The default size of the panel in pixels.
23
+ */
24
+ defaultSize ?: number ;
25
+ /**
26
+ * The direction of the panel group.
27
+ * This is required to accurately calculate the available space for the panel, minus the space taken by the handles.
28
+ */
29
+ panelGroupDirection : Direction ;
30
+ /**
31
+ * A ref to the panel group.
32
+ */
33
+ imperativePanelGroupRef : RefObject < ImperativePanelGroupHandle > ;
34
+ /**
35
+ * Called when the board's collapsed state changes.
36
+ */
37
+ onCollapse ?: ( isCollapsed : boolean ) => void ;
38
+ } ;
57
39
58
40
export type UsePanelReturn = {
59
41
/**
@@ -80,22 +62,28 @@ export type UsePanelReturn = {
80
62
* Resize the panel to the given size in the same units as the minSize.
81
63
*/
82
64
resize : ( size : number ) => void ;
65
+ /**
66
+ * The props to apply to the panel.
67
+ */
83
68
panelProps : Partial < PanelProps & { ref : RefObject < ImperativePanelHandle > } > ;
69
+ /**
70
+ * The props to apply to the resize handle.
71
+ */
84
72
resizeHandleProps : Partial < PanelResizeHandleProps > ;
85
73
} ;
86
74
87
75
export const usePanel = ( arg : UsePanelOptions ) : UsePanelReturn => {
88
76
const imperativePanelRef = useRef < ImperativePanelHandle > ( null ) ;
89
- const [ _minSize , _setMinSize ] = useState < number > ( arg . unit === 'percentages' ? arg . minSize : 0 ) ;
90
- const [ _defaultSize , _setDefaultSize ] = useState < number > ( arg . defaultSize ?? arg . minSize ) ;
77
+ const [ _minSize , _setMinSize ] = useState < number > ( 0 ) ;
78
+ const [ _defaultSize , _setDefaultSize ] = useState < number > ( 0 ) ;
91
79
92
80
// If the units are pixels, we need to calculate the min size as a percentage of the available space,
93
81
// then resize the panel if it is too small.
94
82
useLayoutEffect ( ( ) => {
95
- if ( arg . unit === 'percentages' || ! arg . panelGroupRef . current ) {
83
+ if ( ! arg . imperativePanelGroupRef . current ) {
96
84
return ;
97
85
}
98
- const id = arg . panelGroupRef . current . getId ( ) ;
86
+ const id = arg . imperativePanelGroupRef . current . getId ( ) ;
99
87
const panelGroupElement = getPanelGroupElement ( id ) ;
100
88
const panelGroupHandleElements = getResizeHandleElementsForGroup ( id ) ;
101
89
if ( ! panelGroupElement ) {
@@ -106,7 +94,12 @@ export const usePanel = (arg: UsePanelOptions): UsePanelReturn => {
106
94
return ;
107
95
}
108
96
109
- const minSizePct = getSizeAsPercentage ( arg . minSize , arg . panelGroupRef , arg . panelGroupDirection ) ;
97
+ const minSizePct = getSizeAsPercentage ( arg . minSize , arg . imperativePanelGroupRef , arg . panelGroupDirection ) ;
98
+ const defaultSizePct = getSizeAsPercentage (
99
+ arg . defaultSize || arg . minSize ,
100
+ arg . imperativePanelGroupRef ,
101
+ arg . panelGroupDirection
102
+ ) ;
110
103
111
104
if ( minSizePct > 100 ) {
112
105
// This can happen when the panel is hidden
@@ -115,8 +108,8 @@ export const usePanel = (arg: UsePanelOptions): UsePanelReturn => {
115
108
116
109
_setMinSize ( minSizePct ) ;
117
110
118
- if ( arg . defaultSize && arg . defaultSize > minSizePct ) {
119
- _setDefaultSize ( arg . defaultSize ) ;
111
+ if ( defaultSizePct && defaultSizePct > minSizePct ) {
112
+ _setDefaultSize ( defaultSizePct ) ;
120
113
} else {
121
114
_setDefaultSize ( minSizePct ) ;
122
115
}
@@ -181,14 +174,8 @@ export const usePanel = (arg: UsePanelOptions): UsePanelReturn => {
181
174
182
175
const resize = useCallback (
183
176
( size : number ) => {
184
- // If we are using percentages, we can just resize to the given size
185
- if ( arg . unit === 'percentages' ) {
186
- imperativePanelRef . current ?. resize ( size ) ;
187
- return ;
188
- }
189
-
190
- // If we are using pixels, we need to calculate the size as a percentage of the available space
191
- const sizeAsPct = getSizeAsPercentage ( size , arg . panelGroupRef , arg . panelGroupDirection ) ;
177
+ // We need to calculate the size as a percentage of the available space
178
+ const sizeAsPct = getSizeAsPercentage ( size , arg . imperativePanelGroupRef , arg . panelGroupDirection ) ;
192
179
imperativePanelRef . current ?. resize ( sizeAsPct ) ;
193
180
} ,
194
181
[ arg ]
0 commit comments