Skip to content

Build your personal knowledge base with Trilium Notes

fix(collections): keep overlay tooltips over their buttons

Elian Doran1cd8260779d3
diff --git a/apps/client/src/widgets/react/OverlayControlGroup.spec.tsx b/apps/client/src/widgets/react/OverlayControlGroup.spec.tsx
index f25e1798af..78bd70364c 100644
--- a/apps/client/src/widgets/react/OverlayControlGroup.spec.tsx
+++ b/apps/client/src/widgets/react/OverlayControlGroup.spec.tsx
@@ -175,9 +175,22 @@ describe("OverlayControlGroup", () => {
</OverlayControlGroup>
);
- expect(tooltipFor("Zoom out")).toEqual({ title: "Zoom out", placement: "bottom" });
+ // Never flipped to a side, where the tooltip would cover the neighbouring buttons.
+ expect(tooltipFor("Zoom out")).toEqual({ title: "Zoom out", placement: "bottom", fallbackPlacements: [ "bottom" ] });
// A button placed unlike its neighbours overrides what the group hands down.
- expect(tooltipFor("Elsewhere")).toEqual({ title: "Elsewhere", placement: "left" });
+ expect(tooltipFor("Elsewhere")).toEqual({ title: "Elsewhere", placement: "left", fallbackPlacements: [ "left" ] });
+ });
+
+ it("names its shortcut in the tooltip, but not in the button's name", () => {
+ mount(
+ <OverlayControlGroup>
+ <OverlayControlButton title="Overview" icon="bx-grid-horizontal" shortcut="O" />
+ <OverlayControlButton title="Next slide" icon="bx-chevron-right" shortcut="Space" />
+ </OverlayControlGroup>
+ );
+
+ expect(tooltipFor("Overview")?.title).toBe("Overview (O)");
+ expect(tooltipFor("Next slide")?.title).toMatch(/^Next slide \(.+\)$/);
});
it("opens tooltips away from the bottom edge unless told otherwise", () => {
diff --git a/apps/client/src/widgets/react/OverlayControlGroup.tsx b/apps/client/src/widgets/react/OverlayControlGroup.tsx
index e376ca529e..f899d27541 100644
--- a/apps/client/src/widgets/react/OverlayControlGroup.tsx
+++ b/apps/client/src/widgets/react/OverlayControlGroup.tsx
@@ -5,6 +5,7 @@ import { type ComponentChildren, createContext, type HTMLAttributes } from "prea
import { useContext, useMemo, useRef } from "preact/hooks";
import { t } from "../../services/i18n";
+import { formatShortcut, joinShortcut } from "../../services/keyboard_shortcut_display";
import type { ActionButtonProps } from "./ActionButton";
import Dropdown, { DropdownPanel, type DropdownProps } from "./Dropdown";
import { useStaticTooltip } from "./hooks";
@@ -104,6 +105,8 @@ interface OverlayControlButtonProps extends Pick<HTMLAttributes<HTMLButtonElemen
disabled?: boolean;
/** Overrides the direction the group hands down, for a button placed unlike its neighbours. */
titlePosition?: ActionButtonProps["titlePosition"];
+ /** A key that does what the button does, as `formatShortcut()` reads it, named after the title. */
+ shortcut?: string;
}
/**
@@ -118,18 +121,23 @@ interface OverlayControlButtonProps extends Pick<HTMLAttributes<HTMLButtonElemen
* plain `aria-label`.
*
* Its tooltip opens the way the group it stands on says, so that a group at the foot of the content
- * does not open its tooltips off the bottom edge.
+ * does not open its tooltips off the bottom edge, and never flips to a side, where it would cover the
+ * neighbouring buttons. A `shortcut` follows the title in the tooltip only.
*/
export function OverlayControlButton(props: OverlayControlButtonProps) {
- const { title, icon, text, className, active, disabled, titlePosition, ...restProps } = props;
+ const { title, icon, text, className, active, disabled, titlePosition, shortcut, ...restProps } = props;
const buttonRef = useRef<HTMLButtonElement>(null);
const groupDirection = useContext(TooltipDirection);
const placement = titlePosition ?? groupDirection;
const hasText = "text" in props;
+ const tooltip = title && shortcut ? `${title} (${joinShortcut(formatShortcut(shortcut))})` : title;
// Memoized so the tooltip is only rebuilt when what it says (or where it opens) actually changes,
// rather than on every render of the group.
- const tooltipConfig = useMemo(() => ({ title, placement }), [ title, placement ]);
+ const tooltipConfig = useMemo(
+ () => ({ title: tooltip, placement, fallbackPlacements: placement && [ placement ] }),
+ [ tooltip, placement ]
+ );
useStaticTooltip(buttonRef, tooltipConfig);
return (
@@ -161,10 +169,11 @@ interface OverlayControlDropdownProps extends Pick<DropdownProps, "children" | "
/**
* A button on such a group that opens a menu, or a panel with `panel`. The popup and the tooltip open
- * away from the edge that the group is pinned to.
+ * away from the edge that the group is pinned to, and the tooltip never flips to a side.
*/
export function OverlayControlDropdown({ title, icon, panel, ...props }: OverlayControlDropdownProps) {
const direction = useContext(TooltipDirection);
+ const titleOptions = useMemo(() => ({ fallbackPlacements: direction && [ direction ] }), [ direction ]);
const Toggle = panel ? DropdownPanel : Dropdown;
return (
<Toggle
@@ -174,6 +183,7 @@ export function OverlayControlDropdown({ title, icon, panel, ...props }: Overlay
buttonProps={{ "aria-label": title }}
title={title}
titlePosition={direction}
+ titleOptions={titleOptions}
placement={direction}
hideToggleArrow
noSelectButtonStyle