fix(web): keep composer controls visible while they fit (#12837)
This commit is contained in:
1 parent
a6cb1dd205
commit
6cc7f7006e
6 files changed
+326
-168
No files matched your search
@@ -53,7 +53,6 @@ import { folderDropTarget, resolveDroppedFolderPath } from "./folderDrop";
|
||||
import { createModelSelection, normalizeModelSlug } from "@t3tools/shared/model";
|
||||
import { USAGE_LIMITS_COMMAND } from "@t3tools/shared/usageLimits";
|
||||
import {
|
||||
Fragment,
|
||||
memo,
|
||||
type ComponentProps,
|
||||
type ReactNode,
|
||||
@@ -187,8 +186,6 @@ import {
|
||||
import { useComposerPathSearch } from "../../lib/composerPathSearchState";
|
||||
import { replaceComposerContextReferences } from "@t3tools/shared/composerContextReferences";
|
||||
import {
|
||||
COMPOSER_FOOTER_COMPACT_BREAKPOINT_PX,
|
||||
COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX,
|
||||
getRestingComposerImagePreviewCounts,
|
||||
resolveRestingComposerControlsLayout,
|
||||
shouldAnimateComposerRestingTransition,
|
||||
@@ -197,7 +194,6 @@ import {
|
||||
shouldUseRestingComposerLayout,
|
||||
} from "../composerFooterLayout";
|
||||
import { measureRestingComposerControls } from "./restingComposerControlsMeasurement";
|
||||
import { observeResponsiveBreakpointFade, usePanelAnimationSettings } from "../../panelAnimations";
|
||||
import { type ComposerPromptEditorHandle, ComposerPromptEditor } from "../ComposerPromptEditor";
|
||||
import {
|
||||
ComposerContextActionsContext,
|
||||
@@ -994,22 +990,32 @@ const extendReplacementRangeForTrailingSpace = (
|
||||
return text[rangeEnd] === " " ? rangeEnd + 1 : rangeEnd;
|
||||
};
|
||||
|
||||
function useRestingComposerControlsLayout(host: HTMLDivElement | null) {
|
||||
function useRestingComposerControlsLayout(host: HTMLDivElement | null, useControlsAsHost = false) {
|
||||
const [controls, setControls] = useState<HTMLDivElement | null>(null);
|
||||
const controlsRef = useRef<HTMLDivElement>(null);
|
||||
const attachControls = useCallback((element: HTMLDivElement | null) => {
|
||||
controlsRef.current = element;
|
||||
setControls(element);
|
||||
}, []);
|
||||
const hostRef = useRef(host);
|
||||
hostRef.current = host;
|
||||
const [layout, setLayout] = useState({ hiddenCount: 0, visible: true });
|
||||
const [layout, setLayout] = useState<{
|
||||
hiddenCount: number;
|
||||
iconOnlyCount?: number;
|
||||
visible: boolean;
|
||||
}>({ hiddenCount: 0, visible: true });
|
||||
|
||||
const measure = useCallback(() => {
|
||||
const currentHost = hostRef.current;
|
||||
const controls = controlsRef.current;
|
||||
// The controls only mount while the composer rests, so the expanded
|
||||
// composer pays no layout reads here despite the every-render effect.
|
||||
const currentHost = useControlsAsHost ? controls : hostRef.current;
|
||||
if (currentHost === null || !controls) return;
|
||||
|
||||
const measurement = measureRestingComposerControls(controls);
|
||||
if (!measurement) return;
|
||||
const hostWidth = currentHost.clientWidth;
|
||||
const style = getComputedStyle(currentHost);
|
||||
const hostWidth =
|
||||
currentHost.clientWidth -
|
||||
(Number.parseFloat(style.paddingInlineStart) || 0) -
|
||||
(Number.parseFloat(style.paddingInlineEnd) || 0);
|
||||
|
||||
setLayout((current) => {
|
||||
const next = resolveRestingComposerControlsLayout({
|
||||
@@ -1017,25 +1023,48 @@ function useRestingComposerControlsLayout(host: HTMLDivElement | null) {
|
||||
hostWidth,
|
||||
previous: current,
|
||||
});
|
||||
return next.hiddenCount === current.hiddenCount && next.visible === current.visible
|
||||
return next.hiddenCount === current.hiddenCount &&
|
||||
next.iconOnlyCount === current.iconOnlyCount &&
|
||||
next.visible === current.visible
|
||||
? current
|
||||
: next;
|
||||
});
|
||||
}, []);
|
||||
}, [controls, useControlsAsHost]);
|
||||
|
||||
useLayoutEffect(measure);
|
||||
useLayoutEffect(measure, [measure, host]);
|
||||
useEffect(() => {
|
||||
if (!host) return;
|
||||
const currentHost = useControlsAsHost ? controls : host;
|
||||
if (!currentHost || !controls) return;
|
||||
const observer = new ResizeObserver(measure);
|
||||
observer.observe(host);
|
||||
const observeControls = () => {
|
||||
observer.disconnect();
|
||||
observer.observe(currentHost);
|
||||
observer.observe(controls);
|
||||
controls
|
||||
.querySelectorAll<HTMLElement>(
|
||||
"[data-resting-block], [data-composer-control-label], [data-chat-provider-model-picker-label]",
|
||||
)
|
||||
.forEach((element) => observer.observe(element));
|
||||
measure();
|
||||
};
|
||||
observeControls();
|
||||
const mutations = new MutationObserver(observeControls);
|
||||
mutations.observe(controls, { childList: true, subtree: true, characterData: true });
|
||||
document.fonts.addEventListener("loadingdone", measure);
|
||||
return () => {
|
||||
observer.disconnect();
|
||||
mutations.disconnect();
|
||||
document.fonts.removeEventListener("loadingdone", measure);
|
||||
};
|
||||
}, [host, measure]);
|
||||
}, [host, controls, useControlsAsHost, measure]);
|
||||
|
||||
return { controlsRef, hiddenBlockCount: layout.hiddenCount, controlsVisible: layout.visible };
|
||||
return {
|
||||
controlsRef,
|
||||
attachControls,
|
||||
hiddenBlockCount: layout.hiddenCount,
|
||||
iconOnlyBlockCount: layout.iconOnlyCount ?? 0,
|
||||
controlsVisible: layout.visible,
|
||||
};
|
||||
}
|
||||
|
||||
const ComposerFooterModeControls = memo(function ComposerFooterModeControls(props: {
|
||||
@@ -1092,7 +1121,7 @@ const ComposerFooterModeControls = memo(function ComposerFooterModeControls(prop
|
||||
opticalSize={size === "xs" ? "default" : "large"}
|
||||
/>
|
||||
)}
|
||||
<span className="sr-only sm:not-sr-only">
|
||||
<span data-composer-control-label className="sr-only sm:not-sr-only">
|
||||
{props.interactionMode === "plan" ? "Plan" : "Build"}
|
||||
</span>
|
||||
</TooltipTrigger>
|
||||
@@ -1123,7 +1152,7 @@ const ComposerFooterModeControls = memo(function ComposerFooterModeControls(prop
|
||||
}
|
||||
>
|
||||
<ComposerControlIcon icon={RuntimeModeIcon} size={size} />
|
||||
<SelectValue>{runtimeModeOption.label}</SelectValue>
|
||||
<SelectValue data-composer-control-label>{runtimeModeOption.label}</SelectValue>
|
||||
</TooltipTrigger>
|
||||
<SelectPopup alignItemWithTrigger={false} {...composerFloatingLayerProps}>
|
||||
{runtimeModeOptions.map((mode) => {
|
||||
@@ -2104,8 +2133,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
key: 0,
|
||||
active: false,
|
||||
});
|
||||
const { active: panelAnimationsActive, durationMs: panelAnimationDurationMs } =
|
||||
usePanelAnimationSettings();
|
||||
const isComposerCollapsedMobile =
|
||||
isMobileViewport && !forceExpandedOnMobile && !isComposerFocused && !hasMultilinePrompt;
|
||||
|
||||
@@ -2120,7 +2147,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
});
|
||||
const attachmentInputRef = useRef<HTMLInputElement>(null);
|
||||
const composerFormRef = useRef<HTMLFormElement>(null);
|
||||
const composerFooterControlsRef = useRef<HTMLDivElement>(null);
|
||||
const composerSurfaceRef = useRef<HTMLDivElement>(null);
|
||||
const providerInputRejectedRef = useRef(false);
|
||||
const composerSelectLockRef = useRef(false);
|
||||
@@ -2628,9 +2654,12 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
const providerTraitsPicker = renderProviderTraitsPicker(providerTraitsPickerInput);
|
||||
const {
|
||||
controlsRef: restingComposerControlsRef,
|
||||
attachControls: attachRestingComposerControls,
|
||||
hiddenBlockCount: restingControlsHiddenBlockCount,
|
||||
iconOnlyBlockCount: restingControlsIconOnlyBlockCount,
|
||||
controlsVisible: restingControlsVisible,
|
||||
} = useRestingComposerControlsLayout(restingControlsHost);
|
||||
const expandedControlsLayout = useRestingComposerControlsLayout(null, true);
|
||||
const pendingPrimaryAction = useMemo(
|
||||
() =>
|
||||
activePendingProgress
|
||||
@@ -3177,22 +3206,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
setIsComposerPrimaryActionsCompact(initialCompactness.primaryActionsCompact);
|
||||
setIsComposerFooterCompact(initialCompactness.footerCompact);
|
||||
if (typeof ResizeObserver === "undefined") return;
|
||||
const footerControls = composerFooterControlsRef.current;
|
||||
const stopFooterControlsFade = footerControls
|
||||
? observeResponsiveBreakpointFade({
|
||||
target: footerControls,
|
||||
container: composerForm,
|
||||
active: panelAnimationsActive,
|
||||
durationMs: panelAnimationDurationMs,
|
||||
breakpoint: {
|
||||
value: composerFooterHasWideActions
|
||||
? COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX
|
||||
: COMPOSER_FOOTER_COMPACT_BREAKPOINT_PX,
|
||||
unit: "px",
|
||||
},
|
||||
})
|
||||
: undefined;
|
||||
|
||||
const observer = new ResizeObserver(() => {
|
||||
const nextCompactness = measureFooterCompactness();
|
||||
setIsComposerPrimaryActionsCompact((previous) =>
|
||||
@@ -3208,7 +3221,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
observer.observe(composerForm);
|
||||
return () => {
|
||||
observer.disconnect();
|
||||
stopFooterControlsFade?.();
|
||||
};
|
||||
}, [
|
||||
activeThreadId,
|
||||
@@ -3216,8 +3228,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
composerFooterHasWideActions,
|
||||
isComposerApprovalState,
|
||||
isComposerCollapsedMobile,
|
||||
panelAnimationDurationMs,
|
||||
panelAnimationsActive,
|
||||
]);
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
@@ -4926,11 +4936,15 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
setIsComposerScrollCollapsed,
|
||||
]);
|
||||
|
||||
const restingHiddenBlockCount = composerControlsInStrip ? restingControlsHiddenBlockCount : 0;
|
||||
const composerControlsCompact = !composerControlsInStrip && isComposerFooterCompact;
|
||||
const restingHiddenBlockCount = composerControlsInStrip
|
||||
? restingControlsHiddenBlockCount
|
||||
: expandedControlsLayout.hiddenBlockCount;
|
||||
const iconOnlyBlockCount = composerControlsInStrip
|
||||
? restingControlsIconOnlyBlockCount
|
||||
: expandedControlsLayout.iconOnlyBlockCount;
|
||||
const restingProviderTraitsPicker = renderProviderTraitsPicker({
|
||||
...providerTraitsPickerInput,
|
||||
size: "xs",
|
||||
size: composerControlsInStrip ? "xs" : "sm",
|
||||
hidden: composerControlsHidden || restingHiddenBlockCount > 1,
|
||||
});
|
||||
const restingBlockDefs = [
|
||||
@@ -4941,7 +4955,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
content: (
|
||||
<>
|
||||
<ComposerControlSeparator size={composerControlsInStrip ? "xs" : "sm"} />
|
||||
{composerControlsInStrip ? restingProviderTraitsPicker : providerTraitsPicker}
|
||||
{restingProviderTraitsPicker}
|
||||
</>
|
||||
),
|
||||
},
|
||||
@@ -5048,7 +5062,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
triggerClassName={
|
||||
composerControlsInStrip
|
||||
? "min-w-13 shrink text-xs! @max-[640px]/composer-surface:[&_[data-chat-provider-model-picker-label]]:w-0 @max-[640px]/composer-surface:[&_[data-chat-provider-model-picker-label]]:flex-none"
|
||||
: "-ms-2.5"
|
||||
: "-ms-2.5 min-w-13"
|
||||
}
|
||||
terminalOpen={terminalOpen}
|
||||
open={isComposerModelPickerOpen}
|
||||
@@ -5075,65 +5089,52 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
onOpenProviderSetup={onOpenProviderSetup}
|
||||
/>
|
||||
|
||||
{composerControlsCompact ? (
|
||||
<CompactComposerControlsMenu
|
||||
interactionMode={interactionMode}
|
||||
runtimeMode={runtimeMode}
|
||||
showInteractionModeToggle={planModeUiEnabled}
|
||||
traitsMenuContent={providerTraitsMenuContent}
|
||||
onToggleInteractionMode={toggleInteractionMode}
|
||||
onRuntimeModeChange={handleRuntimeModeChange}
|
||||
/>
|
||||
) : (
|
||||
<>
|
||||
{restingBlockDefs.map((def, index) => {
|
||||
if (!composerControlsInStrip) {
|
||||
return <Fragment key={def.id}>{def.content}</Fragment>;
|
||||
}
|
||||
const hidden = index >= restingBlockDefs.length - restingHiddenBlockCount;
|
||||
return (
|
||||
<div
|
||||
key={def.id}
|
||||
data-resting-block={def.id}
|
||||
aria-hidden={hidden || undefined}
|
||||
inert={hidden || undefined}
|
||||
className={cn(
|
||||
"flex w-max min-w-max shrink-0 items-center gap-1",
|
||||
hidden && "pointer-events-none invisible absolute",
|
||||
)}
|
||||
>
|
||||
{def.content}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
{composerControlsInStrip ? (
|
||||
<>
|
||||
{restingBlockDefs.map((def, index) => {
|
||||
const hidden = index >= restingBlockDefs.length - restingHiddenBlockCount;
|
||||
return (
|
||||
<div
|
||||
data-resting-controls-overflow
|
||||
aria-hidden={hiddenRestingBlockIds.length === 0 || undefined}
|
||||
inert={hiddenRestingBlockIds.length === 0 || undefined}
|
||||
key={def.id}
|
||||
data-resting-block={def.id}
|
||||
data-composer-block-icon-only={
|
||||
index >= restingBlockDefs.length - iconOnlyBlockCount ? "true" : "false"
|
||||
}
|
||||
aria-hidden={hidden || undefined}
|
||||
inert={hidden || undefined}
|
||||
className={cn(
|
||||
"min-w-0 shrink-0",
|
||||
hiddenRestingBlockIds.length === 0 && "pointer-events-none invisible absolute",
|
||||
"flex w-max min-w-max shrink-0 items-center gap-1",
|
||||
hidden && "pointer-events-none invisible absolute",
|
||||
index >= restingBlockDefs.length - iconOnlyBlockCount &&
|
||||
"[&_[data-composer-control-label]]:pointer-events-none [&_[data-composer-control-label]]:invisible [&_[data-composer-control-label]]:absolute [&_[data-composer-control-label]]:w-max [&_[data-composer-control-label]]:max-w-none [&_[data-composer-control-compact-icon]]:[visibility:inherit] [&_[data-composer-control-compact-icon]]:relative",
|
||||
)}
|
||||
>
|
||||
<CompactComposerControlsMenu
|
||||
interactionMode={interactionMode}
|
||||
runtimeMode={runtimeMode}
|
||||
size="xs"
|
||||
hidden={composerControlsHidden || hiddenRestingBlockIds.length === 0}
|
||||
showInteractionModeToggle={
|
||||
planModeUiEnabled && hiddenRestingBlockIds.includes("mode")
|
||||
}
|
||||
traitsMenuContent={
|
||||
hiddenRestingBlockIds.includes("traits") ? providerTraitsMenuContent : undefined
|
||||
}
|
||||
onToggleInteractionMode={toggleInteractionMode}
|
||||
onRuntimeModeChange={handleRuntimeModeChange}
|
||||
/>
|
||||
{def.content}
|
||||
</div>
|
||||
) : null}
|
||||
</>
|
||||
)}
|
||||
);
|
||||
})}
|
||||
<div
|
||||
data-resting-controls-overflow
|
||||
aria-hidden={hiddenRestingBlockIds.length === 0 || undefined}
|
||||
inert={hiddenRestingBlockIds.length === 0 || undefined}
|
||||
className={cn(
|
||||
"min-w-0 shrink-0",
|
||||
hiddenRestingBlockIds.length === 0 && "pointer-events-none invisible absolute",
|
||||
)}
|
||||
>
|
||||
<CompactComposerControlsMenu
|
||||
interactionMode={interactionMode}
|
||||
runtimeMode={runtimeMode}
|
||||
size={composerControlsInStrip ? "xs" : "sm"}
|
||||
hidden={composerControlsHidden || hiddenRestingBlockIds.length === 0}
|
||||
showInteractionModeToggle={planModeUiEnabled && hiddenRestingBlockIds.includes("mode")}
|
||||
traitsMenuContent={
|
||||
hiddenRestingBlockIds.includes("traits") ? providerTraitsMenuContent : undefined
|
||||
}
|
||||
onToggleInteractionMode={toggleInteractionMode}
|
||||
onRuntimeModeChange={handleRuntimeModeChange}
|
||||
/>
|
||||
</div>
|
||||
</>
|
||||
</>
|
||||
);
|
||||
const showTasksTab =
|
||||
@@ -6159,7 +6160,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
{composerControlsInStrip && restingControlsHost
|
||||
? createPortal(
|
||||
<div
|
||||
ref={restingComposerControlsRef}
|
||||
ref={attachRestingComposerControls}
|
||||
data-chat-composer-resting-controls="true"
|
||||
aria-hidden={restingControlsVisible ? undefined : true}
|
||||
inert={restingControlsVisible ? undefined : true}
|
||||
@@ -6961,11 +6962,11 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
)}
|
||||
>
|
||||
<div
|
||||
ref={composerFooterControlsRef}
|
||||
ref={expandedControlsLayout.attachControls}
|
||||
data-chat-composer-controls="left"
|
||||
data-chat-composer-footer-controls="true"
|
||||
className={cn(
|
||||
"-m-1 -ms-3.5 flex min-w-0 flex-1 items-center gap-1 overflow-x-auto p-1 ps-3.5 [scrollbar-width:none] [&::-webkit-scrollbar]:hidden",
|
||||
"relative -m-1 -ms-3.5 flex min-w-0 flex-1 items-center gap-1 overflow-x-auto p-1 ps-3.5 [scrollbar-width:none] [&::-webkit-scrollbar]:hidden",
|
||||
isComposerResting && "hidden",
|
||||
)}
|
||||
>
|
||||
|
||||
@@ -17,7 +17,7 @@ import {
|
||||
} from "@t3tools/shared/model";
|
||||
import { memo, useCallback } from "react";
|
||||
import type { VariantProps } from "class-variance-authority";
|
||||
import { ZapIcon } from "lucide-react";
|
||||
import { GaugeIcon, ZapIcon } from "lucide-react";
|
||||
import { buttonVariants } from "../ui/button";
|
||||
import {
|
||||
Menu,
|
||||
@@ -32,6 +32,7 @@ import { useComposerDraftStore, DraftId } from "../../composerDraftStore";
|
||||
import { getProviderModelCapabilities } from "../../providerModels";
|
||||
import { cn } from "~/lib/utils";
|
||||
import { Badge } from "../ui/badge";
|
||||
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
|
||||
import {
|
||||
ComposerControl,
|
||||
ComposerControlChevron,
|
||||
@@ -589,6 +590,7 @@ export const TraitsPicker = memo(function TraitsPicker({
|
||||
primarySelectDescriptorId: primarySelectDescriptor?.id ?? null,
|
||||
ultrathinkPromptControlled,
|
||||
});
|
||||
const accessibleLabel = showFastModeIcon ? `${triggerLabel}, Fast mode on` : triggerLabel;
|
||||
const fastModeIcon = showFastModeIcon ? (
|
||||
<>
|
||||
<ComposerControlIcon
|
||||
@@ -616,39 +618,66 @@ export const TraitsPicker = memo(function TraitsPicker({
|
||||
setIsMenuOpen(open);
|
||||
}}
|
||||
>
|
||||
<MenuTrigger
|
||||
render={
|
||||
<ComposerControl
|
||||
data-composer-shortcut={isComposerOwned ? "composer.effort" : undefined}
|
||||
variant={triggerVariant ?? "ghost"}
|
||||
size={size}
|
||||
className={cn(
|
||||
isCodexStyle
|
||||
? "min-w-0 max-w-40 shrink justify-start overflow-hidden whitespace-nowrap sm:max-w-48"
|
||||
: "shrink-0 whitespace-nowrap",
|
||||
triggerClassName,
|
||||
)}
|
||||
/>
|
||||
}
|
||||
>
|
||||
{isCodexStyle ? (
|
||||
// The label truncates itself; clipping the wrapper too would cut off
|
||||
// the chevron, whose negative end margin overhangs the wrapper edge.
|
||||
<span
|
||||
className={cn("flex min-w-0 w-full items-center", size === "xs" ? "gap-1" : "gap-1.5")}
|
||||
>
|
||||
{fastModeIcon}
|
||||
<span className="min-w-0 truncate">{triggerLabel}</span>
|
||||
<ComposerControlChevron size={size} />
|
||||
</span>
|
||||
) : (
|
||||
<>
|
||||
{fastModeIcon}
|
||||
<span>{triggerLabel}</span>
|
||||
<ComposerControlChevron size={size} />
|
||||
</>
|
||||
)}
|
||||
</MenuTrigger>
|
||||
<Tooltip>
|
||||
<TooltipTrigger
|
||||
render={
|
||||
<MenuTrigger
|
||||
render={
|
||||
<ComposerControl
|
||||
aria-label={accessibleLabel}
|
||||
data-composer-shortcut={isComposerOwned ? "composer.effort" : undefined}
|
||||
variant={triggerVariant ?? "ghost"}
|
||||
size={size}
|
||||
className={cn(
|
||||
isCodexStyle
|
||||
? "min-w-0 max-w-40 shrink justify-start overflow-hidden whitespace-nowrap sm:max-w-48"
|
||||
: "shrink-0 whitespace-nowrap",
|
||||
triggerClassName,
|
||||
)}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
}
|
||||
>
|
||||
{isCodexStyle ? (
|
||||
// The label truncates itself; clipping the wrapper too would cut off
|
||||
// the chevron, whose negative end margin overhangs the wrapper edge.
|
||||
<span
|
||||
className={cn(
|
||||
"flex min-w-0 w-full items-center",
|
||||
size === "xs" ? "gap-1" : "gap-1.5",
|
||||
)}
|
||||
>
|
||||
{fastModeIcon ?? (
|
||||
<span
|
||||
data-composer-control-compact-icon
|
||||
className="pointer-events-none invisible absolute"
|
||||
>
|
||||
<ComposerControlIcon icon={GaugeIcon} size={size} />
|
||||
</span>
|
||||
)}
|
||||
<span data-composer-control-label className="min-w-0 truncate">
|
||||
{triggerLabel}
|
||||
</span>
|
||||
<ComposerControlChevron size={size} />
|
||||
</span>
|
||||
) : (
|
||||
<>
|
||||
{fastModeIcon ?? (
|
||||
<span
|
||||
data-composer-control-compact-icon
|
||||
className="pointer-events-none invisible absolute"
|
||||
>
|
||||
<ComposerControlIcon icon={GaugeIcon} size={size} />
|
||||
</span>
|
||||
)}
|
||||
<span data-composer-control-label>{triggerLabel}</span>
|
||||
<ComposerControlChevron size={size} />
|
||||
</>
|
||||
)}
|
||||
</TooltipTrigger>
|
||||
<TooltipPopup side="top">{accessibleLabel}</TooltipPopup>
|
||||
</Tooltip>
|
||||
<MenuPopup align="start" {...(isComposerOwned ? composerFloatingLayerProps : {})}>
|
||||
<TraitsMenuContent
|
||||
provider={provider}
|
||||
|
||||
@@ -20,7 +20,13 @@ function measurePicker(input: { clientWidth: number; flexGrow: string; maxWidth?
|
||||
}
|
||||
return null;
|
||||
},
|
||||
querySelectorAll: () => [{ getBoundingClientRect: () => ({ width: 140 }) }],
|
||||
querySelectorAll: () => [
|
||||
{
|
||||
dataset: {},
|
||||
querySelectorAll: () => [],
|
||||
getBoundingClientRect: () => ({ width: 140 }),
|
||||
},
|
||||
],
|
||||
};
|
||||
vi.stubGlobal("getComputedStyle", (element: unknown) => {
|
||||
if (element === label) return { flexGrow: input.flexGrow };
|
||||
@@ -40,6 +46,7 @@ describe("measureRestingComposerControls", () => {
|
||||
expect(resolveRestingComposerControlsNaturalWidth(measurement)).toBe(196);
|
||||
expect(resolveRestingComposerControlsLayout({ ...measurement, hostWidth: 200 })).toEqual({
|
||||
hiddenCount: 0,
|
||||
iconOnlyCount: 0,
|
||||
visible: true,
|
||||
});
|
||||
});
|
||||
@@ -50,6 +57,7 @@ describe("measureRestingComposerControls", () => {
|
||||
expect(measurement.naturalFixedWidth).toBe(192);
|
||||
expect(resolveRestingComposerControlsLayout({ ...measurement, hostWidth: 200 })).toEqual({
|
||||
hiddenCount: 1,
|
||||
iconOnlyCount: 1,
|
||||
visible: true,
|
||||
});
|
||||
});
|
||||
@@ -60,6 +68,7 @@ describe("measureRestingComposerControls", () => {
|
||||
expect(measurement.naturalFixedWidth).toBe(212);
|
||||
expect(resolveRestingComposerControlsLayout({ ...measurement, hostWidth: 200 })).toEqual({
|
||||
hiddenCount: 1,
|
||||
iconOnlyCount: 1,
|
||||
visible: true,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -42,6 +42,33 @@ function providerModelPickerMinimumWidth(picker: HTMLElement): number {
|
||||
return minWidth + elementInlineMarginWidth(picker);
|
||||
}
|
||||
|
||||
function controlBlockWidths(block: HTMLElement): { natural: number; iconOnly: number } {
|
||||
const compact = block.dataset.composerBlockIconOnly === "true";
|
||||
let natural = elementOuterWidth(block);
|
||||
let iconOnly = natural;
|
||||
for (const label of block.querySelectorAll<HTMLElement>("[data-composer-control-label]")) {
|
||||
// Labels remain mounted at natural width when icons replace them. Reading
|
||||
// both variants from one tree avoids duplicate controls or write/read probes.
|
||||
const labelStyle = getComputedStyle(label);
|
||||
const inFlow = labelStyle.position !== "absolute";
|
||||
// Phone widths already hide the build label with sr-only. Its clipping
|
||||
// remains in effect even when compact styles replace its one-pixel width.
|
||||
if (!inFlow && (!compact || labelStyle.clip !== "auto")) continue;
|
||||
const labelWidth = label.scrollWidth;
|
||||
const renderedWidth = inFlow ? label.getBoundingClientRect().width : 0;
|
||||
const gap = Number.parseFloat(getComputedStyle(label.parentElement!).columnGap) || 0;
|
||||
natural += labelWidth - renderedWidth + (inFlow ? 0 : gap);
|
||||
iconOnly -= renderedWidth + (inFlow ? gap : 0);
|
||||
}
|
||||
for (const icon of block.querySelectorAll<HTMLElement>("[data-composer-control-compact-icon]")) {
|
||||
const width = elementOuterWidth(icon);
|
||||
const gap = Number.parseFloat(getComputedStyle(icon.parentElement!).columnGap) || 0;
|
||||
natural -= compact ? width + gap : 0;
|
||||
iconOnly += compact ? 0 : width + gap;
|
||||
}
|
||||
return { natural, iconOnly: Math.min(natural, iconOnly) };
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the natural widths of the resting composer controls from the DOM.
|
||||
*
|
||||
@@ -67,6 +94,7 @@ export function measureRestingComposerControls(
|
||||
const overflow = controls.querySelector<HTMLElement>("[data-resting-controls-overflow]");
|
||||
const separatorAndGapWidth = separatorWidth > 0 ? separatorWidth + gap : 0;
|
||||
const blocks = Array.from(controls.querySelectorAll<HTMLElement>("[data-resting-block]"));
|
||||
const widths = blocks.map(controlBlockWidths);
|
||||
return {
|
||||
gap,
|
||||
naturalFixedWidth:
|
||||
@@ -75,7 +103,8 @@ export function measureRestingComposerControls(
|
||||
minimumFixedWidth:
|
||||
(picker ? providerModelPickerMinimumWidth(picker) : elementOuterWidth(leadingControl)) +
|
||||
separatorAndGapWidth,
|
||||
blockWidths: blocks.map(elementOuterWidth),
|
||||
blockWidths: widths.map((width) => width.natural),
|
||||
iconOnlyBlockWidths: widths.map((width) => width.iconOnly),
|
||||
overflowWidth: overflow ? elementOuterWidth(overflow) : 0,
|
||||
};
|
||||
}
|
||||
@@ -449,3 +449,79 @@ describe("resolveScrollToEndClearance", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("progressive composer controls", () => {
|
||||
const measurement = {
|
||||
gap: 4,
|
||||
naturalFixedWidth: 140,
|
||||
minimumFixedWidth: 80,
|
||||
blockWidths: [80, 140],
|
||||
iconOnlyBlockWidths: [40, 60],
|
||||
overflowWidth: 24,
|
||||
};
|
||||
|
||||
it("keeps labels while they fit and removes trailing labels before controls", () => {
|
||||
for (const [hostWidth, iconOnlyCount, hiddenCount] of [
|
||||
[368, 0, 0],
|
||||
[367, 1, 0],
|
||||
[288, 1, 0],
|
||||
[287, 2, 0],
|
||||
[248, 2, 0],
|
||||
[247, 2, 1],
|
||||
[211, 2, 2],
|
||||
] as const) {
|
||||
expect(resolveRestingComposerControlsLayout({ ...measurement, hostWidth })).toEqual({
|
||||
hiddenCount,
|
||||
iconOnlyCount,
|
||||
visible: true,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it("requires slack to restore labels and controls", () => {
|
||||
for (const [hostWidth, previous, promoted] of [
|
||||
[
|
||||
368,
|
||||
{ hiddenCount: 0, iconOnlyCount: 1, visible: true },
|
||||
{ hiddenCount: 0, iconOnlyCount: 0, visible: true },
|
||||
],
|
||||
[
|
||||
288,
|
||||
{ hiddenCount: 0, iconOnlyCount: 2, visible: true },
|
||||
{ hiddenCount: 0, iconOnlyCount: 1, visible: true },
|
||||
],
|
||||
[
|
||||
248,
|
||||
{ hiddenCount: 1, iconOnlyCount: 2, visible: true },
|
||||
{ hiddenCount: 0, iconOnlyCount: 2, visible: true },
|
||||
],
|
||||
] as const) {
|
||||
expect(resolveRestingComposerControlsLayout({ ...measurement, hostWidth, previous })).toEqual(
|
||||
previous,
|
||||
);
|
||||
expect(
|
||||
resolveRestingComposerControlsLayout({
|
||||
...measurement,
|
||||
hostWidth: hostWidth + 1,
|
||||
previous,
|
||||
}),
|
||||
).toEqual(promoted);
|
||||
}
|
||||
});
|
||||
|
||||
it("settles through fractional label-width changes at each threshold", () => {
|
||||
for (const hostWidth of [368, 288, 248]) {
|
||||
let previous = resolveRestingComposerControlsLayout({ ...measurement, hostWidth });
|
||||
for (let index = 0; index < 10; index += 1) {
|
||||
const next = resolveRestingComposerControlsLayout({
|
||||
...measurement,
|
||||
hostWidth,
|
||||
previous,
|
||||
naturalFixedWidth: 140 + (index % 2) * 0.5,
|
||||
});
|
||||
if (index > 1) expect(next).toEqual(previous);
|
||||
previous = next;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -113,18 +113,29 @@ export interface RestingComposerControlsMeasurement {
|
||||
minimumFixedWidth: number;
|
||||
blockWidths: readonly number[];
|
||||
overflowWidth: number;
|
||||
iconOnlyBlockWidths?: readonly number[];
|
||||
}
|
||||
|
||||
function restingComposerControlsWidth(
|
||||
input: RestingComposerControlsMeasurement,
|
||||
hiddenCount: number,
|
||||
fixedWidth = input.naturalFixedWidth,
|
||||
iconOnlyCount = 0,
|
||||
): number {
|
||||
const { blockWidths, gap } = input;
|
||||
const visibleCount = blockWidths.length - hiddenCount;
|
||||
return (
|
||||
fixedWidth +
|
||||
blockWidths.slice(0, visibleCount).reduce((sum, width) => sum + width, 0) +
|
||||
blockWidths
|
||||
.slice(0, visibleCount)
|
||||
.reduce(
|
||||
(sum, width, index) =>
|
||||
sum +
|
||||
(index >= blockWidths.length - iconOnlyCount
|
||||
? (input.iconOnlyBlockWidths?.[index] ?? width)
|
||||
: width),
|
||||
0,
|
||||
) +
|
||||
(hiddenCount > 0 ? input.overflowWidth : 0) +
|
||||
gap * (visibleCount + (hiddenCount > 0 ? 1 : 0))
|
||||
);
|
||||
@@ -145,10 +156,11 @@ export function resolveRestingComposerControlsNaturalWidth(
|
||||
}
|
||||
|
||||
/**
|
||||
* Decide how many trailing resting control blocks move into the overflow
|
||||
* menu, and whether the cluster can show at all, from natural widths.
|
||||
* Fit footer controls using natural widths: remove trailing labels first,
|
||||
* then move trailing blocks into overflow. Resting and expanded share this
|
||||
* decision, including the slack needed to safely restore controls.
|
||||
*
|
||||
* Trailing blocks hide before the model picker shrinks. Once they are all in
|
||||
* Trailing blocks compact before the model picker shrinks. Once they are all in
|
||||
* the overflow menu, the picker may contract to its minimum readable width;
|
||||
* below that the whole cluster hides rather than clipping.
|
||||
*/
|
||||
@@ -157,39 +169,41 @@ const RESTING_CONTROLS_SLACK_PX = 1;
|
||||
export function resolveRestingComposerControlsLayout(
|
||||
input: RestingComposerControlsMeasurement & {
|
||||
hostWidth: number;
|
||||
previous?: { hiddenCount: number; visible: boolean };
|
||||
previous?: { hiddenCount: number; iconOnlyCount?: number; visible: boolean };
|
||||
},
|
||||
): { hiddenCount: number; visible: boolean } {
|
||||
): { hiddenCount: number; iconOnlyCount?: number; visible: boolean } {
|
||||
const { blockWidths, hostWidth, previous } = input;
|
||||
let hiddenCount = 0;
|
||||
const iconSteps = input.iconOnlyBlockWidths ? blockWidths.length : 0;
|
||||
const previousStep = previous
|
||||
? previous.hiddenCount > 0
|
||||
? iconSteps + Math.min(previous.hiddenCount, blockWidths.length)
|
||||
: Math.min(previous.iconOnlyCount ?? 0, iconSteps)
|
||||
: 0;
|
||||
let step = 0;
|
||||
const widthAtStep = (candidate: number, fixedWidth = input.naturalFixedWidth) =>
|
||||
restingComposerControlsWidth(
|
||||
input,
|
||||
Math.max(0, candidate - iconSteps),
|
||||
fixedWidth,
|
||||
Math.min(candidate, iconSteps),
|
||||
);
|
||||
// Promotions need a pixel of slack: recovering a flexible picker's natural
|
||||
// width can jitter by a fraction of a pixel across renders. Demotions are
|
||||
// immediate so a threshold cannot clip or flip React between layouts.
|
||||
while (
|
||||
hiddenCount < blockWidths.length &&
|
||||
restingComposerControlsWidth(input, hiddenCount) > hostWidth
|
||||
step < iconSteps + blockWidths.length &&
|
||||
widthAtStep(step) > hostWidth - (step < previousStep ? RESTING_CONTROLS_SLACK_PX : 0)
|
||||
) {
|
||||
hiddenCount += 1;
|
||||
step += 1;
|
||||
}
|
||||
// Growing the overflow menu is unconditional, or the controls would clip.
|
||||
// Shrinking it has to earn a pixel of slack first: the picker is flexible,
|
||||
// so its natural width is recovered from a truncated label whose
|
||||
// scrollWidth is integral while the rendered box is fractional. The
|
||||
// composer re-measures on every render, so without that margin a host
|
||||
// sitting exactly on a threshold flips a block in and out until React
|
||||
// gives up with "Maximum update depth exceeded".
|
||||
if (previous) {
|
||||
const previousHiddenCount = Math.min(previous.hiddenCount, blockWidths.length);
|
||||
while (
|
||||
hiddenCount < previousHiddenCount &&
|
||||
restingComposerControlsWidth(input, hiddenCount) > hostWidth - RESTING_CONTROLS_SLACK_PX
|
||||
) {
|
||||
hiddenCount += 1;
|
||||
}
|
||||
}
|
||||
const minimumWidth = restingComposerControlsWidth(input, hiddenCount, input.minimumFixedWidth);
|
||||
const hiddenCount = Math.max(0, step - iconSteps);
|
||||
const iconOnlyCount = Math.min(step, iconSteps);
|
||||
const minimumWidth = widthAtStep(step, input.minimumFixedWidth);
|
||||
const visible =
|
||||
previous && !previous.visible
|
||||
? minimumWidth <= hostWidth - RESTING_CONTROLS_SLACK_PX
|
||||
: minimumWidth <= hostWidth;
|
||||
return { hiddenCount, visible };
|
||||
return { hiddenCount, ...(input.iconOnlyBlockWidths ? { iconOnlyCount } : {}), visible };
|
||||
}
|
||||
|
||||
export function resolveScrollToEndClearance(input: {
|
||||
|
||||
Reference in new issue
Block a user