Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(mobile/text): keep the toolbar above its menus with no keyboard
Elian Doranba911759d7db
diff --git a/apps/client/src/widgets/type_widgets/text/color_menu.tsx b/apps/client/src/widgets/type_widgets/text/color_menu.tsxindex 6f03c6ea00..5b41549306 100644--- a/apps/client/src/widgets/type_widgets/text/color_menu.tsx+++ b/apps/client/src/widgets/type_widgets/text/color_menu.tsx@@ -1,6 +1,7 @@import type { ColorMenuHost, ColorMenuRequest, ColorMenuTarget } from "@triliumnext/ckeditor5";import clsx from "clsx";import { render } from "preact";+import { useCallback } from "preact/hooks";import { t } from "../../../services/i18n";import { isMobile, isNarrowLayout } from "../../../services/utils";@@ -49,7 +50,11 @@ function ColorMenu({ request, onClose }: {onClose(returnFocus: boolean): void;}) {const bottomSheet = isMobile() && isNarrowLayout();- const { isPanel, replacesPanel } = useKeyboardPanel(bottomSheet);+ const { isPanel, replacesPanel, isOwnHeight, measureRef } = useKeyboardPanel(bottomSheet);+ const setElement = useCallback((element: HTMLElement | null) => {+ request.setElement(element);+ measureRef(element);+ }, [ request, measureRef ]);useClosedByToolbarTap(request.anchor, () => onClose(false));return (@@ -58,11 +63,12 @@ function ColorMenu({ request, onClose }: {offset={ANCHOR_GAP}portalClassName="tn-dropdown-portal"className={clsx(- "tn-dropdown-menu", isPanel && "keyboard-panel", replacesPanel && "replaces-panel"+ "tn-dropdown-menu", isPanel && "keyboard-panel",+ isOwnHeight && "keyboard-panel-own-height", replacesPanel && "replaces-panel")}bottomSheet={bottomSheet}keepOpenSelector={MOBILE_TOOLBAR_ROW}- elementRef={request.setElement}+ elementRef={setElement}onDismiss={() => onClose(false)}onClose={() => onClose(true)}>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.cssindex db1dadbc5d..3ade379fe6 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@@ -114,6 +114,12 @@ body.mobile {max-height: var(--tn-keyboard-height);}+ /* With no keyboard up, the menu sets `--tn-keyboard-height` from its own height. */+ .dropdown-menu.mobile-bottom-menu.keyboard-panel.keyboard-panel-own-height.show {+ height: auto;+ max-height: 50vh;+ }+.dropdown-menu.mobile-bottom-menu.keyboard-panel.replaces-panel[data-reveal]:not([data-closing]) {animation: none;}@@ -121,8 +127,16 @@ body.mobile {/* `playClosing()` in Popup.tsx removes the copy once this animation ends or is replaced, asit is when `keyboard-panel-open` goes; 1.5s covers `PRESS_TIMEOUT` plus`KEYBOARD_RETURN_TIMEOUT` in mobile_editor_toolbar.tsx. */- &.keyboard-panel-open .dropdown-menu.mobile-bottom-menu.keyboard-panel[data-closing] {- animation: tn-keyboard-panel-hold 1.5s;+ &.keyboard-panel-open .dropdown-menu.mobile-bottom-menu.keyboard-panel {+ &[data-closing] {+ animation: tn-keyboard-panel-hold 1.5s;+ }++ /* No keyboard comes back under a menu of its own height, and a taller one held here+ covers the toolbar of the next; with no animation, `playClosing()` drops the copy. */+ &.keyboard-panel-own-height[data-closing] {+ animation: none;+ }}&.keyboard-panel-open .classic-toolbar-island,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.tsxindex 5c583ddc8a..ee76977e67 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@@ -62,10 +62,13 @@ describe("useKeyboardPanel", () => {return null;}+ // Without a keyboard, the menu takes a place of its own height.act(() => render(<Harness enabled />, host));- expect(seen.at(-1)?.isPanel).toBe(false);+ expect(seen.at(-1)).toMatchObject({ isPanel: true, isOwnHeight: true });+ expect(isMarked()).toBe(true);+ await act(async () => render(null, host));+ await endPress();expect(isMarked()).toBe(false);- act(() => render(null, host));root.classList.add("virtual-keyboard-opened");act(() => render(<Harness enabled={false} />, host));@@ -74,7 +77,7 @@ describe("useKeyboardPanel", () => {act(() => render(null, host));act(() => render(<Harness enabled />, host));- expect(seen.at(-1)?.isPanel).toBe(true);+ expect(seen.at(-1)).toMatchObject({ isPanel: true, isOwnHeight: false });expect(isMarked()).toBe(true);// The keyboard closes as the menu takes focus, and the menu keeps its place.@@ -88,6 +91,30 @@ describe("useKeyboardPanel", () => {expect(isMarked()).toBe(false);});+ it("sizes the place by the menu with no keyboard, giving it back as the menu closes", async () => {+ const { editable, host } = buildPanelFixture();+ document.body.style.setProperty("--tn-keyboard-height", "300px");+ const height = vi.spyOn(HTMLElement.prototype, "offsetHeight", "get").mockReturnValue(240);+ function Menu() {+ const { measureRef } = useKeyboardPanel(true);+ return <div ref={measureRef} />;+ }++ act(() => render(<Menu />, host));+ expect(isKeyboardPanelOpen()).toBe(true);+ expect(document.body.style.getPropertyValue("--tn-keyboard-height")).toBe("240px");++ // An entry that runs focuses the text, and no keyboard comes up to take the place over.+ await act(async () => {+ render(null, host);+ editable.focus();+ });+ expect(isKeyboardPanelOpen()).toBe(false);+ expect(document.body.style.getPropertyValue("--tn-keyboard-height")).toBe("300px");+ height.mockRestore();+ document.body.style.removeProperty("--tn-keyboard-height");+ });+it("keeps the place for a keyboard that comes back, until it is up", async () => {const { root, editable, host, Harness, seen } = buildPanelFixture();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.tsxindex 5fde7c8da9..dab5fd6bec 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@@ -158,27 +158,45 @@ function useActiveToolbarElement(editor: CKTextEditor | undefined, pluginName: "}/**- * Whether a menu of the formatting toolbar opens in the place of the keyboard, as it does when the- * keyboard is up as it opens: the keyboard closes as the menu takes focus, and the menu fills its- * `--tn-keyboard-height`. While such a menu is mounted, `keyboard-panel-open` on the body keeps- * the layout of an open keyboard. `replacesPanel` is set for a menu that opens as another one- * leaves the place, which it takes over without a reveal.+ * Whether a menu of the formatting toolbar opens in the place of the keyboard: the keyboard closes+ * as the menu takes focus, and the menu fills its `--tn-keyboard-height`. While such a menu is+ * mounted, `keyboard-panel-open` on the body keeps the layout of an open keyboard, with the+ * toolbar on top of the menu. `replacesPanel` is set for a menu that opens as another one leaves+ * 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 while it is open.*/export function useKeyboardPanel(enabled: boolean) {- const [ { isPanel, replacesPanel } ] = useState(() => {+ const [ { isPanel, replacesPanel, isOwnHeight } ] = useState(() => {const root = document.getElementById("root-widget");const isKeyboardUp = !!root?.classList.contains("virtual-keyboard-opened");- const isPanel = enabled && (isKeyboardUp || isKeyboardPanelOpen());- return { isPanel, replacesPanel: isPanel && !!pendingHandBack };+ // A menu taking over the place of a closed one keeps the way it was sized.+ const isOwnHeight = enabled && (isKeyboardPanelOpen() ? hasOwnHeightPlace : !isKeyboardUp);+ return { isPanel: enabled, replacesPanel: enabled && !!pendingHandBack, isOwnHeight };});useLayoutEffect(() => {if (!isPanel) return;- takeKeyboardPlace();+ takeKeyboardPlace(isOwnHeight);return handBackKeyboardPlace;}, [ isPanel ]);- return { isPanel, replacesPanel };+ // 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;++ const measure = () => {+ document.body.style.setProperty("--tn-keyboard-height", `${element.offsetHeight}px`);+ };+ measure();+ const observer = new ResizeObserver(measure);+ observer.observe(element);+ return () => observer.disconnect();+ }, [ element, isOwnHeight ]);++ return { isPanel, replacesPanel, isOwnHeight, measureRef };}/** The row of buttons of the phone's toolbar, which scrolls sideways under a menu of it. */@@ -237,6 +255,10 @@ const PRESS_TIMEOUT = 500;/** The hand-back of a closed panel's place, while it waits for the keyboard. */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 = "";/** Called as `keyboard-panel-open` is set on the body or cleared. */const keyboardPanelListeners = new Set<() => void>();@@ -244,12 +266,16 @@ const keyboardPanelListeners = new Set<() => void>();* Lays the page out as for an open keyboard, or keeps the layout a closed panel still holds for* the keyboard, with the visible bottom it was taken at.*/-function takeKeyboardPlace() {+function takeKeyboardPlace(isOwnHeight: boolean) {if (pendingHandBack) {pendingHandBack.cancel();pendingHandBack = undefined;return;}+ hasOwnHeightPlace = isOwnHeight;+ if (isOwnHeight) {+ keyboardHeight = document.body.style.getPropertyValue("--tn-keyboard-height");+ }setKeyboardPanelOpen(true);holdVisibleBottom(true);}@@ -276,6 +302,10 @@ function handBackKeyboardPlace() {const release = () => {stopWaiting();if (pendingHandBack === handBack) pendingHandBack = undefined;+ if (hasOwnHeightPlace) {+ hasOwnHeightPlace = false;+ document.body.style.setProperty("--tn-keyboard-height", keyboardHeight);+ }setKeyboardPanelOpen(false);holdVisibleBottom(false);};@@ -296,9 +326,10 @@ function handBackKeyboardPlace() {clearTimeout(timer);};};+ // Only a keyboard that was up as the place was taken comes back for it.const settle = () => {if (isCancelled) return;- if (isTextFocused()) {+ if (isTextFocused() && !hasOwnHeightPlace) {waitForKeyboard();} else {release();@@ -309,7 +340,11 @@ function handBackKeyboardPlace() {queueMicrotask(() => {if (isCancelled) return;if (isTextFocused()) {- waitForKeyboard();+ if (hasOwnHeightPlace) {+ release();+ } else {+ waitForKeyboard();+ }return;}// What the click runs, such as opening another menu, runs before `settle`.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.tsxindex ec1e341591..54a0dd78d1 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@@ -52,6 +52,31 @@ describe("createToolbarGroupMenuHost", () => {element.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, button: 0 }));};+ it("takes a place of its own height under the toolbar on a phone with no keyboard", async () => {+ window.glob.device = "mobile";+ const narrow = vi.spyOn(window, "matchMedia").mockImplementation((query) => ({+ matches: true, media: query, onchange: null, dispatchEvent: () => true,+ addEventListener() {}, removeEventListener() {}, addListener() {}, removeListener() {}+ }));+ const height = vi.spyOn(HTMLElement.prototype, "offsetHeight", "get").mockReturnValue(320);+ try {+ const { menu, setElement } = show();+ expect(setElement).toHaveBeenCalledWith(menu);+ expect([ ...menu.classList ]).toEqual(expect.arrayContaining([+ "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.classList).toContain("keyboard-panel-open");+ } finally {+ host.hide();+ window.glob.device = "desktop";+ narrow.mockRestore();+ height.mockRestore();+ }+ });+it("draws the entries as the app's menu, with their SVG icons, and hands the editor its element", () => {const { menu, setElement } = show();diff --git a/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.tsx b/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.tsxindex b5206347b1..479aa42468 100644--- a/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.tsx+++ b/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.tsx@@ -5,6 +5,7 @@ import type {} from "@triliumnext/ckeditor5";import clsx from "clsx";import { Fragment, render } from "preact";+import { useCallback } from "preact/hooks";import { escapeHtml, isMobile, isNarrowLayout } from "../../../services/utils";import {@@ -52,7 +53,11 @@ function ToolbarGroupMenu({ request, onClose }: {onClose(returnFocus: boolean): void;}) {const bottomSheet = isMobile() && isNarrowLayout();- const { isPanel, replacesPanel } = useKeyboardPanel(bottomSheet);+ const { isPanel, replacesPanel, isOwnHeight, measureRef } = useKeyboardPanel(bottomSheet);+ const setElement = useCallback((element: HTMLElement | null) => {+ request.setElement(element);+ measureRef(element);+ }, [ request, measureRef ]);useClosedByToolbarTap(request.anchor, () => onClose(false));return (@@ -61,12 +66,13 @@ function ToolbarGroupMenu({ request, onClose }: {offset={ANCHOR_GAP}portalClassName="tn-dropdown-portal"className={clsx(- "tn-dropdown-menu", isPanel && "keyboard-panel", replacesPanel && "replaces-panel"+ "tn-dropdown-menu", isPanel && "keyboard-panel",+ isOwnHeight && "keyboard-panel-own-height", replacesPanel && "replaces-panel")}bottomSheet={bottomSheet}keepOpenSelector={MOBILE_TOOLBAR_ROW}aria-labelledby={request.anchor.getAttribute("aria-labelledby") ?? undefined}- elementRef={request.setElement}+ elementRef={setElement}onDismiss={() => onClose(false)}onClose={() => onClose(true)}>