-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathGridLayout.tsx
More file actions
63 lines (58 loc) · 2.21 KB
/
Copy pathGridLayout.tsx
File metadata and controls
63 lines (58 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import * as React from 'react';
import type { UseParticipantsOptions } from '../../hooks';
import { useGridLayout, usePagination, useSwipe } from '../../hooks';
import { mergeProps } from '../../utils';
import type { TrackReferenceOrPlaceholder, GridLayoutDefinition } from '@livekit/components-core';
import { TrackLoop } from '../TrackLoop';
import { PaginationControl } from '../controls/PaginationControl';
import { PaginationIndicator } from '../controls/PaginationIndicator';
/** @public */
export interface GridLayoutProps
extends React.HTMLAttributes<HTMLDivElement>, Pick<UseParticipantsOptions, 'updateOnlyOn'> {
children: React.ReactNode;
tracks: TrackReferenceOrPlaceholder[];
gridLayouts?: GridLayoutDefinition[];
}
/**
* The `GridLayout` component displays the nested participants in a grid where every participants has the same size.
* It also supports pagination if there are more participants than the grid can display.
* @remarks
* To ensure visual stability when tiles are reordered due to track updates,
* the component uses the `useVisualStableUpdate` hook.
* @example
* ```tsx
* <LiveKitRoom>
* <GridLayout tracks={tracks}>
* <ParticipantTile />
* </GridLayout>
* <LiveKitRoom>
* ```
* @public
*/
export function GridLayout({ tracks, gridLayouts, ...props }: GridLayoutProps) {
const gridEl = React.createRef<HTMLDivElement>();
const elementProps = React.useMemo(
() => mergeProps(props, { className: 'lk-grid-layout' }),
[props],
);
const { layout } = useGridLayout(gridEl, tracks.length, gridLayouts ? { gridLayouts } : undefined);
const pagination = usePagination(layout.maxTiles, tracks);
useSwipe(gridEl, {
onLeftSwipe: pagination.nextPage,
onRightSwipe: pagination.prevPage,
});
return (
<div ref={gridEl} data-lk-pagination={pagination.totalPageCount > 1} {...elementProps}>
<TrackLoop tracks={pagination.tracks}>{props.children}</TrackLoop>
{tracks.length > layout.maxTiles && (
<>
<PaginationIndicator
totalPageCount={pagination.totalPageCount}
currentPage={pagination.currentPage}
/>
<PaginationControl pagesContainer={gridEl} {...pagination} />
</>
)}
</div>
);
}