Skip to content

πŸ™Œ OpenHands: AI-Driven Development

style(automations): restyle template filter chips as a draggable scroll rail (#18832)

FraterCCCLXIII3064c5aed2e1
diff --git a/__tests__/components/shared/horizontal-scroll-fade.test.tsx b/__tests__/components/shared/horizontal-scroll-fade.test.tsx
new file mode 100644
index 0000000000..b150581687
--- /dev/null
+++ b/__tests__/components/shared/horizontal-scroll-fade.test.tsx
@@ -0,0 +1,197 @@
+import { act, fireEvent, render, screen } from "@testing-library/react";
+import { describe, it, expect, vi } from "vitest";
+import { HorizontalScrollFade } from "#/components/shared/horizontal-scroll-fade";
+
+function mockScrollMetrics(
+ element: HTMLElement,
+ metrics: { scrollWidth: number; clientWidth: number; scrollLeft: number },
+) {
+ Object.defineProperty(element, "scrollWidth", {
+ configurable: true,
+ value: metrics.scrollWidth,
+ });
+ Object.defineProperty(element, "clientWidth", {
+ configurable: true,
+ value: metrics.clientWidth,
+ });
+ Object.defineProperty(element, "scrollLeft", {
+ configurable: true,
+ writable: true,
+ value: metrics.scrollLeft,
+ });
+}
+
+function renderScrollFade({ draggable = true } = {}) {
+ const onClick = vi.fn();
+ render(
+ <HorizontalScrollFade
+ draggable={draggable}
+ scrollTestId="scroller"
+ fadeTestIdPrefix="fade"
+ >
+ <div>
+ <button type="button" data-testid="chip" onClick={onClick} />
+ </div>
+ </HorizontalScrollFade>,
+ );
+ return {
+ scroller: screen.getByTestId("scroller"),
+ chip: screen.getByTestId("chip"),
+ onClick,
+ };
+}
+
+describe("HorizontalScrollFade", () => {
+ it("shows an edge fade only on the clipped side", () => {
+ // Arrange
+ const { scroller } = renderScrollFade({ draggable: false });
+ const leftFade = screen.getByTestId("fade-left");
+ const rightFade = screen.getByTestId("fade-right");
+
+ // Act
+ mockScrollMetrics(scroller, {
+ scrollWidth: 900,
+ clientWidth: 320,
+ scrollLeft: 0,
+ });
+ fireEvent.scroll(scroller);
+
+ // Assert
+ expect(leftFade).toHaveAttribute("data-visible", "false");
+ expect(rightFade).toHaveAttribute("data-visible", "true");
+
+ // Act
+ mockScrollMetrics(scroller, {
+ scrollWidth: 900,
+ clientWidth: 320,
+ scrollLeft: 580,
+ });
+ fireEvent.scroll(scroller);
+
+ // Assert
+ expect(leftFade).toHaveAttribute("data-visible", "true");
+ expect(rightFade).toHaveAttribute("data-visible", "false");
+ });
+
+ describe("draggable", () => {
+ it("scrolls while dragging and swallows the click that ends the drag", () => {
+ // Arrange
+ const { scroller, chip, onClick } = renderScrollFade();
+ mockScrollMetrics(scroller, {
+ scrollWidth: 900,
+ clientWidth: 320,
+ scrollLeft: 100,
+ });
+
+ // Act
+ fireEvent.mouseDown(chip, { button: 0, clientX: 300 });
+ fireEvent.mouseMove(document, { clientX: 250, buttons: 1 });
+ fireEvent.mouseUp(document);
+ fireEvent.click(chip);
+
+ // Assert
+ expect(scroller.scrollLeft).toBe(150);
+ expect(onClick).not.toHaveBeenCalled();
+ });
+
+ it("lets a press without movement click the chip", () => {
+ // Arrange
+ const { chip, onClick } = renderScrollFade();
+
+ // Act
+ fireEvent.mouseDown(chip, { button: 0, clientX: 300 });
+ fireEvent.mouseUp(document);
+ fireEvent.click(chip);
+
+ // Assert
+ expect(onClick).toHaveBeenCalledTimes(1);
+ });
+
+ it("cancels the native drag of child images", () => {
+ // Arrange
+ const { chip } = renderScrollFade();
+
+ // Act
+ const notCancelled = fireEvent.dragStart(chip);
+
+ // Assert
+ expect(notCancelled).toBe(false);
+ });
+
+ it("does not swallow a keyboard click after a drag released outside the content", async () => {
+ // Arrange
+ const { scroller, chip, onClick } = renderScrollFade();
+ mockScrollMetrics(scroller, {
+ scrollWidth: 900,
+ clientWidth: 320,
+ scrollLeft: 100,
+ });
+ fireEvent.mouseDown(chip, { button: 0, clientX: 300 });
+ fireEvent.mouseMove(document, { clientX: 250, buttons: 1 });
+ // Released outside the scroller: its click goes elsewhere.
+ fireEvent.mouseUp(document);
+ await act(async () => {
+ await new Promise((resolve) => {
+ setTimeout(resolve, 0);
+ });
+ });
+
+ // Act
+ fireEvent.click(chip);
+
+ // Assert
+ expect(onClick).toHaveBeenCalledTimes(1);
+ });
+
+ it("turns a vertical wheel turn into a horizontal scroll up to the end", () => {
+ // Arrange
+ const { scroller } = renderScrollFade();
+ mockScrollMetrics(scroller, {
+ scrollWidth: 900,
+ clientWidth: 320,
+ scrollLeft: 500,
+ });
+
+ // Act
+ const notCancelled = fireEvent.wheel(scroller, { deltaY: 100 });
+
+ // Assert: clamped to the end rather than skipped
+ expect(notCancelled).toBe(false);
+ expect(scroller.scrollLeft).toBe(580);
+ });
+
+ it("leaves the wheel to the page once the content is at an edge", () => {
+ // Arrange
+ const { scroller } = renderScrollFade();
+ mockScrollMetrics(scroller, {
+ scrollWidth: 900,
+ clientWidth: 320,
+ scrollLeft: 580,
+ });
+
+ // Act
+ const notCancelled = fireEvent.wheel(scroller, { deltaY: 100 });
+
+ // Assert
+ expect(notCancelled).toBe(true);
+ expect(scroller.scrollLeft).toBe(580);
+ });
+
+ it("leaves the wheel to the page when the content fits", () => {
+ // Arrange
+ const { scroller } = renderScrollFade();
+ mockScrollMetrics(scroller, {
+ scrollWidth: 320,
+ clientWidth: 320,
+ scrollLeft: 0,
+ });
+
+ // Act
+ const notCancelled = fireEvent.wheel(scroller, { deltaY: 100 });
+
+ // Assert
+ expect(notCancelled).toBe(true);
+ expect(scroller.scrollLeft).toBe(0);
+ });
+ });
+});
diff --git a/src/components/features/automations/recommended-automations-section.tsx b/src/components/features/automations/recommended-automations-section.tsx
index 6a880ba462..29e4077c9a 100644
--- a/src/components/features/automations/recommended-automations-section.tsx
+++ b/src/components/features/automations/recommended-automations-section.tsx
@@ -438,7 +438,7 @@ export function RecommendedAutomationsSection({
return (
<section
data-testid="recommended-automations-section"
- className="mt-3 flex flex-col gap-4"
+ className={cn("flex flex-col gap-4", filterChips ? "mt-0" : "mt-3")}
>
{filterChips}
<AutomationsFilteredEmptyState
@@ -467,7 +467,7 @@ export function RecommendedAutomationsSection({
scrollableGrid ? "recommended-automations-scroll-area" : undefined
}
className={cn(
- "mt-3",
+ filterChips ? "mt-0" : "mt-3",
extensionModuleCardGridContainerClassName,
scrollableGrid &&
"min-h-0 flex-1 overflow-y-auto custom-scrollbar-always",
diff --git a/src/components/features/automations/template-filter-chips.tsx b/src/components/features/automations/template-filter-chips.tsx
index 8e19d9e417..2f7adc7ef7 100644
--- a/src/components/features/automations/template-filter-chips.tsx
+++ b/src/components/features/automations/template-filter-chips.tsx
@@ -2,11 +2,12 @@ import type { ReactNode } from "react";
import { Clock, LayoutGrid, Zap, type LucideIcon } from "lucide-react";
import { useTranslation } from "react-i18next";
import { INTEGRATION_CATALOG as MCP_MARKETPLACE } from "@openhands/extensions/integrations";
+import { HorizontalScrollFade } from "#/components/shared/horizontal-scroll-fade";
import { McpLogoBadge } from "#/components/features/mcp-logo-badge";
import { I18nKey } from "#/i18n/declaration";
import type { TemplateTriggerKind } from "#/utils/automation-catalog";
-import { dropdownFilterTriggerClassName } from "#/utils/dropdown-classes";
import { getMarketplaceEntryById } from "#/utils/mcp-marketplace-utils";
+import { pillToggleClassName } from "#/utils/pill-toggle-classes";
import { cn } from "#/utils/utils";
/** The Templates page filters: one integration and one trigger kind, combined. */
@@ -54,11 +55,7 @@ function FilterChip({
data-testid={testId}
aria-pressed={active}
onClick={onClick}
- className={cn(
- dropdownFilterTriggerClassName,
- "h-8 py-0 text-xs",
- active && "border-contrast/60 bg-contrast/10",
- )}
+ className={cn(pillToggleClassName(active), "shrink-0")}
>
{children}
</button>
@@ -85,54 +82,62 @@ export function TemplateFilterChips({
const { t } = useTranslation("openhands");
return (
- <div
- role="group"
- aria-label={t(I18nKey.RECOMMENDED_AUTOMATIONS$FILTERS_LABEL)}
- data-testid="template-filters"
- className="flex flex-wrap items-center gap-2"
+ <HorizontalScrollFade
+ draggable
+ fadeTestIdPrefix="template-filters-fade"
+ className="pb-2"
+ // Room for the chips' focus outline, which the scroller would clip.
+ scrollClassName="p-1"
>
- <FilterChip
- testId="template-filter-all"
- active={!hasActiveTemplateFilter(filters)}
- onClick={() => onChange(EMPTY_TEMPLATE_FILTERS)}
+ <div
+ role="group"
+ aria-label={t(I18nKey.RECOMMENDED_AUTOMATIONS$FILTERS_LABEL)}
+ data-testid="template-filters"
+ className="flex w-max min-w-full flex-nowrap items-center gap-2"
>
- <LayoutGrid className="size-3.5 shrink-0" aria-hidden />
- {t(I18nKey.RECOMMENDED_AUTOMATIONS$FILTER_ALL)}
- </FilterChip>
- {integrationIds.map((id) => {
- const entry = getMarketplaceEntryById(id, MCP_MARKETPLACE);
- const active = filters.integrationId === id;
- return (
- <FilterChip
- key={id}
- testId={`template-filter-integration-${id}`}
- active={active}
- onClick={() =>
- onChange({ ...filters, integrationId: active ? null : id })
- }
- >
- <McpLogoBadge entry={entry} size="xs" />
- {entry?.name ?? id}
- </FilterChip>
- );
- })}
- {TRIGGER_KINDS.map((kind) => {
- const Icon = TRIGGER_KIND_ICON[kind];
- const active = filters.trigger === kind;
- return (
- <FilterChip
- key={kind}
- testId={`template-filter-trigger-${kind}`}
- active={active}
- onClick={() =>
- onChange({ ...filters, trigger: active ? null : kind })
- }
- >
- <Icon className="size-3.5 shrink-0" aria-hidden />
- {t(TRIGGER_KIND_LABEL_KEY[kind])}
- </FilterChip>
- );
- })}
- </div>
+ <FilterChip
+ testId="template-filter-all"
+ active={!hasActiveTemplateFilter(filters)}
+ onClick={() => onChange(EMPTY_TEMPLATE_FILTERS)}
+ >
+ <LayoutGrid className="size-4 shrink-0" aria-hidden />
+ {t(I18nKey.RECOMMENDED_AUTOMATIONS$FILTER_ALL)}
+ </FilterChip>
+ {integrationIds.map((id) => {
+ const entry = getMarketplaceEntryById(id, MCP_MARKETPLACE);
+ const active = filters.integrationId === id;
+ return (
+ <FilterChip
+ key={id}
+ testId={`template-filter-integration-${id}`}
+ active={active}
+ onClick={() =>
+ onChange({ ...filters, integrationId: active ? null : id })
+ }
+ >
+ <McpLogoBadge entry={entry} size="xs" />
+ {entry?.name ?? id}
+ </FilterChip>
+ );
+ })}
+ {TRIGGER_KINDS.map((kind) => {
+ const Icon = TRIGGER_KIND_ICON[kind];
+ const active = filters.trigger === kind;
+ return (
+ <FilterChip
+ key={kind}
+ testId={`template-filter-trigger-${kind}`}
+ active={active}
+ onClick={() =>
+ onChange({ ...filters, trigger: active ? null : kind })
+ }
+ >
+ <Icon className="size-4 shrink-0" aria-hidden />
+ {t(TRIGGER_KIND_LABEL_KEY[kind])}
+ </FilterChip>
+ );
+ })}
+ </div>
+ </HorizontalScrollFade>
);
}
diff --git a/src/components/shared/horizontal-scroll-fade.tsx b/src/components/shared/horizontal-scroll-fade.tsx
new file mode 100644
index 0000000000..e1791a2e72
--- /dev/null
+++ b/src/components/shared/horizontal-scroll-fade.tsx
@@ -0,0 +1,139 @@
+import React from "react";
+import { useDragScroll } from "#/hooks/use-drag-scroll";
+import {
+ readScrollFadeState,
+ SCROLL_EDGE_THRESHOLD_PX,
+ type ScrollFadeState,
+} from "#/utils/scroll-fade-state";
+import { cn } from "#/utils/utils";
+
+const FADE_WIDTH_CLASS = "w-10";
+
+interface HorizontalScrollFadeProps {
+ children: React.ReactNode;
+ className?: string;
+ scrollClassName?: string;
+ scrollTestId?: string;
+ fadeTestIdPrefix?: string;
+ /**
+ * Lets a mouse drag the content sideways and turns a vertical wheel turn
+ * into a horizontal scroll while the content overflows. Trackpads and touch
+ * scroll natively either way.
+ */
+ draggable?: boolean;
+}
+
+export function HorizontalScrollFade({
+ children,
+ className,
+ scrollClassName,
+ scrollTestId,
+ fadeTestIdPrefix = "horizontal-scroll-fade",
+ draggable = false,
+}: HorizontalScrollFadeProps) {
+ const scrollRef = React.useRef<HTMLDivElement>(null);
+ const { handleMouseDown, handleClickCapture, handleDragStart } =
+ useDragScroll(scrollRef);
+ const [fadeState, setFadeState] = React.useState<ScrollFadeState>({
+ left: false,
+ right: false,
+ });
+
+ const updateFadeState = React.useCallback(() => {
+ const element = scrollRef.current;
+ if (!element) {
+ return;
+ }
+ setFadeState(readScrollFadeState(element));
+ }, []);
+
+ React.useLayoutEffect(() => {
+ updateFadeState();
+
+ const element = scrollRef.current;
+ if (!element) {
+ return undefined;
+ }
+
+ const resizeObserver = new ResizeObserver(updateFadeState);
+ resizeObserver.observe(element);
+
+ const content = element.firstElementChild;
+ if (content) {
+ resizeObserver.observe(content);
+ }
+
+ return () => resizeObserver.disconnect();
+ }, [updateFadeState, children]);
+
+ React.useEffect(() => {
+ const element = scrollRef.current;
+ if (!draggable || !element) {
+ return undefined;
+ }
+
+ const handleWheel = (event: WheelEvent) => {
+ if (Math.abs(event.deltaY) <= Math.abs(event.deltaX)) return;
+ const maxScroll = element.scrollWidth - element.clientWidth;
+ if (maxScroll <= SCROLL_EDGE_THRESHOLD_PX) return;
+ // Clamp so a wheel step can reach either end; once the content is at an
+ // edge, leave the event to scroll the page.
+ const next = Math.min(
+ Math.max(element.scrollLeft + event.deltaY, 0),
+ maxScroll,
+ );
+ if (Math.abs(next - element.scrollLeft) < SCROLL_EDGE_THRESHOLD_PX) {
+ return;
+ }
+ event.preventDefault();
+ element.scrollLeft = next;
+ };
+
+ element.addEventListener("wheel", handleWheel, { passive: false });
+ return () => element.removeEventListener("wheel", handleWheel);
+ }, [draggable]);
+
+ return (
+ <div className={cn("relative max-w-full min-w-0", className)}>
+ <div
+ ref={scrollRef}
+ data-testid={scrollTestId}
+ onScroll={updateFadeState}
+ onMouseDown={draggable ? handleMouseDown : undefined}
+ onClickCapture={draggable ? handleClickCapture : undefined}
+ onDragStart={draggable ? handleDragStart : undefined}
+ className={cn(
+ "max-w-full overflow-x-auto scrollbar-hide",
+ draggable && "cursor-grab select-none active:cursor-grabbing",
+ scrollClassName,
+ )}
+ >
+ {children}
+ </div>
+ <div
+ aria-hidden
+ data-testid={`${fadeTestIdPrefix}-left`}
+ data-visible={fadeState.left ? "true" : "false"}
+ className={cn(
+ "pointer-events-none absolute inset-y-0 left-0 z-10",
+ FADE_WIDTH_CLASS,
+ "bg-gradient-to-r from-[var(--oh-scroll-fade-from,var(--oh-color-base))] to-transparent",
+ "transition-opacity duration-300 ease-out motion-reduce:transition-none",
+ fadeState.left ? "opacity-100" : "opacity-0",
+ )}
+ />
+ <div
+ aria-hidden
+ data-testid={`${fadeTestIdPrefix}-right`}
+ data-visible={fadeState.right ? "true" : "false"}
+ className={cn(
+ "pointer-events-none absolute inset-y-0 right-0 z-10",
+ FADE_WIDTH_CLASS,
+ "bg-gradient-to-l from-[var(--oh-scroll-fade-from,var(--oh-color-base))] to-transparent",
+ "transition-opacity duration-300 ease-out motion-reduce:transition-none",
+ fadeState.right ? "opacity-100" : "opacity-0",
+ )}
+ />
+ </div>
+ );
+}
diff --git a/src/hooks/use-drag-scroll.ts b/src/hooks/use-drag-scroll.ts
index 8b11450ea8..301dc46cde 100644
--- a/src/hooks/use-drag-scroll.ts
+++ b/src/hooks/use-drag-scroll.ts
@@ -61,8 +61,10 @@ export function useDragScroll(scrollRef: RefObject<HTMLElement | null>) {
}
// The mouseup can be missed when released outside the window; end the
- // drag once the primary button is no longer held.
+ // drag once the primary button is no longer held. That release sent no
+ // click to the page, so there is nothing left to suppress.
if ((event.buttons & 1) === 0) {
+ dragCommittedRef.current = false;
setIsDragging(false);
return;
}
@@ -79,7 +81,18 @@ export function useDragScroll(scrollRef: RefObject<HTMLElement | null>) {
startScrollLeftRef.current - (event.clientX - startXRef.current);
};
- const handleMouseUp = () => setIsDragging(false);
+ const handleMouseUp = () => {
+ setIsDragging(false);
+ // A release outside the scroller sends its click elsewhere, so the
+ // capture handler never clears the flag and would swallow the next
+ // keyboard or touch click. This release's own click is dispatched
+ // before the timeout runs, so it is still suppressed.
+ if (dragCommittedRef.current) {
+ window.setTimeout(() => {
+ dragCommittedRef.current = false;
+ }, 0);
+ }
+ };
document.addEventListener("mousemove", handleMouseMove);
document.addEventListener("mouseup", handleMouseUp);
diff --git a/src/utils/pill-toggle-classes.ts b/src/utils/pill-toggle-classes.ts
new file mode 100644
index 0000000000..4de327f7b7
--- /dev/null
+++ b/src/utils/pill-toggle-classes.ts
@@ -0,0 +1,15 @@
+import { cn } from "#/utils/utils";
+
+/**
+ * Borderless pill that toggles on and off, shared by the Templates filter
+ * chips and the setup choice tabs. Callers add their own layout classes.
+ */
+export function pillToggleClassName(active: boolean) {
+ return cn(
+ "inline-flex cursor-pointer items-center gap-2 whitespace-nowrap rounded-full border-0 px-3 py-1.5 text-sm transition-colors",
+ "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-focus",
+ active
+ ? "bg-contrast text-contrast-foreground"
+ : "bg-contrast/5 text-tertiary-light hover:bg-surface-raised hover:text-contrast",
+ );
+}
diff --git a/src/utils/scroll-fade-state.ts b/src/utils/scroll-fade-state.ts
index df40c335a7..a5be0e7754 100644
--- a/src/utils/scroll-fade-state.ts
+++ b/src/utils/scroll-fade-state.ts
@@ -1,4 +1,4 @@
-const SCROLL_EDGE_THRESHOLD_PX = 1;
+export const SCROLL_EDGE_THRESHOLD_PX = 1;
export interface ScrollFadeState {
left: boolean;