Skip to content

Commit d36d422

Browse files
committed
refactor(web): the context strip and composer share one free-space read
The context strip and the composer's resting controls decide what fits in the same row, and the strip only stops stealing room once both subtract inline padding the same way. Put that read in `contentInlineWidth`, use it at both sites, and test that it drops both logical padding sides.
1 parent 9c0e3ad commit d36d422

4 files changed

Lines changed: 57 additions & 12 deletions

File tree

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import { useComposerMenuProps } from "./chat/composerEventScope";
6969
import { measureRestingComposerControls } from "./chat/restingComposerControlsMeasurement";
7070
import { resolveRestingComposerControlsNaturalWidth } from "./composerFooterLayout";
7171
import { cn } from "~/lib/utils";
72+
import { contentInlineWidth } from "~/lib/contentInlineWidth";
7273
import { observeResize } from "~/lib/observeResize";
7374

7475
export interface BranchToolbarHandle {
@@ -452,14 +453,10 @@ function useLabelsOverflow(element: HTMLDivElement | null): boolean {
452453
const { element: current, overflows: compact } = stateRef.current;
453454
if (!current) return;
454455
if (current.clientWidth === 0) return;
455-
// The strip's own padding is not room for its children. Counting it let
456-
// labels expand while the composer controls hosted here had already
457-
// collapsed to icons for lack of space.
456+
// Same free-space read the composer applies to the controls host, so the
457+
// labels never expand into room the docked controls already gave up.
458458
const stripStyle = getComputedStyle(current);
459-
const available =
460-
current.clientWidth -
461-
(Number.parseFloat(stripStyle.paddingInlineStart) || 0) -
462-
(Number.parseFloat(stripStyle.paddingInlineEnd) || 0);
459+
const available = contentInlineWidth(current, stripStyle);
463460
// flex-1 stretches the groups to fill the strip, so their own boxes always
464461
// measure "full". Sum the laid-out content instead, skipping hidden form
465462
// artifacts and other out-of-flow nodes.

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ import {
205205
shouldUseRestingComposerLayout,
206206
} from "../composerFooterLayout";
207207
import { measureRestingComposerControls } from "./restingComposerControlsMeasurement";
208+
import { contentInlineWidth } from "~/lib/contentInlineWidth";
208209
import {
209210
type ComposerCitationCommentRequest,
210211
type ComposerPromptEditorHandle,
@@ -1183,11 +1184,7 @@ function useRestingComposerControlsLayout(host: HTMLDivElement | null, useContro
11831184

11841185
const measurement = measureRestingComposerControls(controls);
11851186
if (!measurement) return;
1186-
const style = getComputedStyle(currentHost);
1187-
const hostWidth =
1188-
currentHost.clientWidth -
1189-
(Number.parseFloat(style.paddingInlineStart) || 0) -
1190-
(Number.parseFloat(style.paddingInlineEnd) || 0);
1187+
const hostWidth = contentInlineWidth(currentHost);
11911188

11921189
setLayout((current) => {
11931190
const next = resolveRestingComposerControlsLayout({
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
2+
3+
import { contentInlineWidth } from "./contentInlineWidth";
4+
5+
function box(clientWidth: number, style: Record<string, string>) {
6+
vi.stubGlobal("getComputedStyle", () => style);
7+
return contentInlineWidth({ clientWidth } as unknown as HTMLElement);
8+
}
9+
10+
afterEach(() => vi.unstubAllGlobals());
11+
12+
describe("contentInlineWidth", () => {
13+
it("drops both inline sides of the padding clientWidth counts as room", () => {
14+
// Unequal sides: reading only one of them, or the physical props, answers
15+
// for less than the full padding.
16+
expect(box(724, { paddingInlineStart: "4px", paddingInlineEnd: "8px" })).toBe(712);
17+
});
18+
19+
it("treats an unresolvable padding as none instead of answering NaN", () => {
20+
// Computed padding is px in browsers, but a missing or keyword value must
21+
// not poison a fit decision into "nothing fits".
22+
expect(box(100, { paddingInlineStart: "auto", paddingInlineEnd: "8px" })).toBe(92);
23+
expect(box(100, {})).toBe(100);
24+
});
25+
26+
it("goes negative rather than clipping a box whose padding exceeds it", () => {
27+
// Callers compare against a needed width, so a squeezed box has to read as
28+
// short of room.
29+
expect(box(10, { paddingInlineStart: "8px", paddingInlineEnd: "8px" })).toBe(-6);
30+
});
31+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* The inline room a box leaves its children.
3+
*
4+
* `clientWidth` is the padding box, so it is not room children can occupy: two
5+
* fit decisions that must agree at one container drift apart by its inline
6+
* padding the moment one of them reads `clientWidth` directly. Logical props
7+
* keep that true whichever way the text runs.
8+
*
9+
* `style` lets a caller that already resolved the box's computed style reuse it:
10+
* these reads land in measurement paths that run on every render.
11+
*/
12+
export function contentInlineWidth(
13+
element: HTMLElement,
14+
style: CSSStyleDeclaration = getComputedStyle(element),
15+
): number {
16+
const padding =
17+
(Number.parseFloat(style.paddingInlineStart) || 0) +
18+
(Number.parseFloat(style.paddingInlineEnd) || 0);
19+
return element.clientWidth - padding;
20+
}

0 commit comments

Comments
 (0)