Skip to content

Build your personal knowledge base with Trilium Notes

fix(mobile/ios): keep the text toolbar above the keyboard in the app

Elian Doraneee92f4a4705
diff --git a/apps/client/src/widgets/mobile_widgets/hide_keyboard_button.spec.tsx b/apps/client/src/widgets/mobile_widgets/hide_keyboard_button.spec.tsx
index 3bdf935a04..a9b80ff1f3 100644
--- a/apps/client/src/widgets/mobile_widgets/hide_keyboard_button.spec.tsx
+++ b/apps/client/src/widgets/mobile_widgets/hide_keyboard_button.spec.tsx
@@ -9,9 +9,11 @@ vi.mock("../react/hooks", async (importOriginal) => ({
...(await importOriginal<typeof import("../react/hooks")>()),
useStaticTooltip: vi.fn()
}));
+const platform = vi.hoisted(() => ({ isMobileApp: false }));
vi.mock("../../services/utils", async (importOriginal) => ({
...(await importOriginal<typeof import("../../services/utils")>()),
- isIOS: () => true
+ isIOS: () => true,
+ isMobileApp: () => platform.isMobileApp
}));
import HideKeyboardButton, {
@@ -71,6 +73,7 @@ describe("usePositioningOniOS", () => {
const originalScrollY = Object.getOwnPropertyDescriptor(window, "scrollY");
afterEach(() => {
+ platform.isMobileApp = false;
for (const [ key, descriptor ] of [
[ "visualViewport", originalViewport ],
[ "innerHeight", originalInnerHeight ],
@@ -160,6 +163,19 @@ describe("usePositioningOniOS", () => {
act(() => holdVisibleBottom(false));
expect(element().style.bottom).toBe("");
});
+
+ it("leaves the element to CSS in the iOS app, which resizes the page for the keyboard", () => {
+ // As logged in the app: `visualViewport` and `innerHeight` shrink to 549px before the
+ // layout does, so the anchor still ends at 896px as the resize event fires.
+ platform.isMobileApp = true;
+ const viewport = fakeViewport(896);
+ const { element, anchor } = renderPositioned(true);
+ anchor().getBoundingClientRect = () => new DOMRect(0, 0, 414, 896);
+ Object.defineProperty(window, "innerHeight", { configurable: true, value: 549 });
+ act(() => viewport.set({ height: 549, offsetTop: 0 }, "resize"));
+ expect(element().style.bottom).toBe("");
+ expect(viewport.listenerCount()).toBe(0);
+ });
});
function fakeViewport(innerHeight: number) {
diff --git a/apps/client/src/widgets/mobile_widgets/hide_keyboard_button.tsx b/apps/client/src/widgets/mobile_widgets/hide_keyboard_button.tsx
index 3f329084c9..f800448e2a 100644
--- a/apps/client/src/widgets/mobile_widgets/hide_keyboard_button.tsx
+++ b/apps/client/src/widgets/mobile_widgets/hide_keyboard_button.tsx
@@ -5,7 +5,7 @@ import { RefObject } from "preact";
import { useCallback, useEffect, useRef } from "preact/hooks";
import { t } from "../../services/i18n";
-import { isIOS } from "../../services/utils";
+import { isIOS, isMobileApp } from "../../services/utils";
import ActionButton from "../react/ActionButton";
/**
@@ -70,9 +70,7 @@ export function usePositioningOniOS(
// Safari shrinks `visualViewport` for the keyboard but not the layout, so the element rises by
// how far its anchor reaches below the visible bottom. With the keyboard up Safari scrolls the
// fixed page itself, and client coordinates move with that scroll, so the visible bottom is
- // `pageTop - scrollY + height`: `offsetTop + height` elsewhere, `height` in Safari. Under
- // Capacitor `resize: native` the layout shrinks too, so `obscured` is 0 and CSS stays in
- // charge.
+ // `pageTop - scrollY + height`: `offsetTop + height` elsewhere, `height` in Safari.
const adjustPosition = useCallback(() => {
if (!wrapperRef.current) return;
const viewport = window.visualViewport;
@@ -91,7 +89,9 @@ export function usePositioningOniOS(
}, [ wrapperRef ]);
useEffect(() => {
- if (!isIOS()) return;
+ // Under Capacitor `resize: native` the layout shrinks for the keyboard, so CSS places the
+ // element. `visualViewport` shrinks before the layout does, which would lift it twice.
+ if (!isIOS() || isMobileApp()) return;
// Run once in case the keyboard is already up when mounting
// (e.g. returning from a dialog).
adjustPosition();