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(options): render shortcut tooltips via Bootstrap, not native title
Elian Doran8ce914fa894a
diff --git a/apps/client/src/widgets/react/ActionButton.tsx b/apps/client/src/widgets/react/ActionButton.tsxindex 6471b8e93a..3de71d802e 100644--- a/apps/client/src/widgets/react/ActionButton.tsx+++ b/apps/client/src/widgets/react/ActionButton.tsx@@ -6,7 +6,7 @@ import keyboard_actions from "../../services/keyboard_actions";import { isMobile } from "../../services/utils";import { useStaticTooltip } from "./hooks";-export interface ActionButtonProps extends Pick<HTMLAttributes<HTMLButtonElement>, "onClick" | "onAuxClick" | "onContextMenu" | "style"> {+export interface ActionButtonProps extends Pick<HTMLAttributes<HTMLButtonElement>, "onClick" | "onAuxClick" | "onContextMenu" | "onBlur" | "style"> {text: string;titlePosition?: "top" | "right" | "bottom" | "left";/** Extra class applied to the tooltip popup (e.g. `tooltip-top` to raise its z-index above modals). */diff --git a/apps/client/src/widgets/react/Icon.tsx b/apps/client/src/widgets/react/Icon.tsxindex 1490e024c4..c42253dad1 100644--- a/apps/client/src/widgets/react/Icon.tsx+++ b/apps/client/src/widgets/react/Icon.tsx@@ -1,5 +1,9 @@import clsx from "clsx";import { HTMLAttributes } from "preact";+import { useRef } from "preact/hooks";++import { isMobile } from "../../services/utils";+import { useStaticTooltip } from "./hooks";interface IconProps extends Pick<HTMLAttributes<HTMLSpanElement>, "className" | "onClick" | "title" | "style"> {icon?: string;@@ -14,3 +18,32 @@ export default function Icon({ icon, className, ...restProps }: IconProps) {/>);}++interface TooltipIconProps extends Omit<IconProps, "title"> {+ /** Tooltip text, rendered as a Bootstrap tooltip rather than a native `title`. */+ tooltip: string;+ tooltipPosition?: "top" | "right" | "bottom" | "left";+ /** Extra class applied to the tooltip popup (e.g. `tooltip-top` to raise it above modals). */+ tooltipClass?: string;+}++/** An {@link Icon} that shows a Bootstrap tooltip on hover/focus instead of a native `title`. */+export function TooltipIcon({ icon, className, tooltip, tooltipPosition, tooltipClass, ...restProps }: TooltipIconProps) {+ const ref = useRef<HTMLSpanElement>(null);+ useStaticTooltip(ref, {+ title: tooltip,+ placement: tooltipPosition ?? "top",+ fallbackPlacements: [ tooltipPosition ?? "top" ],+ customClass: tooltipClass ?? "",+ trigger: isMobile() ? "focus" : "hover focus",+ animation: false+ });++ return (+ <span+ ref={ref}+ class={clsx(icon ?? "bx bx-empty", className, "tn-icon")}+ {...restProps}+ />+ );+}diff --git a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx b/apps/client/src/widgets/type_widgets/options/shortcuts.tsxindex abc35889be..d4a78c9146 100644--- a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx+++ b/apps/client/src/widgets/type_widgets/options/shortcuts.tsx@@ -2,6 +2,7 @@ import "./shortcuts.css";import { ActionKeyboardShortcut, KeyboardShortcut, OptionNames } from "@triliumnext/commons";import { Dropdown as BootstrapDropdown } from "bootstrap";+import { ComponentChildren, RefObject } from "preact";import { useCallback, useEffect, useMemo, useRef, useState } from "preact/hooks";import dialog from "../../../services/dialog";@@ -17,7 +18,8 @@ import Button from "../../react/Button";import Dropdown from "../../react/Dropdown";import { FormDropdownDivider, FormListItem } from "../../react/FormList";import FormTextBox from "../../react/FormTextBox";-import { useTriliumEvent } from "../../react/hooks";+import { useStaticTooltip, useTriliumEvent } from "../../react/hooks";+import { TooltipIcon } from "../../react/Icon";import NoItems from "../../react/NoItems";import OptionsRow from "./components/OptionsRow";import OptionsSection from "./components/OptionsSection";@@ -357,7 +359,7 @@ function ShortcutRow({ action, conflicts }: { action: ActionKeyboardShortcut; colabel={<>{isShortcutModified(action) &&- <span class="shortcut-modified-indicator" title={t("shortcuts.modified_from_default")} />}+ <TooltipIcon className="shortcut-modified-indicator" tooltip={t("shortcuts.modified_from_default")} tooltipClass="tooltip-top" />}{action.friendlyName}</>}@@ -408,31 +410,31 @@ function ShortcutEditor({ keyboardShortcut: action, conflicts }: { keyboardShortreturn (<span class={`shortcut-chip ${global ? "global" : ""} ${conflictsWith ? "conflict" : ""}`} key={shortcut}>{conflictsWith &&- <span- class="bx bx-error-circle shortcut-chip-conflict"- title={t("shortcuts.conflict_chip", { actions: conflictsWith.join(", ") })}+ <TooltipIcon+ icon="bx bx-error-circle"+ className="shortcut-chip-conflict"+ tooltip={t("shortcuts.conflict_chip", { actions: conflictsWith.join(", ") })}+ tooltipClass="tooltip-top"/>}{electron? (- <button- type="button"- class={`shortcut-chip-action shortcut-chip-global ${global ? "active" : ""}`}+ <TooltipButton+ className={`shortcut-chip-action shortcut-chip-global ${global ? "active" : ""}`}title={global ? t("shortcuts.make_local") : t("shortcuts.make_global")}onClick={() => toggleGlobal(shortcut)}><span class="bx bx-globe" />- </button>+ </TooltipButton>)- : global && <span class="bx bx-globe shortcut-chip-global-indicator" title={t("shortcuts.global_shortcut")} />}+ : global && <TooltipIcon icon="bx bx-globe" className="shortcut-chip-global-indicator" tooltip={t("shortcuts.global_shortcut")} tooltipClass="tooltip-top" />}<kbd>{stripGlobalPrefix(shortcut)}</kbd>- <button- type="button"- class="shortcut-chip-action shortcut-chip-remove"+ <TooltipButton+ className="shortcut-chip-action shortcut-chip-remove"title={t("shortcuts.remove_shortcut")}onClick={() => saveShortcuts(shortcuts.filter((s) => s !== shortcut))}><span class="bx bx-x" />- </button>+ </TooltipButton></span>);})}@@ -488,16 +490,50 @@ function ShortcutRecorder({ onCapture }: { onCapture: (shortcut: string) => void// Keep the button a fixed-size icon-action in both states so toggling recording never reflows// the row; the recording state is conveyed through styling and the tooltip.return (- <button- type="button"- class={`shortcut-recorder icon-action bx bx-plus ${recording ? "recording" : ""}`}- title={recording ? t("shortcuts.press_keys") : t("shortcuts.record_shortcut")}+ <ActionButton+ className={`shortcut-recorder ${recording ? "recording" : ""}`}+ icon="bx bx-plus"+ text={recording ? t("shortcuts.press_keys") : t("shortcuts.record_shortcut")}+ titlePosition="top"+ tooltipClass="tooltip-top"onClick={() => setRecording((value) => !value)}onBlur={() => setRecording(false)}/>);}+/**+ * Attaches a Bootstrap tooltip to an element on the shortcuts page. Uses the `tooltip-top` popup+ * class so the tooltip renders above the options modal (matching the revert ActionButton), and a+ * focus trigger on mobile where hover is unavailable.+ */+function useShortcutTooltip(elRef: RefObject<Element>, title: string) {+ useStaticTooltip(elRef, {+ title,+ placement: "top",+ fallbackPlacements: [ "top" ],+ customClass: "tooltip-top",+ trigger: isMobile() ? "focus" : "hover focus",+ animation: false+ });+}++/** Icon button carrying a Bootstrap tooltip instead of a native `title`. */+function TooltipButton({ className, title, onClick, children }: {+ className: string;+ title: string;+ onClick: () => void;+ children?: ComponentChildren;+}) {+ const ref = useRef<HTMLButtonElement>(null);+ useShortcutTooltip(ref, title);+ return (+ <button ref={ref} type="button" class={className} onClick={onClick}>+ {children}+ </button>+ );+}+/*** Whether a keystroke should cancel the recorder. Only a bare Escape cancels — Escape held with a* modifier (e.g. Ctrl+Escape) is a valid bindable combination and must be recorded, not swallowed.