Files
element-call/src/grid/GridLayout.tsx
T

108 lines
3.0 KiB
TypeScript
Raw Normal View History

2024-05-02 18:44:36 -04:00
/*
Copyright 2024 New Vector Ltd.
2024-05-02 18:44:36 -04:00
SPDX-License-Identifier: AGPL-3.0-only
Please see LICENSE in the repository root for full details.
2024-05-02 18:44:36 -04:00
*/
import { type CSSProperties, forwardRef, useCallback, useMemo } from "react";
2024-05-17 16:38:00 -04:00
import { distinctUntilChanged } from "rxjs";
2024-05-16 15:23:10 -04:00
import { useObservableEagerState } from "observable-hooks";
2024-05-02 18:44:36 -04:00
import { type GridLayout as GridLayoutModel } from "../state/CallViewModel";
2024-05-02 18:44:36 -04:00
import styles from "./GridLayout.module.css";
import { useInitial } from "../useInitial";
import { type CallLayout, arrangeTiles } from "./CallLayout";
import { type DragCallback, useUpdateLayout } from "./Grid";
2024-05-02 18:44:36 -04:00
interface GridCSSProperties extends CSSProperties {
"--gap": string;
"--width": string;
"--height": string;
}
2024-07-18 11:33:20 -04:00
/**
* An implementation of the "grid" layout, in which all participants are shown
* together in a scrolling grid.
*/
2024-05-17 16:38:00 -04:00
export const makeGridLayout: CallLayout<GridLayoutModel> = ({
minBounds,
2024-06-07 16:59:56 -04:00
spotlightAlignment,
2024-05-17 16:38:00 -04:00
}) => ({
scrollingOnTop: false,
2024-05-02 18:44:36 -04:00
// The "fixed" (non-scrolling) part of the layout is where the spotlight tile
// lives
fixed: forwardRef(function GridLayoutFixed({ model, Slot }, ref) {
2024-07-25 12:50:28 -04:00
useUpdateLayout();
const alignment = useObservableEagerState(
useInitial(() =>
2024-06-07 16:59:56 -04:00
spotlightAlignment.pipe(
distinctUntilChanged(
(a1, a2) => a1.block === a2.block && a1.inline === a2.inline,
2024-05-02 18:44:36 -04:00
),
),
),
);
const onDragSpotlight: DragCallback = useCallback(
2024-05-02 18:44:36 -04:00
({ xRatio, yRatio }) =>
2024-06-07 16:59:56 -04:00
spotlightAlignment.next({
2024-05-02 18:44:36 -04:00
block: yRatio < 0.5 ? "start" : "end",
inline: xRatio < 0.5 ? "start" : "end",
}),
[],
);
return (
2024-07-24 16:57:20 -04:00
<div ref={ref} className={styles.fixed}>
2024-11-06 04:36:48 -05:00
{model.spotlight && (
<Slot
className={styles.slot}
id="spotlight"
2024-11-06 04:36:48 -05:00
model={model.spotlight}
onDrag={onDragSpotlight}
data-block-alignment={alignment.block}
data-inline-alignment={alignment.inline}
/>
)}
</div>
);
}),
2024-05-02 18:44:36 -04:00
// The scrolling part of the layout is where all the grid tiles live
scrolling: forwardRef(function GridLayout({ model, Slot }, ref) {
2024-07-25 12:50:28 -04:00
useUpdateLayout();
const { width, height: minHeight } = useObservableEagerState(minBounds);
2024-06-07 16:59:56 -04:00
const { gap, tileWidth, tileHeight } = useMemo(
() => arrangeTiles(width, minHeight, model.grid.length),
[width, minHeight, model.grid.length],
);
return (
<div
ref={ref}
className={styles.scrolling}
style={
{
width,
"--gap": `${gap}px`,
2024-06-07 16:59:56 -04:00
"--width": `${Math.floor(tileWidth)}px`,
"--height": `${Math.floor(tileHeight)}px`,
} as GridCSSProperties
}
>
2024-11-06 04:36:48 -05:00
{model.grid.map((m) => (
<Slot
key={m.id}
className={styles.slot}
id={m.id}
model={m}
onVisibilityChange={m.setVisible}
/>
))}
</div>
);
}),
2024-05-02 18:44:36 -04:00
});