Open History to see the last commit
1import { useLocation } from "@tanstack/react-router";
2import { useState } from "react";
3import type { ReactNode } from "react";
4import { createPortal } from "react-dom";
5
6import { BrowserFrame } from "@/components/browser/browser-frame";
7import { GuidePanel } from "@/components/guide/guide-panel";
8import { FixedNotesLayout } from "@/components/notes/notes-layout";
9import { usePageTitle } from "@/components/notes/use-page-title";
10import { ResizableHandle, ResizablePanel, ResizablePanelGroup } from "@/components/ui/resizable";
11import { useMediaQuery } from "@/hooks/use-media-query";
12
13import { AppSlot } from "./app-slot";
14import { MobileWorkspace } from "./mobile-workspace";
15
16/** At this width and up, the guide and the app sit side by side. Below it, the guide comes first. */
17export const SPLIT_LAYOUT_MIN_WIDTH = 768;
18
19/**
20 * The page. Wide screens: the guide and the app (in a fake browser) as two
21 * framed panels with a resizable gap between them. Small screens: the guide
22 * full screen, with the app in a sheet that "Try it" buttons open.
23 *
24 * The app is rendered exactly once, here, and portalled into whichever
25 * layout is showing (see `AppSlot`), so resizing across the breakpoint never
26 * remounts it. Only its look changes: on small screens it's pinned to the
27 * iPhone layout.
28 */
29export function Workspace({ app }: { app: ReactNode }) {
30 const isSplit = useMediaQuery(`(min-width: ${SPLIT_LAYOUT_MIN_WIDTH}px)`);
31 // This route renders on the client only (`ssr: false`), so `document` exists.
32 const [appHost] = useState(() => {
33 const host = document.createElement("div");
34 host.className = "h-full";
35 return host;
36 });
37 const appSlot = <AppSlot host={appHost} />;
38
39 return (
40 <>
41 {isSplit ? <SplitWorkspace appSlot={appSlot} /> : <MobileWorkspace appSlot={appSlot} />}
42 {createPortal(
43 <FixedNotesLayout layout={isSplit ? null : "ios"}>{app}</FixedNotesLayout>,
44 appHost,
45 )}
46 </>
47 );
48}
49
50function SplitWorkspace({ appSlot }: { appSlot: ReactNode }) {
51 // Tablets and small laptop windows get a narrower guide and smaller minimums
52 // (the app switches to its iPhone layout when its pane gets narrow). Default
53 // sizes only apply on mount; crossing this width later only changes the limits.
54 const isWide = useMediaQuery("(min-width: 1024px)");
55 const title = usePageTitle();
56 const path = useLocation({ select: (location) => location.pathname });
57
58 return (
59 <main className="h-dvh bg-muted p-2 lg:p-4 dark:bg-[#0c0c0d]">
60 <ResizablePanelGroup orientation="horizontal">
61 <ResizablePanel
62 id="guide"
63 defaultSize={isWide ? "34%" : "42%"}
64 minSize={isWide ? 320 : 280}
65 >
66 <Frame>
67 <GuidePanel />
68 </Frame>
69 </ResizablePanel>
70 <GapHandle />
71 <ResizablePanel id="app" defaultSize={isWide ? "66%" : "58%"} minSize={isWide ? 720 : 360}>
72 <Frame>
73 <BrowserFrame title={title} path={path}>
74 {appSlot}
75 </BrowserFrame>
76 </Frame>
77 </ResizablePanel>
78 </ResizablePanelGroup>
79 </main>
80 );
81}
82
83function Frame({ children }: { children: ReactNode }) {
84 return (
85 <div className="h-full overflow-hidden rounded-xl border bg-background shadow-sm lg:rounded-2xl">
86 {children}
87 </div>
88 );
89}
90
91/** The divider lives in the gap between the two panels. */
92function GapHandle() {
93 return (
94 <ResizableHandle className="group/handle w-3 bg-transparent after:hidden focus-visible:ring-0 lg:w-4">
95 <span className="h-10 w-1 rounded-full bg-foreground/15 transition-colors group-hover/handle:bg-foreground/40 group-data-[separator=hover]/handle:bg-foreground/40 group-data-[separator=active]/handle:bg-guide-accent group-focus-visible/handle:bg-guide-accent" />
96 </ResizableHandle>
97 );
98}