🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(model-switch): keep panel within available width (#19852)
Tsuki63df8872bbbb
diff --git a/src/features/ModelSwitchPanel/components/PanelContent.tsx b/src/features/ModelSwitchPanel/components/PanelContent.tsxindex c838ef75d0a..d56fc98fe1a 100644--- a/src/features/ModelSwitchPanel/components/PanelContent.tsx+++ b/src/features/ModelSwitchPanel/components/PanelContent.tsx@@ -9,9 +9,10 @@ import { useUserStore } from '@/store/user';import { userGeneralSettingsSelectors } from '@/store/user/slices/settings/selectors/general';import type { EnabledProviderWithModels } from '@/types/aiProvider';-import { DEFAULT_WIDTH, ENABLE_RESIZING, MAX_WIDTH, MIN_WIDTH } from '../const';+import { DEFAULT_WIDTH, ENABLE_RESIZING, MAX_WIDTH } from '../const';import { usePanelSize } from '../hooks/usePanelSize';import { usePanelState } from '../hooks/usePanelState';+import { usePanelWidthConstraints } from '../hooks/usePanelWidthConstraints';import { List } from './List';import type { PricingMode } from './ModelDetailPanel';import { Toolbar } from './Toolbar';@@ -43,6 +44,12 @@ export const PanelContent: FC<PanelContentProps> = ({const isDevMode = useUserStore((s) => userGeneralSettingsSelectors.config(s).isDevMode);const { groupMode, handleGroupModeChange } = usePanelState();const { panelHeight, panelWidth, handlePanelWidthChange } = usePanelSize(enabledList.length);+ const {+ constrainedWidth,+ maxWidth: constrainedMaxWidth,+ minWidth: constrainedMinWidth,+ probeRef,+ } = usePanelWidthConstraints(panelWidth);useBusinessModelPricingPrefetch();@@ -76,32 +83,47 @@ export const PanelContent: FC<PanelContentProps> = ({if (isDevMode) {return (- <Rnd- disableDragging- enableResizing={ENABLE_RESIZING}- // Rnd needs a numeric `size.height`, but a fixed height overflows the top- // edge when the upward-opening popup meets a short viewport. Pass the- // clamp through Rnd's own `maxHeight` (a `style.maxHeight` gets overridden- // by Rnd's internal default) so CSS caps the box to the space base-ui- // exposes via `--available-height`; the inner list then flex-shrinks and- // scrolls. Height resize is disabled, so this doesn't fight drag logic.- maxHeight={`min(${panelHeight}px, var(--available-height, ${panelHeight}px))`}- maxWidth={MAX_WIDTH}- minWidth={MIN_WIDTH}- position={{ x: 0, y: 0 }}- size={{ height: panelHeight, width: panelWidth }}- style={{- display: 'flex',- flexDirection: 'column',- minHeight: 0,- position: 'relative',- }}- onResizeStop={(_e, _direction, ref) => {- handlePanelWidthChange(ref.offsetWidth);- }}- >- {content}- </Rnd>+ <>+ <div+ aria-hidden+ ref={probeRef}+ style={{+ pointerEvents: 'none',+ position: 'absolute',+ visibility: 'hidden',+ width: `min(${MAX_WIDTH}px, var(--available-width, ${MAX_WIDTH}px))`,+ }}+ />+ <Rnd+ disableDragging+ enableResizing={ENABLE_RESIZING}+ // Rnd needs a numeric `size.height`, but a fixed height overflows the top+ // edge when the upward-opening popup meets a short viewport. Pass the+ // clamp through Rnd's own `maxHeight` (a `style.maxHeight` gets overridden+ // by Rnd's internal default) so CSS caps the box to the space base-ui+ // exposes via `--available-height`; the inner list then flex-shrinks and+ // scrolls. Height resize is disabled, so this doesn't fight drag logic.+ maxHeight={`min(${panelHeight}px, var(--available-height, ${panelHeight}px))`}+ // re-resizable cannot parse CSS min() while dragging. Measure Base UI's+ // collision-aware width with the hidden probe and pass numeric bounds so+ // resize remains usable when a foldable or split window narrows the popup.+ maxWidth={constrainedMaxWidth}+ minWidth={constrainedMinWidth}+ position={{ x: 0, y: 0 }}+ size={{ height: panelHeight, width: constrainedWidth }}+ style={{+ display: 'flex',+ flexDirection: 'column',+ minHeight: 0,+ position: 'relative',+ }}+ onResizeStop={(_e, _direction, ref) => {+ handlePanelWidthChange(ref.offsetWidth);+ }}+ >+ {content}+ </Rnd>+ </>);}@@ -118,7 +140,7 @@ export const PanelContent: FC<PanelContentProps> = ({// height (not max-height) lets the inner list flex-shrink and scroll.height: `min(${panelHeight}px, var(--available-height, ${panelHeight}px))`,position: 'relative',- width: DEFAULT_WIDTH,+ width: `min(${DEFAULT_WIDTH}px, var(--available-width, ${DEFAULT_WIDTH}px))`,}}>{content}diff --git a/src/features/ModelSwitchPanel/hooks/usePanelWidthConstraints.test.ts b/src/features/ModelSwitchPanel/hooks/usePanelWidthConstraints.test.tsnew file mode 100644index 00000000000..5376f6d19c7--- /dev/null+++ b/src/features/ModelSwitchPanel/hooks/usePanelWidthConstraints.test.ts@@ -0,0 +1,70 @@+import { act, renderHook } from '@testing-library/react';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';++import { MAX_WIDTH, MIN_WIDTH } from '../const';+import { usePanelWidthConstraints } from './usePanelWidthConstraints';++let resizeCallback: ResizeObserverCallback;+const disconnect = vi.fn();+const observe = vi.fn();++beforeEach(() => {+ vi.stubGlobal(+ 'ResizeObserver',+ class {+ constructor(callback: ResizeObserverCallback) {+ resizeCallback = callback;+ }++ disconnect = disconnect;+ observe = observe;+ },+ );+});++afterEach(() => {+ vi.unstubAllGlobals();+ vi.clearAllMocks();+});++describe('usePanelWidthConstraints', () => {+ it('keeps react-rnd constraints numeric while following the available popup width', () => {+ const { result, unmount } = renderHook(() => usePanelWidthConstraints(550));+ let measuredWidth = 240;+ const probe = {+ get clientWidth() {+ return measuredWidth;+ },+ } as HTMLDivElement;++ act(() => result.current.probeRef(probe));++ expect(result.current).toMatchObject({+ constrainedWidth: 240,+ maxWidth: 240,+ minWidth: 240,+ });+ expect(observe).toHaveBeenCalledWith(probe);++ measuredWidth = 450;+ act(() => resizeCallback([], {} as ResizeObserver));++ expect(result.current).toMatchObject({+ constrainedWidth: 450,+ maxWidth: 450,+ minWidth: MIN_WIDTH,+ });++ measuredWidth = 700;+ act(() => resizeCallback([], {} as ResizeObserver));++ expect(result.current).toMatchObject({+ constrainedWidth: 550,+ maxWidth: MAX_WIDTH,+ minWidth: MIN_WIDTH,+ });++ unmount();+ expect(disconnect).toHaveBeenCalled();+ });+});diff --git a/src/features/ModelSwitchPanel/hooks/usePanelWidthConstraints.ts b/src/features/ModelSwitchPanel/hooks/usePanelWidthConstraints.tsnew file mode 100644index 00000000000..58596b6e352--- /dev/null+++ b/src/features/ModelSwitchPanel/hooks/usePanelWidthConstraints.ts@@ -0,0 +1,40 @@+import { useCallback, useEffect, useRef, useState } from 'react';++import { MAX_WIDTH, MIN_WIDTH } from '../const';++export const usePanelWidthConstraints = (panelWidth: number) => {+ const [availableWidth, setAvailableWidth] = useState(MAX_WIDTH);+ const observerRef = useRef<ResizeObserver | null>(null);++ const probeRef = useCallback((node: HTMLDivElement | null) => {+ observerRef.current?.disconnect();+ observerRef.current = null;++ if (!node) return;++ const measure = () => {+ if (node.clientWidth > 0) setAvailableWidth(node.clientWidth);+ };++ measure();++ if (typeof ResizeObserver === 'undefined') return;++ observerRef.current = new ResizeObserver(measure);+ observerRef.current.observe(node);+ }, []);++ useEffect(+ () => () => {+ observerRef.current?.disconnect();+ },+ [],+ );++ return {+ constrainedWidth: Math.min(panelWidth, availableWidth),+ maxWidth: Math.min(MAX_WIDTH, availableWidth),+ minWidth: Math.min(MIN_WIDTH, availableWidth),+ probeRef,+ };+};