Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(composer): the caret stays at the end of the text in Simple mode (#3718)
Milind Soni6e0e143935be
diff --git a/docs/verification/evidence/composer-caret/after-simple.png b/docs/verification/evidence/composer-caret/after-simple.pngnew file mode 100644index 0000000000..f0526b1997Binary files /dev/null and b/docs/verification/evidence/composer-caret/after-simple.png differdiff --git a/docs/verification/evidence/composer-caret/before-simple.png b/docs/verification/evidence/composer-caret/before-simple.pngnew file mode 100644index 0000000000..e59477fe40Binary files /dev/null and b/docs/verification/evidence/composer-caret/before-simple.png differdiff --git a/src/components/MentionTextarea.dom.test.ts b/src/components/MentionTextarea.dom.test.tsnew file mode 100644index 0000000000..1f80155770--- /dev/null+++ b/src/components/MentionTextarea.dom.test.ts@@ -0,0 +1,29 @@+// @vitest-environment happy-dom+import { act, createElement, createRef } from "react";+import { createRoot } from "react-dom/client";+import { afterEach, expect, it, vi } from "vitest";+import { MentionTextarea } from "./MentionTextarea";++(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;+afterEach(() => {+ document.head.innerHTML = "";+ document.body.innerHTML = "";+ vi.unstubAllGlobals();+});++it("draws its words in the field's own type and padding, so the caret stays at the end of the text", async () => {+ vi.stubGlobal("ResizeObserver", class { observe() {} disconnect() {} });+ document.head.appendChild(document.createElement("style")).textContent =+ ".dock { font-size: 14px; line-height: 20px; padding: 4px 2px } .wide { font-size: 15px; line-height: 24px; padding: 4px }";+ const root = createRoot(document.body.appendChild(document.createElement("div")));+ const inputRef = createRef<HTMLTextAreaElement>();+ const show = (className: string) => act(async () => {+ root.render(createElement(MentionTextarea, { inputRef, peers: [], className, value: "asdasdasdasjashdjas", onChange: () => {} }));+ });+ const mirror = () => document.querySelector<HTMLElement>(".mention-editor-mirror")!.style;+ await show("dock");+ expect([mirror().fontSize, mirror().lineHeight, mirror().paddingLeft]).toEqual(["14px", "20px", "2px"]);+ await show("wide");+ expect([mirror().fontSize, mirror().lineHeight, mirror().paddingLeft], "and follows the field when its classes change").toEqual(["15px", "24px", "4px"]);+ act(() => root.unmount());+});diff --git a/src/components/MentionTextarea.tsx b/src/components/MentionTextarea.tsxindex c740ca2654..87b9a469f3 100644--- a/src/components/MentionTextarea.tsx+++ b/src/components/MentionTextarea.tsx@@ -3,6 +3,9 @@ import { useCallback, useLayoutEffect, useRef, type RefObject, type TextareaHTMLimport { type MentionPeer } from "@/lib/mentions";import { MentionText } from "./MentionText";+const TEXT_LAYOUT = ["font-family", "font-size", "font-weight", "font-style", "font-stretch", "font-feature-settings", "font-variation-settings",+ "letter-spacing", "word-spacing", "line-height", "text-indent", "tab-size", "padding-top", "padding-right", "padding-bottom", "padding-left"] as const;+export function MentionTextarea({ inputRef, peers, everyone = false, wrapperClassName, ...props }: TextareaHTMLAttributes<HTMLTextAreaElement> & {wrapperClassName?: string;inputRef: RefObject<HTMLTextAreaElement | null>;@@ -14,6 +17,8 @@ export function MentionTextarea({ inputRef, peers, everyone = false, wrapperClasconst input = inputRef.current;const mirror = mirrorRef.current;if (!input || !mirror) return;+ const style = getComputedStyle(input);+ for (const property of TEXT_LAYOUT) mirror.style.setProperty(property, style.getPropertyValue(property));mirror.style.width = `${input.clientWidth}px`;mirror.scrollTop = input.scrollTop;mirror.scrollLeft = input.scrollLeft;@@ -28,7 +33,7 @@ export function MentionTextarea({ inputRef, peers, everyone = false, wrapperClasinput.scrollTop = scrollTop;sync();}, [inputRef, sync]);- useLayoutEffect(resize, [props.value, resize]);+ useLayoutEffect(resize, [props.value, props.className, resize]);useLayoutEffect(() => {let width = inputRef.current?.clientWidth;const observer = new ResizeObserver(() => {diff --git a/src/styles.css b/src/styles.cssindex 203fdf6a54..3f1aaa286f 100644--- a/src/styles.css+++ b/src/styles.css@@ -47,9 +47,6 @@.mention-editor-mirror {/* Match the native textarea's direction independently for each paragraph. */unicode-bidi: plaintext;- padding: 4px;- font-size: 15px;- line-height: 24px;white-space: pre-wrap;overflow-wrap: break-word;color: var(--color-ink);@@ -65,10 +62,9 @@}/* Safari zooms smaller form text on focus. In a standalone phone window thatcan leave the header under the status bar after the keyboard closes. Keep- the native editor and its mention mirror at the same readable touch size. */+ the native editor at a readable touch size. */@media (pointer: coarse) {- .mention-editor textarea,- .mention-editor-mirror { font-size: 16px; }+ .mention-editor textarea { font-size: 16px; }}@media (forced-colors: active) {.mention-editor-mirror { display: none; }