Skip to content

Build your personal knowledge base with Trilium Notes

refactor(client/overlay): put sibling navigation on the control group

Elian Dorandf34fa38f3cb
diff --git a/apps/client/src/widgets/react/OverlayControlGroup.spec.tsx b/apps/client/src/widgets/react/OverlayControlGroup.spec.tsx
index f40884fd0f..c205600fb3 100644
--- a/apps/client/src/widgets/react/OverlayControlGroup.spec.tsx
+++ b/apps/client/src/widgets/react/OverlayControlGroup.spec.tsx
@@ -53,6 +53,20 @@ describe("OverlayControlGroup", () => {
expect(text.textContent).toBe("100%");
});
+ it("leaves a readout to be named by what it shows, with nothing to say on hover", () => {
+ mount(
+ <OverlayControlGroup>
+ <OverlayControlButton className="my-readout" disabled>3/12</OverlayControlButton>
+ </OverlayControlGroup>
+ );
+
+ const readout = container.querySelector("button");
+ expect(readout?.hasAttribute("aria-label")).toBe(false);
+ expect(readout?.textContent).toBe("3/12");
+ // No title, so the hook finds nothing to build a tooltip from.
+ expect(staticTooltipSpy.mock.calls.at(-1)?.[1]?.title).toBeUndefined();
+ });
+
it("marks a button held down or refused when asked to", () => {
mount(
<OverlayControlGroup>
diff --git a/apps/client/src/widgets/react/OverlayControlGroup.tsx b/apps/client/src/widgets/react/OverlayControlGroup.tsx
index d63e889001..4ba3cbd3d2 100644
--- a/apps/client/src/widgets/react/OverlayControlGroup.tsx
+++ b/apps/client/src/widgets/react/OverlayControlGroup.tsx
@@ -36,8 +36,12 @@ export default function OverlayControlGroup({ className, titlePosition, children
}
interface OverlayControlButtonProps extends Pick<HTMLAttributes<HTMLButtonElement>, "onClick"> {
- /** What the button does: shown as its tooltip, and read out as its accessible name. */
- title: string;
+ /**
+ * What the button does: shown as its tooltip, and read out as its accessible name. A readout that
+ * says what it is through its own children — an index, a percentage — needs none, and is left to
+ * be named by what it shows.
+ */
+ title?: string;
/**
* The boxicons name of the mark it wears (`bx-plus-circle`), for a button that speaks in a glyph.
* Given one, the button is drawn at an icon's width; without one it is drawn at a word's, and
diff --git a/apps/client/src/widgets/react/SiblingNavigator.tsx b/apps/client/src/widgets/react/SiblingNavigator.tsx
index 67aa40d795..bda60be20a 100644
--- a/apps/client/src/widgets/react/SiblingNavigator.tsx
+++ b/apps/client/src/widgets/react/SiblingNavigator.tsx
@@ -1,7 +1,7 @@
import "./SiblingNavigator.css";
import type { RefObject } from "preact";
-import { useEffect, useMemo, useRef, useState } from "preact/hooks";
+import { useEffect, useRef, useState } from "preact/hooks";
import type NoteContext from "../../components/note_context";
import type FNote from "../../entities/fnote";
@@ -9,7 +9,8 @@ import froca from "../../services/froca";
import { t } from "../../services/i18n";
import type { ViewScope } from "../../services/link";
import type LoadResults from "../../services/load_results";
-import { useStaticTooltip, useTriliumEvent } from "./hooks";
+import { useTriliumEvent } from "./hooks";
+import OverlayControlGroup, { OverlayControlButton } from "./OverlayControlGroup";
import { codeToSiblingDirection, getParentFromNotePath, getSiblingNavigation, isInteractiveTarget, isTextEntryTarget, sameRoleAttachments } from "./sibling_navigation";
const NO_KEYS: readonly string[] = [];
@@ -73,9 +74,6 @@ export interface SiblingNavigationProvider {
* from the caller-provided i18n keys. Renders nothing when there is no sibling to move between.
*/
export default function SiblingNavigator({ note, noteContext, viewScope, siblingType, previousTooltipI18nKey, nextTooltipI18nKey, keyboardTarget, extraPreviousKeys = NO_KEYS, extraNextKeys = NO_KEYS }: SiblingNavigatorProps) {
- const previousRef = useRef<HTMLButtonElement>(null);
- const nextRef = useRef<HTMLButtonElement>(null);
-
// Viewing a single attachment → cycle the note's attachments; otherwise its note siblings.
const provider = viewScope?.attachmentId
? attachmentSiblingProvider(note, noteContext, viewScope)
@@ -83,40 +81,26 @@ export default function SiblingNavigator({ note, noteContext, viewScope, sibling
const navigation = useSiblingNavigation(provider);
useSiblingKeyboard(navigation, noteContext, keyboardTarget, extraPreviousKeys, extraNextKeys);
- const previousText = navigation ? t(previousTooltipI18nKey, { title: navigation.previousTitle }) : "";
- const nextText = navigation ? t(nextTooltipI18nKey, { title: navigation.nextTitle }) : "";
- // Memoize so the bootstrap tooltip is only recreated when the target's name actually changes.
- const previousConfig = useMemo(() => ({ title: previousText, placement: "top" as const }), [ previousText ]);
- const nextConfig = useMemo(() => ({ title: nextText, placement: "top" as const }), [ nextText ]);
- useStaticTooltip(previousRef, previousConfig);
- useStaticTooltip(nextRef, nextConfig);
-
if (!navigation) return null;
return (
- <div className="sibling-navigator tn-overlay-control-group">
- <button
- ref={previousRef}
- type="button"
- className="tn-overlay-icon-button bx bx-chevron-left"
- aria-label={previousText}
+ <OverlayControlGroup className="sibling-navigator">
+ <OverlayControlButton
+ title={t(previousTooltipI18nKey, { title: navigation.previousTitle })}
+ icon="bx-chevron-left"
onClick={() => navigation.navigatePrevious()}
/>
- <button
- className="sibling-navigator-index tn-overlay-text-button"
- disabled>
+ <OverlayControlButton className="sibling-navigator-index" disabled>
{navigation.index}/{navigation.total}
- </button>
-
- <button
- ref={nextRef}
- type="button"
- className="tn-overlay-icon-button bx bx-chevron-right"
- aria-label={nextText}
+ </OverlayControlButton>
+
+ <OverlayControlButton
+ title={t(nextTooltipI18nKey, { title: navigation.nextTitle })}
+ icon="bx-chevron-right"
onClick={() => navigation.navigateNext()}
/>
- </div>
+ </OverlayControlGroup>
);
}