numbered-lines.tsx
This file
Path
apps/web/src/features/code/numbered-lines.tsx
68 lines · 2.2 KiB · TypeScript
Last changed by
Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
History of this fileTake it
Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 1why, one step back1/**Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
lines 2–3why, one step back2 * Takes Pierre's measures for a file drawn without a header, so the3 * highlighting arrives in place and nothing moves.Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 4–68why, one step back4 */5import type { ReactNode } from "react";67import { linesOf } from "./file-header.tsx";89export const FALLBACK_LINES = 2000;1011const GUTTER =12 "border-background text-muted-foreground hover:text-foreground min-w-[calc(6ch+2px)] border-r-2 pr-[1ch] pl-[2ch] text-right tabular-nums no-underline select-none";1314export const NumberedLines = ({15 bands,16 text,17}: {18 readonly text: string;19 readonly bands?: ReadonlyMap<number, ReactNode>;20}) => {21 const lines = linesOf(text);22 const shown = lines.slice(0, FALLBACK_LINES);2324 return (25 <div className="flex min-w-0 flex-col" data-code="plain">26 <pre className="text-mono leading-code overflow-x-auto py-2 font-mono">27 <code className="grid min-w-full grid-cols-[min-content_1fr]">28 {shown.map((line, index) => {29 const number = index + 1;30 const band = bands?.get(number);3132 // The anchor sits on the line's text: the row around it is33 // `display: contents`, which draws no box to scroll to.34 return (35 <span className="contents" key={number}>36 {band === undefined ? null : (37 <>38 <span39 aria-hidden="true"40 className="border-background border-r-2"41 />42 <span className="font-sans whitespace-normal">{band}</span>43 </>44 )}45 <a className={GUTTER} href={`#L${number}`}>46 {number}47 </a>48 <span49 className="px-[1ch] whitespace-pre"50 data-line={number}51 id={`L${number}`}52 >53 {line}54 </span>55 </span>56 );57 })}58 </code>59 </pre>60 {lines.length > shown.length ? (61 <p className="text-ui text-muted-foreground px-4 py-2">62 The first {FALLBACK_LINES.toLocaleString("en")} of{" "}63 {lines.length.toLocaleString("en")} lines.64 </p>65 ) : null}66 </div>67 );68};