Skip to content

Build your personal knowledge base with Trilium Notes

fix(text): cap a toolbar menu at the keyboard's height

Elian Doranc23e9882223b
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 41a6f0b445..1a35fe7103 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
@@ -85,7 +85,10 @@ body.mobile {
&.keyboard-panel-open {
--keyboard-panel-lift: max(
0px,
- calc(var(--tn-keyboard-height, 0px) - var(--tn-layout-shrink, 0px))
+ calc(
+ var(--tn-keyboard-place-height, var(--tn-keyboard-height, 0px))
+ - var(--tn-layout-shrink, 0px)
+ )
);
--mobile-toolbar-clearance: calc(
var(--mobile-toolbar-height) + var(--mobile-toolbar-gap) + var(--keyboard-panel-lift)
@@ -119,7 +122,7 @@ body.mobile {
.dropdown-menu.mobile-bottom-menu.keyboard-panel.keyboard-panel-own-height.show {
height: auto;
- max-height: 50vh;
+ max-height: min(50vh, calc(var(--tn-keyboard-height, 50vh) + var(--mobile-toolbar-gap)));
}
.dropdown-menu.mobile-bottom-menu.keyboard-panel.replaces-panel[data-reveal]:not([data-closing]) {
diff --git a/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.spec.tsx b/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.spec.tsx
index 850625c1dd..8854b568ce 100644
--- a/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.spec.tsx
@@ -101,10 +101,14 @@ describe("useKeyboardPanel", () => {
return <div ref={measureRef} />;
}
- // The menu rises by the toolbar's gap above the place, to meet the toolbar.
+ const placeHeight = () => document.body.style.getPropertyValue("--tn-keyboard-place-height");
+
+ // The menu rises by the toolbar's gap above the place, to meet the toolbar. The keyboard's
+ // own height stays, for the stylesheet to cap the menu at.
act(() => render(<Menu />, host));
expect(isKeyboardPanelOpen()).toBe(true);
- expect(document.body.style.getPropertyValue("--tn-keyboard-height")).toBe("234px");
+ expect(placeHeight()).toBe("234px");
+ expect(document.body.style.getPropertyValue("--tn-keyboard-height")).toBe("300px");
document.body.style.removeProperty("--mobile-toolbar-gap");
// An entry that runs focuses the text, and no keyboard comes up to take the place over.
@@ -113,6 +117,7 @@ describe("useKeyboardPanel", () => {
editable.focus();
});
expect(isKeyboardPanelOpen()).toBe(false);
+ expect(placeHeight()).toBe("");
expect(document.body.style.getPropertyValue("--tn-keyboard-height")).toBe("300px");
height.mockRestore();
document.body.style.removeProperty("--tn-keyboard-height");
diff --git a/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.tsx b/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.tsx
index f615b09a4d..be7b12952f 100644
--- a/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.tsx
+++ b/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.tsx
@@ -165,7 +165,8 @@ function useActiveToolbarElement(editor: CKTextEditor | undefined, pluginName: "
* the place, which it takes over without a reveal.
*
* With no keyboard up, the menu takes a place of its own height (`isOwnHeight`): `measureRef` on
- * its element sets `--tn-keyboard-height` to that height, less the toolbar's gap, while it is open.
+ * its element sets `--tn-keyboard-place-height` to that height, less the toolbar's gap, while it is
+ * open. The stylesheet caps such a menu at the keyboard's `--tn-keyboard-height`.
*/
export function useKeyboardPanel(enabled: boolean) {
const [ { isPanel, replacesPanel, isOwnHeight } ] = useState(() => {
@@ -182,7 +183,6 @@ export function useKeyboardPanel(enabled: boolean) {
return handBackKeyboardPlace;
}, [ isPanel ]);
- // Measured once the place is taken, which saves the keyboard's height first.
const [ element, measureRef ] = useState<HTMLElement | null>(null);
useLayoutEffect(() => {
if (!element || !isOwnHeight) return;
@@ -190,7 +190,7 @@ export function useKeyboardPanel(enabled: boolean) {
// The menu rises by `--mobile-toolbar-gap` above the place, to meet the toolbar.
const gap = parseFloat(getComputedStyle(document.body).getPropertyValue("--mobile-toolbar-gap")) || 0;
const measure = () => {
- document.body.style.setProperty("--tn-keyboard-height", `${element.offsetHeight - gap}px`);
+ document.body.style.setProperty("--tn-keyboard-place-height", `${element.offsetHeight - gap}px`);
};
measure();
const observer = new ResizeObserver(measure);
@@ -265,8 +265,6 @@ const PRESS_TIMEOUT = 500;
let pendingHandBack: { cancel(): void } | undefined;
/** Whether the place was taken with no keyboard up, sized by the menu in it. */
let hasOwnHeightPlace = false;
-/** The `--tn-keyboard-height` of the keyboard, which a place of the menu's own height covers. */
-let keyboardHeight = "";
/** Whether `useClosedByToolbarTap` is closing a menu from within the click of its press. */
let isClosingInClick = false;
/** Called as `keyboard-panel-open` is set on the body or cleared. */
@@ -283,9 +281,6 @@ function takeKeyboardPlace(isOwnHeight: boolean) {
return;
}
hasOwnHeightPlace = isOwnHeight;
- if (isOwnHeight) {
- keyboardHeight = document.body.style.getPropertyValue("--tn-keyboard-height");
- }
setKeyboardPanelOpen(true);
holdVisibleBottom(true);
}
@@ -315,7 +310,7 @@ function handBackKeyboardPlace() {
if (pendingHandBack === handBack) pendingHandBack = undefined;
if (hasOwnHeightPlace) {
hasOwnHeightPlace = false;
- document.body.style.setProperty("--tn-keyboard-height", keyboardHeight);
+ document.body.style.removeProperty("--tn-keyboard-place-height");
}
setKeyboardPanelOpen(false);
holdVisibleBottom(false);
diff --git a/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.spec.tsx b/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.spec.tsx
index 0051f1db98..8ee0372b2b 100644
--- a/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.spec.tsx
@@ -66,7 +66,7 @@ describe("createToolbarGroupMenuHost", () => {
"mobile-bottom-menu", "keyboard-panel", "keyboard-panel-own-height"
]));
await vi.waitFor(() => {
- expect(document.body.style.getPropertyValue("--tn-keyboard-height")).toBe("320px");
+ expect(document.body.style.getPropertyValue("--tn-keyboard-place-height")).toBe("320px");
});
expect(document.body.classList).toContain("keyboard-panel-open");
} finally {