From b2bc9d64bfadb2b8bfa0a34eccb453d4e99f21c0 Mon Sep 17 00:00:00 2001 From: cc Date: Thu, 3 Dec 2026 17:20:03 +0100 Subject: [PATCH] perf(find): CSS-only overflow button; stable tree props MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - The … button on a value row shows only when chips wrapped out of sight, with no JavaScript: the chip box's scroll timeline is active only when it has something to scroll, and the button's visibility animation runs only then (browsers without scroll-driven animations always show it). Replaces the measured +N count and its ResizeObserver. - osFor and onPickValue are memoized, so typing in the filter no longer re-renders every row of the memoized tree. Co-Authored-By: Claude Opus 5.5 --- src/app/globals.css | 28 ++++++++ src/app/os/find/page.tsx | 32 +++++---- src/components/value-preview.tsx | 116 +++---------------------------- 3 files changed, 59 insertions(+), 117 deletions(-) diff --git a/src/app/globals.css b/src/app/globals.css index 312613e..58a055a 100644 --- a/src/app/globals.css +++ b/src/app/globals.css @@ -200,3 +200,31 @@ .plist-code .linked-key a { color: var(--primary); } + +/* + * A value row's … button shows only when some chips are hidden, in CSS: the + * chip box (overflow: hidden, so a scroll container) has a scroll timeline, + * which is active only when there is something to scroll, i.e. chips wrapped + * onto the hidden second line. The animation that makes the button visible + * runs only while that timeline is active. Browsers without scroll-driven + * animations always show the button. + */ +@supports (animation-timeline: scroll()) { + .value-row { + timeline-scope: --value-chips; + } + .value-chips { + scroll-timeline: --value-chips block; + } + .value-more { + visibility: hidden; + animation: value-more-show linear both; + animation-timeline: --value-chips; + } +} +@keyframes value-more-show { + from, + to { + visibility: visible; + } +} diff --git a/src/app/os/find/page.tsx b/src/app/os/find/page.tsx index 0b49750..d8e79c2 100644 --- a/src/app/os/find/page.tsx +++ b/src/app/os/find/page.tsx @@ -201,13 +201,13 @@ export default function FindByKey() { ); const hasValues = allValues.length > 0 && uniformValue === undefined; - const setSelectedAtoms = (atoms: string[]) => { + const setSelectedAtoms = useCallback((atoms: string[]) => { setSelectedAtomsState(atoms); const next = new URLSearchParams(window.location.search); next.delete("v"); for (const atom of atoms) next.append("v", atom); window.history.replaceState(null, "", `?${next.toString()}`); - }; + }, []); const showChanges = changesOnly && diffReady; @@ -282,6 +282,22 @@ export default function FindByKey() { ? `${group}/${versionTag(activeCompare)}` : os; + // Stable props for the (memoized) tree: inline functions made every + // render, e.g. each keystroke in the filter, re-render all of its rows. + const osFor = useCallback( + (path: string) => (statusOf(path) === "removed" ? compareOs : undefined), + [compareOs, statusOf], + ); + const pickValue = useCallback( + (atom: string) => + setSelectedAtoms( + selectedAtoms.includes(atom) + ? selectedAtoms.filter((a) => a !== atom) + : [...selectedAtoms, atom], + ), + [selectedAtoms, setSelectedAtoms], + ); + /** Keys page at namespace `ns`, keeping an active comparison. */ const keysUrl = (ns: string) => { const query = new URLSearchParams({ os }); @@ -608,18 +624,10 @@ export default function FindByKey() { list={list} expandAll={expandAll} status={diffReady ? statusOf : undefined} - osFor={(path) => - statusOf(path) === "removed" ? compareOs : undefined - } + osFor={osFor} valueFor={hasValues ? valueFor : undefined} highlight={selectedAtoms} - onPickValue={(atom) => - setSelectedAtoms( - selectedAtoms.includes(atom) - ? selectedAtoms.filter((a) => a !== atom) - : [...selectedAtoms, atom], - ) - } + onPickValue={pickValue} /> )} diff --git a/src/components/value-preview.tsx b/src/components/value-preview.tsx index 7aa0e19..f5696ca 100644 --- a/src/components/value-preview.tsx +++ b/src/components/value-preview.tsx @@ -1,6 +1,6 @@ "use client"; -import { useLayoutEffect, useRef, useState } from "react"; +import { MoreHorizontal } from "lucide-react"; import { Popover, @@ -14,74 +14,6 @@ const TITLE_LIMIT = 4000; const collator = new Intl.Collator(undefined, { sensitivity: "base" }); -/** - * Overflow counting (+N), batched across every row. A row's chips wrap onto - * a hidden second line; after layout we count those and end the chip box at - * the last visible chip so +N follows it. Done per row, each measure (reset - * width, read, set width) forced its own layout: ~900 layouts per resize or - * filter change. Here all rows reset, then all read, then all write, in one - * frame: one forced layout for the whole tree. - */ -interface Target { - box: HTMLElement; - setHidden: (hidden: number) => void; -} - -/** Room kept after the last visible chip for the +N button. */ -const MORE_RESERVE = 48; - -const targets = new Map(); -let pending: Set | null = null; -let observer: ResizeObserver | null = null; - -function measureAll() { - const batch = pending ? [...pending] : []; - pending = null; - for (const { box } of batch) box.style.width = ""; - const results = batch.map(({ box }) => { - const items = [...box.children]; - if (items.length < 2) return null; - const rects = items.map((el) => el.getBoundingClientRect()); - const top = rects[0].top; - if (rects.every((r) => r.top === top)) return null; - // Some chips wrapped: keep those that fit with room for +N. - const limit = box.getBoundingClientRect().right - MORE_RESERVE; - const visible = Math.max( - 1, - rects.findIndex((r) => r.top !== top || r.right > limit), - ); - const left = box.getBoundingClientRect().left; - return { - hidden: items.length - visible, - width: Math.ceil(rects[visible - 1].right - left), - }; - }); - batch.forEach(({ box, setHidden }, i) => { - const result = results[i]; - box.style.width = result ? `${result.width}px` : ""; - setHidden(result?.hidden ?? 0); - }); -} - -function schedule(target: Target) { - if (!pending) { - pending = new Set(); - requestAnimationFrame(measureAll); - } - pending.add(target); -} - -/** One observer for every row; its callback batches all resized rows. */ -function sharedObserver() { - observer ??= new ResizeObserver((entries) => { - for (const entry of entries) { - const target = targets.get(entry.target); - if (target) schedule(target); - } - }); - return observer; -} - function titleFor(value: unknown) { const json = JSON.stringify(value, null, 2); return json.length > TITLE_LIMIT ? `${json.slice(0, TITLE_LIMIT)}\n…` : json; @@ -148,32 +80,7 @@ export function ValuePreview({ let body: React.ReactNode; /** Every item, for the overflow menu. */ let all: React.ReactNode = null; - let count: number | null = null; - const outer = useRef(null); - const chips = useRef(null); - // Chips wrapped onto the hidden second line, counted after layout. - const [hidden, setHidden] = useState(0); - - useLayoutEffect(() => { - const el = outer.current; - const box = chips.current; - if (!el || !box) return; - const target: Target = { box, setHidden }; - targets.set(el, target); - sharedObserver().observe(el); - return () => { - targets.delete(el); - observer?.unobserve(el); - pending?.delete(target); - }; - }, []); - // The chips changed (value, or filter hits moving first): re-measure. - // biome-ignore lint/correctness/useExhaustiveDependencies: triggers only - useLayoutEffect(() => { - const target = outer.current && targets.get(outer.current); - if (target) schedule(target); - }, [value, highlight]); - + let count = 1; if (isList || isDict) { // Nested arrays/dicts show as JSON and aren't atoms, so not pickable. const items = isList @@ -213,35 +120,34 @@ export function ValuePreview({ return ( {/* Chips that don't fit wrap onto a hidden second line, so none is - cut mid-word; the +N menu right after the last one lists them all. */} + cut mid-word. The … button lists them all; CSS shows it only when + some are hidden (see .value-more in globals.css). */} {body} - {count !== null && hidden > 0 && ( + {count > 1 && (