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
refactor(client/overlay): put the shortcut-hints button on the control group
Elian Doranb36ac95cc0a9
diff --git a/apps/client/src/widgets/shortcut_hints/shortcut_hint_button.tsx b/apps/client/src/widgets/shortcut_hints/shortcut_hint_button.tsxindex 25080e1a6b..4fc885a52a 100644--- a/apps/client/src/widgets/shortcut_hints/shortcut_hint_button.tsx+++ b/apps/client/src/widgets/shortcut_hints/shortcut_hint_button.tsx@@ -2,37 +2,39 @@ import "./shortcut_hints_kbd.css";import "./shortcut_hint_button.css";import clsx from "clsx";-import { useCallback, useContext, useEffect, useRef, useState } from "preact/hooks";+import type { TargetedMouseEvent } from "preact";+import { useCallback, useContext, useEffect, useState } from "preact/hooks";import appContext from "../../components/app_context.js";import { t } from "../../services/i18n.js";import keyboard_actions from "../../services/keyboard_actions.js";import { formatShortcut, joinShortcut } from "../../services/keyboard_shortcut_display.js";import { collectShortcutHints } from "../../services/shortcut_hints.js";-import { useStaticTooltip } from "../react/hooks.js";+import OverlayControlGroup, { OverlayControlButton } from "../react/OverlayControlGroup.js";import { ParentComponent } from "../react/react_utils.js";/*** Standalone shortcut-hints button in its own overlay group; `className` positions the group. Use* this when the widget has no existing overlay controls. To add the button to an *existing*- * `.tn-overlay-control-group`, use the {@link ShortcutHintOverlayButton} named export instead.+ * {@link OverlayControlGroup}, use the {@link ShortcutHintOverlayButton} named export instead.*/export default function ShortcutHintButton({ className }: { className?: string }) {return (- <div className={clsx("tn-overlay-control-group", "shortcut-hint-button-group", className)}>+ // The group stands at the head of what it is put over, so its tooltip opens downwards, away+ // from that edge.+ <OverlayControlGroup className={clsx("shortcut-hint-button-group", className)} titlePosition="bottom"><ShortcutHintOverlayButton />- </div>+ </OverlayControlGroup>);}/**- * Just the overlay `<button>` (no group wrapper), for placing alongside other buttons inside an- * existing `.tn-overlay-control-group`. It opens the contextual shortcut-hints pane as a dropdown,- * collecting the hints from its own widget context.+ * Just the overlay button (no group wrapper), for placing alongside other buttons inside an existing+ * {@link OverlayControlGroup} — whose tooltip direction it then takes, as its neighbours do. It opens+ * the contextual shortcut-hints pane as a dropdown, collecting the hints from its own widget context.*/export function ShortcutHintOverlayButton() {const parentComponent = useContext(ParentComponent);- const buttonRef = useRef<HTMLButtonElement>(null);const [ shortcut, setShortcut ] = useState("Alt+F1");useEffect(() => {@@ -42,23 +44,21 @@ export function ShortcutHintOverlayButton() {});}, []);- useStaticTooltip(buttonRef, { title: t("shortcut_hints.show_button"), placement: "bottom" });-- const onClick = useCallback(() => {+ // The pane is anchored to the button the press landed on, which the press itself names — no ref+ // of our own to hold it.+ const onClick = useCallback((e: TargetedMouseEvent<HTMLButtonElement>) => {const sections = collectShortcutHints(parentComponent);- appContext.triggerEvent("shortcutHintsRequested", { sections, anchor: buttonRef.current });+ appContext.triggerEvent("shortcutHintsRequested", { sections, anchor: e.currentTarget });}, [ parentComponent ]);return (- <button- ref={buttonRef}- type="button"- className="tn-overlay-text-button shortcut-hint-button tn-shortcut-hints-kbd"- aria-label={t("shortcut_hints.show_button")}+ <OverlayControlButton+ title={t("shortcut_hints.show_button")}+ className="shortcut-hint-button tn-shortcut-hints-kbd"onClick={onClick}><kbd>?</kbd><span className="shortcut-hint-button-key">{shortcut}</span>- </button>+ </OverlayControlButton>);}