Skip to content
TypeScript

fix(web): settle the resting composer layout with a pixel of slack (#9482)

Matheus Timbó Pereira0ba06a122bc7
diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx
index 5baf34c4ae3..d5bf8524da3 100644
--- a/apps/web/src/components/chat/ChatComposer.tsx
+++ b/apps/web/src/components/chat/ChatComposer.tsx
@@ -877,7 +877,11 @@ function useRestingComposerControlsLayout(host: HTMLDivElement | null) {
const hostWidth = currentHost.clientWidth;
setLayout((current) => {
- const next = resolveRestingComposerControlsLayout({ ...measurement, hostWidth });
+ const next = resolveRestingComposerControlsLayout({
+ ...measurement,
+ hostWidth,
+ previous: current,
+ });
return next.hiddenCount === current.hiddenCount && next.visible === current.visible
? current
: next;
diff --git a/apps/web/src/components/composerFooterLayout.test.ts b/apps/web/src/components/composerFooterLayout.test.ts
index ea2a1de2064..89de512e2ea 100644
--- a/apps/web/src/components/composerFooterLayout.test.ts
+++ b/apps/web/src/components/composerFooterLayout.test.ts
@@ -300,3 +300,82 @@ describe("context strip labels and resting composer controls", () => {
).toEqual({ hiddenCount: 2, visible: true });
});
});
+
+describe("resolveRestingComposerControlsLayout hysteresis", () => {
+ // Same cluster as above: picker 140 natural / 96 minimum plus a 9px
+ // separator, traits 60, mode 140, overflow 24, gap 4.
+ const base = {
+ gap: 4,
+ naturalFixedWidth: 149,
+ minimumFixedWidth: 105,
+ blockWidths: [60, 140],
+ overflowWidth: 24,
+ };
+
+ it("keeps a block in overflow when re-showing it would leave no slack", () => {
+ // 149 + 60 + 140 + 4 * 2 = 357 fills the host exactly.
+ expect(
+ resolveRestingComposerControlsLayout({
+ ...base,
+ hostWidth: 357,
+ previous: { hiddenCount: 1, visible: true },
+ }),
+ ).toEqual({ hiddenCount: 1, visible: true });
+ });
+
+ it("re-shows a block once the host clears the slack margin", () => {
+ expect(
+ resolveRestingComposerControlsLayout({
+ ...base,
+ hostWidth: 358,
+ previous: { hiddenCount: 1, visible: true },
+ }),
+ ).toEqual({ hiddenCount: 0, visible: true });
+ });
+
+ it("still resolves from scratch when there is no previous layout", () => {
+ expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 357 })).toEqual({
+ hiddenCount: 0,
+ visible: true,
+ });
+ });
+
+ it("settles when the measured picker width jitters below a pixel", () => {
+ // Regression guard for React error #185 ("Maximum update depth
+ // exceeded"). The composer re-measures on every render, so the picker's
+ // recovered natural width can land a fraction of a pixel apart between
+ // renders. Without hysteresis that flips hiddenCount forever.
+ let layout = { hiddenCount: 0, visible: true };
+ const settled: string[] = [];
+ for (let index = 0; index < 10; index += 1) {
+ layout = resolveRestingComposerControlsLayout({
+ ...base,
+ // The picker's recovered natural width lands a half pixel apart
+ // between renders.
+ naturalFixedWidth: index % 2 === 0 ? 149 : 149.5,
+ hostWidth: 357,
+ previous: layout,
+ });
+ if (index >= 2) settled.push(`${layout.hiddenCount}:${layout.visible}`);
+ }
+ expect(new Set(settled).size).toBe(1);
+ });
+
+ it("keeps the cluster hidden until its minimum width clears the slack", () => {
+ // 105 + 24 + 4 = 133 is the exact minimum for the hidden cluster.
+ expect(
+ resolveRestingComposerControlsLayout({
+ ...base,
+ hostWidth: 133,
+ previous: { hiddenCount: 2, visible: false },
+ }),
+ ).toEqual({ hiddenCount: 2, visible: false });
+ expect(
+ resolveRestingComposerControlsLayout({
+ ...base,
+ hostWidth: 134,
+ previous: { hiddenCount: 2, visible: false },
+ }),
+ ).toEqual({ hiddenCount: 2, visible: true });
+ });
+});
diff --git a/apps/web/src/components/composerFooterLayout.ts b/apps/web/src/components/composerFooterLayout.ts
index 56a9d43de99..e4435c37c0b 100644
--- a/apps/web/src/components/composerFooterLayout.ts
+++ b/apps/web/src/components/composerFooterLayout.ts
@@ -111,10 +111,15 @@ export function resolveRestingComposerControlsNaturalWidth(
* the overflow menu, the picker may contract to its minimum readable width;
* below that the whole cluster hides rather than clipping.
*/
+const RESTING_CONTROLS_SLACK_PX = 1;
+
export function resolveRestingComposerControlsLayout(
- input: RestingComposerControlsMeasurement & { hostWidth: number },
+ input: RestingComposerControlsMeasurement & {
+ hostWidth: number;
+ previous?: { hiddenCount: number; visible: boolean };
+ },
): { hiddenCount: number; visible: boolean } {
- const { blockWidths, hostWidth } = input;
+ const { blockWidths, hostWidth, previous } = input;
let hiddenCount = 0;
while (
hiddenCount < blockWidths.length &&
@@ -122,7 +127,22 @@ export function resolveRestingComposerControlsLayout(
) {
hiddenCount += 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 && hiddenCount < previous.hiddenCount) {
+ if (restingComposerControlsWidth(input, hiddenCount) > hostWidth - RESTING_CONTROLS_SLACK_PX) {
+ hiddenCount = Math.min(previous.hiddenCount, blockWidths.length);
+ }
+ }
+ const minimumWidth = restingComposerControlsWidth(input, hiddenCount, input.minimumFixedWidth);
const visible =
- restingComposerControlsWidth(input, hiddenCount, input.minimumFixedWidth) <= hostWidth;
+ previous && !previous.visible
+ ? minimumWidth <= hostWidth - RESTING_CONTROLS_SLACK_PX
+ : minimumWidth <= hostWidth;
return { hiddenCount, visible };
}