import { FloatingCardAnchorPosition } from "@/components/popout/positions/FloatingCardAnchorPosition"; import { FloatingCardMobilePosition } from "@/components/popout/positions/FloatingCardMobilePosition"; import { useIsMobile } from "@/hooks/useIsMobile"; import { PopoutSection } from "@/video/components/popouts/PopoutUtils"; import { useSpringValue, animated, easings } from "@react-spring/web"; import { ReactNode, useCallback, useEffect, useRef, useState } from "react"; import { Icon, Icons } from "../Icon"; import { FloatingDragHandle, MobilePopoutSpacer } from "./FloatingDragHandle"; interface FloatingCardProps { children?: ReactNode; onClose?: () => void; for: string; } interface RootFloatingCardProps extends FloatingCardProps { className?: string; } function CardBase(props: { children: ReactNode }) { const ref = useRef(null); const { isMobile } = useIsMobile(); const height = useSpringValue(0, { config: { easing: easings.easeInOutSine, duration: 300 }, }); const width = useSpringValue(0, { config: { easing: easings.easeInOutSine, duration: 300 }, }); const [pages, setPages] = useState | null>(null); const getNewHeight = useCallback( (updateList = true) => { if (!ref.current) return; const children = ref.current.querySelectorAll( ":scope *[data-floating-page='true']" ); if (updateList) setPages(children); if (children.length === 0) { height.start(0); width.start(0); return; } const lastChild = children[children.length - 1]; const rect = lastChild.getBoundingClientRect(); const rectHeight = lastChild.scrollHeight; if (height.get() === 0) { height.set(rectHeight); width.set(rect.width); } else { height.start(rectHeight); width.start(rect.width); } }, [height, width] ); useEffect(() => { if (!ref.current) return; getNewHeight(); const observer = new MutationObserver(() => { getNewHeight(); }); observer.observe(ref.current, { attributes: false, childList: true, subtree: false, }); return () => { observer.disconnect(); }; }, [getNewHeight]); useEffect(() => { const observer = new ResizeObserver(() => { getNewHeight(false); }); pages?.forEach((el) => observer.observe(el)); return () => { observer.disconnect(); }; }, [pages, getNewHeight]); return ( {props.children} ); } export function FloatingCard(props: RootFloatingCardProps) { const { isMobile } = useIsMobile(); const content = {props.children}; if (isMobile) return ( {content} ); return ( {content} ); } export function PopoutFloatingCard(props: FloatingCardProps) { return ( ); } export const FloatingCardView = { Header(props: { title: string; description: string; close?: boolean; goBack: () => any; action?: React.ReactNode; backText?: string; }) { let left = (
{props.backText || "Go back"}
); if (props.close) left = (
Close
); return (
{left}
{props.action ?? null}

{props.title}

{props.description}

); }, Content(props: { children: React.ReactNode; noSection?: boolean }) { return (
{props.noSection ? (
{props.children}
) : ( {props.children} )}
); }, };