Skip to content

Commit b586983

Browse files
committed
fix(web): make right panel tabs easier to scroll
1 parent 4e89d74 commit b586983

1 file changed

Lines changed: 128 additions & 1 deletion

File tree

‎apps/web/src/components/RightPanelTabs.tsx‎

Lines changed: 128 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { getTerminalLabel } from "@t3tools/shared/terminalLabels";
99
import {
1010
Bot,
1111
ChevronDown,
12+
ChevronLeft,
13+
ChevronRight,
1214
FileDiff,
1315
Files,
1416
GitPullRequest,
@@ -170,6 +172,12 @@ type TabContextMenuAction =
170172
| "close-to-right"
171173
| "close-all";
172174

175+
const TAB_SCROLL_EDGE_TOLERANCE = 1;
176+
177+
function tabScrollViewport(root: HTMLDivElement | null): HTMLDivElement | null {
178+
return root?.querySelector<HTMLDivElement>('[data-slot="scroll-area-viewport"]') ?? null;
179+
}
180+
173181
/**
174182
* Desktop preview tab backing a surface, or null for non-preview surfaces, the
175183
* "new browser tab" placeholder, and the web build where no desktop tab exists.
@@ -720,6 +728,42 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
720728
const { resolvedTheme } = useTheme();
721729
const tabListRef = useRef<HTMLDivElement>(null);
722730
const [addSurfaceMenuOpen, setAddSurfaceMenuOpen] = useState(false);
731+
const [tabScrollState, setTabScrollState] = useState({
732+
hasOverflow: false,
733+
canScrollLeft: false,
734+
canScrollRight: false,
735+
});
736+
737+
const updateTabScrollState = useCallback(() => {
738+
const viewport = tabScrollViewport(tabListRef.current);
739+
if (!viewport) return;
740+
741+
const hasOverflow = viewport.scrollWidth - viewport.clientWidth > TAB_SCROLL_EDGE_TOLERANCE;
742+
const canScrollLeft = hasOverflow && viewport.scrollLeft > TAB_SCROLL_EDGE_TOLERANCE;
743+
const canScrollRight =
744+
hasOverflow &&
745+
viewport.scrollLeft + viewport.clientWidth < viewport.scrollWidth - TAB_SCROLL_EDGE_TOLERANCE;
746+
setTabScrollState((current) => {
747+
if (
748+
current.hasOverflow === hasOverflow &&
749+
current.canScrollLeft === canScrollLeft &&
750+
current.canScrollRight === canScrollRight
751+
) {
752+
return current;
753+
}
754+
return { hasOverflow, canScrollLeft, canScrollRight };
755+
});
756+
}, []);
757+
758+
const scrollTabs = useCallback((direction: -1 | 1) => {
759+
const viewport = tabScrollViewport(tabListRef.current);
760+
if (!viewport) return;
761+
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
762+
viewport.scrollBy({
763+
left: direction * Math.max(120, viewport.clientWidth * 0.75),
764+
behavior: reduceMotion ? "auto" : "smooth",
765+
});
766+
}, []);
723767

724768
const addSurfaceActions = [
725769
{
@@ -890,6 +934,45 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
890934
activeTab?.scrollIntoView({ block: "nearest", inline: "nearest" });
891935
}, [props.activeSurfaceId]);
892936

937+
useEffect(() => {
938+
const viewport = tabScrollViewport(tabListRef.current);
939+
if (!viewport) return;
940+
941+
const content = viewport.firstElementChild;
942+
const resizeObserver = new ResizeObserver(updateTabScrollState);
943+
resizeObserver.observe(viewport);
944+
if (content) resizeObserver.observe(content);
945+
viewport.addEventListener("scroll", updateTabScrollState, { passive: true });
946+
updateTabScrollState();
947+
948+
return () => {
949+
resizeObserver.disconnect();
950+
viewport.removeEventListener("scroll", updateTabScrollState);
951+
};
952+
}, [updateTabScrollState]);
953+
954+
useEffect(() => {
955+
const viewport = tabScrollViewport(tabListRef.current);
956+
if (!viewport) return;
957+
958+
const handleWheel = (event: WheelEvent) => {
959+
if (event.ctrlKey) return;
960+
let delta = Math.abs(event.deltaX) > Math.abs(event.deltaY) ? event.deltaX : event.deltaY;
961+
if (event.deltaMode === WheelEvent.DOM_DELTA_LINE) delta *= 16;
962+
if (event.deltaMode === WheelEvent.DOM_DELTA_PAGE) delta *= viewport.clientWidth;
963+
if (delta === 0) return;
964+
965+
const previousScrollLeft = viewport.scrollLeft;
966+
viewport.scrollLeft += delta;
967+
if (viewport.scrollLeft === previousScrollLeft) return;
968+
event.preventDefault();
969+
updateTabScrollState();
970+
};
971+
972+
viewport.addEventListener("wheel", handleWheel, { passive: false });
973+
return () => viewport.removeEventListener("wheel", handleWheel);
974+
}, [updateTabScrollState]);
975+
893976
return (
894977
<PreviewPanelShell
895978
mode={props.mode}
@@ -905,6 +988,7 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
905988
// the titlebar's height: a compact row re-centers the layout
906989
// controls a few pixels higher and the cluster jumps on open.
907990
props.mode === "inline" && !props.layoutControls ? "pr-28" : "pr-3",
991+
ownsDesktopTitleBar && "drag-region",
908992
ownsDesktopTitleBar && "wco:pr-[calc(var(--workspace-native-controls-inset)+6rem)]",
909993
props.mode === "inline" && props.maximized && COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS,
910994
)}
@@ -914,7 +998,10 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
914998
ref={tabListRef}
915999
hideScrollbars
9161000
scrollFade
917-
className={cn("min-w-0 flex-1 rounded-none", ownsDesktopTitleBar && "drag-region")}
1001+
className={cn(
1002+
"min-w-0 flex-1 rounded-none",
1003+
ownsDesktopTitleBar && "[-webkit-app-region:no-drag]",
1004+
)}
9181005
data-right-panel-tab-list
9191006
>
9201007
<div className="flex h-full w-max min-w-full items-center gap-1">
@@ -1099,6 +1186,46 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
10991186
) : null}
11001187
</div>
11011188
</ScrollArea>
1189+
{tabScrollState.hasOverflow ? (
1190+
<div
1191+
className="flex shrink-0 items-center gap-0.5 [-webkit-app-region:no-drag]"
1192+
role="group"
1193+
aria-label="Scroll panel tabs"
1194+
>
1195+
<Tooltip>
1196+
<TooltipTrigger
1197+
render={
1198+
<Button
1199+
aria-label="Scroll tabs left"
1200+
disabled={!tabScrollState.canScrollLeft}
1201+
onClick={() => scrollTabs(-1)}
1202+
size="icon-xs"
1203+
variant="ghost"
1204+
/>
1205+
}
1206+
>
1207+
<ChevronLeft />
1208+
</TooltipTrigger>
1209+
<TooltipPopup>Scroll tabs left</TooltipPopup>
1210+
</Tooltip>
1211+
<Tooltip>
1212+
<TooltipTrigger
1213+
render={
1214+
<Button
1215+
aria-label="Scroll tabs right"
1216+
disabled={!tabScrollState.canScrollRight}
1217+
onClick={() => scrollTabs(1)}
1218+
size="icon-xs"
1219+
variant="ghost"
1220+
/>
1221+
}
1222+
>
1223+
<ChevronRight />
1224+
</TooltipTrigger>
1225+
<TooltipPopup>Scroll tabs right</TooltipPopup>
1226+
</Tooltip>
1227+
</div>
1228+
) : null}
11021229
{props.layoutControls}
11031230
</div>
11041231
<div className="flex min-h-0 flex-1 flex-col" data-right-panel-surface-content>

0 commit comments

Comments
 (0)