2023-06-12 18:06:18 -04:00
|
|
|
/*
|
|
|
|
Copyright 2023 New Vector Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-06-28 10:59:36 -04:00
|
|
|
import React, { memo, ReactNode, RefObject, useRef } from "react";
|
2023-06-12 18:06:18 -04:00
|
|
|
import { EventTypes, Handler, useDrag } from "@use-gesture/react";
|
|
|
|
import { SpringValue, to } from "@react-spring/web";
|
|
|
|
|
|
|
|
import { ChildrenProperties } from "./VideoGrid";
|
|
|
|
|
2023-06-13 12:33:46 -04:00
|
|
|
interface Props<T> {
|
2023-06-12 18:06:18 -04:00
|
|
|
id: string;
|
|
|
|
onDragRef: RefObject<
|
|
|
|
(
|
|
|
|
tileId: string,
|
|
|
|
state: Parameters<Handler<"drag", EventTypes["drag"]>>[0]
|
|
|
|
) => void
|
|
|
|
>;
|
|
|
|
targetWidth: number;
|
|
|
|
targetHeight: number;
|
2023-06-13 12:33:46 -04:00
|
|
|
data: T;
|
2023-06-12 18:06:18 -04:00
|
|
|
opacity: SpringValue<number>;
|
|
|
|
scale: SpringValue<number>;
|
|
|
|
shadow: SpringValue<number>;
|
|
|
|
shadowSpread: SpringValue<number>;
|
|
|
|
zIndex: SpringValue<number>;
|
|
|
|
x: SpringValue<number>;
|
|
|
|
y: SpringValue<number>;
|
|
|
|
width: SpringValue<number>;
|
|
|
|
height: SpringValue<number>;
|
2023-06-13 12:33:46 -04:00
|
|
|
children: (props: ChildrenProperties<T>) => ReactNode;
|
2023-06-12 18:06:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A wrapper around a tile in a video grid. This component exists to decouple
|
|
|
|
* child components from the grid.
|
|
|
|
*/
|
2023-06-28 10:59:36 -04:00
|
|
|
export const TileWrapper = memo(
|
2023-06-12 18:06:18 -04:00
|
|
|
({
|
|
|
|
id,
|
|
|
|
onDragRef,
|
|
|
|
targetWidth,
|
|
|
|
targetHeight,
|
2023-06-13 12:33:46 -04:00
|
|
|
data,
|
2023-06-12 18:06:18 -04:00
|
|
|
opacity,
|
|
|
|
scale,
|
|
|
|
shadow,
|
|
|
|
shadowSpread,
|
|
|
|
zIndex,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
children,
|
|
|
|
}) => {
|
|
|
|
const ref = useRef<HTMLElement | null>(null);
|
|
|
|
|
|
|
|
useDrag((state) => onDragRef?.current!(id, state), {
|
|
|
|
target: ref,
|
|
|
|
filterTaps: true,
|
|
|
|
preventScroll: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{children({
|
|
|
|
ref,
|
|
|
|
style: {
|
|
|
|
opacity,
|
|
|
|
scale,
|
|
|
|
zIndex,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
boxShadow: to(
|
|
|
|
[shadow, shadowSpread],
|
|
|
|
(s, ss) => `rgba(0, 0, 0, 0.5) 0px ${s}px ${2 * s}px ${ss}px`
|
|
|
|
),
|
|
|
|
},
|
|
|
|
targetWidth,
|
|
|
|
targetHeight,
|
2023-06-13 12:33:46 -04:00
|
|
|
data,
|
2023-06-12 18:06:18 -04:00
|
|
|
})}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
2023-06-28 10:59:36 -04:00
|
|
|
// We pretend this component is a simple function rather than a
|
|
|
|
// NamedExoticComponent, because that's the only way we can fit in a type
|
|
|
|
// parameter
|
|
|
|
) as <T>(props: Props<T>) => JSX.Element;
|