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

ui.tsx

282 lines8.3 KB

1import { createLink, useRouter } from "@tanstack/react-router";
2import { ChevronDown, ChevronLeft, ChevronRight, Mic, Search, SquarePen } from "lucide-react";
3import type { ComponentProps, ReactNode } from "react";
4​
5import { useCurrentEntrySheetOpen, usePreviousEntry } from "@/lib/history-tracker";
6import { isRecordingSheetState } from "@/lib/sheet-aware-history";
7import { cn } from "@/lib/utils";
8​
9/* iOS 26 "Liquid Glass": translucent capsules floating over the content. */
10const glass =
11 "border border-ios-glass-border bg-ios-glass text-ios-text shadow-[0_6px_24px_rgb(0_0_0/0.12)] backdrop-blur-xl backdrop-saturate-150";
12​
13export function GlassCircle({ className, ...props }: ComponentProps<"button">) {
14 return (
15 <button
16 type="button"
17 className={cn(
18 glass,
19 "grid size-11 shrink-0 place-items-center rounded-full [&_svg]:size-[22px]",
20 className,
21 )}
22 {...props}
23 />
24 );
26​
27/**
28 * Goes up the iPhone navigation stack to `href`: pops history when the parent
29 * screen is the previous entry, like a real navigation stack (so history
30 * doesn't grow and the parent's scroll is restored). Returns `false` when it
31 * can't, and the caller should navigate instead.
32 */
33export function usePopTo(): (href: string) => boolean {
34 const router = useRouter();
35 const previous = usePreviousEntry();
36 const sheetOpen = useCurrentEntrySheetOpen();
37​
38 return (href) => {
39 // Not if that entry is from before the app sheet opened: popping it would close the sheet.
40 const parentIsPrevious =
41 previous?.pathname === new URL(href, window.location.href).pathname &&
42 (!isRecordingSheetState() || previous.sheetOpen === sheetOpen);
43 if (!parentIsPrevious) return false;
44 router.history.back();
45 return true;
46 };
48​
49function BackAnchor({ className, href, onClick, ...props }: ComponentProps<"a">) {
50 const popTo = usePopTo();
51​
52 return (
53 <a
54 {...props}
55 href={href}
56 onClick={(event) => {
57 const plainClick = !event.metaKey && !event.ctrlKey && !event.shiftKey && !event.altKey;
58 if (plainClick && href !== undefined && popTo(href)) {
59 event.preventDefault();
60 return;
61 }
62 // Opened directly (or from elsewhere): go to the parent screen.
63 onClick?.(event);
64 }}
65 className={cn(glass, "grid size-11 shrink-0 place-items-center rounded-full", className)}
66 >
67 <ChevronLeft className="size-6 -translate-x-px" strokeWidth={2.2} />
68 </a>
69 );
71​
72/**
73 * Round floating back button (top left of every pushed screen). A typed router
74 * link to the parent screen that pops history when the parent is the previous entry.
75 */
76export const BackLink = createLink(BackAnchor);
77​
78export function GlassPill({ className, children }: { className?: string; children: ReactNode }) {
79 return (
80 <div className={cn(glass, "flex h-11 items-center rounded-full px-1.5", className)}>
81 {children}
82 </div>
83 );
85​
86export function GlassPillButton({ className, ...props }: ComponentProps<"button">) {
87 return (
88 <button
89 type="button"
90 className={cn("grid h-11 min-w-11 place-items-center px-2 [&_svg]:size-[21px]", className)}
91 {...props}
92 />
93 );
95​
96interface IosScreenProps {
97 /** `grouped` for lists of inset cards, `plain` for the editor. */
98 background?: "grouped" | "plain";
99 leading?: ReactNode;
100 trailing?: ReactNode;
101 bottom?: ReactNode;
102 children: ReactNode;
104​
105/**
106 * One full-screen page of the iPhone navigation stack. The floating buttons
107 * sit on blurred bars (iOS 26's scroll edge effect), and the content is padded
108 * so its first and last rows can always scroll clear of them.
109 */
110export function IosScreen({
111 background = "grouped",
112 leading,
113 trailing,
114 bottom,
115 children,
116}: IosScreenProps) {
117 const grouped = background === "grouped";
118 return (
119 <div
120 className={cn(
121 "group/screen relative h-full font-sans text-ios-text",
122 // Height of the bottom bar: a 3rem pill, 1rem below it, plus the home indicator.
123 "[--ios-bottom-bar:calc(4rem+env(safe-area-inset-bottom))]",
124 grouped ? "bg-ios-bg" : "bg-ios-plain",
125 )}
126 >
127 <div
128 data-page-scroll
129 className={cn(
130 "notes-scroll h-full overflow-y-auto px-4 pt-[68px]",
131 bottom
132 ? "pb-[calc(var(--ios-bottom-bar)+1.5rem)]"
133 : "pb-[calc(2rem+env(safe-area-inset-bottom))]",
134 )}
135 >
136 {children}
137 </div>
138​
139 <div
140 aria-hidden
141 className={cn(
142 "pointer-events-none absolute inset-x-0 top-0 h-[68px] mask-b-from-70% backdrop-blur-xl",
143 grouped ? "bg-ios-bg/85" : "bg-ios-plain/85",
144 )}
145 />
146 <div className="absolute inset-x-0 top-0 flex items-center justify-between gap-2 px-4 pt-3">
147 <div className="flex items-center gap-2">{leading}</div>
148 <div className="flex items-center gap-2">{trailing}</div>
149 </div>
150​
151 {bottom && (
152 <>
153 <div
154 aria-hidden
155 className={cn(
156 "pointer-events-none absolute inset-x-0 bottom-0 h-(--ios-bottom-bar) mask-t-from-50% backdrop-blur-md",
157 grouped ? "bg-ios-bg/70" : "bg-ios-plain/70",
158 )}
159 />
160 <div className="absolute inset-x-0 bottom-0 flex items-center gap-3 px-5 pb-[calc(1rem+env(safe-area-inset-bottom))]">
161 {bottom}
162 </div>
163 </>
164 )}
165 </div>
166 );
168​
169export function LargeTitle({ title, subtitle }: { title: string; subtitle?: string }) {
170 return (
171 <header className="mb-4 px-1">
172 <h1 className="text-[32px] leading-tight font-bold tracking-[-0.02em]">{title}</h1>
173 {subtitle && <p className="text-[15px] text-ios-secondary">{subtitle}</p>}
174 </header>
175 );
177​
178export function SectionHeader({
179 children,
180 chevron,
181 onToggle,
182 open = true,
183}: {
184 children: ReactNode;
185 chevron?: boolean;
186 onToggle?: () => void;
187 open?: boolean;
188}) {
189 const content = (
190 <>
191 <span className="flex-1 text-left">{children}</span>
192 {chevron && (
193 <ChevronDown
194 className={cn("size-5 text-ios-secondary transition-transform", !open && "-rotate-90")}
195 />
196 )}
197 </>
198 );
199 const className = "flex w-full items-center px-1 pt-6 pb-2.5 text-[21px] font-bold";
200 return onToggle ? (
201 <button type="button" onClick={onToggle} aria-expanded={open} className={className}>
202 {content}
203 </button>
204 ) : (
205 <h2 className={className}>{content}</h2>
206 );
208​
209/** A rounded, inset grouped list card. */
210export function InsetCard({
211 className,
212 children,
213 ...props
214}: { className?: string; children: ReactNode } & { "data-spotlight"?: string }) {
215 return (
216 <ul className={cn("overflow-hidden rounded-[26px] bg-ios-card", className)} {...props}>
217 {children}
218 </ul>
219 );
221​
222/**
223 * A row in an inset card. The separator starts where the text starts
224 * (`separatorInset`), like UIKit's inset separators.
225 */
226export function InsetRow({
227 children,
228 first,
229 separatorInset = 16,
230}: {
231 children: ReactNode;
232 first?: boolean;
233 separatorInset?: number;
234}) {
235 return (
236 <li className="relative">
237 {!first && (
238 <span
239 aria-hidden
240 className="absolute top-0 right-4 h-px bg-ios-separator"
241 style={{ left: separatorInset }}
242 />
243 )}
244 {children}
245 </li>
246 );
248​
249export function RowChevron() {
250 return <ChevronRight className="size-[18px] shrink-0 text-ios-secondary/70" strokeWidth={2.4} />;
252​
253/** Bottom bar of list screens: search capsule + round compose button. */
254export function SearchAndCompose({ onCompose }: { onCompose: (() => void) | undefined }) {
255 return (
256 <>
257 <label className={cn(glass, "flex h-12 min-w-0 flex-1 items-center gap-2 rounded-full px-4")}>
258 <Search className="size-5 shrink-0 text-ios-text" />
259 <input
260 type="search"
261 placeholder="Search"
262 className="min-w-0 flex-1 bg-transparent text-[17px] outline-none placeholder:text-ios-secondary"
263 />
264 <Mic className="size-5 shrink-0 text-ios-text" />
265 </label>
266 <ComposeButton onCompose={onCompose} />
267 </>
268 );
270​
271export function ComposeButton({ onCompose }: { onCompose: (() => void) | undefined }) {
272 return (
273 <GlassCircle
274 aria-label="New note"
275 className="size-12 disabled:opacity-40"
276 disabled={!onCompose}
277 onClick={onCompose}
278 >
279 <SquarePen />
280 </GlassCircle>
281 );