"use client"; import { Monitor, Smartphone, Tablet } from "lucide-react"; import React from "react"; import { PanelImperativeHandle } from "react-resizable-panels"; import { ComponentErrorBoundary } from "@/components/error-boundary"; import { ResizableHandle, ResizablePanel, ResizablePanelGroup } from "@/components/ui/resizable"; import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"; import { cn } from "@/lib/utils"; type BlockViewerContext = { resizablePanelRef: React.RefObject; toggleValue: string; setToggleValue: (value: string) => void; }; const BlockViewerContext = React.createContext(null); function useBlockViewer() { const context = React.useContext(BlockViewerContext); if (!context) { throw new Error("useBlockViewer must be used within a BlockViewerProvider."); } return context; } function BlockViewerProvider({ children }: { children: React.ReactNode }) { const resizablePanelRef = React.useRef(null); const [toggleValue, setToggleValue] = React.useState("100"); return ( {children} ); } export function BlockViewer({ className, name, children, ...props }: React.ComponentProps<"div"> & { name: string; }) { return (
{children}
); } export function BlockViewerToolbar({ name, className, toolbarControls, }: React.ComponentProps<"div"> & { name: string; className?: string; toolbarControls?: React.ReactNode; }) { const { resizablePanelRef, toggleValue, setToggleValue } = useBlockViewer(); return (
{!!toolbarControls ? ( toolbarControls ) : ( {name} )}
{ if (value && resizablePanelRef?.current) { resizablePanelRef.current.resize(parseInt(value)); setToggleValue(value); } }} >
); } export function BlockViewerDisplay({ name, className, children, ...props }: React.ComponentProps<"div"> & { name: string; }) { const { resizablePanelRef, setToggleValue } = useBlockViewer(); // Auto-resize to full width when screen goes under lg breakpoint (1024px) React.useEffect(() => { const mql = window.matchMedia("(max-width: 1023px)"); const resizePanel = () => { if (window.innerWidth < 1024 && resizablePanelRef?.current) { resizablePanelRef.current.resize(100); setToggleValue("100"); } }; resizePanel(); mql.addEventListener("change", resizePanel); return () => mql.removeEventListener("change", resizePanel); }, [resizablePanelRef, setToggleValue]); return (
{children}
); }