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(menus): keep a menu's item tooltips on one side
Elian Doranb1e7f4b0e524
diff --git a/apps/client/src/widgets/react/FormList.spec.tsx b/apps/client/src/widgets/react/FormList.spec.tsxindex 300bfea90a..dbf618b469 100644--- a/apps/client/src/widgets/react/FormList.spec.tsx+++ b/apps/client/src/widgets/react/FormList.spec.tsx@@ -17,7 +17,7 @@ vi.mock("../../services/keyboard_actions", () => ({getActionSync: () => ({ effectiveShortcuts: [ "CommandOrControl+Q" ] })}));-import FormList, { focusListItem, FormDropdownDivider, FormDropdownSubmenu, FormListCustomItem, FormListHeader, FormListItem, FormListToggleableItem } from "./FormList";+import FormList, { focusListItem, FormDropdownDivider, FormDropdownSubmenu, FormListCustomItem, FormListHeader, FormListItem, FormListToggleableItem, menuItemTooltipSide } from "./FormList";describe("FormDropdownDivider", () => {it("draws a separator among the rows, whose click closes no menu", () => {@@ -401,3 +401,33 @@ function press(el: Element, key: string) {el.dispatchEvent(event);return event;}++describe("menuItemTooltipSide", () => {+ /** A row spanning `left`–`right` and a tooltip of `width`, which must not sway the side. */+ function sideFor(left: number, right: number, width: number) {+ const row = document.createElement("li");+ row.getBoundingClientRect = () => ({ left, right } as DOMRect);+ const tip = document.createElement("div");+ tip.style.width = `${width}px`;+ return menuItemTooltipSide(undefined, tip, row);+ }++ it("keeps every row of a menu on one side, whatever its tooltip's width", () => {+ const { innerWidth } = window;+ window.innerWidth = 1000;+ try {+ window.glob.isRtl = false;+ // Room for the widest tooltip on the right.+ expect([ 40, 400 ].map((width) => sideFor(500, 780, width))).toStrictEqual([ "right", "right" ]);+ // Not enough of it: both go left, the narrow one included.+ expect([ 40, 400 ].map((width) => sideFor(600, 900, width))).toStrictEqual([ "left", "left" ]);++ window.glob.isRtl = true;+ expect(sideFor(300, 600, 40)).toBe("left");+ expect(sideFor(100, 400, 40)).toBe("right");+ } finally {+ window.glob.isRtl = false;+ window.innerWidth = innerWidth;+ }+ });+});diff --git a/apps/client/src/widgets/react/FormList.tsx b/apps/client/src/widgets/react/FormList.tsxindex 33afe3a40b..9198539be2 100644--- a/apps/client/src/widgets/react/FormList.tsx+++ b/apps/client/src/widgets/react/FormList.tsx@@ -187,8 +187,8 @@ const EMPTY_ICON = "bx bx-empty";const TEXT_ENTRY = "input:not([type='checkbox'], [type='radio']), textarea, select, [contenteditable='true']";const TOOLTIP_CONFIG: Partial<Tooltip.Options> = {- placement: handleRightToLeftPlacement("right"),- fallbackPlacements: [ handleRightToLeftPlacement("right"), handleRightToLeftPlacement("left") ],+ placement: menuItemTooltipSide,+ fallbackPlacements: [],animation: false};@@ -295,6 +295,27 @@ export function FormListItem({ className, icon, iconSvg, iconClassName, value, t);}+/** Bootstrap's default `--bs-tooltip-max-width` and the gap its `offset` leaves beside the row. */+const TOOLTIP_MAX_WIDTH = 200;+const TOOLTIP_OFFSET = 6;++/**+ * Puts a row's tooltip on the reading-direction end of the menu, or on the other side when the+ * viewport has no room there for a tooltip at its widest. The room is measured against that widest+ * tooltip rather than this one, so every row of a menu gets the same side whatever its text.+ */+export function menuItemTooltipSide(_tooltip?: unknown, tip?: HTMLElement, row?: Element): "left" | "right" {+ const preferred = handleRightToLeftPlacement("right");+ if (!tip || !row) return preferred;++ const maxWidth = parseFloat(getComputedStyle(tip).getPropertyValue("--bs-tooltip-max-width"));+ const needed = (Number.isNaN(maxWidth) ? TOOLTIP_MAX_WIDTH : maxWidth) + TOOLTIP_OFFSET;+ const { left, right } = row.getBoundingClientRect();+ const room = preferred === "right" ? window.innerWidth - right : left;+ if (room >= needed) return preferred;+ return preferred === "right" ? "left" : "right";+}+export function FormListToggleableItem({title, currentValue, onChange, disabled, helpPage, helpTooltip, ...props}: Omit<FormListItemOpts, "onClick" | "children"> & {