Skip to content

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

Show the native IME underline in the composer

Kaito-Rowingc517e27cd408
diff --git a/src/components/MentionTextarea.dom.test.ts b/src/components/MentionTextarea.dom.test.ts
index 1f80155770..3ab9ee1090 100644
--- a/src/components/MentionTextarea.dom.test.ts
+++ b/src/components/MentionTextarea.dom.test.ts
@@ -27,3 +27,25 @@ it("draws its words in the field's own type and padding, so the caret stays at t
expect([mirror().fontSize, mirror().lineHeight, mirror().paddingLeft], "and follows the field when its classes change").toEqual(["15px", "24px", "4px"]);
act(() => root.unmount());
});
+
+it("uses native textarea rendering during composition and restores mention rendering on end or blur", () => {
+ vi.stubGlobal("ResizeObserver", class { observe() {} disconnect() {} });
+ const host = document.body.appendChild(document.createElement("div"));
+ const root = createRoot(host);
+ const inputRef = createRef<HTMLTextAreaElement>();
+ const onCompositionStart = vi.fn();
+ const onCompositionEnd = vi.fn();
+ act(() => root.render(createElement(MentionTextarea, { inputRef, peers: [], onCompositionStart, onCompositionEnd })));
+ const input = inputRef.current!;
+ const editor = host.querySelector(".mention-editor")!;
+ act(() => input.dispatchEvent(new CompositionEvent("compositionstart", { bubbles: true })));
+ expect(editor.hasAttribute("data-composing")).toBe(true);
+ expect(onCompositionStart).toHaveBeenCalledOnce();
+ act(() => input.dispatchEvent(new CompositionEvent("compositionend", { bubbles: true })));
+ expect(editor.hasAttribute("data-composing")).toBe(false);
+ expect(onCompositionEnd).toHaveBeenCalledOnce();
+ act(() => input.dispatchEvent(new CompositionEvent("compositionstart", { bubbles: true })));
+ act(() => input.dispatchEvent(new FocusEvent("focusout", { bubbles: true })));
+ expect(editor.hasAttribute("data-composing")).toBe(false);
+ act(() => root.unmount());
+});
diff --git a/src/components/MentionTextarea.tsx b/src/components/MentionTextarea.tsx
index 87b9a469f3..b79430e014 100644
--- a/src/components/MentionTextarea.tsx
+++ b/src/components/MentionTextarea.tsx
@@ -1,5 +1,5 @@
import { cn } from "@/lib/cn";
-import { useCallback, useLayoutEffect, useRef, type RefObject, type TextareaHTMLAttributes } from "react";
+import { useCallback, useLayoutEffect, useRef, useState, type RefObject, type TextareaHTMLAttributes } from "react";
import { type MentionPeer } from "@/lib/mentions";
import { MentionText } from "./MentionText";
@@ -12,6 +12,7 @@ export function MentionTextarea({ inputRef, peers, everyone = false, wrapperClas
peers: readonly MentionPeer[];
everyone?: boolean;
}) {
+ const [composing, setComposing] = useState(false);
const mirrorRef = useRef<HTMLDivElement>(null);
const sync = useCallback(() => {
const input = inputRef.current;
@@ -45,10 +46,14 @@ export function MentionTextarea({ inputRef, peers, everyone = false, wrapperClas
if (inputRef.current) observer.observe(inputRef.current);
return () => observer.disconnect();
}, [inputRef, resize, sync]);
- return <div className={cn("mention-editor relative min-w-0 flex-1 self-center", wrapperClassName)}>
+ return <div data-composing={composing || undefined} className={cn("mention-editor relative min-w-0 flex-1 self-center", wrapperClassName)}>
<div ref={mirrorRef} dir={props.dir} aria-hidden="true" className="mention-editor-mirror pointer-events-none absolute inset-0 overflow-hidden">
<MentionText text={String(props.value ?? "")} peers={peers} everyone={everyone} />{"\n"}
</div>
- <textarea {...props} ref={inputRef} onScroll={(event) => { sync(); props.onScroll?.(event); }} />
+ <textarea {...props} ref={inputRef}
+ onCompositionStart={(event) => { setComposing(true); props.onCompositionStart?.(event); }}
+ onCompositionEnd={(event) => { setComposing(false); props.onCompositionEnd?.(event); }}
+ onBlur={(event) => { setComposing(false); props.onBlur?.(event); }}
+ onScroll={(event) => { sync(); props.onScroll?.(event); }} />
</div>;
}
diff --git a/src/styles.css b/src/styles.css
index 8527e26c6f..692f912bdd 100644
--- a/src/styles.css
+++ b/src/styles.css
@@ -56,6 +56,12 @@
color: transparent;
caret-color: var(--color-ink);
}
+.mention-editor[data-composing] .mention-editor-mirror {
+ visibility: hidden;
+}
+.mention-editor[data-composing] textarea {
+ color: var(--color-ink);
+}
.mention-editor textarea::selection {
color: var(--color-ink);
background: var(--color-raised-hover);
@@ -68,7 +74,7 @@
}
@media (forced-colors: active) {
.mention-editor-mirror { display: none; }
- .mention-editor textarea { color: CanvasText; caret-color: CanvasText; }
+ .mention-editor textarea, .mention-editor[data-composing] textarea { color: CanvasText; caret-color: CanvasText; }
}
/* ─────────────────────────────────────────────────────────────────────────