@@ -13,11 +13,34 @@ export type SplitElement = {
1313 minLength : number ;
1414} ;
1515
16- export function useSplit ( minLengths : number [ ] , direction : Orientation ) {
16+ /**
17+ * The `useSplit` function in TypeScript is used to manage resizable elements with specified initial
18+ * lengths and minimum lengths in a given direction.
19+ * @param {number[] | undefined } initialLengths - The `initialLengths` parameter is an array of numbers
20+ * representing the initial lengths of the elements to be split. It can be either `undefined` or an
21+ * array of numbers. If it is `undefined`, if its length is not equal to the length of the
22+ * `minLengths` array or if its sum is not equal to 1, the `initialLengths` will be set proportionally equal.
23+ * @param {number[] } minLengths - The `minLengths` parameter in the `useSplit` function represents an
24+ * array of minimum lengths for each split element. These minimum lengths determine the smallest size
25+ * each element can be resized to. If a user tries to resize an element below its minimum length, the
26+ * element will be collapsed.
27+ * @param {Orientation } direction - The `direction` parameter in the `useSplit` function is used to
28+ * determine the orientation of the split elements. It is of type `Orientation`, which is likely an
29+ * enum or type that specifies whether the split should be horizontal or vertical. This helps in
30+ * calculating the resizing of elements based on the direction
31+ * @returns The `useSplit` function returns an array containing the `elements` state and the
32+ * `handleMouseDown` function from the `separatorEventHandler`.
33+ */
34+ export function useSplit ( minLengths : number [ ] , direction : Orientation , initialLengths ?: number [ ] ) {
35+
36+ if ( ! initialLengths || initialLengths . length != minLengths . length || initialLengths . reduce ( ( prev , curr ) => prev + curr , 0 ) != 1 ) {
37+ initialLengths = minLengths . map ( ( ) => 1 / minLengths . length ) ;
38+ }
39+
1740 const [ elements , setElements ] = useState < SplitElement [ ] > (
18- minLengths . map ( ( minLength ) => ( {
19- length : 1 / minLengths . length ,
20- minLength : minLength ,
41+ initialLengths . map ( ( length , index ) => ( {
42+ length,
43+ minLength : minLengths [ index ] ,
2144 } ) )
2245 ) ;
2346
0 commit comments