1
1
import * as React from 'react' ;
2
2
import { TileLoop } from '../TileLoop' ;
3
3
import { ParticipantFilter , useParticipants } from '../../hooks' ;
4
- import { mergeProps } from '../../utils' ;
4
+ import { mergeProps , useSize } from '../../utils' ;
5
5
6
6
export interface GridLayoutProps extends React . HTMLAttributes < HTMLDivElement > {
7
7
/**
@@ -25,22 +25,33 @@ export interface GridLayoutProps extends React.HTMLAttributes<HTMLDivElement> {
25
25
*/
26
26
export function GridLayout ( { filter, filterDependencies, ...props } : GridLayoutProps ) {
27
27
const participants = useParticipants ( { filter, filterDependencies } ) ;
28
+ const containerEl = React . createRef < HTMLDivElement > ( ) ;
28
29
const gridEl = React . createRef < HTMLDivElement > ( ) ;
29
30
31
+ const { width, height } = useSize ( containerEl ) ;
32
+
30
33
React . useEffect ( ( ) => {
31
- gridEl . current ?. style . setProperty ( '--lk-p-count' , participants . length . toFixed ( 0 ) ) ;
32
- gridEl . current ?. style . setProperty (
33
- '--lk-col-count' ,
34
- Math . ceil ( Math . sqrt ( participants . length ) ) . toString ( ) ,
35
- ) ;
36
- } , [ participants , gridEl ] ) ;
34
+ const containerRatio = width / height ;
35
+ const tileRatio = 16 / 10 ;
36
+ const colAdjust = Math . sqrt ( containerRatio / tileRatio ) ;
37
+ const colFraction = Math . sqrt ( participants . length ) * colAdjust ;
38
+ const cols = Math . max ( participants . length === 1 ? 1 : 2 , Math . round ( colFraction ) ) ;
39
+ const widthAdjust = Math . min ( 100 , 100 + ( cols > colFraction ? 1 : - 1 ) * ( colFraction % 1 ) * 50 ) ;
40
+ if ( gridEl . current ) {
41
+ gridEl . current . style . setProperty ( '--lk-col-count' , cols . toString ( ) ) ;
42
+ gridEl . current . style . width = `${ widthAdjust } %` ;
43
+ }
44
+ } , [ width , height , participants , gridEl . current ] ) ;
45
+
37
46
const elementProps = React . useMemo (
38
47
( ) => mergeProps ( props , { className : 'lk-grid-layout' } ) ,
39
48
[ props ] ,
40
49
) ;
41
50
return (
42
- < div ref = { gridEl } { ...elementProps } >
43
- { props . children ?? < TileLoop filter = { filter } filterDependencies = { filterDependencies } /> }
51
+ < div ref = { containerEl } className = "lk-grid-layout-wrapper" >
52
+ < div ref = { gridEl } { ...elementProps } >
53
+ { props . children ?? < TileLoop filter = { filter } filterDependencies = { filterDependencies } /> }
54
+ </ div >
44
55
</ div >
45
56
) ;
46
57
}
0 commit comments