fix(webui): simplify mobile composer controls and context sheet

This commit is contained in:
Xubin Ren committed 2026-09-13 23:35:16 +08:00
1 parent 3a14935eb1
commit 827dbe137b
7 files changed
+395 -219

No files matched your search

@@ -1,4 +1,4 @@
import { Fragment, useMemo, useRef } from "react";
import { Fragment, useMemo, useRef, type ReactNode } from "react";
import { useTranslation } from "react-i18next";
import {
@@ -6,6 +6,7 @@ import {
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";
import { Sheet, SheetContent, SheetTitle, SheetTrigger } from "@/components/ui/sheet";
import {
Tooltip,
TooltipContent,
@@ -55,12 +56,15 @@ function normalizeRounds(
export function ComposerUsagePopover({
context,
rounds,
showLabel = false,
bottomSheet = false,
}: {
context: ComposerContextUsage | null;
rounds: readonly ComposerRoundUsage[];
showLabel?: boolean;
bottomSheet?: boolean;
}) {
const { t, i18n } = useTranslation();
const panelRef = useRef<HTMLDivElement>(null);
const normalizedRounds = useMemo(() => normalizeRounds(rounds), [rounds]);
const hasContext = !!context
&& Number.isFinite(context.contextTokens)
@@ -110,109 +114,104 @@ export function ComposerUsagePopover({
minute: "2-digit",
});
return (
<Popover>
<TooltipProvider>
<Tooltip>
<TooltipTrigger asChild>
<PopoverTrigger asChild>
<button
type="button"
data-testid="composer-context-usage"
aria-label={triggerLabel}
className={cn(
"touch-target inline-flex size-5 shrink-0 items-center justify-center rounded-full",
"text-muted-foreground/75 transition-colors hover:text-foreground/85",
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
)}
>
{contextPercentage === null ? (
<svg viewBox="0 0 16 16" aria-hidden="true" className="size-[15px]">
<path
d="M3 12V9m5 3V5m5 7V2"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
/>
</svg>
) : (
<svg
viewBox="0 0 16 16"
aria-hidden="true"
className={cn(
"size-[15px] shrink-0 -rotate-90",
status === "critical" && "text-destructive",
status === "caution" && "text-amber-600 dark:text-amber-400",
status === "normal" && "text-muted-foreground/75",
)}
>
<circle
cx="8"
cy="8"
r="6"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
className="opacity-20"
/>
<circle
cx="8"
cy="8"
r="6"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeDasharray={`${ringLength} ${ringCircumference}`}
data-testid="composer-context-meter"
/>
</svg>
)}
</button>
</PopoverTrigger>
</TooltipTrigger>
<TooltipContent
side="top"
align="center"
sideOffset={8}
className="w-fit max-w-[calc(100vw-2rem)] rounded-full border-border/70 px-2.5 py-1 text-xs font-medium shadow-[0_8px_24px_rgba(15,23,42,0.13)]"
const Root = bottomSheet ? Sheet : Popover;
const Trigger = bottomSheet ? SheetTrigger : PopoverTrigger;
const trigger = (
<Trigger asChild>
<button
type="button"
data-testid="composer-context-usage"
aria-label={triggerLabel}
className={cn(
"touch-target inline-flex size-5 shrink-0 items-center justify-center rounded-full",
"text-muted-foreground/75 transition-colors hover:text-foreground/85",
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
showLabel && "w-auto gap-1.5 px-2 text-xs",
)}
>
{contextPercentage === null ? (
<svg viewBox="0 0 16 16" aria-hidden="true" className="size-[15px]">
<path
d="M3 12V9m5 3V5m5 7V2"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
/>
</svg>
) : (
<svg
viewBox="0 0 16 16"
aria-hidden="true"
className={cn(
"size-[15px] shrink-0 -rotate-90",
status === "critical" && "text-destructive",
status === "caution" && "text-amber-600 dark:text-amber-400",
status === "normal" && "text-muted-foreground/75",
)}
>
<span className="whitespace-nowrap tabular-nums">{contextDescription}</span>
</TooltipContent>
</Tooltip>
<circle
cx="8"
cy="8"
r="6"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
className="opacity-20"
/>
<circle
cx="8"
cy="8"
r="6"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeDasharray={`${ringLength} ${ringCircumference}`}
data-testid="composer-context-meter"
/>
</svg>
)}
{showLabel ? <span aria-hidden>{contextDescription}</span> : null}
</button>
</Trigger>
);
<PopoverContent
ref={panelRef}
side="top"
align="end"
sideOffset={10}
onOpenAutoFocus={(event) => {
event.preventDefault();
panelRef.current?.focus();
}}
aria-label={t("thread.composer.context.panelTitle", {
defaultValue: "Context usage",
})}
className="w-[min(22rem,calc(100vw-1.5rem))] p-0"
>
return (
<Root>
<TooltipProvider>
{bottomSheet ? trigger : (
<Tooltip>
<TooltipTrigger asChild>{trigger}</TooltipTrigger>
<TooltipContent
side="top"
align="center"
sideOffset={8}
className="w-fit max-w-[calc(100vw-2rem)] rounded-full border-border/70 px-2.5 py-1 text-xs font-medium shadow-[0_8px_24px_rgba(15,23,42,0.13)]"
>
<span className="whitespace-nowrap tabular-nums">{contextDescription}</span>
</TooltipContent>
</Tooltip>
)}
<UsagePanel bottomSheet={bottomSheet}>
<div className="px-4 pb-4 pt-3.5">
{contextPercentage !== null ? (
<>
<div className="flex items-baseline justify-between gap-3">
<div className="flex min-w-0 items-baseline gap-2">
<span className="shrink-0 text-[12px] font-medium text-foreground">
<span className={cn("shrink-0 font-medium text-foreground", bottomSheet ? "text-sm" : "text-[12px]")}>
{t("thread.composer.context.contextTitle", {
defaultValue: "Context",
})}
</span>
<span className="truncate text-[11px] tabular-nums text-muted-foreground">
<span className={cn("truncate tabular-nums text-muted-foreground", bottomSheet ? "text-xs" : "text-[11px]")}>
{formatCompactTokenCount(context!.contextTokens)} / {formatCompactTokenCount(
context!.contextWindowTokens!,
)}
</span>
</div>
<span className="shrink-0 text-[11px] tabular-nums text-muted-foreground">
<span className={cn("shrink-0 tabular-nums text-muted-foreground", bottomSheet ? "text-xs" : "text-[11px]")}>
{contextPercentage}%
</span>
</div>
@@ -243,12 +242,12 @@ export function ComposerUsagePopover({
"flex items-baseline justify-between gap-3",
contextPercentage === null ? "mt-0" : "mt-5",
)}>
<span className="text-[12px] font-medium text-foreground">
<span className={cn("font-medium text-foreground", bottomSheet ? "text-sm" : "text-[12px]")}>
{t("thread.composer.context.recentRounds", {
defaultValue: "Recent rounds",
})}
</span>
<span className="text-[11px] text-muted-foreground">
<span className={cn("text-muted-foreground", bottomSheet ? "text-xs" : "text-[11px]")}>
{t("thread.composer.context.inputTrend", {
defaultValue: "Input tokens",
})}
@@ -403,8 +402,56 @@ export function ComposerUsagePopover({
) : null}
</div>
</PopoverContent>
</UsagePanel>
</TooltipProvider>
</Popover>
</Root>
);
}
function UsagePanel({ bottomSheet, children }: { bottomSheet: boolean; children: ReactNode }) {
const { t } = useTranslation();
const panelRef = useRef<HTMLDivElement>(null);
const title = t("thread.composer.context.panelTitle", { defaultValue: "Context usage" });
const focusPanel = (event: Event) => {
event.preventDefault();
panelRef.current?.focus({ preventScroll: true });
};
if (bottomSheet) {
return (
<SheetContent
ref={panelRef}
side="bottom"
aria-describedby={undefined}
onOpenAutoFocus={focusPanel}
className={cn(
"mx-auto max-h-[60dvh] w-full max-w-md gap-0 rounded-t-3xl border-t border-border/60 p-0 shadow-xl outline-none",
"data-[state=open]:duration-300 data-[state=open]:ease-out data-[state=closed]:duration-200 data-[state=closed]:ease-in",
)}
closeButtonClassName="right-3 top-5 flex size-11 items-center justify-center rounded-full"
>
<div aria-hidden="true" className="mx-auto mt-2.5 h-1 w-8 shrink-0 rounded-full bg-foreground/15" />
<div className="shrink-0 px-5 pb-2 pt-5 pr-16">
<SheetTitle className="text-base font-medium">{title}</SheetTitle>
</div>
<div className="min-h-0 overflow-y-auto overscroll-contain px-1 pb-[max(0.75rem,env(safe-area-inset-bottom))]">
{children}
</div>
</SheetContent>
);
}
return (
<PopoverContent
ref={panelRef}
side="top"
align="end"
sideOffset={10}
onOpenAutoFocus={focusPanel}
aria-label={title}
className="w-[min(22rem,calc(100vw-1.5rem))] p-0"
>
{children}
</PopoverContent>
);
}
@@ -483,7 +483,7 @@ function PresetOption({
className={cn(
floatingItemClassName,
floatingItemFocusClassName,
"flex min-h-9 w-full cursor-pointer gap-2.5 px-2.5 py-1.5 text-left hover:bg-muted/55",
"flex min-h-9 w-full cursor-pointer gap-2.5 px-2.5 py-1.5 text-left hover:bg-muted/55 max-sm:min-h-11",
selected && "bg-muted/55 text-foreground",
)}
>
@@ -493,8 +493,8 @@ function PresetOption({
provider={preset.provider}
isHero={false}
/>
<span className="flex min-w-0 flex-1 items-baseline gap-1.5 overflow-hidden whitespace-nowrap">
<span className="shrink-0 text-[13px] font-medium text-foreground">{preset.name}</span>
<span className="flex min-w-0 flex-1 items-baseline gap-1.5 overflow-hidden whitespace-nowrap max-sm:flex-col max-sm:items-start max-sm:gap-0.5 max-sm:whitespace-normal">
<span className="shrink-0 text-[13px] font-medium text-foreground max-sm:max-w-full max-sm:break-words">{preset.name}</span>
{detail && detail !== preset.name ? (
<span className="truncate text-[12px] text-muted-foreground">{detail}</span>
) : null}
+80 -45
View File
@@ -963,6 +963,32 @@ export function ThreadComposer({
const textareaRef = useRef<HTMLTextAreaElement>(null);
const mentionOverlayRef = useRef<HTMLDivElement>(null);
const formRef = useRef<HTMLFormElement>(null);
const [compactControls, setCompactControls] = useState(false);
useLayoutEffect(() => {
const form = formRef.current;
if (!form) return;
let previousWidth = 0;
const update = () => {
const width = form.getBoundingClientRect().width;
if (width <= 0 || width === previousWidth) return;
previousWidth = width;
setCompactControls(width <= 512);
const input = textareaRef.current;
if (input) {
input.style.height = "auto";
input.style.height = `${Math.min(input.scrollHeight, 260)}px`;
}
};
update();
const observer = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(update);
observer?.observe(form);
window.addEventListener("resize", update);
return () => {
observer?.disconnect();
window.removeEventListener("resize", update);
};
}, []);
const fileInputRef = useRef<HTMLInputElement>(null);
const chipRefs = useRef(new Map<string, HTMLButtonElement>());
const queuedPromptCounterRef = useRef(0);
@@ -1524,7 +1550,7 @@ export function ThreadComposer({
el.style.height = "auto";
el.style.height = `${Math.min(el.scrollHeight, 260)}px`;
if (mentionOverlayRef.current) mentionOverlayRef.current.scrollTop = el.scrollTop;
}, [mentionInput.isComposing, mentionInput.value]);
}, [compactControls, mentionInput.isComposing, mentionInput.value]);
// Runs before paint so switching sessions never flashes stale draft text.
useLayoutEffect(() => {
@@ -2201,12 +2227,12 @@ export function ThreadComposer({
: t("thread.composer.voice.hint");
const showStopButton = isStreaming && !!onStop;
const relaxedHeroInput = isHero && images.length === 0 && !isStreaming;
const compactIdle = compactWhenIdle && !isHero && !composerFocused
const compactIdle = compactWhenIdle && !compactControls && !isHero && !composerFocused
&& value.length === 0 && images.length === 0 && !inlineError
&& !normalizedQuotedContext && !queuedPrompts.length && !goalState?.active
&& !isDragging && !sessionDragPreview && !voiceRecorder.isRecording && !showProjectPicker;
useLayoutEffect(() => {
if (!compactWhenIdle) return;
if (!compactWhenIdle || compactControls) return;
const form = formRef.current;
const primary = form?.querySelector<HTMLElement>(".thread-composer-footer-primary");
const actions = form?.querySelector<HTMLElement>(".thread-composer-footer-actions");
@@ -2225,20 +2251,53 @@ export function ThreadComposer({
controls.forEach((control) => observer?.observe(control));
observer?.observe(actions);
return () => observer?.disconnect();
}, [compactWhenIdle, modelLabel, voiceRecorder.isRecording, workspaceScope]);
}, [compactControls, compactWhenIdle, modelLabel, voiceRecorder.isRecording, workspaceScope]);
const accessControl = workspaceScope && !workspaceControlsHidden ? (
<WorkspaceAccessMenu
scope={workspaceScope}
disabled={interactionDisabled || workspaceScopeDisabled}
canUseFullAccess={workspaceControls?.can_use_full_access !== false}
isHero={isHero}
onChange={onWorkspaceScopeChange}
/>
) : null;
const modelControl = modelLabel && !voiceRecorder.isRecording ? (
<ModelPresetBadge
label={modelLabel}
modelDetail={modelDetail}
modelPreset={modelPreset}
modelPresets={modelPresets}
onPresetChange={onModelPresetChange}
onManageModels={onManageModels}
onRequestComposerFocus={() => textareaRef.current?.focus()}
provider={modelProvider}
providerLabel={modelProviderLabel}
needsSetup={modelNeedsSetup}
attentionRequest={modelSetupAttentionRequest}
fallbackModelName={fallbackModelName}
isHero={isHero && !compactControls}
onClick={modelNeedsSetup ? onModelBadgeClick : undefined}
/>
) : null;
const usageControl = !voiceRecorder.isRecording ? (
<ComposerUsagePopover context={contextUsage} rounds={recentRoundUsage} showLabel={compactControls} bottomSheet={compactControls} />
) : null;
const inputTextClasses = cn(
"w-full resize-none bg-transparent",
isHero
? cn(
"min-h-[78px] px-4 text-[16px] leading-6 sm:px-5",
relaxedHeroInput ? "pb-2 pt-[27px]" : "pb-1.5 pt-4",
)
: "min-h-[50px] px-3.5 pb-1.5 pt-3 text-[16px] leading-5 sm:px-4",
compactControls
? "min-h-[52px] px-4 pb-2 pt-3 text-[16px] leading-6"
: isHero
? cn(
"min-h-[78px] px-4 text-[16px] leading-6 sm:px-5",
relaxedHeroInput ? "pb-2 pt-[27px]" : "pb-1.5 pt-4",
)
: "min-h-[50px] px-3.5 pb-1.5 pt-3 text-[16px] leading-5 sm:px-4",
);
return (
<form
ref={formRef}
data-compact-controls={compactControls ? "true" : undefined}
onFocusCapture={() => {
if (!compactWhenIdle) return;
// Portaled model controls belong to this composer too: keep its layout
@@ -2488,7 +2547,7 @@ export function ThreadComposer({
aria-label={t("thread.composer.attachImage")}
onClick={() => fileInputRef.current?.click()}
className={cn(
"thread-composer-action touch-target rounded-full text-muted-foreground hover:text-foreground",
"thread-composer-action thread-composer-round-action touch-target rounded-full text-muted-foreground hover:text-foreground",
isHero
? "h-8 w-8 border border-border/55 bg-card shadow-[0_2px_8px_rgba(15,23,42,0.05)] hover:bg-card"
: "h-9 w-9 border border-border/55 bg-card shadow-[0_2px_8px_rgba(15,23,42,0.05)] hover:bg-card",
@@ -2504,15 +2563,7 @@ export function ThreadComposer({
isHero={isHero}
levels={voiceRecorder.levels}
/>
) : workspaceScope && !workspaceControlsHidden ? (
<WorkspaceAccessMenu
scope={workspaceScope}
disabled={interactionDisabled || workspaceScopeDisabled}
canUseFullAccess={workspaceControls?.can_use_full_access !== false}
isHero={isHero}
onChange={onWorkspaceScopeChange}
/>
) : null}
) : compactControls ? modelControl : accessControl}
</div>
<div
className={cn(
@@ -2520,30 +2571,8 @@ export function ThreadComposer({
isHero ? "gap-1.5" : "gap-2",
)}
>
{modelLabel && !voiceRecorder.isRecording ? (
<ModelPresetBadge
label={modelLabel}
modelDetail={modelDetail}
modelPreset={modelPreset}
modelPresets={modelPresets}
onPresetChange={onModelPresetChange}
onManageModels={onManageModels}
onRequestComposerFocus={() => textareaRef.current?.focus()}
provider={modelProvider}
providerLabel={modelProviderLabel}
needsSetup={modelNeedsSetup}
attentionRequest={modelSetupAttentionRequest}
fallbackModelName={fallbackModelName}
isHero={isHero}
onClick={modelNeedsSetup ? onModelBadgeClick : undefined}
/>
) : null}
{!voiceRecorder.isRecording ? (
<ComposerUsagePopover
context={contextUsage}
rounds={recentRoundUsage}
/>
) : null}
{!compactControls ? modelControl : null}
{!compactControls ? usageControl : null}
{showVoiceButton ? (
<TooltipProvider>
<Tooltip>
@@ -2604,7 +2633,7 @@ export function ThreadComposer({
}
onClick={showStopButton ? handleStop : modelNeedsSetup ? onModelBadgeClick : undefined}
className={cn(
"thread-composer-action touch-target rounded-full transition-transform",
"thread-composer-action thread-composer-round-action touch-target rounded-full transition-transform",
showStopButton
? "border border-border/70 bg-card text-foreground/85 shadow-[0_3px_10px_rgba(15,23,42,0.08)] hover:bg-muted/65 hover:text-foreground disabled:text-muted-foreground/50"
: isHero
@@ -2648,6 +2677,12 @@ export function ThreadComposer({
</div>
) : null}
</div>
{compactControls ? (
<div className="thread-composer-meta mx-auto flex w-full max-w-[58rem] items-center justify-between gap-2 px-2">
{accessControl}
<div className="ml-auto">{usageControl}</div>
</div>
) : null}
</form>
);
}
@@ -389,7 +389,7 @@ function AccessMenuItem({
disabled={disabled}
onSelect={onSelect}
className={cn(
"flex h-10 items-center gap-3 px-3 text-[13.5px] font-semibold",
"flex h-10 items-center gap-3 px-3 text-[13.5px] font-semibold max-sm:min-h-11",
warning && "text-orange-600 focus:text-orange-600 dark:text-orange-300 dark:focus:text-orange-300",
)}
>
+5 -2
View File
@@ -7,6 +7,7 @@ import { useTranslation } from "react-i18next";
import { cn } from "@/lib/utils";
const Sheet = DialogPrimitive.Root;
const SheetTrigger = DialogPrimitive.Trigger;
const SheetPortal = DialogPrimitive.Portal;
const SheetOverlay = React.forwardRef<
@@ -19,6 +20,7 @@ const SheetOverlay = React.forwardRef<
"fixed inset-0 z-50 bg-black/40",
"data-[state=open]:animate-in data-[state=closed]:animate-out",
"data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
"motion-reduce:data-[state=open]:animate-none motion-reduce:data-[state=closed]:animate-none",
className,
)}
{...props}
@@ -85,7 +87,8 @@ const SheetContent = React.forwardRef<
className={cn(
sheetVariants({ side }),
"data-[state=open]:animate-in data-[state=closed]:animate-out",
"duration-300",
"duration-300 motion-reduce:transition-none",
"motion-reduce:data-[state=open]:animate-none motion-reduce:data-[state=closed]:animate-none",
className,
)}
{...props}
@@ -134,4 +137,4 @@ const SheetDescription = React.forwardRef<
));
SheetDescription.displayName = DialogPrimitive.Description.displayName;
export { Sheet, SheetContent, SheetDescription, SheetTitle };
export { Sheet, SheetTrigger, SheetContent, SheetDescription, SheetTitle };