Skip to content

Commit bdeab23

Browse files
wpfleger96npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7
andauthored
fix(desktop): disable spellcheck/autocorrect/autocapitalize on emoji picker search (#1438)
Signed-off-by: Will Pfleger <[email protected]> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co>
1 parent b1d9d95 commit bdeab23

4 files changed

Lines changed: 213 additions & 21 deletions

File tree

‎desktop/src/features/custom-emoji/ui/EmojiPicker.tsx‎

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,58 @@ function warmEmojiIndex() {
2828
}
2929
warmEmojiIndex();
3030

31+
/**
32+
* Reach into the `em-emoji-picker` shadow root and disable spellcheck,
33+
* autocorrect, and autocapitalize on its search input. When `autoFocus` is
34+
* true, also focus the input so the cursor lands there immediately — this
35+
* makes focus deterministic and owned by us, preventing Radix's focus-scope
36+
* from racing emoji-mart's own async focus call and winning.
37+
*
38+
* emoji-mart mounts the custom element and its shadow content asynchronously,
39+
* so the input is not present on first render. A MutationObserver on the
40+
* shadow root watches for the input to appear, applies the attributes once,
41+
* then disconnects. Returns a cleanup function for `useEffect`.
42+
*/
43+
function disableSearchInputCorrections(
44+
host: HTMLElement,
45+
autoFocus: boolean,
46+
): () => void {
47+
const picker = host.querySelector("em-emoji-picker");
48+
const shadowRoot = picker?.shadowRoot ?? null;
49+
if (!shadowRoot) {
50+
return () => undefined;
51+
}
52+
// shadowRoot is ShadowRoot here; capture as a typed const so the nested
53+
// closure doesn't re-widen to ShadowRoot | null.
54+
const root: ShadowRoot = shadowRoot;
55+
56+
function applyAttributes() {
57+
const input = root.querySelector<HTMLInputElement>('input[type="search"]');
58+
if (!input) return false;
59+
input.spellcheck = false;
60+
input.setAttribute("autocorrect", "off");
61+
input.setAttribute("autocapitalize", "off");
62+
if (autoFocus) {
63+
input.focus();
64+
}
65+
return true;
66+
}
67+
68+
// Fast path: input already present (e.g. picker re-mount from hot cache).
69+
if (applyAttributes()) {
70+
return () => undefined;
71+
}
72+
73+
// Slow path: wait for the shadow subtree to include the input.
74+
const observer = new MutationObserver(() => {
75+
if (applyAttributes()) {
76+
observer.disconnect();
77+
}
78+
});
79+
observer.observe(root, { childList: true, subtree: true });
80+
return () => observer.disconnect();
81+
}
82+
3183
/**
3284
* The one emoji picker for the whole app. Every place that lets a user choose
3385
* an emoji — composing a message, reacting to a regular or system message,
@@ -64,27 +116,35 @@ export const EmojiPicker = React.memo(function EmojiPicker({
64116
() => buildCustomEmojiCategory(customEmoji),
65117
[customEmoji],
66118
);
119+
const hostRef = React.useRef<HTMLDivElement>(null);
120+
121+
React.useEffect(() => {
122+
if (!hostRef.current) return;
123+
return disableSearchInputCorrections(hostRef.current, autoFocus);
124+
}, [autoFocus]);
67125

68126
return (
69-
<Picker
70-
autoFocus={autoFocus}
71-
custom={custom}
72-
data={data}
73-
maxFrequentRows={2}
74-
onEmojiSelect={(emoji: { native?: string; id?: string }) => {
75-
// Standard emoji carry a `native` glyph. Custom emoji don't — emit
76-
// their `:shortcode:` (emoji-mart `id` == shortcode) instead. Ignore a
77-
// malformed selection that has neither.
78-
const value = emoji.native ?? (emoji.id ? `:${emoji.id}:` : "");
79-
if (value) {
80-
onSelect(value);
81-
}
82-
}}
83-
perLine={8}
84-
previewPosition="bottom"
85-
set="native"
86-
skinTonePosition="search"
87-
theme="auto"
88-
/>
127+
<div ref={hostRef}>
128+
<Picker
129+
autoFocus={autoFocus}
130+
custom={custom}
131+
data={data}
132+
maxFrequentRows={2}
133+
onEmojiSelect={(emoji: { native?: string; id?: string }) => {
134+
// Standard emoji carry a `native` glyph. Custom emoji don't — emit
135+
// their `:shortcode:` (emoji-mart `id` == shortcode) instead. Ignore a
136+
// malformed selection that has neither.
137+
const value = emoji.native ?? (emoji.id ? `:${emoji.id}:` : "");
138+
if (value) {
139+
onSelect(value);
140+
}
141+
}}
142+
perLine={8}
143+
previewPosition="bottom"
144+
set="native"
145+
skinTonePosition="search"
146+
theme="auto"
147+
/>
148+
</div>
89149
);
90150
});

‎desktop/src/features/messages/ui/ComposerEmojiPicker.tsx‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import { Tooltip, TooltipContent, TooltipTrigger } from "@/shared/ui/tooltip";
88

99
type ComposerEmojiPickerProps = {
1010
disabled?: boolean;
11+
/** Called when the popover closes without an emoji selection (Escape,
12+
* click-outside). Use this to restore focus to the editor. */
13+
onClose?: () => void;
1114
onEmojiSelect: (emoji: string) => void;
1215
onOpenChange: (open: boolean) => void;
1316
onTriggerMouseDown: () => void;
@@ -16,6 +19,7 @@ type ComposerEmojiPickerProps = {
1619

1720
export const ComposerEmojiPicker = React.memo(function ComposerEmojiPicker({
1821
disabled = false,
22+
onClose,
1923
onEmojiSelect,
2024
onOpenChange,
2125
onTriggerMouseDown,
@@ -44,10 +48,23 @@ export const ComposerEmojiPicker = React.memo(function ComposerEmojiPicker({
4448
<PopoverContent
4549
align="start"
4650
className="w-auto p-0 rounded-2xl overflow-hidden border-0 bg-transparent shadow-none"
51+
// Prevent Radix's FocusScope from stealing focus on open — our
52+
// disableSearchInputCorrections MutationObserver owns focus for
53+
// the shadow-DOM search input (autoFocus path).
54+
onOpenAutoFocus={(e) => e.preventDefault()}
55+
// Suppress Radix's default trigger-return on close. On the
56+
// emoji-select path, insertEmoji already called editor.chain().focus()
57+
// before the popover closes, so the editor owns focus — let it stand.
58+
// On Escape/click-outside, onClose() restores editor focus explicitly
59+
// so the user can keep typing without an extra click.
60+
onCloseAutoFocus={(e) => {
61+
e.preventDefault();
62+
onClose?.();
63+
}}
4764
side="top"
4865
sideOffset={10}
4966
>
50-
<EmojiPicker onSelect={onEmojiSelect} />
67+
<EmojiPicker autoFocus onSelect={onEmojiSelect} />
5168
</PopoverContent>
5269
</Popover>
5370
);

‎desktop/src/features/messages/ui/MessageComposerToolbar.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ export const MessageComposerToolbar = React.memo(
238238
</Tooltip>
239239
<ComposerEmojiPicker
240240
disabled={composerDisabled}
241+
onClose={() => editor?.commands.focus()}
241242
onEmojiSelect={onEmojiSelect}
242243
onOpenChange={onEmojiPickerOpenChange}
243244
onTriggerMouseDown={onCaptureSelection}

‎desktop/tests/e2e/custom-emoji.spec.ts‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,3 +432,117 @@ test("a system message accepts a custom-emoji reaction", async ({ page }) => {
432432
.locator(`img[alt=':${REACTION_SHORTCODE}:']`);
433433
await expect(reactionImg).toBeVisible();
434434
});
435+
436+
test("emoji picker search input has spellcheck, autocorrect, and autocapitalize disabled", async ({
437+
page,
438+
}) => {
439+
await openGeneral(page);
440+
441+
// Open the reaction picker on the seeded reactable message.
442+
const row = reactionTargetRow(page);
443+
await expect(row).toBeVisible();
444+
await row.hover();
445+
await row.getByLabel("Open reactions").click();
446+
447+
// Wait for the picker to be visible, then read the shadow-root input attributes.
448+
const picker = page.locator("em-emoji-picker");
449+
await expect(picker.locator("input[type='search']")).toBeVisible();
450+
451+
const attrs = await picker.evaluate((el: Element) => {
452+
const input = (
453+
el as HTMLElement & { shadowRoot: ShadowRoot }
454+
).shadowRoot?.querySelector<HTMLInputElement>('input[type="search"]');
455+
if (!input) throw new Error("search input not found in shadow root");
456+
return {
457+
spellcheck: input.spellcheck,
458+
autocorrect: input.getAttribute("autocorrect"),
459+
autocapitalize: input.getAttribute("autocapitalize"),
460+
};
461+
});
462+
463+
expect(attrs.spellcheck).toBe(false);
464+
expect(attrs.autocorrect).toBe("off");
465+
expect(attrs.autocapitalize).toBe("off");
466+
});
467+
468+
// Regression guard for PR #1438: after the spellcheck fix, the shadow-DOM
469+
// traversal must also own autofocus so Radix's focus-scope can't steal it.
470+
// Will reported that opening the picker required a manual click before typing.
471+
test("emoji picker search input is focused immediately on open (no manual click needed)", async ({
472+
page,
473+
}) => {
474+
await openGeneral(page);
475+
476+
// Open the reaction picker — it passes autoFocus, so the input must receive
477+
// focus via our shadow traversal before the user interacts.
478+
const row = reactionTargetRow(page);
479+
await expect(row).toBeVisible();
480+
await row.hover();
481+
await row.getByLabel("Open reactions").click();
482+
483+
const picker = page.locator("em-emoji-picker");
484+
await expect(picker.locator("input[type='search']")).toBeVisible();
485+
486+
// The search input must be the active element inside the shadow root.
487+
// If Radix's focus-scope wins instead, shadowRoot.activeElement is the
488+
// popover host (or null), not the input — this assertion catches that.
489+
const isFocused = await picker.evaluate((el: Element) => {
490+
const host = el as HTMLElement & { shadowRoot: ShadowRoot };
491+
const input = host.shadowRoot?.querySelector<HTMLInputElement>(
492+
'input[type="search"]',
493+
);
494+
if (!input) return false;
495+
return host.shadowRoot?.activeElement === input;
496+
});
497+
expect(isFocused).toBe(true);
498+
});
499+
500+
test("composer emoji picker search input is focused immediately on open", async ({
501+
page,
502+
}) => {
503+
await openGeneral(page);
504+
505+
// Open the composer emoji picker via the toolbar button.
506+
await page.getByTestId("composer-emoji-button").click();
507+
508+
const picker = page.locator("em-emoji-picker");
509+
await expect(picker.locator("input[type='search']")).toBeVisible();
510+
511+
// The search input must be the active element inside the shadow root so
512+
// the user can type immediately without a manual click.
513+
const isFocused = await picker.evaluate((el: Element) => {
514+
const host = el as HTMLElement & { shadowRoot: ShadowRoot };
515+
const input = host.shadowRoot?.querySelector<HTMLInputElement>(
516+
'input[type="search"]',
517+
);
518+
if (!input) return false;
519+
return host.shadowRoot?.activeElement === input;
520+
});
521+
expect(isFocused).toBe(true);
522+
523+
// Pick an emoji and verify focus returns to the composer message input,
524+
// not left stranded in the now-closed popover. The insertEmoji handler
525+
// calls editor.chain().focus() before setIsEmojiPickerOpen(false), so
526+
// the Tiptap editor owns focus before Radix's onCloseAutoFocus fires.
527+
await picker.getByRole("button", { name: "😀" }).first().click();
528+
await expect(page.getByTestId("message-input")).toBeFocused();
529+
});
530+
531+
test("composer emoji picker restores editor focus on Escape dismiss", async ({
532+
page,
533+
}) => {
534+
await openGeneral(page);
535+
536+
// Open the composer emoji picker via the toolbar button.
537+
await page.getByTestId("composer-emoji-button").click();
538+
539+
const picker = page.locator("em-emoji-picker");
540+
await expect(picker.locator("input[type='search']")).toBeVisible();
541+
542+
// Dismiss via Escape without selecting an emoji. The onClose callback
543+
// in ComposerEmojiPicker must restore focus to the editor so the user
544+
// can keep typing without an extra click.
545+
await page.keyboard.press("Escape");
546+
547+
await expect(page.getByTestId("message-input")).toBeFocused();
548+
});

0 commit comments

Comments
 (0)