Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix[mobile]: better input focus handling (#2488)

Peter Chinman0324b8a40637
diff --git a/js/app/packages/app/component/useAppSquishHandlers.ts b/js/app/packages/app/component/useAppSquishHandlers.ts
index 6e77785573..55fc090591 100644
--- a/js/app/packages/app/component/useAppSquishHandlers.ts
+++ b/js/app/packages/app/component/useAppSquishHandlers.ts
@@ -62,6 +62,8 @@ export function useAppSquishHandlers() {
const handleResize = () => {
if (window.visualViewport) {
const newViewportHeight = window.visualViewport.height;
+ // We can reliably use the visual viewport shrinking in size as a strong signal that the virtual keyboard is visible. This is better than using focusIn as a signal, because with focusIn we have to guess the timing of the virtual keyboard appearing.
+ // We can NOT reliably use the visual viewport growing for the inverse. Recent versions of iOS Safari cause this to fire immediately on virtual keyboard appearance. Instead we need to rely on focusOut.
if (newViewportHeight < previousViewportHeight) {
setVirtualKeyboardVisible(true);
const vh = newViewportHeight * 0.01;
@@ -69,9 +71,6 @@ export function useAppSquishHandlers() {
setTimeout(() => {
window.scrollTo(0, 0);
});
- } else {
- setVirtualKeyboardVisible(false);
- document.documentElement.style.setProperty('--dvh', '1dvh');
}
previousViewportHeight = newViewportHeight;
}
@@ -101,14 +100,16 @@ export function useAppSquishHandlers() {
handleResize();
window.visualViewport.addEventListener('scroll', handleResize);
}
- document.addEventListener('focusout', handleFocusOut);
+ document.addEventListener('focusout', handleFocusOut, { capture: true });
onCleanup(() => {
if (window.visualViewport) {
window.visualViewport.removeEventListener('resize', handleResize);
window.visualViewport.removeEventListener('scroll', handleResize);
}
- document.removeEventListener('focusout', handleFocusOut);
+ document.removeEventListener('focusout', handleFocusOut, {
+ capture: true,
+ });
});
});
}
diff --git a/js/app/packages/channel/Channel/create-inline-input-keyboard-handler.ts b/js/app/packages/channel/Channel/create-inline-input-keyboard-handler.ts
index e58ada0cae..919f6e2e3b 100644
--- a/js/app/packages/channel/Channel/create-inline-input-keyboard-handler.ts
+++ b/js/app/packages/channel/Channel/create-inline-input-keyboard-handler.ts
@@ -5,59 +5,35 @@ import {
type Accessor,
type Setter,
} from 'solid-js';
-import { isMobile } from '@core/mobile/isMobile';
import { isNativeMobilePlatform } from '@core/mobile/isNativeMobilePlatform';
-import { virtualKeyboardVisible } from '@core/mobile/virtualKeyboard';
+import {
+ virtualKeyboardHeight,
+ virtualKeyboardVisible,
+} from '@core/mobile/virtualKeyboard';
import { scrollElementAboveKeyboard } from '../scroll-utils';
+import { isPlatform } from '@core/util/platform';
+import { isTouchDevice } from '@core/mobile/isTouchDevice';
const INPUT_CONTAINER_SELECTOR = '[data-inline-input-container-id]';
export function createInlineInputKeyboardHandler(
containerEl: Accessor<HTMLElement | undefined>,
setIsChannelInputHidden: Setter<boolean>
-): void {
- let getActiveInputContainer: (() => HTMLElement | undefined) | undefined;
-
- createEffect(() => {
- if (!isMobile()) return;
- const el = containerEl();
- if (!el) return;
- const result = attachInlineInputKeyboardHandler(
- el,
- setIsChannelInputHidden
- );
- getActiveInputContainer = result.getActiveInputContainer;
- onCleanup(() => {
- result.cleanup();
- getActiveInputContainer = undefined;
- });
- });
+) {
+ let activeInputContainer: HTMLElement | undefined;
- // Mobile web fallback: watch for virtualKeyboardVisible signal, and scroll active input listner into view.
- createEffect(
- on(virtualKeyboardVisible, () => {
- if (!isMobile() || isNativeMobilePlatform()) return;
- if (!virtualKeyboardVisible()) return;
- const container = getActiveInputContainer?.();
- if (container) {
- setTimeout(() => {
- container.scrollIntoView({ block: 'center', behavior: 'smooth' });
- }, 0);
- }
- }),
- { defer: true }
- );
-}
+ const reset = () => {
+ setIsChannelInputHidden(false);
+ activeInputContainer = undefined;
+ };
-function attachInlineInputKeyboardHandler(
- containerEl: HTMLElement,
- setIsChannelInputHidden: Setter<boolean>
-): {
- getActiveInputContainer: () => HTMLElement | undefined;
- cleanup: () => void;
-} {
- let keyboardWillShowHandler: ((e: Event) => void) | undefined;
- let activeInputContainer: HTMLElement | undefined;
+ const keyboardWillShowHandler = (event: Event) => {
+ const height =
+ (event as CustomEvent<{ height: number }>).detail?.height ?? 0;
+ if (activeInputContainer) {
+ scrollElementAboveKeyboard(activeInputContainer, height);
+ }
+ };
const handleFocusIn = (e: FocusEvent) => {
const inputContainer = (e.target as HTMLElement).closest<HTMLElement>(
@@ -66,62 +42,77 @@ function attachInlineInputKeyboardHandler(
if (!inputContainer) return;
activeInputContainer = inputContainer;
+ // HACK: on mobile safari, we need to ensure that the input container is scrolled into view BEFORE we hide the input, and then perform the subsequent scroll. Some sort of weird Safari focus behavior going on.
+ if (!isPlatform('ios')) {
+ activeInputContainer.scrollIntoView({ block: 'end' });
+ }
+
setIsChannelInputHidden(true);
- const currentKeyboardHeight = parseFloat(
- getComputedStyle(document.documentElement).getPropertyValue(
- '--virtual-keyboard-height'
- )
- );
- if (currentKeyboardHeight > 0) {
- scrollElementAboveKeyboard(activeInputContainer, currentKeyboardHeight);
+
+ if (isPlatform('ios')) {
+ const currentKeyboardHeight = virtualKeyboardHeight();
+
+ if (currentKeyboardHeight > 0) {
+ scrollElementAboveKeyboard(activeInputContainer, currentKeyboardHeight);
+ } else {
+ window.addEventListener('keyboardWillShow', keyboardWillShowHandler, {
+ once: true,
+ });
+ }
} else {
- if (keyboardWillShowHandler)
- window.removeEventListener('keyboardWillShow', keyboardWillShowHandler);
- keyboardWillShowHandler = (event: Event) => {
- const height =
- (event as CustomEvent<{ height: number }>).detail?.height ?? 0;
- if (activeInputContainer)
- scrollElementAboveKeyboard(activeInputContainer, height);
- keyboardWillShowHandler = undefined;
- };
- window.addEventListener('keyboardWillShow', keyboardWillShowHandler, {
- once: true,
- });
+ // HACK: on mobile safari need to jettison this scroll out past the layout changes caused by the virtual keyboard appearing
+ setTimeout(() => {
+ if (!activeInputContainer) return;
+ activeInputContainer.scrollIntoView({ block: 'end' });
+ }, 500);
}
};
const handleFocusOut = (e: FocusEvent) => {
- const inputContainer = (e.target as HTMLElement).closest<HTMLElement>(
- INPUT_CONTAINER_SELECTOR
- );
- if (!inputContainer) return;
+ if (!activeInputContainer) return;
const nextInputContainer = (e.relatedTarget as HTMLElement | null)?.closest(
INPUT_CONTAINER_SELECTOR
);
if (!nextInputContainer) {
- setIsChannelInputHidden(false);
- activeInputContainer = undefined;
- if (keyboardWillShowHandler) {
- window.removeEventListener('keyboardWillShow', keyboardWillShowHandler);
- keyboardWillShowHandler = undefined;
- }
+ reset();
}
};
- containerEl.addEventListener('focusin', handleFocusIn);
- containerEl.addEventListener('focusout', handleFocusOut as EventListener);
+ // Attach focus in handler
+ createEffect(
+ on(containerEl, () => {
+ if (!isTouchDevice()) return;
+ const el = containerEl();
+ if (!el) return;
+ el.addEventListener('focusin', handleFocusIn);
+ el.addEventListener('focusout', handleFocusOut);
- return {
- getActiveInputContainer: () => activeInputContainer,
- cleanup: () => {
- containerEl.removeEventListener('focusin', handleFocusIn);
- containerEl.removeEventListener(
- 'focusout',
- handleFocusOut as EventListener
- );
- if (keyboardWillShowHandler) {
- window.removeEventListener('keyboardWillShow', keyboardWillShowHandler);
+ onCleanup(() => {
+ el.removeEventListener('focusin', handleFocusIn);
+ el.removeEventListener('focusout', handleFocusOut);
+ });
+ })
+ );
+
+ createEffect(
+ on(virtualKeyboardVisible, () => {
+ if (!isTouchDevice()) return;
+ if (!virtualKeyboardVisible()) {
+ if (activeInputContainer) {
+ reset();
+ }
+ return;
}
- },
- };
+ // Mobile web only: scroll active input into view when keyboard appears.
+ if (isNativeMobilePlatform()) return;
+ setTimeout(() => {
+ if (!activeInputContainer) return;
+ activeInputContainer.scrollIntoView({
+ block: 'center',
+ behavior: 'smooth',
+ });
+ }, 0);
+ }),
+ { defer: true }
+ );
}