diff --git a/.changeset/fix-mobile-sheet-keyboard.md b/.changeset/fix-mobile-sheet-keyboard.md new file mode 100644 index 000000000..8de0a4a22 --- /dev/null +++ b/.changeset/fix-mobile-sheet-keyboard.md @@ -0,0 +1,5 @@ +--- +default: patch +--- + +Dismiss the mobile keyboard when opening composer sheets (emoji, attachments, personas) and keep it dismissed after they close. diff --git a/src/app/components/editor/Editor.test.tsx b/src/app/components/editor/Editor.test.tsx index efc78859e..94a8cec55 100644 --- a/src/app/components/editor/Editor.test.tsx +++ b/src/app/components/editor/Editor.test.tsx @@ -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 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(); + } + }); +}); diff --git a/src/app/components/editor/Editor.tsx b/src/app/components/editor/Editor.tsx index 24eea2e71..d41633fda 100644 --- a/src/app/components/editor/Editor.tsx +++ b/src/app/components/editor/Editor.tsx @@ -116,6 +116,30 @@ export const CustomEditor = forwardRef( [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( 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); - }} /> {(after || (responsiveAfter && !showResponsiveAfterInFooter)) && ( diff --git a/src/app/components/editor/prosemirrorController.ts b/src/app/components/editor/prosemirrorController.ts index 13c866469..483ae8f99 100644 --- a/src/app/components/editor/prosemirrorController.ts +++ b/src/app/components/editor/prosemirrorController.ts @@ -29,6 +29,11 @@ export type EditorAutocompleteQuery = { 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 = { export class ProseMirrorEditorController { private attributes: Record = {}; 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; diff --git a/src/app/features/room/RoomInput.test.tsx b/src/app/features/room/RoomInput.test.tsx index 5ffc56fe7..f928135c6 100644 --- a/src/app/features/room/RoomInput.test.tsx +++ b/src/app/features/room/RoomInput.test.tsx @@ -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(); + 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(); + 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(); + 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(); render(); diff --git a/src/app/features/room/RoomInput.tsx b/src/app/features/room/RoomInput.tsx index 0ef22a1af..f6ee15778 100644 --- a/src/app/features/room/RoomInput.tsx +++ b/src/app/features/room/RoomInput.tsx @@ -678,6 +678,7 @@ export const RoomInput = forwardRef( const [AddMenuAnchor, setAddMenuAnchor] = useState(); 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(); @@ -722,7 +723,13 @@ export const RoomInput = forwardRef( }, []); 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( } 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( <> { - 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( dialogLabel="Emoji picker" sheetClassName={messageCss.MessageMobileOptionsContainerPicker} keyboardAware + skipReturnFocusRef={emojiBoardSkipReturnFocusRef} > {() => emojiBoard} diff --git a/src/app/features/room/persona-picker/PersonaPicker.tsx b/src/app/features/room/persona-picker/PersonaPicker.tsx index 93e072d1d..50e83436e 100644 --- a/src/app/features/room/persona-picker/PersonaPicker.tsx +++ b/src/app/features/room/persona-picker/PersonaPicker.tsx @@ -453,6 +453,10 @@ export function PersonaPicker({ { + if (isMobileOrTablet()) { + const activeElement = document.activeElement; + if (activeElement instanceof HTMLElement) activeElement.blur(); + } setAddPersonaMenuAnchor(evt.currentTarget.getBoundingClientRect()); }} onPointerDown={suppressEditorRefocus}