Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(web): search fields own their clear button instead of the browser's (#19232)

Der Gerätcfb198a4a9be
diff --git a/web/src/components/design-system/DropdownMenu/DropdownMenu.stories.tsx b/web/src/components/design-system/DropdownMenu/DropdownMenu.stories.tsx
index e5f85f8b79..63e7a50273 100644
--- a/web/src/components/design-system/DropdownMenu/DropdownMenu.stories.tsx
+++ b/web/src/components/design-system/DropdownMenu/DropdownMenu.stories.tsx
@@ -511,3 +511,35 @@ export const TestNestedMenusStayWithinViewport = meta.story({
).toBeVisible();
},
});
+
+export const TestClearsTheSearch = meta.story({
+ name: "(Test) Clears the search",
+ args: {
+ search: { placeholder: "Search items" },
+ items: [
+ { type: "item", id: "report", title: "Create report", onClick: fn() },
+ { type: "item", id: "help", title: "Help", onClick: fn() },
+ ],
+ },
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+ const body = within(canvasElement.ownerDocument.body);
+
+ await userEvent.click(canvas.getByRole("button", { name: "Open menu" }));
+ const search = body.getByRole("searchbox", { name: "Search items" });
+ await expect(
+ body.queryByRole("button", { name: "Clear search" }),
+ ).toBeNull();
+
+ await userEvent.type(search, "report");
+ await expect(body.queryByRole("menuitem", { name: "Help" })).toBeNull();
+
+ await userEvent.click(body.getByRole("button", { name: "Clear search" }));
+ await expect(search).toHaveValue("");
+ // Clearing filters the list back open without dismissing the menu around
+ // it, and leaves focus in the field.
+ await expect(body.getByRole("menu", { name: "Actions" })).toBeVisible();
+ await expect(body.getByRole("menuitem", { name: "Help" })).toBeVisible();
+ await expect(search).toHaveFocus();
+ },
+});
diff --git a/web/src/components/design-system/DropdownMenu/DropdownMenu.tsx b/web/src/components/design-system/DropdownMenu/DropdownMenu.tsx
index 336c533754..793e780e84 100644
--- a/web/src/components/design-system/DropdownMenu/DropdownMenu.tsx
+++ b/web/src/components/design-system/DropdownMenu/DropdownMenu.tsx
@@ -24,7 +24,7 @@ import {
type Placement,
} from "@floating-ui/react";
import { cva } from "class-variance-authority";
-import { type IconComponent } from "@langfuse/icons";
+import { X, type IconComponent } from "@langfuse/icons";
import { DropdownIndicator } from "@/src/components/design-system/DropdownIndicator/DropdownIndicator";
import Link from "next/link";
import * as React from "react";
@@ -219,6 +219,7 @@ function DropdownMenuNode({
const tree = useFloatingTree();
const isNested = parentId !== null;
const layerContainer = useLayerContainer("popover");
+ const searchInputRef = React.useRef<HTMLInputElement>(null);
const listRef = React.useRef<Array<HTMLElement | null>>([]);
const labelsRef = React.useRef<Array<string | null>>([]);
const closeMenu = React.useCallback(() => {
@@ -358,39 +359,63 @@ function DropdownMenuNode({
) : null}
{search ? (
<div className="border-border bg-popover sticky top-0 z-1 border-b p-1.5">
- <input
- type="search"
- value={searchQuery}
- placeholder={search.placeholder}
- aria-label={search.placeholder}
- className="border-input bg-background focus:ring-ring h-8 w-full rounded-md border px-2 text-sm outline-hidden focus:ring-1"
- onChange={(event) => {
- setSearchQuery(event.target.value);
- setActiveIndex(null);
- }}
- onKeyDown={(event) => {
- if (event.key === "Escape") {
- closeMenu();
- event.preventDefault();
- } else if (event.key === "ArrowDown") {
- const firstIndex = listRef.current
- .slice(0, visibleItems.length)
- .findIndex((element) => element !== null);
- setActiveIndex(firstIndex === -1 ? null : firstIndex);
- listRef.current[firstIndex]?.focus();
- event.preventDefault();
- } else if (event.key === "ArrowUp") {
- const lastIndex = listRef.current
- .slice(0, visibleItems.length)
- .findLastIndex((element) => element !== null);
- setActiveIndex(lastIndex === -1 ? null : lastIndex);
- listRef.current[lastIndex]?.focus();
- event.preventDefault();
- }
+ <div className="relative">
+ <input
+ ref={searchInputRef}
+ type="search"
+ value={searchQuery}
+ placeholder={search.placeholder}
+ aria-label={search.placeholder}
+ className="border-input bg-background focus:ring-ring h-8 w-full rounded-md border pr-8 pl-2 text-sm outline-hidden focus:ring-1 [&::-webkit-search-cancel-button]:hidden"
+ onChange={(event) => {
+ setSearchQuery(event.target.value);
+ setActiveIndex(null);
+ }}
+ onKeyDown={(event) => {
+ if (event.key === "Escape") {
+ closeMenu();
+ event.preventDefault();
+ } else if (event.key === "ArrowDown") {
+ const firstIndex = listRef.current
+ .slice(0, visibleItems.length)
+ .findIndex((element) => element !== null);
+ setActiveIndex(firstIndex === -1 ? null : firstIndex);
+ listRef.current[firstIndex]?.focus();
+ event.preventDefault();
+ } else if (event.key === "ArrowUp") {
+ const lastIndex = listRef.current
+ .slice(0, visibleItems.length)
+ .findLastIndex((element) => element !== null);
+ setActiveIndex(lastIndex === -1 ? null : lastIndex);
+ listRef.current[lastIndex]?.focus();
+ event.preventDefault();
+ }
- event.stopPropagation();
- }}
- />
+ event.stopPropagation();
+ }}
+ />
+ {searchQuery !== "" && (
+ <button
+ type="button"
+ aria-label="Clear search"
+ // Not a tab stop, matching the native control it
+ // replaces.
+ tabIndex={-1}
+ // The button unmounts as soon as the query clears, so
+ // hand focus back to the field. That also keeps DOM
+ // focus and the cleared activeIndex in agreement when
+ // the list had been arrowed into.
+ onClick={() => {
+ setSearchQuery("");
+ setActiveIndex(null);
+ searchInputRef.current?.focus();
+ }}
+ className="text-muted-foreground hover:bg-accent hover:text-accent-foreground absolute top-1/2 right-1.5 flex size-6 -translate-y-1/2 items-center justify-center rounded-sm"
+ >
+ <X size="base" aria-hidden />
+ </button>
+ )}
+ </div>
</div>
) : null}
<div
diff --git a/web/src/components/design-system/SearchInput/SearchInput.stories.tsx b/web/src/components/design-system/SearchInput/SearchInput.stories.tsx
index 04f2313bb9..dedf43f4ed 100644
--- a/web/src/components/design-system/SearchInput/SearchInput.stories.tsx
+++ b/web/src/components/design-system/SearchInput/SearchInput.stories.tsx
@@ -48,6 +48,13 @@ const meta = preview.meta({
export const Default = meta.story({});
+/** The clear button appears only while the field has a value. */
+export const WithValue = meta.story({
+ args: {
+ value: "Prompt name",
+ },
+});
+
/** `default` for embedded toolbars, `large` for full-page lists. */
export const Sizes = meta.story({
render: (args) => (
@@ -67,6 +74,7 @@ export const WithDropdown = meta.story({
export const Disabled = meta.story({
args: {
disabled: true,
+ value: "Prompt name",
dropdown,
},
});
@@ -94,3 +102,26 @@ export const TestOpensDropdown = meta.story({
await expect(trigger).toHaveAttribute("data-state", "open");
},
});
+
+export const TestClearsTheValue = meta.story({
+ name: "(Test) Clears the Value",
+ args: {
+ value: "Prompt name",
+ },
+ play: async ({ args, canvasElement }) => {
+ const canvas = within(canvasElement);
+ const input = canvas.getByRole("searchbox");
+ await userEvent.click(canvas.getByRole("button", { name: "Clear search" }));
+ await expect(input).toHaveValue("");
+ await expect(args.onChange).toHaveBeenCalledWith("");
+ await expect(
+ canvas.queryByRole("button", { name: "Clear search" }),
+ ).not.toBeInTheDocument();
+ // Clearing is a draft change, like typing — applying it stays with Enter
+ // and the search button.
+ await expect(args.onSubmit).not.toHaveBeenCalled();
+ // The field keeps focus so the next query can be typed straight away,
+ // even though the button that was clicked has gone.
+ await expect(input).toHaveFocus();
+ },
+});
diff --git a/web/src/components/design-system/SearchInput/SearchInput.tsx b/web/src/components/design-system/SearchInput/SearchInput.tsx
index 958d983a75..954e3713da 100644
--- a/web/src/components/design-system/SearchInput/SearchInput.tsx
+++ b/web/src/components/design-system/SearchInput/SearchInput.tsx
@@ -2,7 +2,7 @@
"use client";
import * as React from "react";
-import { Search } from "@langfuse/icons";
+import { Search, X } from "@langfuse/icons";
import { DropdownIndicator } from "@/src/components/design-system/DropdownIndicator/DropdownIndicator";
import { cva, type VariantProps } from "class-variance-authority";
@@ -27,6 +27,10 @@ const searchInputVariants = cva(
},
);
+/** The square actions pinned to either end of the field. */
+const edgeActionClassName =
+ "text-foreground-tertiary hover:bg-accent hover:text-accent-foreground flex aspect-square shrink-0 items-center justify-center disabled:cursor-not-allowed disabled:opacity-50";
+
type SearchInputProps = {
value: string;
onChange: (value: string) => void;
@@ -56,63 +60,91 @@ export const SearchInput = React.forwardRef<HTMLInputElement, SearchInputProps>(
dropdown,
},
ref,
- ) => (
- <div className={searchInputVariants({ size })}>
- <button
- type="button"
- aria-label="Search"
- disabled={disabled}
- onClick={() => onSubmit(value)}
- className="text-foreground-tertiary hover:bg-accent hover:text-accent-foreground flex aspect-square shrink-0 items-center justify-center disabled:cursor-not-allowed disabled:opacity-50"
- >
- <Search size="base" />
- </button>
- <input
- ref={ref}
- type="search"
- data-1p-ignore
- value={value}
- placeholder={placeholder}
- disabled={disabled}
- autoFocus={autoFocus}
- onChange={(event) => onChange(event.currentTarget.value)}
- onBlur={() => onBlur?.(value)}
- onKeyDown={(event) => {
- if (event.key === "Enter") onSubmit(value);
- }}
- className="placeholder:text-muted-foreground disabled:bg-muted/50 min-w-0 flex-1 appearance-none border-0 bg-transparent py-1 pr-2 pl-0 font-mono text-xs shadow-none outline-hidden focus:border-0 focus:shadow-none focus:ring-0 focus:ring-offset-0 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 [&::-webkit-search-cancel-button]:cursor-pointer"
- />
- {dropdown && (
- <DropdownMenu>
- <DropdownMenuTrigger asChild>
- <button
- type="button"
- disabled={disabled}
- className="hover:bg-accent hover:text-accent-foreground flex w-30 shrink-0 items-center justify-between gap-1 px-3 py-1 text-sm disabled:cursor-not-allowed disabled:opacity-50"
- >
- <span className="flex min-w-0 items-center gap-1">
- <span
- className="truncate"
- title={
- typeof dropdown.label === "string"
- ? dropdown.label
- : undefined
- }
- >
- {dropdown.label}
+ ) => {
+ const inputRef = React.useRef<HTMLInputElement>(null);
+ React.useImperativeHandle(ref, () => inputRef.current as HTMLInputElement);
+
+ return (
+ <div className={searchInputVariants({ size })}>
+ <button
+ type="button"
+ aria-label="Search"
+ disabled={disabled}
+ onClick={() => onSubmit(value)}
+ className={edgeActionClassName}
+ >
+ <Search size="base" aria-hidden />
+ </button>
+ <input
+ ref={inputRef}
+ type="search"
+ data-1p-ignore
+ value={value}
+ placeholder={placeholder}
+ disabled={disabled}
+ autoFocus={autoFocus}
+ onChange={(event) => onChange(event.currentTarget.value)}
+ onBlur={() => onBlur?.(value)}
+ onKeyDown={(event) => {
+ if (event.key === "Enter") onSubmit(value);
+ }}
+ className="placeholder:text-muted-foreground disabled:bg-muted/50 min-w-0 flex-1 appearance-none border-0 bg-transparent py-1 pr-2 pl-0 font-mono text-xs shadow-none outline-hidden focus:border-0 focus:shadow-none focus:ring-0 focus:ring-offset-0 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 [&::-webkit-search-cancel-button]:hidden"
+ />
+ {value !== "" && (
+ <button
+ type="button"
+ aria-label="Clear search"
+ disabled={disabled}
+ // Not a tab stop, matching the native control it replaces.
+ tabIndex={-1}
+ // Prevent the input from blurring (and firing onBlur) when this
+ // button is pressed. Without this, on mobile filter sheets the
+ // onBlur handler submits the stale draft value before the click
+ // handler can clear it.
+ onMouseDown={(e) => e.preventDefault()}
+ // The button unmounts as soon as the value clears, so hand focus to
+ // the field rather than letting it fall to the document.
+ onClick={() => {
+ onChange("");
+ inputRef.current?.focus();
+ }}
+ className={edgeActionClassName}
+ >
+ <X size="base" aria-hidden />
+ </button>
+ )}
+ {dropdown && (
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <button
+ type="button"
+ disabled={disabled}
+ className="hover:bg-accent hover:text-accent-foreground flex w-30 shrink-0 items-center justify-between gap-1 px-3 py-1 text-sm disabled:cursor-not-allowed disabled:opacity-50"
+ >
+ <span className="flex min-w-0 items-center gap-1">
+ <span
+ className="truncate"
+ title={
+ typeof dropdown.label === "string"
+ ? dropdown.label
+ : undefined
+ }
+ >
+ {dropdown.label}
+ </span>
+ {dropdown.labelAccessory}
</span>
- {dropdown.labelAccessory}
- </span>
- <DropdownIndicator />
- </button>
- </DropdownMenuTrigger>
- <DropdownMenuContent align="end">
- {dropdown.content}
- </DropdownMenuContent>
- </DropdownMenu>
- )}
- </div>
- ),
+ <DropdownIndicator />
+ </button>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="end">
+ {dropdown.content}
+ </DropdownMenuContent>
+ </DropdownMenu>
+ )}
+ </div>
+ );
+ },
);
SearchInput.displayName = "SearchInput";