TypeScript
fix(web): size the model picker to its content (#15152)
Alex Southwella6aebbe2e06e
diff --git a/apps/web/src/components/chat/ModelPickerContent.tsx b/apps/web/src/components/chat/ModelPickerContent.tsxindex 70181947bc4..53a2e9c45ba 100644--- a/apps/web/src/components/chat/ModelPickerContent.tsx+++ b/apps/web/src/components/chat/ModelPickerContent.tsx@@ -144,6 +144,7 @@ export function adjacentModelPickerProvider(input: {}const EMPTY_MODEL_JUMP_LABELS = new Map<string, string>();+const MODEL_LIST_ESTIMATED_ITEM_SIZE = 52;function ModelListSeparator() {return <div className="h-0.5" />;@@ -197,6 +198,7 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {const [showBottomScrollFade, setShowBottomScrollFade] = useState(false);const searchInputRef = useRef<HTMLInputElement>(null);const modelListRef = useRef<LegendListRef | null>(null);+ const pickerContentRef = useRef<HTMLDivElement>(null);const highlightedModelKeyRef = useRef<string | null>(null);const favorites = useClientSettings((s) => s.favorites ?? []);const activeEntry = props.instanceEntries.find(@@ -699,6 +701,17 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {),[visibleModels],);+ const [modelListContentSize, setModelListContentSize] = useState(+ () => filteredItemKeys.length * MODEL_LIST_ESTIMATED_ITEM_SIZE,+ );+ const [searchHeight, setSearchHeight] = useState(0);+ useLayoutEffect(+ () => modelListRef.current?.getState().listen("totalSize", setModelListContentSize),+ [],+ );+ // Fit the list to its rows plus the combobox list `py-1` and LegendList `py-1.5`.+ const modelListHeight =+ filteredItemKeys.length === 0 ? 0 : `calc(${modelListContentSize}px + var(--spacing) * 5)`;const updateModelListScrollFades = useCallback(() => {const scrollElement = modelListRef.current?.getScrollableNode();if (!(scrollElement instanceof HTMLElement)) {@@ -815,7 +828,10 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {return (<TooltipProvider delay={0}><div- className="relative flex h-screen max-h-86.5 w-screen max-w-90 flex-row overflow-hidden"+ ref={pickerContentRef}+ className="relative flex max-h-86.5 w-screen max-w-90 flex-row overflow-hidden"+ // Hold the height from when the search started; results scroll instead of resizing.+ style={isSearching ? { height: searchHeight } : undefined}data-model-picker-content="true">{/* Sidebar */}@@ -890,7 +906,10 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {ref={searchInputRef}placeholder="Search models..."value={searchQuery}- onChange={(e) => setSearchQuery(e.target.value)}+ onChange={(e) => {+ if (!isSearching) setSearchHeight(pickerContentRef.current?.offsetHeight ?? 0);+ setSearchQuery(e.target.value);+ }}onKeyDown={(e) => {if (showSidebar &&@@ -944,7 +963,10 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {/>{/* Model list */}- <div className="relative min-h-0 flex-1 overflow-hidden pr-px">+ <div+ className="relative min-h-0 overflow-hidden pr-px"+ style={{ height: modelListHeight }}+ ><ComboboxListVirtualized><LegendList<string>ref={modelListRef}@@ -1013,7 +1035,7 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {/>);}}- estimatedItemSize={52}+ estimatedItemSize={MODEL_LIST_ESTIMATED_ITEM_SIZE}drawDistance={480}recycleItemscontentContainerClassName="pl-2 pr-px"