[web] tiling: fix tabs width bug (#1637)

This commit is contained in:
Zhou Yuhang 2025-08-07 08:26:59 +08:00 committed by GitHub
parent 5ccd8acfd9
commit 3c1f0fccf4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 26 additions and 3 deletions

View File

@ -1,5 +1,5 @@
import clsx from 'clsx';
import { useEffect, useRef } from 'react';
import { useEffect, useRef, useState } from 'react';
import { useDrag, useDrop } from 'react-dnd';
import { getEmptyImage } from 'react-dnd-html5-backend';
@ -41,6 +41,8 @@ export default function TilesPanelTabsSection<TabType extends string>({
useDragHighlightContext();
const tabListRef = useRef<HTMLDivElement>(null);
const tabRightEmptySpaceRef = useRef<HTMLDivElement>(null);
const [isStabilizingWidth, setIsStabilizingWidth] = useState(false);
const [stableWidth, setStableWidth] = useState<number | null>(null);
const [{ isOver }, drop] = useDrop<
TilesPanelDragItem<TabType> | TilesPanelDragPanel,
void,
@ -107,6 +109,22 @@ export default function TilesPanelTabsSection<TabType extends string>({
},
});
useEffect(() => {
const currentWidth = tabListRef.current?.scrollWidth;
if (currentWidth) {
setStableWidth(currentWidth);
setIsStabilizingWidth(true);
const timeoutId = setTimeout(() => {
setIsStabilizingWidth(false);
setStableWidth(null);
}, 10);
return () => clearTimeout(timeoutId);
}
}, [tabs.length]);
const [{ isDragging }, drag, dragPreview] = useDrag<
TilesPanelDragPanel,
unknown,
@ -181,9 +199,14 @@ export default function TilesPanelTabsSection<TabType extends string>({
ref={tabListRef}
scrollbars="horizontal"
size="thin"
style={
isStabilizingWidth && stableWidth
? { width: `${stableWidth}px` }
: undefined
}
viewportClass="flex items-center"
widthClass="w-fit">
<div className="size-full flex gap-x-2 overflow-y-hidden py-1.5">
widthClass={isStabilizingWidth && stableWidth ? 'w-full' : 'w-fit'}>
<div className="flex size-full gap-x-2 overflow-y-hidden py-1.5">
{tabs.map((tabItem, index) => {
const isActive =
mode === 'interactive' && activeTabId === tabItem.id;