Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Advanced composer: a message past one line gets the full width above the buttons (#3919)

Omkar Satpute3f24359e866c
diff --git a/src/components/Composer.simple.test.ts b/src/components/Composer.simple.test.ts
index 4c29abc609..1c526995af 100644
--- a/src/components/Composer.simple.test.ts
+++ b/src/components/Composer.simple.test.ts
@@ -318,4 +318,75 @@ describe("the Advanced composer", () => {
expect(container.querySelector('[aria-label="Finish together"]')?.getAttribute("aria-pressed")).toBe("false");
expect(fixture.groupCall).toBeNull();
});
+
+ describe("with a message longer than one line", () => {
+ const type = (value: string) => {
+ const field = container.querySelector("textarea")!;
+ flushSync(() => {
+ Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, "value")!.set!.call(field, value);
+ field.dispatchEvent(new Event("input", { bubbles: true }));
+ });
+ };
+ const classes = (selector: string) => container.querySelector(selector)!.className.split(" ");
+ const stacked = () => classes(".mention-editor").includes("basis-full");
+ /** A field 20 characters wide: longer text wraps onto a second 24px line. */
+ const wrapPast20 = () => vi.spyOn(HTMLTextAreaElement.prototype, "scrollHeight", "get")
+ .mockImplementation(function (this: HTMLTextAreaElement) { return this.value.length > 20 ? 48 : 24; });
+
+ it("gives the text the full width above the buttons, and puts it back beside them on one line", () => {
+ fixture.advanced = true;
+ render({ bot });
+ expect(stacked()).toBe(false);
+ type("Okay I have done my outreach.\nAnother line of text.");
+ expect(classes(".mention-editor")).toEqual(expect.arrayContaining(["order-first", "basis-full"]));
+ expect(classes("[data-composer-row]")).toContain("flex-wrap");
+ expect(classes("[data-composer-actions]")).toContain("ml-auto");
+ type("Okay I have done my outreach.");
+ expect(stacked()).toBe(false);
+ expect(classes("[data-composer-row]")).not.toContain("flex-wrap");
+ });
+
+ it("stacks once the text wraps beside the buttons, and stays stacked until it is shorter than where it wrapped", () => {
+ wrapPast20();
+ fixture.advanced = true;
+ render({ bot });
+ type("a".repeat(25));
+ expect(stacked()).toBe(true);
+ // Wider on its own line, it would fit on one row there; it still must not flip back and forth.
+ type("a".repeat(30));
+ expect(stacked()).toBe(true);
+ type("a".repeat(24));
+ expect(stacked()).toBe(true);
+ type("a".repeat(10));
+ expect(stacked()).toBe(false);
+ });
+
+ it("measures each new message afresh, so a wrap from a narrower composer does not stick", () => {
+ const wrap = wrapPast20();
+ fixture.advanced = true;
+ render({ bot });
+ type("a".repeat(25));
+ expect(stacked()).toBe(true);
+ type("");
+ // Wider now: 30 characters fit on the one row.
+ wrap.mockImplementation(function (this: HTMLTextAreaElement) { return this.value.length > 40 ? 48 : 24; });
+ type("a".repeat(30));
+ expect(stacked()).toBe(false);
+ });
+
+ it("never stacks an empty field, even when its placeholder wraps", () => {
+ vi.spyOn(HTMLTextAreaElement.prototype, "scrollHeight", "get").mockReturnValue(48);
+ fixture.advanced = true;
+ render({ bot });
+ expect(stacked()).toBe(false);
+ });
+
+ it("leaves Simple's pill alone", () => {
+ wrapPast20();
+ render({ bot });
+ type("a".repeat(25) + "\nb");
+ expect(stacked()).toBe(false);
+ expect(classes("[data-composer-row]")).not.toContain("flex-wrap");
+ });
+ });
});
diff --git a/src/components/Composer.tsx b/src/components/Composer.tsx
index 227cc073fb..ee8a2fe893 100644
--- a/src/components/Composer.tsx
+++ b/src/components/Composer.tsx
@@ -1,6 +1,6 @@
import { directMessageSent, roomMessageSent } from "@/lib/product-events";
import { useComposerDictation } from "@/lib/use-composer-dictation";
-import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, type SetStateAction } from "react";
+import { useCallback, useEffect, useMemo, useRef, useState, type SetStateAction } from "react";
import { ArrowUp, Clock, Mic, Paperclip, Square, X } from "lucide-react";
import { useStore, visibleMessages, currentTaskBot, type Bot, type Group, type InstanceInfo, type Message } from "@/state/store";
import { cn } from "@/lib/cn";
@@ -69,6 +69,7 @@ import { GroupCallButton } from "./GroupCallView";
import { ApprovalChip, ComposerPlusMenu, GoalToggle } from "./ComposerPlusMenu";
import { PhonePlus } from "./PhonePlus";
import { useThreadApproval } from "./thread-approval";
+import { useStackedField, useTallPill } from "./composer-wrap";
import { useDesktopCapabilities } from "./DesktopCapabilities";
import { ComposerReplyStrip } from "./ReplyQuote";
import { escapeCancelsReply } from "@/lib/replies";
@@ -224,13 +225,14 @@ export function Composer({
draftId, text, onText: editText, available: capabilities.dictation.available,
});
const [caret, setCaret] = useState(0);
- const [tall, setTall] = useState(false);
const shellRef = useRef<HTMLDivElement>(null);
+ const tall = useTallPill(shellRef, simpleDock);
const multiline = simpleDock && (tall || text.includes("\n"));
const [highlight, setHighlight] = useState(0);
const [dismissedAt, setDismissedAt] = useState<number | null>(null);
const [dismissedSlashAt, setDismissedSlashAt] = useState<number | null>(null);
const inputRef = useRef<HTMLTextAreaElement>(null);
+ const stacked = useStackedField(inputRef, advanced && !singleRow, text);
const draftIdRef = useRef(draftId);
draftIdRef.current = draftId;
const caretRef = useRef(0);
@@ -255,16 +257,6 @@ export function Composer({
});
return () => cancelAnimationFrame(frame);
}, []);
- // Anything that grows the pill past its one 44px row (a second line, a reply strip) rounds its corners in.
- useLayoutEffect(() => {
- const shell = shellRef.current;
- if (!simpleDock || !shell || typeof ResizeObserver === "undefined") return;
- const measure = () => setTall(shell.getBoundingClientRect().height > 44.5);
- measure();
- const observer = new ResizeObserver(measure);
- observer.observe(shell);
- return () => observer.disconnect();
- }, [simpleDock]);
const replyToId = replyTo?.id ?? null;
const focusedReplyRef = useRef(replyToId);
useEffect(() => {
@@ -808,7 +800,7 @@ export function Composer({
)}
<div className={singleRow ? cn("min-w-0 flex-1 rounded-[26px] px-3 py-1.5", ELEVATED_FLOAT) : "contents"}>
{replyTo && <ComposerReplyStrip message={replyTo} fallbackName={bot?.name} onClear={onClearReply} />}
- <div data-composer-row className={cn("flex items-end", simpleDock ? "gap-1.5" : "gap-1", !singleRow && !simpleDock && "@max-[30rem]/composer:flex-wrap")}>
+ <div data-composer-row className={cn("flex items-end", simpleDock ? "gap-1.5" : "gap-1", !singleRow && !simpleDock && "@max-[30rem]/composer:flex-wrap", stacked && "flex-wrap")}>
<input
ref={fileInput}
type="file"
@@ -848,7 +840,7 @@ export function Composer({
</div>
)}
<MentionTextarea
- wrapperClassName={singleRow || simpleDock ? undefined : "@max-[30rem]/composer:order-first @max-[30rem]/composer:basis-full"}
+ wrapperClassName={singleRow || simpleDock ? undefined : cn("@max-[30rem]/composer:order-first @max-[30rem]/composer:basis-full", stacked && "order-first basis-full")}
inputRef={inputRef}
peers={[...(group ? members ?? [] : state.bots.filter((member) => member.id !== bot?.id)), ...teammates.peers]}
everyone={Boolean(group && !group.dm)}
@@ -947,7 +939,7 @@ export function Composer({
simpleDock ? "min-h-7 px-0.5 py-1 text-[14px] leading-5" : "min-h-6 py-1 leading-6",
)}
/>
- <div data-composer-actions className={cn("flex items-center", simpleDock ? "gap-2" : "gap-1", !singleRow && !simpleDock && "@max-[30rem]/composer:ml-auto")}>
+ <div data-composer-actions className={cn("flex items-center", simpleDock ? "gap-2" : "gap-1", !singleRow && !simpleDock && "@max-[30rem]/composer:ml-auto", stacked && "ml-auto")}>
{(simpleDock ? stoppable : busy) && !locked && (
<button
onClick={interruptTurn}
diff --git a/src/components/composer-wrap.ts b/src/components/composer-wrap.ts
new file mode 100644
index 0000000000..63f8c24e3e
--- /dev/null
+++ b/src/components/composer-wrap.ts
@@ -0,0 +1,44 @@
+import { useLayoutEffect, useState, type RefObject } from "react";
+
+/** Simple: anything that grows the pill past its one 44px row (a second line, a reply strip) rounds its corners in. */
+export function useTallPill(shellRef: RefObject<HTMLDivElement | null>, on: boolean): boolean {
+ const [tall, setTall] = useState(false);
+ useLayoutEffect(() => {
+ const shell = shellRef.current;
+ if (!on || !shell || typeof ResizeObserver === "undefined") return;
+ const measure = () => setTall(shell.getBoundingClientRect().height > 44.5);
+ measure();
+ const observer = new ResizeObserver(measure);
+ observer.observe(shell);
+ return () => observer.disconnect();
+ }, [shellRef, on]);
+ return tall;
+}
+
+/**
+ * Advanced: a message past one line takes the composer's full width above the buttons instead of wrapping in the gap
+ * between them. `wrapAt` is the length at which the one-row field wrapped; the field goes back beside the buttons
+ * only below it, so wider text on its own line cannot flip back and forth.
+ */
+export function useStackedField(inputRef: RefObject<HTMLTextAreaElement | null>, on: boolean, text: string): boolean {
+ const [wrapAt, setWrapAt] = useState<number | null>(null);
+ const stacked = on && (text.includes("\n") || (wrapAt !== null && text.length >= wrapAt));
+ // Width changes (a resized window, a panel opening) can wrap the one-row field too, so watch its size, not just the text.
+ useLayoutEffect(() => {
+ const input = inputRef.current;
+ if (!on || stacked || !input || typeof ResizeObserver === "undefined") return;
+ const measure = () => {
+ // An empty field's height is its placeholder's, which may wrap; that is not a message. The next message measures afresh.
+ if (!input.value) return setWrapAt(null);
+ const style = getComputedStyle(input);
+ const line = parseFloat(style.lineHeight) || 24;
+ const content = input.scrollHeight - (parseFloat(style.paddingTop) || 0) - (parseFloat(style.paddingBottom) || 0);
+ if (content > line * 1.5) setWrapAt(input.value.length);
+ };
+ measure();
+ const observer = new ResizeObserver(measure);
+ observer.observe(input);
+ return () => observer.disconnect();
+ }, [inputRef, on, stacked, text]);
+ return stacked;
+}