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

browser-frame.tsx

145 lines4.0 KB

1import { ArrowLeft, ArrowRight, Lock, Plus, RotateCw, X } from "lucide-react";
2import type { ReactNode } from "react";
3​
4import { cn } from "@/lib/utils";
5​
6import { useBrowserHistory } from "./use-browser-history";
7​
8export const FAKE_HOST = "notes.app";
9​
10interface BrowserFrameProps {
11 /** Shown in the tab. */
12 title: string;
13 /** Path part of the fake URL, e.g. `/folders/work`. */
14 path: string;
15 /** Mobile: one compact toolbar row and no tab strip. */
16 compact?: boolean;
17 /** Reserved for latency and failure controls (phase 4). */
18 controls?: ReactNode;
19 children: ReactNode;
21​
22export function BrowserFrame({
23 title,
24 path,
25 compact = false,
26 controls,
27 children,
28}: BrowserFrameProps) {
29 return (
30 <div className="flex h-full min-h-0 flex-col bg-browser-frame">
31 {!compact && <TabStrip title={title} />}
32 <Toolbar path={path} compact={compact} controls={controls} />
33 <div className="relative min-h-0 flex-1 overflow-hidden">{children}</div>
34 </div>
35 );
37​
38function TabStrip({ title }: { title: string }) {
39 return (
40 <div className="flex h-10 shrink-0 items-end gap-2 px-3">
41 <div className="mr-2 flex h-full items-center gap-2" aria-hidden>
42 <span className="size-3 rounded-full bg-[#ff5f57]" />
43 <span className="size-3 rounded-full bg-[#febc2e]" />
44 <span className="size-3 rounded-full bg-[#28c840]" />
45 </div>
46 <div className="relative flex h-8 w-56 min-w-0 items-center gap-2 rounded-t-lg bg-browser-toolbar pr-2 pl-3 text-xs">
47 <NotesFavicon />
48 <span className="min-w-0 flex-1 truncate">{title}</span>
49 <X className="size-3.5 shrink-0 text-muted-foreground" aria-hidden />
50 </div>
51 <Plus className="mb-2 size-4 text-muted-foreground" aria-hidden />
52 </div>
53 );
55​
56function Toolbar({
57 path,
58 compact,
59 controls,
60}: {
61 path: string;
62 compact: boolean;
63 controls?: ReactNode;
64}) {
65 const { canGoBack, canGoForward, back, forward, reload } = useBrowserHistory();
66​
67 return (
68 <div
69 className={cn(
70 "flex shrink-0 items-center gap-1 border-b bg-browser-toolbar px-2",
71 compact ? "h-10" : "h-11",
72 )}
73 >
74 <ToolbarButton label="Back" onClick={back} disabled={!canGoBack}>
75 <ArrowLeft />
76 </ToolbarButton>
77 <ToolbarButton label="Forward" onClick={forward} disabled={!canGoForward}>
78 <ArrowRight />
79 </ToolbarButton>
80 {!compact && (
81 <ToolbarButton label="Reload" onClick={reload}>
82 <RotateCw />
83 </ToolbarButton>
84 )}
85 <AddressBar path={path} />
86 <div data-slot="browser-controls" className="flex min-w-0 items-center gap-1 empty:hidden">
87 {controls}
88 </div>
89 </div>
90 );
92​
93function AddressBar({ path }: { path: string }) {
94 const fakePath = path === "/" ? "" : path;
95 return (
96 <div
97 className="mx-1 flex h-7 min-w-0 flex-1 items-center gap-1.5 rounded-full bg-browser-field px-3 text-[13px]"
98 aria-label="Address"
99 role="textbox"
100 aria-readonly
101 >
102 <Lock className="size-3 shrink-0 text-muted-foreground" aria-hidden />
103 <span className="min-w-0 truncate">
104 <span className="text-foreground">{FAKE_HOST}</span>
105 <span className="text-muted-foreground">{fakePath}</span>
106 </span>
107 </div>
108 );
110​
111function ToolbarButton({
112 label,
113 disabled,
114 onClick,
115 children,
116}: {
117 label: string;
118 disabled?: boolean;
119 onClick: () => void;
120 children: ReactNode;
121}) {
122 return (
123 <button
124 type="button"
125 aria-label={label}
126 title={label}
127 disabled={disabled}
128 onClick={onClick}
129 className="grid size-7 shrink-0 place-items-center rounded-md text-foreground/70 transition-colors hover:bg-black/5 disabled:opacity-35 disabled:hover:bg-transparent dark:hover:bg-white/10 [&_svg]:size-4"
130 >
131 {children}
132 </button>
133 );
135​
136function NotesFavicon() {
137 return (
138 <span
139 aria-hidden
140 className="grid size-4 shrink-0 place-items-center rounded-[4px] bg-gradient-to-b from-[#ffd54a] to-[#f2b705]"
141 >
142 <span className="h-[2px] w-2 rounded bg-white/90" />
143 </span>
144 );