fix(mobile): keep composer sheets from popping the soft keyboard (#1866)

<!-- Please read
https://github.com/SableClient/Sable/blob/dev/CONTRIBUTING.md before
submitting your pull request -->

### Description

<!-- Please include a summary of the change. Please also include
relevant motivation and context. List any dependencies that are required
for this change. -->

Fixes #

#### Type of change

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] This change requires a documentation update

### Checklist:

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings

### AI disclosure:

- [ ] Partially AI assisted (clarify which code was AI assisted and
briefly explain what it does).
- [ ] Fully AI generated (explain what all the generated code does in
moderate detail).
<!-- Write any explanation required here, but do not generate the
explanation using AI!! You must prove you understand what the code in
this PR does. -->
This commit is contained in:
Erwan Leboucher authored and GitHub committed 2026-08-19 13:13:47 +02:00
commit adc889fd20
7 files changed
+182 -27

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
default: patch
---
Dismiss the mobile keyboard when opening composer sheets (emoji, attachments, personas) and keep it dismissed after they close.
+68 -7
View File
@@ -4,12 +4,13 @@ import { CustomEditor } from './Editor';
import { ProseMirrorEditorController } from './prosemirrorController';
import * as css from './Editor.css';
let isIosApp = false;
const platformState = vi.hoisted(() => ({ isIosApp: false, isMobile: false }));
let nativeClipboardText = '';
vi.mock(import('$utils/platform'), async (importOriginal) => ({
...(await importOriginal()),
iosApp: () => isIosApp,
iosApp: () => platformState.isIosApp,
isMobileOrTablet: () => platformState.isMobile,
}));
vi.mock(import('$utils/dom'), async (importOriginal) => ({
@@ -24,7 +25,8 @@ beforeAll(() => {
});
beforeEach(() => {
isIosApp = false;
platformState.isIosApp = false;
platformState.isMobile = false;
nativeClipboardText = '';
});
@@ -159,7 +161,7 @@ const pasteWith = (container: HTMLElement, clipboardData: Record<string, string>
describe('CustomEditor paste', () => {
it('reads the native clipboard when the ios webview delivers an empty paste event', async () => {
isIosApp = true;
platformState.isIosApp = true;
nativeClipboardText = 'from the native clipboard';
const { container, editor } = renderEditor();
@@ -169,7 +171,7 @@ describe('CustomEditor paste', () => {
});
it('leaves an empty paste event alone outside the ios webview', async () => {
isIosApp = false;
platformState.isIosApp = false;
nativeClipboardText = 'from the native clipboard';
const { container, editor } = renderEditor();
@@ -180,7 +182,7 @@ describe('CustomEditor paste', () => {
});
it('does not read the native clipboard when the event already carries text', async () => {
isIosApp = true;
platformState.isIosApp = true;
nativeClipboardText = 'from the native clipboard';
const { container, editor } = renderEditor();
@@ -191,7 +193,7 @@ describe('CustomEditor paste', () => {
});
it('lets a consumer handler pre-empt the native clipboard fallback', async () => {
isIosApp = true;
platformState.isIosApp = true;
nativeClipboardText = 'from the native clipboard';
const { container, editor } = renderEditor({
onPaste: (event) => event.preventDefault(),
@@ -203,3 +205,62 @@ describe('CustomEditor paste', () => {
expect(editor.getText()).toBe('');
});
});
const focusableRival = () => document.body.appendChild(document.createElement('button'));
describe('CustomEditor mobile keyboard', () => {
it('refocuses the editor when focus moves to a non-editable element on mobile', () => {
platformState.isMobile = true;
const { container, editor } = renderEditor();
const focusSpy = vi.spyOn(editor, 'focus');
const editable = container.querySelector('.ProseMirror') as HTMLElement;
const rival = focusableRival();
try {
editable.focus();
rival.focus();
expect(focusSpy).toHaveBeenCalledOnce();
} finally {
rival.remove();
}
});
it('does not refocus on desktop', () => {
const { container, editor } = renderEditor();
const focusSpy = vi.spyOn(editor, 'focus');
const editable = container.querySelector('.ProseMirror') as HTMLElement;
const rival = focusableRival();
try {
editable.focus();
rival.focus();
expect(focusSpy).not.toHaveBeenCalled();
} finally {
rival.remove();
}
});
it('keeps a programmatic blur so sheets can dismiss the keyboard', () => {
platformState.isMobile = true;
const { container, editor } = renderEditor();
const focusSpy = vi.spyOn(editor, 'focus');
const editable = container.querySelector('.ProseMirror') as HTMLElement;
editable.focus();
editable.blur();
expect(focusSpy).not.toHaveBeenCalled();
expect(document.activeElement).not.toBe(editable);
});
it('does not refocus while the composer suppresses it', () => {
platformState.isMobile = true;
const { container, editor } = renderEditor({ suppressBlurRefocusRef: { current: true } });
const focusSpy = vi.spyOn(editor, 'focus');
const editable = container.querySelector('.ProseMirror') as HTMLElement;
const rival = focusableRival();
try {
editable.focus();
rival.focus();
expect(focusSpy).not.toHaveBeenCalled();
} finally {
rival.remove();
}
});
});
+24 -18
View File
@@ -116,6 +116,30 @@ export const CustomEditor = forwardRef<HTMLDivElement, CustomEditorProps>(
[editor, onPaste]
);
useEffect(() => {
editor.setDomEventHandlers({
blur: (event) => {
if (!isMobileOrTablet() || suppressBlurRefocusRef?.current) return;
const next = event.relatedTarget as HTMLElement | null;
if (!next || next.isContentEditable) return;
editor.focus();
},
focus: () => {
if (!isMobileOrTablet()) return;
const editable = document.activeElement;
window.clearTimeout(focusScrollTimerRef.current);
const scrollIntoView = () => {
if (editable && editable === document.activeElement) {
rootRef.current?.scrollIntoView({ block: 'nearest' });
}
};
window.visualViewport?.addEventListener('resize', scrollIntoView, { once: true });
focusScrollTimerRef.current = window.setTimeout(scrollIntoView, 500);
},
});
return () => editor.setDomEventHandlers({});
}, [editor, suppressBlurRefocusRef]);
const setRootRef = useCallback(
(element: HTMLDivElement | null) => {
rootRef.current = element;
@@ -167,24 +191,6 @@ export const CustomEditor = forwardRef<HTMLDivElement, CustomEditorProps>(
onKeyDown={handleKeyDown}
onKeyUp={onKeyUp}
onPaste={handlePaste}
onBlur={(event) => {
if (!isMobileOrTablet() || suppressBlurRefocusRef?.current) return;
const next = event.relatedTarget as HTMLElement | null;
if (!next || (next !== event.currentTarget && next.isContentEditable)) return;
editor.focus();
}}
onFocus={() => {
if (!isMobileOrTablet()) return;
const editable = document.activeElement;
window.clearTimeout(focusScrollTimerRef.current);
const scrollIntoView = () => {
if (editable && editable === document.activeElement) {
rootRef.current?.scrollIntoView({ block: 'nearest' });
}
};
window.visualViewport?.addEventListener('resize', scrollIntoView, { once: true });
focusScrollTimerRef.current = window.setTimeout(scrollIntoView, 500);
}}
/>
</Scroll>
{(after || (responsiveAfter && !showResponsiveAfterInFooter)) && (
@@ -29,6 +29,11 @@ export type EditorAutocompleteQuery<TPrefix extends string> = {
to: number;
};
export type EditorDomEventHandlers = {
blur?: (event: FocusEvent) => void;
focus?: (event: FocusEvent) => void;
};
/**
* The sole editor-engine seam. Consumers exchange Sable documents and never
* retain an EditorState or EditorView.
@@ -36,6 +41,7 @@ export type EditorAutocompleteQuery<TPrefix extends string> = {
export class ProseMirrorEditorController {
private attributes: Record<string, string> = {};
private document: EditorDocument;
private domEventHandlers: EditorDomEventHandlers = {};
private listeners = new Set<(document: EditorDocument) => void>();
private renderContext: EditorRenderContext = defaultEditorRenderContext;
private view: EditorView | undefined;
@@ -65,6 +71,10 @@ export class ProseMirrorEditorController {
this.view?.setProps({ nodeViews: buildEditorNodeViews(() => this.renderContext) });
}
setDomEventHandlers(handlers: EditorDomEventHandlers): void {
this.domEventHandlers = handlers;
}
get children(): EditorDocument {
return this.getDocument();
}
@@ -146,6 +156,16 @@ export class ProseMirrorEditorController {
{ mount: element },
{
attributes: this.viewAttributes,
handleDOMEvents: {
focus: (_view, event) => {
this.domEventHandlers.focus?.(event as FocusEvent);
return false;
},
blur: (_view, event) => {
this.domEventHandlers.blur?.(event as FocusEvent);
return false;
},
},
handlePaste: (view, event) => {
const text = event.clipboardData?.getData('text/plain');
if (text === undefined || text === '') return false;
+45
View File
@@ -1423,6 +1423,51 @@ describe('RoomInput submit regressions', () => {
expect(screen.getByRole('button', { name: 'Open emoji board' })).toBeInTheDocument();
});
it('blurs the editor when opening the emoji board on mobile to dismiss the keyboard', () => {
testState.isMobile = true;
testState.editorTriggerButtons = true;
render(<RoomInputHarness />);
const editor = screen.getByTestId('room-input-editor');
editor.focus();
expect(document.activeElement).toBe(editor);
fireEvent.click(screen.getByRole('button', { name: 'Open emoji board' }));
expect(document.activeElement).not.toBe(editor);
});
it('blurs the editor when opening the attachment sheet on mobile to dismiss the keyboard', () => {
testState.isMobile = true;
render(<RoomInputHarness />);
const editor = screen.getByTestId('room-input-editor');
editor.focus();
expect(document.activeElement).toBe(editor);
fireEvent.click(screen.getByRole('button', { name: 'Add new Item' }));
expect(document.activeElement).not.toBe(editor);
});
it('does not refocus the editor when the emoji sheet closes on mobile', () => {
testState.isMobile = true;
testState.editorTriggerButtons = true;
render(<RoomInputHarness />);
const editor = screen.getByTestId('room-input-editor');
editor.focus();
const activeElement = vi
.spyOn(Object.getPrototypeOf(document), 'activeElement', 'get')
.mockReturnValue(editor);
try {
fireEvent.click(screen.getByRole('button', { name: 'Open emoji board' }));
fireEvent.click(screen.getByRole('button', { name: 'Open emoji board' }));
} finally {
activeElement.mockRestore();
}
expect(document.activeElement).not.toBe(editor);
});
it('keeps an unsent draft across a composer remount', async () => {
const input = render(<RoomInputHarness />);
render(<DraftObserver />);
+16 -2
View File
@@ -678,6 +678,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
const [AddMenuAnchor, setAddMenuAnchor] = useState<RectCords>();
const [showAttachmentSheet, setShowAttachmentSheet] = useState(false);
const attachmentSkipReturnFocusRef = useRef(false);
const emojiBoardSkipReturnFocusRef = useRef(true);
const [showPollPicker, setShowPollPicker] = useState(false);
const [showLocationPicker, setShowLocationPicker] = useState(false);
const [scheduleMenuAnchor, setScheduleMenuAnchor] = useState<RectCords>();
@@ -722,7 +723,13 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
}, []);
const toggleEmojiBoardTab = useCallback((tab: EmojiBoardTab) => {
setEmojiBoardTab((prev) => {
if (prev !== tab) return tab;
if (prev !== tab) {
if (prev === undefined && isMobileOrTablet()) {
const activeElement = document.activeElement;
if (activeElement instanceof HTMLElement) activeElement.blur();
}
return tab;
}
if (isMobileOrTablet()) {
const activeElement = document.activeElement;
if (activeElement instanceof HTMLElement) activeElement.blur();
@@ -1498,6 +1505,10 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
}
if (outgoing.kind === 'gifSearch') {
restoreReplyClaim(submission.replyClaim);
if (isMobileOrTablet()) {
const activeElement = document.activeElement;
if (activeElement instanceof HTMLElement) activeElement.blur();
}
setInitialGifSearch(outgoing.query);
setEmojiBoardTab(EmojiBoardTab.Gif);
return;
@@ -2215,7 +2226,9 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
<>
<IconButton
onClick={() => {
attachmentSkipReturnFocusRef.current = false;
attachmentSkipReturnFocusRef.current = true;
const activeElement = document.activeElement;
if (activeElement instanceof HTMLElement) activeElement.blur();
setShowAttachmentSheet(true);
}}
onPointerDown={suppressEditorRefocus}
@@ -2442,6 +2455,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
dialogLabel="Emoji picker"
sheetClassName={messageCss.MessageMobileOptionsContainerPicker}
keyboardAware
skipReturnFocusRef={emojiBoardSkipReturnFocusRef}
>
{() => emojiBoard}
</MobileSwipeDownModal>
@@ -453,6 +453,10 @@ export function PersonaPicker({
<IconButton
aria-pressed={!!AddPersonaMenuAnchor}
onClick={(evt) => {
if (isMobileOrTablet()) {
const activeElement = document.activeElement;
if (activeElement instanceof HTMLElement) activeElement.blur();
}
setAddPersonaMenuAnchor(evt.currentTarget.getBoundingClientRect());
}}
onPointerDown={suppressEditorRefocus}