fix(home): composer settles smoothly when the empty-home notice goes away on send (BUG-2356)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DbBXp2Vp62Ur3C7yRWQdSq
This commit is contained in:
+2
-2
@@ -32637,8 +32637,8 @@
|
||||
- `globals.css`:`.chat-ai-notice` 外边距 原值 `0 auto var(--space-3)`(下 12px)→ 新值 `var(--space-2) auto 0`(上 8px);手机 原值 `margin-bottom: 8px` → 新值 `margin-top: 6px`。字号、颜色、居中、换行不变。BUG-2323 的「键盘弹起时说明隐藏」规则不变,说明在输入框下面,键盘弹起时输入框本身贴在键盘上方,说明不会压在键盘上。
|
||||
- 没有新增或改动给用户看的文字。
|
||||
- 位置(390×844,组件静态标记加本分支编译的产品样式表,非真实页面):开场问题 553–747,输入框 755–811,说明 817–836(底部留 8px);模拟键盘弹起:说明 `display: none`,输入框 384–472 贴在 480 的可见区底部。桌面 1280:输入框 454–518,说明 526–544,居中。
|
||||
- 已知:手机上从「点开场问题」开始对话(键盘收起)时,说明不再渲染,输入框会下移约 25px(说明一行 19px + 上边距 6px);从输入框打字开始时键盘弹起、说明已隐藏,输入框不动。要做到点开场问题也完全不动,只能在对话中给输入框下面留同样高的空白,这与 BUG-2309(手机输入区太高)相反,本条没有这样做,请负责人决定。
|
||||
- 验证:新增 `frontend/tests/notice-below-composer-2356.test.ts`(说明是输入框块里最后一项、只渲染一次、只在空首页;CSS 上边距、居中浅色、无下边距;键盘弹起时隐藏);`ai-generated-notice-20261014`、`onboarding-three-screens-2144` 的位置断言按原值 / 新值 / 原因改写。tsc、ESLint(改动文件 0 错误)、读改动文件的前端测试和动效、守护测试逐个跑过;快检。截图:`/workspace/eval-read-1116/chat-ux-prototype/build/notice-2356/`。
|
||||
- 对话开始时(负责人 2026-11-26 选方案 1):手机上从空首页发出第一问时,说明不再渲染,输入框要下移约 25px(说明一行 19px + 上边距 6px)。新增 `frontend/src/lib/composer-settle.ts`:页面每次提交布局时记下说明的高度;发送那一帧(空首页 → 对话、当前对话就是待回答的那条)给 `.composer` 加 `is-settling`,从上方 25px 用 `transform` 平滑落到新位置(`--motion-standard` / `--ease-standard`,只动 transform,不动高度),动画结束即移除,之后的帧不再触发。键盘弹起时(BUG-2323 已隐藏说明、输入框贴键盘)、桌面、减少动态效果、打开别的对话时都不播放,后者直接到位。
|
||||
- 验证:新增 `frontend/tests/composer-settle-2356.test.ts`(发送那一帧只播一次、之后的帧不再加;键盘弹起、桌面、减少动态效果、非发送都不播;动画只用 transform 和 1.2 动效变量)和 `frontend/tests/notice-below-composer-2356.test.ts`(说明是输入框块里最后一项、只渲染一次、只在空首页;CSS 上边距、居中浅色、无下边距;键盘弹起时隐藏);`ai-generated-notice-20261014`、`onboarding-three-screens-2144` 的位置断言按原值 / 新值 / 原因改写。tsc、ESLint(改动文件 0 错误)、读改动文件的前端测试和动效、守护测试逐个跑过;快检。截图:`/workspace/eval-read-1116/chat-ux-prototype/build/notice-2356/`。
|
||||
- 防复发:位置由上述测试锁住;把说明移回输入框上面或放进对话中会让测试失败。
|
||||
- 关联记录:BUG-2309(说明只在空首页)、BUG-2323(键盘与输入框贴底)、BUG-1347(说明句来源)。
|
||||
- 复发自:无。
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import { prefetchBillingPanelWhenIdle } from "@/lib/billing-panel-loader";
|
||||
import type { BirthHistorySubject, ConsultationBirthHistory } from "@/lib/consultation-birth-history";
|
||||
import { useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
||||
import { useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
||||
import type { FormEvent, KeyboardEvent } from "react";
|
||||
import { useAccountDialogs } from "@/components/account-dialog-provider";
|
||||
import { navigateInApp } from "@/lib/app-navigation";
|
||||
@@ -26,6 +26,7 @@ import { homeChartShown, readStoredChatChartOpen } from "@/lib/chat-chart-panel-
|
||||
import { preloadChatChartPanel } from "@/lib/chat-chart-preload";
|
||||
import { OnboardingRedeemPaywall } from "@/components/onboarding-redeem-paywall";
|
||||
import { ChatComposer } from "@/components/chat-composer";
|
||||
import { createComposerSettle } from "@/lib/composer-settle";
|
||||
import { JumpToLatestButton } from "@/components/jump-to-latest-button";
|
||||
import { SidebarTrigger } from "@/components/ui/sidebar";
|
||||
import { useHomeShellRegistration } from "@/hooks/use-home-shell-registration";
|
||||
@@ -266,6 +267,7 @@ export default function Home() {
|
||||
const onboardingPaywallShown = useRef(false);
|
||||
const starterGreetingUnseen = useRef(true);
|
||||
const composerInput = useRef<HTMLTextAreaElement>(null);
|
||||
const [composerSettle] = useState(createComposerSettle);
|
||||
const consultationRecoveryWakeup = useRef<() => void>(() => undefined);
|
||||
const consultationRecoveryCheck = useRef<() => void>(() => undefined);
|
||||
const consultationReplay = useRef<() => void>(() => undefined);
|
||||
@@ -562,6 +564,11 @@ export default function Home() {
|
||||
const starterHomeVisible = profileComplete
|
||||
&& Boolean(activeSession?.messagesHydrated)
|
||||
&& !activeSession?.messages.length;
|
||||
// BUG-2356: on a phone the AI notice under the composer goes away when the first question is sent; the composer
|
||||
// settles the notice's height down with one transform instead of jumping (once, keyboard down, motion allowed).
|
||||
useLayoutEffect(() => {
|
||||
composerSettle({ starterHome: starterHomeVisible, sending: isLoading, input: composerInput.current, win: window, root: document.documentElement });
|
||||
});
|
||||
const blankHomeSession = !activeSession || (
|
||||
activeSession.sessionType === "consultation"
|
||||
&& activeSession.messagesHydrated
|
||||
|
||||
@@ -3600,6 +3600,13 @@ input:not([type="radio"]):not([type="checkbox"]):not([class^="ant-"]):not([class
|
||||
html[data-chat-keyboard="open"] .composer-wrap-starter .chat-ai-notice { display: none; }
|
||||
}
|
||||
|
||||
/* BUG-2356 (owner 2026-11-26, option 1): when the first question is sent on a phone, the AI notice under the composer
|
||||
is no longer rendered and the composer drops by its height. lib/composer-settle.ts starts the composer that far up
|
||||
(--composer-settle-from) and this settles it with a transform only; reduced motion collapses the token to 0. */
|
||||
@keyframes composer-settle { from { transform: translateY(var(--composer-settle-from, 0px)); } to { transform: none; } }
|
||||
.composer.is-settling { animation: composer-settle var(--motion-standard) var(--ease-standard) both; }
|
||||
@media (prefers-reduced-motion: reduce) { .composer.is-settling { animation: none; } }
|
||||
|
||||
/* First run (BUG-2144): three screens, one centred card each, the question on
|
||||
top in the display font. A new screen fades in; the global reduced-motion
|
||||
rule cuts the fade to nothing. */
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* BUG-2356 (owner 2026-11-26, option 1): on a phone the empty home shows the AI notice under the composer. When the
|
||||
* first question is sent the notice is no longer rendered, so the composer drops by the notice's height (about 25px).
|
||||
* Instead of jumping, the composer starts that far up and settles to its new place with one transform animation
|
||||
* (--motion-standard / --ease-standard, see `.composer.is-settling` in globals.css).
|
||||
*
|
||||
* - Only on the empty home -> conversation switch of a send (the active session is the pending one), once.
|
||||
* - Only on a phone (<= 640px). Desktop moves the whole composer block anyway (centred home -> bottom).
|
||||
* - Not while the phone keyboard is up (BUG-2323 pins the composer to the visual viewport and hides the notice, so
|
||||
* nothing moves), and not under reduced motion (the move is instant).
|
||||
*/
|
||||
export const COMPOSER_SETTLE_CLASS = "is-settling";
|
||||
export const COMPOSER_SETTLE_VAR = "--composer-settle-from";
|
||||
|
||||
type SettleWindow = Pick<Window, "getComputedStyle" | "matchMedia">;
|
||||
|
||||
export type ComposerSettleFrame = {
|
||||
/** The empty home is on screen (the AI notice is rendered). */
|
||||
starterHome: boolean;
|
||||
/** A question was just sent from this conversation (the active session is the pending one). */
|
||||
sending: boolean;
|
||||
/** The composer's text field; its `.composer-wrap` holds `.composer` and, on the empty home, `.chat-ai-notice`. */
|
||||
input: HTMLElement | null;
|
||||
win: SettleWindow;
|
||||
/** `<html>`, for BUG-2323's `data-chat-keyboard`. */
|
||||
root: HTMLElement;
|
||||
};
|
||||
|
||||
/** Height the notice takes under the composer (its box plus its top margin), 0 when it is not shown. */
|
||||
export function noticeBlockHeight(notice: HTMLElement | null, win: SettleWindow): number {
|
||||
if (!notice) return 0;
|
||||
const style = win.getComputedStyle(notice);
|
||||
if (style.display === "none") return 0;
|
||||
const height = notice.getBoundingClientRect().height + (Number.parseFloat(style.marginTop) || 0);
|
||||
return Number.isFinite(height) && height > 0 ? Math.round(height) : 0;
|
||||
}
|
||||
|
||||
/** Plays the settle on the composer: starts `offset` px higher and eases to its place. Removes itself when done. */
|
||||
export function playComposerSettle(composer: HTMLElement, offset: number) {
|
||||
composer.style.setProperty(COMPOSER_SETTLE_VAR, `${-offset}px`);
|
||||
composer.classList.add(COMPOSER_SETTLE_CLASS);
|
||||
const done = () => {
|
||||
composer.classList.remove(COMPOSER_SETTLE_CLASS);
|
||||
composer.style.removeProperty(COMPOSER_SETTLE_VAR);
|
||||
composer.removeEventListener("animationend", done);
|
||||
composer.removeEventListener("animationcancel", done);
|
||||
};
|
||||
composer.addEventListener("animationend", done);
|
||||
composer.addEventListener("animationcancel", done);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call on every layout commit of the home page. While the empty home is shown it remembers the notice's height;
|
||||
* on the commit where a send replaces the empty home it plays the settle once. Returns the offset it played (0 = none).
|
||||
*/
|
||||
export function createComposerSettle() {
|
||||
let lastNoticeHeight = 0;
|
||||
let wasStarterHome = false;
|
||||
return function onCommit(frame: ComposerSettleFrame): number {
|
||||
const { starterHome, sending, input, win, root } = frame;
|
||||
const wrap = input?.closest<HTMLElement>(".composer-wrap") ?? null;
|
||||
if (starterHome) {
|
||||
wasStarterHome = true;
|
||||
lastNoticeHeight = noticeBlockHeight(wrap?.querySelector<HTMLElement>(".chat-ai-notice") ?? null, win);
|
||||
return 0;
|
||||
}
|
||||
if (!wasStarterHome) return 0;
|
||||
wasStarterHome = false;
|
||||
const offset = lastNoticeHeight;
|
||||
lastNoticeHeight = 0;
|
||||
if (!sending || offset <= 0 || !wrap) return 0;
|
||||
if (root.dataset.chatKeyboard === "open") return 0;
|
||||
if (!win.matchMedia("(max-width: 640px)").matches) return 0;
|
||||
if (win.matchMedia("(prefers-reduced-motion: reduce)").matches) return 0;
|
||||
const composer = wrap.querySelector<HTMLElement>(".composer");
|
||||
if (!composer) return 0;
|
||||
playComposerSettle(composer, offset);
|
||||
return offset;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFileSync } from "node:fs";
|
||||
import test from "node:test";
|
||||
|
||||
import { COMPOSER_SETTLE_CLASS, COMPOSER_SETTLE_VAR, createComposerSettle } from "../src/lib/composer-settle.ts";
|
||||
|
||||
const page = readFileSync(new URL("../src/app/(app)/page.tsx", import.meta.url), "utf8");
|
||||
const css = readFileSync(new URL("../src/app/globals.css", import.meta.url), "utf8");
|
||||
|
||||
type Listener = () => void;
|
||||
|
||||
function fakeComposer() {
|
||||
const classes = new Set<string>();
|
||||
const props = new Map<string, string>();
|
||||
const listeners = new Map<string, Listener[]>();
|
||||
let added = 0;
|
||||
const el = {
|
||||
classList: {
|
||||
add: (name: string) => { classes.add(name); added += 1; },
|
||||
remove: (name: string) => { classes.delete(name); },
|
||||
contains: (name: string) => classes.has(name),
|
||||
},
|
||||
style: {
|
||||
setProperty: (name: string, value: string) => { props.set(name, value); },
|
||||
removeProperty: (name: string) => { props.delete(name); },
|
||||
},
|
||||
addEventListener: (type: string, fn: Listener) => { listeners.set(type, [...(listeners.get(type) ?? []), fn]); },
|
||||
removeEventListener: (type: string, fn: Listener) => { listeners.set(type, (listeners.get(type) ?? []).filter((f) => f !== fn)); },
|
||||
};
|
||||
return {
|
||||
el: el as unknown as HTMLElement,
|
||||
classes,
|
||||
props,
|
||||
addedCount: () => added,
|
||||
fire: (type: string) => { for (const fn of listeners.get(type) ?? []) fn(); },
|
||||
};
|
||||
}
|
||||
|
||||
function setup({ phone = true, reduced = false, noticeDisplay = "block" } = {}) {
|
||||
const composer = fakeComposer();
|
||||
let noticeShown = true;
|
||||
const notice = { getBoundingClientRect: () => ({ height: 19 }) } as unknown as HTMLElement;
|
||||
const wrap = {
|
||||
querySelector: (selector: string) => {
|
||||
if (selector === ".composer") return composer.el;
|
||||
if (selector === ".chat-ai-notice") return noticeShown ? notice : null;
|
||||
return null;
|
||||
},
|
||||
} as unknown as HTMLElement;
|
||||
const win = {
|
||||
getComputedStyle: () => ({ display: noticeDisplay, marginTop: "6px" }),
|
||||
matchMedia: (query: string) => ({ matches: query.includes("max-width: 640px") ? phone : query.includes("reduce") ? reduced : false }),
|
||||
} as unknown as Window;
|
||||
const input = { closest: (selector: string) => (selector === ".composer-wrap" ? wrap : null) } as unknown as HTMLElement;
|
||||
const root = { dataset: {} as Record<string, string> } as unknown as HTMLElement;
|
||||
return { composer, wrap: input, win, root, hideNotice: () => { noticeShown = false; } };
|
||||
}
|
||||
|
||||
test("BUG-2356: the settle plays once on the empty home -> conversation switch of a send, not on later frames", () => {
|
||||
const { composer, wrap, win, root, hideNotice } = setup();
|
||||
const onCommit = createComposerSettle();
|
||||
assert.equal(onCommit({ starterHome: true, sending: false, input: wrap, win, root }), 0);
|
||||
assert.equal(onCommit({ starterHome: true, sending: false, input: wrap, win, root }), 0);
|
||||
assert.equal(composer.addedCount(), 0, "nothing moves while the empty home is shown");
|
||||
|
||||
hideNotice();
|
||||
assert.equal(onCommit({ starterHome: false, sending: true, input: wrap, win, root }), 25, "notice 19px + 6px top margin");
|
||||
assert.ok(composer.classes.has(COMPOSER_SETTLE_CLASS));
|
||||
assert.equal(composer.props.get(COMPOSER_SETTLE_VAR), "-25px", "starts the notice's height higher, settles down to 0");
|
||||
|
||||
// Later frames of the same conversation (streaming, more turns) never restart it.
|
||||
for (let frame = 0; frame < 5; frame += 1) assert.equal(onCommit({ starterHome: false, sending: true, input: wrap, win, root }), 0);
|
||||
assert.equal(composer.addedCount(), 1);
|
||||
|
||||
composer.fire("animationend");
|
||||
assert.ok(!composer.classes.has(COMPOSER_SETTLE_CLASS), "the class is removed when the animation ends");
|
||||
assert.equal(composer.props.has(COMPOSER_SETTLE_VAR), false);
|
||||
});
|
||||
|
||||
test("BUG-2356: no settle with the keyboard up, on desktop, under reduced motion, or when not a send", () => {
|
||||
const cases: Array<[string, ReturnType<typeof setup>, boolean]> = [
|
||||
["keyboard up (BUG-2323 already hid the notice)", setup({ noticeDisplay: "none" }), true],
|
||||
["desktop", setup({ phone: false }), true],
|
||||
["reduced motion", setup({ reduced: true }), true],
|
||||
["opening another conversation, not a send", setup(), false],
|
||||
];
|
||||
for (const [label, { composer, wrap, win, root }, sending] of cases) {
|
||||
const onCommit = createComposerSettle();
|
||||
onCommit({ starterHome: true, sending: false, input: wrap, win, root });
|
||||
assert.equal(onCommit({ starterHome: false, sending, input: wrap, win, root }), 0, label);
|
||||
assert.equal(composer.addedCount(), 0, label);
|
||||
}
|
||||
const keyboard = setup();
|
||||
const onCommit = createComposerSettle();
|
||||
onCommit({ starterHome: true, sending: false, input: keyboard.wrap, win: keyboard.win, root: keyboard.root });
|
||||
(keyboard.root.dataset as Record<string, string>).chatKeyboard = "open";
|
||||
assert.equal(onCommit({ starterHome: false, sending: true, input: keyboard.wrap, win: keyboard.win, root: keyboard.root }), 0);
|
||||
});
|
||||
|
||||
test("BUG-2356: the settle is a transform-only animation on the 1.2 motion tokens, wired on every page commit", () => {
|
||||
assert.match(css, /@keyframes composer-settle \{ from \{ transform: translateY\(var\(--composer-settle-from, 0px\)\); \} to \{ transform: none; \} \}/);
|
||||
assert.match(css, /\.composer\.is-settling \{ animation: composer-settle var\(--motion-standard\) var\(--ease-standard\) both; \}/);
|
||||
assert.match(css, /@media \(prefers-reduced-motion: reduce\) \{ \.composer\.is-settling \{ animation: none; \} \}/);
|
||||
const keyframes = css.slice(css.indexOf("@keyframes composer-settle"), css.indexOf("@keyframes composer-settle") + 200);
|
||||
assert.doesNotMatch(keyframes.split("}")[0] + keyframes.split("}")[1], /height|margin|top:/);
|
||||
assert.match(page, /useLayoutEffect\(\(\) => \{\n\s*composerSettle\(\{ starterHome: starterHomeVisible, sending: isLoading, input: composerInput\.current, win: window, root: document\.documentElement \}\);\n\s*\}\);/);
|
||||
});
|
||||
Reference in New Issue
Block a user