Skip to content
Myr

project-frame.tsx

This file

Path

apps/web/src/features/project/project-frame.tsx

104 lines · 3.5 KiB · TypeScript

Last changed by

Merge redesign into mainoscabrielbefore adoptioncaedd21

History of this file

Take it

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 1–104why, one step back
1/**
2 * Every page of a project inside one frame: the project's header across the3 * top, the Code tab's bar under it on that tab's pages, then a page's side4 * column (the file tree beside code, a finish's files) and the page itself.5 */6import { useLocation } from "@tanstack/react-router";7import type { ReactNode } from "react";89import { projectTabOf } from "../shared/shell/sections.ts";10import {11 SideColumnOutlet,12 useSideColumn,13} from "../shared/shell/side-column.tsx";1415/**16 * The header and its tabs stick under the top bar together, so what sticks17 * inside the frame sits under both: `--stick-bar`, `--stick-toolbar` and18 * `--stick-column` move down by `--project-head`, the height the header is19 * drawn at. Until the browser has measured it (the server's HTML, before any20 * scroll), a header with a lede, a facts row and its tabs stands in.21 */22const UNDER_HEAD =23 "[--stick-bar:calc(var(--top-bar)_+_var(--project-head,11.25rem)_-_1px)] [--stick-toolbar:calc(var(--top-bar)_+_var(--project-head,11.25rem)_+_var(--toolbar)_-_1px)] [--stick-column:calc(var(--top-bar)_+_var(--project-head,11.25rem))]";2425const UNDER_BAR = "[--stick-column:var(--top-bar)]";2627const HEAD_HEIGHT = "--project-head";2829/** The frame keeps its header's drawn height, so what sticks below clears it. */30const keepHeight = ([entry]: readonly ResizeObserverEntry[]) => {31 const frame = entry?.target.parentElement;32 const height = entry?.borderBoxSize[0]?.blockSize;3334 if (frame !== null && frame !== undefined && height !== undefined) {35 frame.style.setProperty(HEAD_HEIGHT, `${height}px`);36 }37};3839/**40 * The header's height is read, never assumed: it wraps on a narrow screen41 * and has no lede on a project with no README.42 */43const measureHead = (head: HTMLDivElement | null) => {44 const observer = new ResizeObserver(keepHeight);4546 if (head !== null) {47 observer.observe(head);48 }4950 return () => {51 observer.disconnect();52 head?.parentElement?.style.removeProperty(HEAD_HEIGHT);53 };54};5556/**57 * From `columns` the side column stands beside the page. Below it the column58 * is hidden rather than stacked: the code bar's Files button opens the tree59 * as a sheet, and a finish's toolbar opens its files.60 */61export const ProjectFrame = ({62 children,63 codeBar,64 header,65}: {66 readonly header?: ReactNode;67 /** The Code tab's bar, drawn under the header on that tab's pages only. */68 readonly codeBar?: ReactNode;69 readonly children: ReactNode;70}) => {71 const column = useSideColumn() !== undefined;72 const pathname = useLocation({ select: (location) => location.pathname });7374 const code =75 header !== undefined &&76 codeBar !== undefined &&77 projectTabOf(pathname) === "code";7879 return (80 <div81 className={`flex w-full min-w-0 flex-1 flex-col ${header === undefined ? UNDER_BAR : UNDER_HEAD}`}82 data-project-frame=""83 >84 {header === undefined ? null : (85 <div86 className="bg-background sticky top-(--top-bar) z-20"87 data-project-head=""88 ref={measureHead}89 >90 {header}91 </div>92 )}93 {code ? codeBar : null}94 <div className="flex w-full min-w-0 flex-1">95 {column ? (96 <div className="border-rule columns:flex sticky top-(--stick-column) hidden h-[calc(100dvh-var(--stick-column))] w-(--column-tree) shrink-0 flex-col border-r">97 <SideColumnOutlet />98 </div>99 ) : null}100 <div className="flex min-w-0 flex-1 flex-col">{children}</div>101 </div>102 </div>103 );104};