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