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

ui.tsx

229 lines6.5 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 { usePreviousPath } from "@/lib/history-tracker";
6import { cn } from "@/lib/utils";
7​
8/* iOS 26 "Liquid Glass": translucent capsules floating over the content. */
9const glass =
10 "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";
11​
12export function GlassCircle({ className, ...props }: ComponentProps<"button">) {
13 return (
14 <button
15 type="button"
16 className={cn(
17 glass,
18 "grid size-11 shrink-0 place-items-center rounded-full [&_svg]:size-[22px]",
19 className,
20 )}
21 {...props}
22 />
23 );
25​
26function BackAnchor({ className, href, onClick, ...props }: ComponentProps<"a">) {
27 const router = useRouter();
28 const previousPath = usePreviousPath();
29​
30 return (
31 <a
32 {...props}
33 href={href}
34 onClick={(event) => {
35 const plainClick = !event.metaKey && !event.ctrlKey && !event.shiftKey && !event.altKey;
36 // Came here from the parent screen: pop back to it, like a real navigation
37 // stack, so history doesn't grow and the parent's scroll is restored.
38 if (plainClick && href && previousPath === new URL(href, window.location.href).pathname) {
39 event.preventDefault();
40 router.history.back();
41 return;
42 }
43 // Opened directly (or from elsewhere): go to the parent screen.
44 onClick?.(event);
45 }}
46 className={cn(glass, "grid size-11 shrink-0 place-items-center rounded-full", className)}
47 >
48 <ChevronLeft className="size-6 -translate-x-px" strokeWidth={2.2} />
49 </a>
50 );
52​
53/**
54 * Round floating back button (top left of every pushed screen). A typed router
55 * link to the parent screen that pops history when the parent is the previous entry.
56 */
57export const BackLink = createLink(BackAnchor);
58​
59export function GlassPill({ className, children }: { className?: string; children: ReactNode }) {
60 return (
61 <div className={cn(glass, "flex h-11 items-center rounded-full px-1.5", className)}>
62 {children}
63 </div>
64 );
66​
67export function GlassPillButton({ className, ...props }: ComponentProps<"button">) {
68 return (
69 <button
70 type="button"
71 className={cn("grid h-11 min-w-11 place-items-center px-2 [&_svg]:size-[21px]", className)}
72 {...props}
73 />
74 );
76​
77interface IosScreenProps {
78 /** `grouped` for lists of inset cards, `plain` for the editor. */
79 background?: "grouped" | "plain";
80 leading?: ReactNode;
81 trailing?: ReactNode;
82 bottom?: ReactNode;
83 children: ReactNode;
85​
86/** One full-screen page of the iPhone navigation stack. */
87export function IosScreen({
88 background = "grouped",
89 leading,
90 trailing,
91 bottom,
92 children,
93}: IosScreenProps) {
94 return (
95 <div
96 className={cn(
97 "group/screen relative h-full font-sans text-ios-text",
98 background === "grouped" ? "bg-ios-bg" : "bg-ios-plain",
99 )}
100 >
101 <div data-page-scroll className="notes-scroll h-full overflow-y-auto px-4 pt-[68px] pb-28">
102 {children}
103 </div>
104​
105 <div
106 className={cn(
107 "pointer-events-none absolute inset-x-0 top-0 h-16 bg-gradient-to-b to-transparent",
108 background === "grouped"
109 ? "from-ios-bg via-ios-bg/80"
110 : "from-ios-plain via-ios-plain/80",
111 )}
112 />
113 <div className="absolute inset-x-0 top-0 flex items-center justify-between gap-2 px-4 pt-3">
114 <div className="flex items-center gap-2">{leading}</div>
115 <div className="flex items-center gap-2">{trailing}</div>
116 </div>
117​
118 {bottom && (
119 <div className="absolute inset-x-0 bottom-0 flex items-center gap-3 px-5 pb-4">
120 {bottom}
121 </div>
122 )}
123 </div>
124 );
126​
127export function LargeTitle({ title, subtitle }: { title: string; subtitle?: string }) {
128 return (
129 <header className="mb-4 px-1">
130 <h1 className="text-[32px] leading-tight font-bold tracking-[-0.02em]">{title}</h1>
131 {subtitle && <p className="text-[15px] text-ios-secondary">{subtitle}</p>}
132 </header>
133 );
135​
136export function SectionHeader({
137 children,
138 chevron,
139 onToggle,
140 open = true,
141}: {
142 children: ReactNode;
143 chevron?: boolean;
144 onToggle?: () => void;
145 open?: boolean;
146}) {
147 const content = (
148 <>
149 <span className="flex-1 text-left">{children}</span>
150 {chevron && (
151 <ChevronDown
152 className={cn("size-5 text-ios-secondary transition-transform", !open && "-rotate-90")}
153 />
154 )}
155 </>
156 );
157 const className = "flex w-full items-center px-1 pt-6 pb-2.5 text-[21px] font-bold";
158 return onToggle ? (
159 <button type="button" onClick={onToggle} aria-expanded={open} className={className}>
160 {content}
161 </button>
162 ) : (
163 <h2 className={className}>{content}</h2>
164 );
166​
167/** A rounded, inset grouped list card. */
168export function InsetCard({ className, children }: { className?: string; children: ReactNode }) {
169 return (
170 <ul className={cn("overflow-hidden rounded-[26px] bg-ios-card", className)}>{children}</ul>
171 );
173​
174/**
175 * A row in an inset card. The separator starts where the text starts
176 * (`separatorInset`), like UIKit's inset separators.
177 */
178export function InsetRow({
179 children,
180 first,
181 separatorInset = 16,
182}: {
183 children: ReactNode;
184 first?: boolean;
185 separatorInset?: number;
186}) {
187 return (
188 <li className="relative">
189 {!first && (
190 <span
191 aria-hidden
192 className="absolute top-0 right-4 h-px bg-ios-separator"
193 style={{ left: separatorInset }}
194 />
195 )}
196 {children}
197 </li>
198 );
200​
201export function RowChevron() {
202 return <ChevronRight className="size-[18px] shrink-0 text-ios-secondary/70" strokeWidth={2.4} />;
204​
205/** Bottom bar of list screens: search capsule + round compose button. */
206export function SearchAndCompose() {
207 return (
208 <>
209 <label className={cn(glass, "flex h-12 min-w-0 flex-1 items-center gap-2 rounded-full px-4")}>
210 <Search className="size-5 shrink-0 text-ios-text" />
211 <input
212 type="search"
213 placeholder="Search"
214 className="min-w-0 flex-1 bg-transparent text-[17px] outline-none placeholder:text-ios-secondary"
215 />
216 <Mic className="size-5 shrink-0 text-ios-text" />
217 </label>
218 <ComposeButton />
219 </>
220 );
222​
223export function ComposeButton() {
224 return (
225 <GlassCircle aria-label="New note" className="size-12">
226 <SquarePen />
227 </GlassCircle>
228 );