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
feat(mobile/text): swap toolbar menus in place without a reveal
Elian Doran625c15ecb01c
diff --git a/apps/client/src/widgets/type_widgets/text/color_menu.tsx b/apps/client/src/widgets/type_widgets/text/color_menu.tsxindex e425e46e7b..8dfab99aab 100644--- a/apps/client/src/widgets/type_widgets/text/color_menu.tsx+++ b/apps/client/src/widgets/type_widgets/text/color_menu.tsx@@ -1,4 +1,5 @@import type { ColorMenuHost, ColorMenuRequest, ColorMenuTarget } from "@triliumnext/ckeditor5";+import clsx from "clsx";import { render } from "preact";import { t } from "../../../services/i18n";@@ -46,14 +47,16 @@ function ColorMenu({ request, onClose }: {onClose(returnFocus: boolean): void;}) {const bottomSheet = isMobile() && isNarrowLayout();- const keyboardPanel = useKeyboardPanel(bottomSheet);+ const { isPanel, replacesPanel } = useKeyboardPanel(bottomSheet);return (<Menuanchor={request.anchor}offset={ANCHOR_GAP}portalClassName="tn-dropdown-portal"- className={keyboardPanel ? "tn-dropdown-menu keyboard-panel" : "tn-dropdown-menu"}+ className={clsx(+ "tn-dropdown-menu", isPanel && "keyboard-panel", replacesPanel && "replaces-panel"+ )}bottomSheet={bottomSheet}elementRef={request.setElement}onDismiss={() => onClose(false)}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 ccef1e2bf7..86ea74e86e 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@@ -102,6 +102,10 @@ body.mobile {max-height: var(--tn-keyboard-height);}+ .dropdown-menu.mobile-bottom-menu.keyboard-panel.replaces-panel[data-reveal]:not([data-closing]) {+ animation: none;+ }+&.keyboard-panel-open .classic-toolbar-island,.virtual-keyboard-opened .classic-toolbar-island {--mobile-toolbar-fade: 24px;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 c8f38a4d4a..38484ceb8b 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@@ -54,7 +54,7 @@ describe("useKeyboardPanel", () => {root.id = "root-widget";document.body.append(root);const isMarked = isKeyboardPanelOpen;- const seen: boolean[] = [];+ const seen: ReturnType<typeof useKeyboardPanel>[] = [];const host = document.createElement("div");function Harness({ enabled }: { enabled: boolean }) {seen.push(useKeyboardPanel(enabled));@@ -62,24 +62,24 @@ describe("useKeyboardPanel", () => {}act(() => render(<Harness enabled />, host));- expect(seen.at(-1)).toBe(false);+ expect(seen.at(-1)?.isPanel).toBe(false);expect(isMarked()).toBe(false);act(() => render(null, host));root.classList.add("virtual-keyboard-opened");act(() => render(<Harness enabled={false} />, host));- expect(seen.at(-1)).toBe(false);+ expect(seen.at(-1)?.isPanel).toBe(false);expect(isMarked()).toBe(false);act(() => render(null, host));act(() => render(<Harness enabled />, host));- expect(seen.at(-1)).toBe(true);+ expect(seen.at(-1)?.isPanel).toBe(true);expect(isMarked()).toBe(true);// The keyboard closes as the menu takes focus, and the menu keeps its place.root.classList.remove("virtual-keyboard-opened");act(() => render(<Harness enabled />, host));- expect(seen.at(-1)).toBe(true);+ expect(seen.at(-1)?.isPanel).toBe(true);expect(isMarked()).toBe(true);await act(async () => render(null, host));@@ -103,7 +103,7 @@ describe("useKeyboardPanel", () => {// A menu opened while the keyboard is on its way takes the place over.act(() => render(<Harness />, host));- expect(seen.at(-1)).toBe(true);+ expect(seen.at(-1)?.isPanel).toBe(true);await act(async () => render(null, host));expect(isKeyboardPanelOpen()).toBe(true);@@ -119,6 +119,7 @@ describe("useKeyboardPanel", () => {root.classList.add("virtual-keyboard-opened");act(() => render(<Harness />, host));+ expect(seen.at(-1)?.replacesPanel).toBe(false);root.classList.remove("virtual-keyboard-opened");// A press on another button of the toolbar closes the menu as it starts, and its click@@ -130,7 +131,9 @@ describe("useKeyboardPanel", () => {render(<Harness />, host);await new Promise((resolve) => setTimeout(resolve));});- expect(seen.at(-1)).toBe(true);+ expect(seen.at(-1)?.isPanel).toBe(true);+ // It shows at once, in the place the closed one leaves.+ expect(seen.at(-1)?.replacesPanel).toBe(true);expect(isKeyboardPanelOpen()).toBe(true);await act(async () => render(null, host));@@ -193,7 +196,7 @@ function buildPanelFixture() {editable.className = "ck-editor__editable";editable.tabIndex = 0;document.body.append(root, editable);- const seen: boolean[] = [];+ const seen: ReturnType<typeof useKeyboardPanel>[] = [];const host = document.createElement("div");function Harness() {seen.push(useKeyboardPanel(true));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 cb7682d307..10199b4106 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@@ -149,13 +149,15 @@ export function useShownToolbarElement(editor: CKTextEditor | undefined) {* 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.+ * 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.*/export function useKeyboardPanel(enabled: boolean) {- const [ isPanel ] = useState(() => {+ const [ { isPanel, replacesPanel } ] = useState(() => {const root = document.getElementById("root-widget");const isKeyboardUp = !!root?.classList.contains("virtual-keyboard-opened");- return enabled && (isKeyboardUp || isKeyboardPanelOpen());+ const isPanel = enabled && (isKeyboardUp || isKeyboardPanelOpen());+ return { isPanel, replacesPanel: isPanel && !!pendingHandBack };});useLayoutEffect(() => {@@ -164,7 +166,7 @@ export function useKeyboardPanel(enabled: boolean) {return handBackKeyboardPlace;}, [ isPanel ]);- return isPanel;+ return { isPanel, replacesPanel };}/** Whether a menu of the toolbar is in the keyboard's place, for its host to tell the editor. */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 a674a8e8a1..ffa12904c6 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@@ -3,6 +3,7 @@ import "./toolbar_group_menu.css";import type {ToolbarGroupMenuEntry, ToolbarGroupMenuHost, ToolbarGroupMenuItem, ToolbarGroupMenuRequest} from "@triliumnext/ckeditor5";+import clsx from "clsx";import { render } from "preact";import { escapeHtml, isMobile, isNarrowLayout } from "../../../services/utils";@@ -53,14 +54,16 @@ function ToolbarGroupMenu({ request, onClose }: {onClose(returnFocus: boolean): void;}) {const bottomSheet = isMobile() && isNarrowLayout();- const keyboardPanel = useKeyboardPanel(bottomSheet);+ const { isPanel, replacesPanel } = useKeyboardPanel(bottomSheet);return (<Menuanchor={request.anchor}offset={ANCHOR_GAP}portalClassName="tn-dropdown-portal"- className={keyboardPanel ? "tn-dropdown-menu keyboard-panel" : "tn-dropdown-menu"}+ className={clsx(+ "tn-dropdown-menu", isPanel && "keyboard-panel", replacesPanel && "replaces-panel"+ )}bottomSheet={bottomSheet}aria-labelledby={request.anchor.getAttribute("aria-labelledby") ?? undefined}elementRef={request.setElement}