Skip to content
TypeScript

revert(web): composer body ref goes back to the #25079 ref callback (#25412)

Julius Marmingecc93cc564b05
diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx
index ce4b9258fd6..2420604c8aa 100644
--- a/apps/web/src/components/chat/ChatComposer.tsx
+++ b/apps/web/src/components/chat/ChatComposer.tsx
@@ -1096,7 +1096,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
null,
);
const [composerMenuAnchor, setComposerMenuAnchor] = useState<HTMLDivElement | null>(null);
- const hasWrappedPrompt = useComposerMultilinePrompt(composerMenuAnchor);
+ const [hasWrappedPrompt, observeWrappedPrompt] = useComposerMultilinePrompt();
const hasMultilinePrompt = prompt.includes("\n") || hasWrappedPrompt;
const [isTasksDrawerOpen, setIsTasksDrawerOpen] = useState(false);
// A pending question stays expanded: its card moves into the banner dock,
@@ -3652,7 +3652,14 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
) : null}
<div
- ref={setComposerMenuAnchor}
+ ref={(node) => {
+ setComposerMenuAnchor(node);
+ const stopObserving = observeWrappedPrompt(node);
+ return () => {
+ setComposerMenuAnchor(null);
+ stopObserving?.();
+ };
+ }}
data-chat-composer-body="true"
className={cn(
"relative px-3 pb-2 sm:px-4",
diff --git a/apps/web/src/components/chat/useComposerMultilinePrompt.ts b/apps/web/src/components/chat/useComposerMultilinePrompt.ts
index b5025b1becf..c09c371aa99 100644
--- a/apps/web/src/components/chat/useComposerMultilinePrompt.ts
+++ b/apps/web/src/components/chat/useComposerMultilinePrompt.ts
@@ -1,4 +1,4 @@
-import { useLayoutEffect, useState } from "react";
+import { useState } from "react";
import { observeResize } from "../../lib/observeResize";
function measureComposerMultilinePrompt(body: HTMLElement): boolean | null {
@@ -19,10 +19,13 @@ function measureComposerMultilinePrompt(body: HTMLElement): boolean | null {
return bounds.height > lineHeight + 1 || bounds.width > expandedWidth + 1;
}
-export function useComposerMultilinePrompt(body: HTMLElement | null): boolean {
+/** Whether the prompt wraps, and the ref callback that measures it on the composer body. */
+export function useComposerMultilinePrompt(): readonly [
+ boolean,
+ (body: HTMLElement | null) => (() => void) | undefined,
+] {
const [isMultiline, setIsMultiline] = useState(false);
-
- useLayoutEffect(() => {
+ const observeBody = (body: HTMLElement | null) => {
if (!body) return;
const measure = () => {
const next = measureComposerMultilinePrompt(body);
@@ -37,7 +40,6 @@ export function useComposerMultilinePrompt(body: HTMLElement | null): boolean {
stopObserving();
mutationObserver.disconnect();
};
- }, [body]);
-
- return isMultiline;
+ };
+ return [isMultiline, observeBody] as const;
}