From 6fa80ac895ffb2ea74d6f7766c51e807f5863eb6 Mon Sep 17 00:00:00 2001 From: netcatty-bot <308658023+netcatty-bot@users.noreply.github.com> Date: Fri, 28 Aug 2026 14:38:22 +0000 Subject: [PATCH 01/10] fix(#3195): automated AI fix --- components/combobox.test.ts | 45 +++++++++++++++++++- components/ui/combobox.tsx | 85 +++++++++++++++++++++++++++++++++++-- 2 files changed, 125 insertions(+), 5 deletions(-) diff --git a/components/combobox.test.ts b/components/combobox.test.ts index 3d4bcbdbc..cf7b256b5 100644 --- a/components/combobox.test.ts +++ b/components/combobox.test.ts @@ -4,12 +4,16 @@ import test from "node:test"; import { applyComboboxWheelScroll, + comboboxNextRenderLimit, comboboxWheelDeltaToPixels, filterComboboxOptions, focusComboboxInput, selectComboboxInputIfFocused, canComboboxOpen, getNextComboboxActiveIndex, + shouldExpandComboboxWindow, + COMBOBOX_INITIAL_RENDER_LIMIT, + COMBOBOX_RENDER_LIMIT_STEP, type ComboboxScrollableTarget, } from "./ui/combobox.tsx"; @@ -85,7 +89,7 @@ test("combobox exposes active-option semantics for keyboard navigation", () => { assert.match(source, /role="combobox"/); assert.match(source, /aria-activedescendant=/); assert.match(source, /role=\{listbox \? "listbox" : undefined\}/); - assert.match(source, //); + assert.match(source, //); assert.match(source, /\s*\{/); assert.match(source, /role="option"/); assert.match(source, /open && !disabled && hasActiveOption/); @@ -93,6 +97,45 @@ test("combobox exposes active-option semantics for keyboard navigation", () => { assert.match(source, /const handleCreate = \(\) => \{[\s\S]*?setOpen\(false\)\s*setActiveIndex\(-1\)/); }); +test("combobox render window grows in bounded steps and stops at the end", () => { + assert.equal(comboboxNextRenderLimit(COMBOBOX_INITIAL_RENDER_LIMIT, 10), null); + assert.equal( + comboboxNextRenderLimit(COMBOBOX_INITIAL_RENDER_LIMIT, 500), + COMBOBOX_INITIAL_RENDER_LIMIT + COMBOBOX_RENDER_LIMIT_STEP, + ); + assert.equal(comboboxNextRenderLimit(480, 500), 500); + assert.equal(comboboxNextRenderLimit(500, 500), null); + assert.equal(comboboxNextRenderLimit(600, 500), null); +}); + +test("combobox render window expands when the list is scrolled near its bottom", () => { + const target = (scrollTop: number): ComboboxScrollableTarget => ({ + clientHeight: 280, + scrollHeight: 500 * 40, + scrollTop, + }); + + // Far from the bottom: keep the window as-is. + assert.equal(shouldExpandComboboxWindow(target(0), COMBOBOX_INITIAL_RENDER_LIMIT, 500), false); + // Near the bottom of the mounted slice: expand it. + assert.equal( + shouldExpandComboboxWindow(target(500 * 40 - 280 - 10), COMBOBOX_INITIAL_RENDER_LIMIT, 500), + true, + ); + // Everything already rendered: never expand. + assert.equal(shouldExpandComboboxWindow(target(0), 500, 500), false); + // Empty list cannot expand. + assert.equal(shouldExpandComboboxWindow(target(0), 0, 0), false); +}); + +test("combobox renders options incrementally so large font lists cannot freeze the picker", () => { + assert.match(source, /const \[renderLimit, setRenderLimit\] = React\.useState\(COMBOBOX_INITIAL_RENDER_LIMIT\)/); + assert.match(source, /filteredOptions\.slice\(0, renderLimit\)/); + assert.match(source, /renderedOptions\.map\(\(option, optionIndex\)/); + assert.match(source, /onScrollCapture=\{handleOptionsScrollCapture\}/); + assert.match(source, /if \(activeIndex < renderedOptions\.length\) return\s*expandRenderWindow\(\)/); +}); + test("combobox trigger shows a focus-within ring for keyboard users", () => { assert.match(source, /focus-within:outline-none focus-within:ring-1 focus-within:ring-ring/); }); diff --git a/components/ui/combobox.tsx b/components/ui/combobox.tsx index 08299aa2d..8a1965842 100644 --- a/components/ui/combobox.tsx +++ b/components/ui/combobox.tsx @@ -101,14 +101,44 @@ export const selectComboboxInputIfFocused = ( export const canComboboxOpen = (disabled: boolean, nextOpen: boolean): boolean => !disabled || !nextOpen +/** + * Incremental rendering limits for large option lists (e.g. the font pickers + * can list hundreds of locally installed fonts). Rendering every option at + * once freezes the UI, so we mount an initial slice and grow the window as + * the user scrolls or arrow-navigates deeper. + */ +export const COMBOBOX_INITIAL_RENDER_LIMIT = 60 +export const COMBOBOX_RENDER_LIMIT_STEP = 120 +const COMBOBOX_EXPAND_SCROLL_THRESHOLD_PX = 80 + +export const comboboxNextRenderLimit = ( + currentLimit: number, + optionCount: number, +): number | null => { + if (optionCount <= currentLimit) return null + return Math.min(currentLimit + COMBOBOX_RENDER_LIMIT_STEP, optionCount) +} + +export const shouldExpandComboboxWindow = ( + target: ComboboxScrollableTarget, + renderedCount: number, + optionCount: number, +): boolean => { + if (renderedCount >= optionCount) return false + const distanceFromBottom = target.scrollHeight - (target.scrollTop + target.clientHeight) + return distanceFromBottom <= COMBOBOX_EXPAND_SCROLL_THRESHOLD_PX +} + function ComboboxOptionsList({ children, id, listbox = false, + onScrollCapture, }: { children: React.ReactNode; id?: string; listbox?: boolean; + onScrollCapture?: React.UIEventHandler; }) { const handleWheelCapture = (event: React.WheelEvent) => { const handled = applyComboboxWheelScroll(event.currentTarget, event.deltaY, event.deltaMode) @@ -125,6 +155,7 @@ function ComboboxOptionsList({ role={listbox ? "listbox" : undefined} className="max-h-[280px] overflow-y-auto overscroll-contain p-1" onWheelCapture={handleWheelCapture} + onScrollCapture={onScrollCapture} > {children} @@ -155,6 +186,8 @@ export function Combobox({ const [activeIndex, setActiveIndex] = React.useState(-1) // Track if user is actively searching (typed something after opening) const [isSearching, setIsSearching] = React.useState(false) + // Incremental rendering window for very large option lists + const [renderLimit, setRenderLimit] = React.useState(COMBOBOX_INITIAL_RENDER_LIMIT) const inputRef = React.useRef(null) const wasOpenRef = React.useRef(false) const activeOptionRef = React.useRef(null) @@ -188,6 +221,50 @@ export function Combobox({ return filterComboboxOptions(options, inputValue, isSearching) }, [options, inputValue, isSearching]) + // Restart the window from its initial size whenever the picker opens, the + // option set, or the search mode changes, so a previously grown window + // never persists across uses. + React.useEffect(() => { + setRenderLimit(COMBOBOX_INITIAL_RENDER_LIMIT) + }, [options, isSearching, open]) + + const renderedOptions = React.useMemo(() => { + if (filteredOptions.length <= renderLimit) return filteredOptions + return filteredOptions.slice(0, renderLimit) + }, [filteredOptions, renderLimit]) + + const expandRenderWindow = React.useCallback(() => { + setRenderLimit((current) => { + const next = comboboxNextRenderLimit(current, options.length) + return next ?? current + }) + }, [options.length]) + + // Keyboard navigation must never land on an option that is not mounted + // yet, otherwise its aria-activedescendant id would not exist. + React.useEffect(() => { + if (activeIndex < renderedOptions.length) return + expandRenderWindow() + }, [activeIndex, renderedOptions.length, expandRenderWindow]) + + const handleOptionsScrollCapture = React.useCallback((event: React.UIEvent) => { + const target = event.currentTarget + if ( + !shouldExpandComboboxWindow( + { + clientHeight: target.clientHeight, + scrollHeight: target.scrollHeight, + scrollTop: target.scrollTop, + }, + renderedOptions.length, + filteredOptions.length, + ) + ) { + return + } + expandRenderWindow() + }, [renderedOptions.length, filteredOptions.length, expandRenderWindow]) + const showCreateOption = React.useMemo(() => { if (!allowCreate || !inputValue.trim() || !isSearching) return false const lower = inputValue.toLowerCase().trim() @@ -199,7 +276,7 @@ export function Combobox({ React.useEffect(() => { activeOptionRef.current?.scrollIntoView({ block: 'nearest' }) - }, [activeIndex]) + }, [activeIndex, renderedOptions.length]) React.useEffect(() => { if (!disabled || !open) return @@ -357,7 +434,7 @@ export function Combobox({ style={{ width: 'var(--radix-popover-trigger-width)' }} > {/* Options List */} - + {filteredOptions.length === 0 && !showCreateOption ? (
{emptyText} @@ -391,8 +468,8 @@ export function Combobox({
)} - {/* Existing options */} - {filteredOptions.map((option, optionIndex) => { + {/* Existing options (rendered incrementally for large lists) */} + {renderedOptions.map((option, optionIndex) => { const selectableIndex = optionIndex + (showCreateOption ? 1 : 0) return (