Skip to content

Build your personal knowledge base with Trilium Notes

feat(mobile): show the hide-keyboard button on every platform

Elian Doran4821624b99b8
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 2e3968aa0a..b37b59ba5c 100644
--- a/apps/client/src/widgets/mobile_widgets/hide_keyboard_button.css
+++ b/apps/client/src/widgets/mobile_widgets/hide_keyboard_button.css
@@ -2,32 +2,42 @@
--hide-keyboard-button-size: 38px;
}
+body.mobile :is(.hide-keyboard-button, .hide-keyboard-action) {
+ display: none;
+
+ .icon-action {
+ --icon-button-size: var(--hide-keyboard-button-size);
+ width: var(--hide-keyboard-button-size);
+ height: var(--hide-keyboard-button-size);
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ }
+}
+
+body.mobile .virtual-keyboard-opened :is(.hide-keyboard-button, .hide-keyboard-action) {
+ display: flex;
+}
+
body.mobile .hide-keyboard-button {
contain: none;
position: fixed;
bottom: 0;
inset-inline-end: 0;
+ margin-inline-end: var(--mobile-toolbar-gap);
+ margin-bottom: var(--mobile-toolbar-gap);
/* Above every dialog (2000, plus 10 per stacked one in `dialog.ts`) and menu,
below tooltips (9999). */
z-index: 5000;
transform: translateY(var(--tn-keyboard-gap, 0px));
.icon-action {
- --icon-button-size: var(--hide-keyboard-button-size);
- width: var(--hide-keyboard-button-size);
- height: var(--hide-keyboard-button-size);
- display: flex;
- align-items: center;
- justify-content: center;
border: 1px solid var(--main-border-color);
- border-inline-end: none;
- border-bottom: none;
- border-radius: 0;
- border-start-start-radius: 8px;
+ border-radius: 6px;
background-color: var(--main-background-color);
}
}
-body.mobile:has(.hide-keyboard-button .icon-action) .classic-toolbar-island {
- margin-inline-end: calc(var(--hide-keyboard-button-size) + var(--mobile-toolbar-gap));
+body.mobile:has(:is(.note-split, .modal):focus-within .classic-toolbar-outer-container.visible) .hide-keyboard-button {
+ display: none;
}
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 794be82dd6..2b813d5190 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
@@ -3,106 +3,36 @@ import { afterEach, describe, expect, it, vi } from "vitest";
import { renderInto } from "../../test/render";
-const platform = vi.hoisted(() => ({ isMobileApp: true, isIOS: true }));
-
-vi.mock("../../services/utils", async (importOriginal) => ({
- ...(await importOriginal<typeof import("../../services/utils")>()),
- isMobileApp: () => platform.isMobileApp,
- isIOS: () => platform.isIOS
-}));
-
vi.mock("../react/hooks", async (importOriginal) => ({
...(await importOriginal<typeof import("../react/hooks")>()),
useStaticTooltip: vi.fn()
}));
-import HideKeyboardButton, { isEditable } from "./hide_keyboard_button";
+import HideKeyboardButton from "./hide_keyboard_button";
describe("HideKeyboardButton", () => {
+ let input: HTMLInputElement | undefined;
+
afterEach(() => {
- platform.isMobileApp = true;
- platform.isIOS = true;
- for (const element of added) element.remove();
- added.length = 0;
+ input?.remove();
});
- const added: HTMLElement[] = [];
-
- function attach(...elements: HTMLElement[]) {
- document.body.append(...elements);
- added.push(...elements);
- }
-
- function mount() {
+ it("blurs the focus when pressed, without taking it first", () => {
+ input = document.createElement("input");
+ document.body.append(input);
let container: HTMLElement | undefined;
act(() => { container = renderInto(<HideKeyboardButton />); });
- if (!container) throw new Error("not rendered");
- return container;
- }
-
- function focus(element: HTMLElement) {
- act(() => element.focus());
- }
-
- it("shows while an editable element has the focus and blurs it when pressed", () => {
- const input = document.createElement("input");
- const checkbox = document.createElement("input");
- checkbox.type = "checkbox";
- attach(input, checkbox);
- const container = mount();
-
- expect(container.querySelector("button")).toBeNull();
- focus(input);
- const button = container.querySelector("button");
- expect(button).not.toBeNull();
+ const button = container?.querySelector("button");
+ expect(button).toBeInstanceOf(HTMLButtonElement);
const press = new MouseEvent("mousedown", { bubbles: true, cancelable: true });
button?.dispatchEvent(press);
expect(press.defaultPrevented).toBe(true);
+ act(() => input?.focus());
+ expect(document.activeElement).toBe(input);
act(() => button?.click());
expect(document.activeElement).not.toBe(input);
- expect(container.querySelector("button")).toBeNull();
-
- focus(input);
- expect(container.querySelector("button")).not.toBeNull();
- focus(checkbox);
- expect(container.querySelector("button")).toBeNull();
- });
-
- it("stays away outside the iOS app", () => {
- const input = document.createElement("input");
- attach(input);
-
- platform.isIOS = false;
- const android = mount();
- platform.isIOS = true;
- platform.isMobileApp = false;
- const safari = mount();
-
- focus(input);
- expect(android.querySelector("button")).toBeNull();
- expect(safari.querySelector("button")).toBeNull();
- });
-
- it("counts only elements that take typed text as editable", () => {
- const make = <K extends keyof HTMLElementTagNameMap>(tag: K, setup?: (el: HTMLElementTagNameMap[K]) => void) => {
- const element = document.createElement(tag);
- setup?.(element);
- return element;
- };
-
- expect(isEditable(make("input"))).toBe(true);
- expect(isEditable(make("input", (el) => { el.type = "search"; }))).toBe(true);
- expect(isEditable(make("textarea"))).toBe(true);
- expect(isEditable(make("div", (el) => { el.contentEditable = "true"; }))).toBe(true);
-
- expect(isEditable(make("input", (el) => { el.type = "checkbox"; }))).toBe(false);
- expect(isEditable(make("input", (el) => { el.readOnly = true; }))).toBe(false);
- expect(isEditable(make("textarea", (el) => { el.disabled = true; }))).toBe(false);
- expect(isEditable(make("div"))).toBe(false);
- expect(isEditable(make("button"))).toBe(false);
- expect(isEditable(null)).toBe(false);
});
});
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 3c242ed926..cae29b1129 100644
--- a/apps/client/src/widgets/mobile_widgets/hide_keyboard_button.tsx
+++ b/apps/client/src/widgets/mobile_widgets/hide_keyboard_button.tsx
@@ -1,66 +1,84 @@
import "./hide_keyboard_button.css";
-import { useEffect, useState } from "preact/hooks";
+import { RefObject } from "preact";
+import { useCallback, useEffect, useRef } from "preact/hooks";
import { t } from "../../services/i18n";
-import { isIOS, isMobileApp } from "../../services/utils";
+import { isIOS } from "../../services/utils";
import ActionButton from "../react/ActionButton";
/**
- * Closes the keyboard in the iOS app, which hides the keyboard's input accessory bar and with it the
- * "Done" button (see `ViewController.hideKeyboardInputAccessoryView()`). The button stands at the
- * bottom-end corner, just above the keyboard, while an editable element has the focus, whatever the
- * note type. Android needs none: the system Back closes the keyboard.
+ * Closes the virtual keyboard by blurring the focused element. The button stands at the bottom-end
+ * corner, just above the keyboard, while `RootContainer` marks the keyboard as open with
+ * `virtual-keyboard-opened`. While the focus is in a note split or dialog that shows
+ * `MobileEditorToolbar`, the toolbar carries the button instead (see `hide_keyboard_button.css`).
+ * In the iOS app, blurring also hides the keyboard's input accessory bar (see
+ * `ViewController.hideKeyboardInputAccessoryView()`).
*/
export default function HideKeyboardButton() {
- const enabled = isMobileApp() && isIOS();
- const [ editableFocused, setEditableFocused ] = useState(false);
+ const containerRef = useRef<HTMLDivElement>(null);
+ usePositioningOniOS(containerRef);
- useEffect(() => {
- if (!enabled) return;
-
- const onFocusIn = (e: FocusEvent) => setEditableFocused(isEditable(e.target));
- const onFocusOut = (e: FocusEvent) => setEditableFocused(isEditable(e.relatedTarget));
- setEditableFocused(isEditable(document.activeElement));
- document.addEventListener("focusin", onFocusIn);
- document.addEventListener("focusout", onFocusOut);
- return () => {
- document.removeEventListener("focusin", onFocusIn);
- document.removeEventListener("focusout", onFocusOut);
- };
- }, [ enabled ]);
+ 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 }: {
+ className: string;
+ containerRef?: RefObject<HTMLDivElement | null>;
+}) {
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 className="hide-keyboard-button" onMouseDown={(e) => e.preventDefault()}>
- {enabled && editableFocused && (
- <ActionButton
- icon="bx bx-chevron-down"
- text={t("hide_keyboard_button.title")}
- noTooltipOnTouch
- onClick={() => {
- if (document.activeElement instanceof HTMLElement) {
- document.activeElement.blur();
- }
- }}
- />
- )}
+ <div ref={containerRef} className={className} onMouseDown={(e) => e.preventDefault()}>
+ <ActionButton
+ icon="bx bx-chevron-down"
+ text={t("hide_keyboard_button.title")}
+ noTooltipOnTouch
+ onClick={() => {
+ if (document.activeElement instanceof HTMLElement) {
+ document.activeElement.blur();
+ }
+ }}
+ />
</div>
);
}
-/** Input types that take no typed text, so focusing them opens no keyboard. */
-const NON_TEXT_INPUT_TYPES = new Set([
- "button", "checkbox", "color", "file", "hidden", "image", "radio", "range", "reset", "submit"
-]);
+/**
+ * Lifts an element anchored to the bottom of the screen above the keyboard in plain mobile Safari,
+ * which does not resize the page for the keyboard.
+ */
+export function usePositioningOniOS(wrapperRef: RefObject<HTMLDivElement | null>) {
+ // Fallback path for plain mobile Safari (no Capacitor): the WKWebView
+ // isn't resized when the keyboard opens, so visualViewport.height
+ // shrinks while window.innerHeight stays the same. Push the element
+ // up by the difference. Under Capacitor `resize: native` both values
+ // shrink together, so `obscured` is 0 and we leave CSS in charge.
+ const adjustPosition = useCallback(() => {
+ if (!wrapperRef.current) return;
+ const viewport = window.visualViewport;
+ if (!viewport) return;
+
+ const obscured = Math.max(0, Math.round(
+ window.innerHeight - viewport.height - viewport.offsetTop
+ ));
+
+ if (obscured <= 1) {
+ wrapperRef.current.style.removeProperty("bottom");
+ } else {
+ wrapperRef.current.style.bottom = `${obscured}px`;
+ }
+ }, [ wrapperRef ]);
-export function isEditable(element: EventTarget | null) {
- if (element instanceof HTMLInputElement) {
- return !element.readOnly && !element.disabled && !NON_TEXT_INPUT_TYPES.has(element.type);
- }
- if (element instanceof HTMLTextAreaElement) {
- return !element.readOnly && !element.disabled;
- }
- return element instanceof HTMLElement && element.isContentEditable;
+ useEffect(() => {
+ if (!isIOS()) return;
+ // Run once in case the keyboard is already up when mounting
+ // (e.g. returning from a dialog).
+ adjustPosition();
+ window.visualViewport?.addEventListener("resize", adjustPosition);
+ return () => {
+ window.visualViewport?.removeEventListener("resize", adjustPosition);
+ };
+ }, [ adjustPosition ]);
}
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 d1f42b2b97..67c14bd07e 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
@@ -56,6 +56,24 @@ body.mobile {
border-start-end-radius: 0;
}
+ .virtual-keyboard-opened .classic-toolbar-island .classic-toolbar-widget {
+ inset-inline-end: var(--hide-keyboard-button-size);
+ }
+
+ .classic-toolbar-island .hide-keyboard-action {
+ position: absolute;
+ inset-block: 0;
+ inset-inline-end: 0;
+ align-items: center;
+
+ .icon-action {
+ border-inline-start: 1px solid var(--main-border-color);
+ border-radius: 0;
+ border-start-end-radius: 5px;
+ border-end-end-radius: 5px;
+ }
+ }
+
.classic-toolbar-widget .ck.ck-toolbar {
--ck-color-toolbar-background: transparent;
--ck-color-button-default-background: transparent;
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 79c3cf19e5..4565a34563 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
@@ -1,10 +1,10 @@
import "./mobile_editor_toolbar.css";
import type { CKTextEditor, ClassicEditor } from "@triliumnext/ckeditor5";
-import { RefObject } from "preact";
-import { useCallback, useEffect, useRef, useState } from "preact/hooks";
+import { useEffect, useRef, useState } from "preact/hooks";
import { isIOS } from "../../../services/utils";
+import { HideKeyboardAction, usePositioningOniOS } from "../../mobile_widgets/hide_keyboard_button";
import { useIsNoteReadOnly, useNoteContext, useNoteProperty, useTriliumEvent } from "../../react/hooks";
import { useNestedEditor } from "./editable_embed";
@@ -71,45 +71,12 @@ export default function MobileEditorToolbar() {
<div className={`classic-toolbar-outer-container ${!shouldDisplay ? "hidden-ext" : "visible"} ${isIOS() ? "ios" : ""}`}>
<div ref={islandRef} className="classic-toolbar-island">
<div ref={containerRef} className={`classic-toolbar-widget ${dropdownActive ? "dropdown-active" : ""}`} />
+ <HideKeyboardAction className="hide-keyboard-action" />
</div>
</div>
);
}
-function usePositioningOniOS(wrapperRef: RefObject<HTMLDivElement | null>) {
- // Fallback path for plain mobile Safari (no Capacitor): the WKWebView
- // isn't resized when the keyboard opens, so visualViewport.height
- // shrinks while window.innerHeight stays the same. Push the toolbar
- // up by the difference. Under Capacitor `resize: native` both values
- // shrink together, so `obscured` is 0 and we leave CSS in charge.
- const adjustPosition = useCallback(() => {
- if (!wrapperRef.current) return;
- const viewport = window.visualViewport;
- if (!viewport) return;
-
- const obscured = Math.max(0, Math.round(
- window.innerHeight - viewport.height - viewport.offsetTop
- ));
-
- if (obscured <= 1) {
- wrapperRef.current.style.removeProperty("bottom");
- } else {
- wrapperRef.current.style.bottom = `${obscured}px`;
- }
- }, [ wrapperRef ]);
-
- useEffect(() => {
- if (!isIOS()) return;
- // Run once in case the keyboard is already up when mounting
- // (e.g. returning from a dialog).
- adjustPosition();
- window.visualViewport?.addEventListener("resize", adjustPosition);
- return () => {
- window.visualViewport?.removeEventListener("resize", adjustPosition);
- };
- }, [ adjustPosition ]);
-}
-
/** The editors whose toolbar dropdowns open upwards already. */
const repositionedEditors = new WeakSet<CKTextEditor>();
diff --git a/docs/User Guide/User Guide/Installation & Setup/Mobile Frontend.md b/docs/User Guide/User Guide/Installation & Setup/Mobile Frontend.md
index d68857ebe2..dacbd43071 100644
--- a/docs/User Guide/User Guide/Installation & Setup/Mobile Frontend.md
+++ b/docs/User Guide/User Guide/Installation & Setup/Mobile Frontend.md
@@ -20,7 +20,7 @@ Here is a non-exhaustive list of differences between the desktop version and the
* The <a class="reference-link" href="../Basic%20Concepts%20and%20Features/UI%20Elements/Launch%20Bar.md">Launch Bar</a> is displayed at the bottom of the screen.
* The launch bar uses a different configuration for icons than the desktop version. See the dedicated page for more information on how to configure it.
* Most of the note-related actions are grouped in the horizontal dots icon on the top-right of the note.
-* In the iOS app, the keyboard has no _Done_ button. While you type, press the <span class="tn-icon bx bx-chevron-down"></span> button in the bottom-right corner, just above the keyboard, to hide it. This works in every note type and in every text field.
+* While the on-screen keyboard is open, a <span class="tn-icon bx bx-chevron-down"></span> button in the bottom-right corner, just above the keyboard, hides it. In a text note, the button is at the right end of the formatting toolbar. This works in every note type and in every text field, and is the only way to hide the keyboard in the iOS app, where the keyboard has no _Done_ button.
* The <a class="reference-link" href="../Basic%20Concepts%20and%20Features/UI%20Elements/Tabs.md">Tabs</a> are grouped under a tab switcher in the <a class="reference-link" href="../Basic%20Concepts%20and%20Features/UI%20Elements/Launch%20Bar.md">Launch Bar</a>, where the tabs are displayed in a full-screen grid with preview for easy switching, as well as additional options such as reopening closed tabs.
* Since v0.100.0, <a class="reference-link" href="../Basic%20Concepts%20and%20Features/UI%20Elements/Split%20View.md">Split View</a> can also be used in mobile view, but with a maximum of two panes at once. The splits are displayed vertically instead of horizontally.