Skip to content

Build your personal knowledge base with Trilium Notes

fix(mobile/ios): stop toolbar menus jumping as the keyboard moves

Elian Doran7395ed6615ff
diff --git a/apps/client/src/widgets/containers/root_container.ts b/apps/client/src/widgets/containers/root_container.ts
index a41b98a541..54c7038f2d 100644
--- a/apps/client/src/widgets/containers/root_container.ts
+++ b/apps/client/src/widgets/containers/root_container.ts
@@ -6,7 +6,7 @@ import { applyCustomFontsFromOptions, hasCustomFontContentChanged } from "../../
import { applyFontsFromOptions } from "../../services/font.js";
import options from "../../services/options.js";
import { applyThemeFromOptions, onEffectiveThemeStyleChange, updateColorSchemeClasses, updateThemeCapabilities } from "../../services/theme.js";
-import utils, { isIOS, isMobile } from "../../services/utils.js";
+import utils, { isIOS, isMobile, isMobileApp } from "../../services/utils.js";
import type BasicWidget from "../basic_widget.js";
import FlexContainer from "./flex_container.js";
@@ -66,7 +66,11 @@ export default class RootContainer extends FlexContainer<BasicWidget> {
this.#setExperimentalFeatures();
this.#initThemeColor();
- return super.render();
+ const $widget = super.render();
+ if (isIOS() && isMobileApp()) {
+ this.#trackLayoutInsets();
+ }
+ return $widget;
}
entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
@@ -122,13 +126,7 @@ export default class RootContainer extends FlexContainer<BasicWidget> {
}
#onMobileResize() {
- // The virtual keyboard never changes the window width, so a width change means the device was
- // rotated (or the window resized) and the stored height no longer describes this orientation.
- // Keeping it would make the shorter landscape window look like an open keyboard (#10835).
- if (window.innerWidth !== this.baselineWindowWidth) {
- this.baselineWindowWidth = window.innerWidth;
- this.baselineWindowHeight = window.innerHeight;
- }
+ this.#syncBaseline();
const viewportHeight = window.visualViewport?.height ?? window.innerHeight;
const windowHeight = Math.max(window.innerHeight, this.baselineWindowHeight); // inner height changes when keyboard is opened, we need to compare with the original height to detect it.
@@ -144,6 +142,38 @@ export default class RootContainer extends FlexContainer<BasicWidget> {
}
}
+ #syncBaseline() {
+ // The virtual keyboard never changes the window width, so a width change means the device was
+ // rotated (or the window resized) and the stored height no longer describes this orientation.
+ // Keeping it would make the shorter landscape window look like an open keyboard (#10835).
+ if (window.innerWidth !== this.baselineWindowWidth) {
+ this.baselineWindowWidth = window.innerWidth;
+ this.baselineWindowHeight = window.innerHeight;
+ }
+ }
+
+ /**
+ * In the iOS app, `Keyboard.resize: "native"` resizes the web view for the keyboard, and the
+ * layout follows `innerHeight` only some time later. `--tn-layout-shrink` is how much shorter
+ * than the window the layout is, and `--tn-layout-overhang` how far it reaches below the visible
+ * bottom, so `mobile_editor_toolbar.css` places the toolbar and its menus for the layout as it
+ * stands.
+ */
+ #trackLayoutInsets() {
+ const root = this.$widget[0];
+ const style = document.body.style;
+ const update = () => {
+ this.#syncBaseline();
+ const height = root.getBoundingClientRect().height;
+ const windowHeight = Math.max(window.innerHeight, this.baselineWindowHeight);
+ style.setProperty("--tn-layout-shrink", `${Math.max(0, Math.round(windowHeight - height))}px`);
+ style.setProperty("--tn-layout-overhang", `${Math.max(0, Math.round(height - window.innerHeight))}px`);
+ };
+ update();
+ new ResizeObserver(update).observe(root);
+ window.visualViewport?.addEventListener("resize", update);
+ }
+
#setMaxContentWidth() {
const width = Math.max(options.getInt("maxContentWidth") || 0, 640);
document.body.style.setProperty("--preferred-max-content-width", `${width}px`);
diff --git a/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.css b/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.css
index 281c892f6e..ef7586ca2c 100644
--- a/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.css
+++ b/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.css
@@ -27,7 +27,7 @@ body.mobile {
.classic-toolbar-island {
position: absolute;
- bottom: 0;
+ bottom: var(--tn-layout-overhang, 0px);
inset-inline-start: 0;
inset-inline-end: 0;
height: var(--mobile-toolbar-height);
@@ -83,9 +83,12 @@ body.mobile {
/* A menu of the toolbar in the place of the keyboard (see `useKeyboardPanel`). */
&.keyboard-panel-open {
+ --keyboard-panel-lift: max(
+ 0px,
+ calc(var(--tn-keyboard-height, 0px) - var(--tn-layout-shrink, 0px))
+ );
--mobile-toolbar-clearance: calc(
- var(--mobile-toolbar-height) + var(--mobile-toolbar-gap)
- + var(--tn-keyboard-height, 0px)
+ var(--mobile-toolbar-height) + var(--mobile-toolbar-gap) + var(--keyboard-panel-lift)
);
#mobile-bottom-bar {
@@ -93,7 +96,7 @@ body.mobile {
}
.classic-toolbar-island {
- bottom: var(--tn-keyboard-height, 0px);
+ bottom: var(--keyboard-panel-lift);
margin-bottom: 0;
border-end-start-radius: 0;
border-end-end-radius: 0;
@@ -110,6 +113,7 @@ body.mobile {
}
.dropdown-menu.mobile-bottom-menu.keyboard-panel.show {
+ --dropdown-bottom: calc(-1 * var(--tn-layout-shrink, 0px));
height: var(--tn-keyboard-height);
max-height: var(--tn-keyboard-height);
}