Skip to content

Build your personal knowledge base with Trilium Notes

feat(mobile/text): close a toolbar menu with an X in its place

Elian Doran7e62be955c6d
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 508a2baef7..69449ae7b7 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -4837,6 +4837,7 @@
"default_parent_placeholder": "Where they are created"
},
"hide_keyboard_button": {
- "title": "Hide keyboard"
+ "title": "Hide keyboard",
+ "close_menu": "Close menu"
}
}
diff --git a/apps/client/src/widgets/mobile_widgets/hide_keyboard_button.css b/apps/client/src/widgets/mobile_widgets/hide_keyboard_button.css
index e667604513..2906362563 100644
--- a/apps/client/src/widgets/mobile_widgets/hide_keyboard_button.css
+++ b/apps/client/src/widgets/mobile_widgets/hide_keyboard_button.css
@@ -27,6 +27,10 @@ body.mobile :is(.hide-keyboard-button, .hide-keyboard-action) {
}
}
+body.mobile .hide-keyboard-action.closes-menu .icon-action::after {
+ content: none;
+}
+
body.mobile .virtual-keyboard-opened :is(.hide-keyboard-button, .hide-keyboard-action),
body.mobile.keyboard-panel-open .hide-keyboard-action {
display: flex;
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 6c9e10032d..3bdf935a04 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
@@ -14,7 +14,9 @@ vi.mock("../../services/utils", async (importOriginal) => ({
isIOS: () => true
}));
-import HideKeyboardButton, { holdVisibleBottom, usePositioningOniOS } from "./hide_keyboard_button";
+import HideKeyboardButton, {
+ HideKeyboardAction, holdVisibleBottom, usePositioningOniOS
+} from "./hide_keyboard_button";
describe("HideKeyboardButton", () => {
let input: HTMLInputElement | undefined;
@@ -41,6 +43,26 @@ describe("HideKeyboardButton", () => {
act(() => button?.click());
expect(document.activeElement).not.toBe(input);
});
+
+ it("closes the menu instead, as an X, while it is given one to close", () => {
+ input = document.createElement("input");
+ document.body.append(input);
+ const closeMenu = vi.fn();
+ let container: HTMLElement | undefined;
+ act(() => {
+ container = renderInto(<HideKeyboardAction className="action" closeMenu={closeMenu} />);
+ });
+
+ const button = container?.querySelector("button");
+ expect(button).toBeInstanceOf(HTMLButtonElement);
+ expect(button?.classList.contains("bx-x")).toBe(true);
+ expect(container?.querySelector(".action")?.classList.contains("closes-menu")).toBe(true);
+
+ act(() => input?.focus());
+ act(() => button?.click());
+ expect(closeMenu).toHaveBeenCalledTimes(1);
+ expect(document.activeElement).toBe(input);
+ });
});
describe("usePositioningOniOS", () => {
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 6d70ab1d8b..020535d613 100644
--- a/apps/client/src/widgets/mobile_widgets/hide_keyboard_button.tsx
+++ b/apps/client/src/widgets/mobile_widgets/hide_keyboard_button.tsx
@@ -1,5 +1,6 @@
import "./hide_keyboard_button.css";
+import clsx from "clsx";
import { RefObject } from "preact";
import { useCallback, useEffect, useRef } from "preact/hooks";
@@ -22,21 +23,34 @@ export default function HideKeyboardButton() {
return <HideKeyboardAction className="hide-keyboard-button" containerRef={containerRef} />;
}
-/** The button alone, which `MobileEditorToolbar` places at the end of its island. */
-export function HideKeyboardAction({ className, containerRef }: {
+/**
+ * The button alone, which `MobileEditorToolbar` places at the end of its island. Given
+ * `closeMenu`, as while a menu of the toolbar is in the keyboard's place, it is an X that calls
+ * `closeMenu` instead of hiding the keyboard.
+ */
+export function HideKeyboardAction({ className, containerRef, closeMenu }: {
className: string;
containerRef?: RefObject<HTMLDivElement | null>;
+ closeMenu?: () => void;
}) {
return (
// Keeps the press from moving the focus onto the button: an open dialog's focus trap would
// send it back into the dialog and bring the keyboard up again.
- <div ref={containerRef} className={className} onMouseDown={(e) => e.preventDefault()}>
+ <div
+ ref={containerRef}
+ className={clsx(className, closeMenu && "closes-menu")}
+ onMouseDown={(e) => e.preventDefault()}
+ >
<ActionButton
- icon="bx bxs-keyboard"
- text={t("hide_keyboard_button.title")}
+ icon={closeMenu ? "bx bx-x" : "bx bxs-keyboard"}
+ text={closeMenu
+ ? t("hide_keyboard_button.close_menu")
+ : t("hide_keyboard_button.title")}
noTooltipOnTouch
onClick={() => {
- if (document.activeElement instanceof HTMLElement) {
+ if (closeMenu) {
+ closeMenu();
+ } else if (document.activeElement instanceof HTMLElement) {
document.activeElement.blur();
}
}}
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 f28c7647cf..5c933991a2 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
@@ -4,7 +4,8 @@ import { act } from "preact/test-utils";
import { afterEach, describe, expect, it, vi } from "vitest";
import {
- isKeyboardPanelOpen, useEditorsFocused, useKeyboardPanel, useShownToolbarElement
+ isKeyboardPanelOpen, useEditorsFocused, useIsKeyboardPanelOpen, useKeyboardPanel,
+ useShownToolbarElement
} from "./mobile_editor_toolbar";
describe("useEditorsFocused", () => {
@@ -177,6 +178,30 @@ describe("useKeyboardPanel", () => {
act(() => render(null, focusHost));
});
+ it("tells whether a panel holds the place, until it is given back", async () => {
+ const { root, host, Harness } = buildPanelFixture();
+ const seen: boolean[] = [];
+ const probeHost = document.createElement("div");
+ function Probe() {
+ seen.push(useIsKeyboardPanelOpen());
+ return null;
+ }
+ act(() => render(<Probe />, probeHost));
+ expect(seen.at(-1)).toBe(false);
+
+ root.classList.add("virtual-keyboard-opened");
+ act(() => render(<Harness />, host));
+ expect(seen.at(-1)).toBe(true);
+ root.classList.remove("virtual-keyboard-opened");
+
+ // The press on the X closes the menu as it starts, and its click still finds it open.
+ await act(async () => render(null, host));
+ expect(seen.at(-1)).toBe(true);
+ await endPress();
+ expect(seen.at(-1)).toBe(false);
+ act(() => render(null, probeHost));
+ });
+
it("gives the place up after its press when focus goes elsewhere, or after 1s", async () => {
const { root, editable, host, Harness } = 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.tsx
index d9124c7fc1..388749046e 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
@@ -31,6 +31,9 @@ export default function MobileEditorToolbar() {
const editor = nestedEditor?.ui.view.toolbar ? nestedEditor : noteEditor;
const toolbarElement = useShownToolbarElement(editor);
const isActive = useEditorsFocused(noteEditor, nestedEditor) || dropdownActive;
+ // A press outside the menu closes it as it starts, so the X's click only brings the keyboard
+ // back, focusing the text within the tap.
+ const isPanelOpen = useIsKeyboardPanelOpen();
usePositioningOniOS(islandRef, outerRef);
@@ -83,7 +86,10 @@ export default function MobileEditorToolbar() {
>
<div ref={islandRef} className="classic-toolbar-island">
<div ref={containerRef} className={`classic-toolbar-widget ${dropdownActive ? "dropdown-active" : ""}`} />
- <HideKeyboardAction className="hide-keyboard-action" />
+ <HideKeyboardAction
+ className="hide-keyboard-action"
+ closeMenu={isPanelOpen ? () => editor?.editing.view.focus() : undefined}
+ />
</div>
</div>
);
@@ -178,6 +184,22 @@ export function isKeyboardPanelOpen() {
return document.body.classList.contains("keyboard-panel-open");
}
+/** {@link isKeyboardPanelOpen}, followed as the place is taken and given back. */
+export function useIsKeyboardPanelOpen() {
+ const [ isOpen, setIsOpen ] = useState(isKeyboardPanelOpen);
+
+ useEffect(() => {
+ const update = () => setIsOpen(isKeyboardPanelOpen());
+ update();
+ keyboardPanelListeners.add(update);
+ return () => {
+ keyboardPanelListeners.delete(update);
+ };
+ }, []);
+
+ return isOpen;
+}
+
/** How long a closed panel keeps the keyboard's place for a keyboard that does not come back. */
const KEYBOARD_RETURN_TIMEOUT = 1000;
/** How long a closed panel waits for the click of the press that closed it, which can scroll. */