Skip to content

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

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.png
new file mode 100644
index 0000000000..f0526b1997
Binary files /dev/null and b/docs/verification/evidence/composer-caret/after-simple.png differ
diff --git a/docs/verification/evidence/composer-caret/before-simple.png b/docs/verification/evidence/composer-caret/before-simple.png
new file mode 100644
index 0000000000..e59477fe40
Binary files /dev/null and b/docs/verification/evidence/composer-caret/before-simple.png differ
diff --git a/src/components/MentionTextarea.dom.test.ts b/src/components/MentionTextarea.dom.test.ts
new file mode 100644
index 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.tsx
index c740ca2654..87b9a469f3 100644
--- a/src/components/MentionTextarea.tsx
+++ b/src/components/MentionTextarea.tsx
@@ -3,6 +3,9 @@ import { useCallback, useLayoutEffect, useRef, type RefObject, type TextareaHTML
import { 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, wrapperClas
const 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, wrapperClas
input.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.css
index 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 that
can 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; }