Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

mobile-workspace.tsx

78 lines2.9 KB

1import { useLocation } from "@tanstack/react-router";
2import { useEffect, useLayoutEffect, useRef } from "react";
3import type { ReactNode } from "react";
4​
5import { AppSheet } from "@/components/app-sheet/app-sheet";
6import {
7 rememberSheetLocation,
8 useIsAppSheetOpen,
9 useOpenAppSheet,
10} from "@/components/app-sheet/app-sheet-store";
11import { GuideDocument } from "@/components/guide/guide-document";
12import { MobileGuideHeader } from "@/components/guide/mobile-guide-header";
13import { TryItProvider } from "@/components/guide/try-it";
14import { useFollowActiveSection } from "@/components/guide/use-follow-active-section";
15import { useReadingPosition } from "@/components/guide/use-reading-position";
16import { captureScrollAnchor, restoreScrollAnchor } from "@/lib/scroll-anchor";
17import { setRecordSheetState } from "@/lib/sheet-aware-history";
18import type { ScrollAnchor } from "@/lib/scroll-anchor";
19​
20/**
21 * Small screens: the guide fills the screen, and each hands-on section's
22 * "Try it" slides the app up as a sheet, already on the screen the section is
23 * about. Whether the sheet is open follows browser history (Back closes it);
24 * closing it puts the guide back exactly where it was.
25 */
26export function MobileWorkspace({ appSlot }: { appSlot: ReactNode }) {
27 const open = useIsAppSheetOpen();
28 const openAppSheet = useOpenAppSheet();
29 const href = useLocation({ select: (location) => location.href });
30 const scrollRef = useRef<HTMLDivElement>(null);
31 const savedScroll = useRef<ScrollAnchor | null>(null);
32​
33 // While this layout shows, history entries record whether the sheet is open.
34 useLayoutEffect(() => {
35 setRecordSheetState(true);
36 return () => setRecordSheetState(false);
37 }, []);
38​
39 // Same place in the guide as before a layout change…
40 useReadingPosition(scrollRef);
41​
42 // …and as before the sheet opened, once it closes.
43 useLayoutEffect(() => {
44 const container = scrollRef.current;
45 if (!container) return;
46 if (open) {
47 savedScroll.current = captureScrollAnchor(container);
48 } else if (savedScroll.current) {
49 restoreScrollAnchor(container, savedScroll.current);
50 savedScroll.current = null;
51 }
52 }, [open]);
53​
54 // The highlighted section can change while the sheet covers the guide: it
55 // lights up now, and the guide scrolls to it once the sheet closes.
56 useFollowActiveSection(scrollRef, open);
57​
58 // Where the app was, so the next "Try it" for this screen picks up there.
59 useEffect(() => {
60 if (open) rememberSheetLocation(href);
61 }, [open, href]);
62​
63 return (
64 <TryItProvider onTryIt={openAppSheet}>
65 <div className="flex h-dvh flex-col bg-background" inert={open}>
66 <MobileGuideHeader scrollRef={scrollRef} />
67 <div
68 ref={scrollRef}
69 data-wrap-code
70 className="min-h-0 flex-1 overflow-y-auto overscroll-contain pb-[env(safe-area-inset-bottom)]"
71 >
72 <GuideDocument />
73 </div>
74 </div>
75 <AppSheet app={appSlot} />
76 </TryItProvider>
77 );