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): flip a menu tooltip by the width it can take
Elian Doran608d67249249
diff --git a/apps/client/src/widgets/react/FormList.spec.tsx b/apps/client/src/widgets/react/FormList.spec.tsxindex 291c5e1ff7..f472fe3909 100644--- a/apps/client/src/widgets/react/FormList.spec.tsx+++ b/apps/client/src/widgets/react/FormList.spec.tsx@@ -413,13 +413,23 @@ function press(el: Element, key: string) {}describe("menuItemTooltipSide", () => {- /** A row spanning `left`–`right` and a tooltip of `width`, which must not sway the side. */+ /** A row spanning `left`–`right`, and a tooltip laid out at `width` but capped by its inner. */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);+ const inner = document.createElement("div");+ inner.className = "tooltip-inner";+ inner.style.maxWidth = "500px";+ tip.append(inner);+ // Attached, as Bootstrap shows it, so the inner's computed cap can be read.+ document.body.append(tip);+ try {+ return menuItemTooltipSide(tip, row);+ } finally {+ tip.remove();+ }}it("keeps every row of a menu on one side, whatever its tooltip's width", () => {@@ -427,14 +437,14 @@ describe("menuItemTooltipSide", () => {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" ]);+ // Room for a tooltip at its widest on the right.+ expect([ 100, 400 ].map((width) => sideFor(100, 480, 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" ]);+ expect([ 100, 400 ].map((width) => sideFor(400, 780, width))).toStrictEqual([ "left", "left" ]);window.glob.isRtl = true;- expect(sideFor(300, 600, 40)).toBe("left");- expect(sideFor(100, 400, 40)).toBe("right");+ expect(sideFor(700, 990, 100)).toBe("left");+ expect(sideFor(300, 600, 100)).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 d6c04a2019..a4edcbdf6c 100644--- a/apps/client/src/widgets/react/FormList.tsx+++ b/apps/client/src/widgets/react/FormList.tsx@@ -294,7 +294,7 @@ 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. */+/** Bootstrap's default `--bs-tooltip-max-width`, for a tooltip whose inner carries no cap of its own. */const TOOLTIP_MAX_WIDTH = 200;const TOOLTIP_OFFSET = 6;@@ -302,19 +302,34 @@ 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.+ *+ * Bootstrap calls this with the tip first and the row it stands beside second, `this` being the+ * `Tooltip`.*/-export function menuItemTooltipSide(_tooltip?: unknown, tip?: HTMLElement, row?: Element): "left" | "right" {+export function menuItemTooltipSide(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 needed = tooltipWidth(tip) + 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";}+/**+ * The widest the tooltip can be: the cap its inner carries, which `tooltip.css` sets well above+ * Bootstrap's, on `.tooltip-inner`.+ */+function tooltipWidth(tip: HTMLElement) {+ const inner = tip.querySelector(".tooltip-inner");+ const cap = inner ? parseFloat(getComputedStyle(inner).maxWidth) : NaN;+ if (!Number.isNaN(cap)) return cap;++ const bootstrapCap = parseFloat(getComputedStyle(tip).getPropertyValue("--bs-tooltip-max-width"));+ return Number.isNaN(bootstrapCap) ? TOOLTIP_MAX_WIDTH : bootstrapCap;+}+export function FormListToggleableItem({title, currentValue, onChange, disabled, helpPage, helpTooltip, className, ...props}: Omit<FormListItemOpts, "onClick" | "children"> & {