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