Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): a glance at the sidebar no longer undocks it

Elian Doran95e3888fb5df
diff --git a/apps/client/src/widgets/sidebar/RightPanelContainer.spec.tsx b/apps/client/src/widgets/sidebar/RightPanelContainer.spec.tsx
index ce77ff2438..098168e16c 100644
--- a/apps/client/src/widgets/sidebar/RightPanelContainer.spec.tsx
+++ b/apps/client/src/widgets/sidebar/RightPanelContainer.spec.tsx
@@ -1,6 +1,26 @@
import { describe, expect, it } from "vitest";
-import { groupIntoTabs, RightPanelWidgetDefinition } from "./RightPanelContainer";
+import { groupIntoTabs, reduceTabSelection, RightPanelWidgetDefinition } from "./RightPanelContainer";
+
+describe("reduceTabSelection", () => {
+ it("opens a closed pane, and brings a tab that is not the one on show to the front", () => {
+ expect(reduceTabSelection("closed", undefined, { tabId: "chat" })).toBe("open");
+ expect(reduceTabSelection("closed", "outline", { tabId: "connections", peek: true })).toBe("open");
+ expect(reduceTabSelection("docked", "outline", { tabId: "chat" })).toBe("select");
+ expect(reduceTabSelection("peek", "outline", { tabId: "connections", peek: true })).toBe("select");
+ });
+
+ it("puts away what the press itself opened: its peek, or the pane an entry point docks", () => {
+ expect(reduceTabSelection("peek", "connections", { tabId: "connections", peek: true })).toBe("dismiss");
+ expect(reduceTabSelection("peek", "chat", { tabId: "chat" })).toBe("dismiss");
+ expect(reduceTabSelection("docked", "chat", { tabId: "chat" })).toBe("close");
+ });
+
+ it("leaves a docked pane docked for a glance at the tab it is already showing", () => {
+ // The dock is the user's layout; the press is left to expand the widget it is about.
+ expect(reduceTabSelection("docked", "connections", { tabId: "connections", peek: true })).toBe("select");
+ });
+});
describe("groupIntoTabs", () => {
it("keeps the enabled widgets only, in position order, and drops the tabs left empty", () => {
diff --git a/apps/client/src/widgets/sidebar/RightPanelContainer.tsx b/apps/client/src/widgets/sidebar/RightPanelContainer.tsx
index 1764d503a7..484a3bca0b 100644
--- a/apps/client/src/widgets/sidebar/RightPanelContainer.tsx
+++ b/apps/client/src/widgets/sidebar/RightPanelContainer.tsx
@@ -75,13 +75,18 @@ export default function RightPanelContainer({ widgetsByParent }: { widgetsByPare
useTriliumEvent("toggleRightPane", toggleDocked);
useTriliumEvent("peekRightPane", togglePeek);
- // An entry point aimed at one tab (the chat launcher, the status bar's note paths badge): it opens
- // the pane on that tab, brings it to the front if the pane is already open on another one, and
- // closes it if it is the tab on show — so the button it comes from keeps behaving like a toggle for
- // its own tab. `peek` opens it as a glance instead of a dock; `expandWidgetId` opens the one widget
- // the entry point is really about, in case the user has it collapsed.
+ // An entry point aimed at one tab (the chat launcher, the status bar's connection badges): it opens
+ // the pane on that tab, brings it to the front if the pane is already open on another one, and puts
+ // it away again when it is the tab on show — see reduceTabSelection for what that amounts to.
+ // `peek` opens it as a glance instead of a dock; `expandWidgetId` opens the one widget the entry
+ // point is really about, in case the user has it collapsed.
useTriliumEvent("selectRightPaneTab", ({ tabId, peek, expandWidgetId }) => {
- if (visible && activeTab?.id === tabId) {
+ const action = reduceTabSelection(mode, activeTab?.id, { tabId, peek });
+ if (action === "dismiss") {
+ dismiss();
+ return;
+ }
+ if (action === "close") {
close();
return;
}
@@ -91,7 +96,7 @@ export default function RightPanelContainer({ widgetsByParent }: { widgetsByPare
if (expandWidgetId) {
setExpandRequest((prev) => ({ id: expandWidgetId, counter: (prev?.counter ?? 0) + 1 }));
}
- if (!visible) {
+ if (action === "open") {
if (peek) {
togglePeek();
} else {
@@ -298,6 +303,28 @@ function useItems(rightPaneVisible: boolean, widgetsByParent: WidgetsByParent):
return groupIntoTabs(definitions, !!note);
}
+export type TabSelection = "open" | "select" | "dismiss" | "close";
+
+/**
+ * What a `selectRightPaneTab` press amounts to, given how the pane stands (pure):
+ *
+ * - `open`: the pane is closed, so open it — peeked or docked, as the entry point asks.
+ * - `select`: bring the tab to the front of an open pane.
+ * - `dismiss`: put away the peek the press is toggling, softly so that the next one is instant.
+ * - `close`: an entry point that opens the pane docked closes it again, so its button stays a toggle
+ * for its own tab.
+ *
+ * A `peek` entry point is only a glance, so pressing it for the tab already on show in a *docked* pane
+ * selects rather than closes: the dock is the user's layout, not the badge's to undo — the press is
+ * left to expand the widget it is about.
+ */
+export function reduceTabSelection(mode: PaneMode, activeTabId: RightPaneTabId | undefined, { tabId, peek }: { tabId: RightPaneTabId; peek?: boolean }): TabSelection {
+ if (mode === "closed") return "open";
+ if (activeTabId !== tabId) return "select";
+ if (mode === "peek") return "dismiss";
+ return peek ? "select" : "close";
+}
+
/**
* Splits the enabled widgets into the pane's tabs, each in position order. A tab with no enabled widget
* is dropped rather than shown empty, which is what makes the strip follow the note: a plain image note
diff --git a/apps/client/src/widgets/sidebar/RightPanelWidget.spec.tsx b/apps/client/src/widgets/sidebar/RightPanelWidget.spec.tsx
index 9615b7f484..ef4420668c 100644
--- a/apps/client/src/widgets/sidebar/RightPanelWidget.spec.tsx
+++ b/apps/client/src/widgets/sidebar/RightPanelWidget.spec.tsx
@@ -77,5 +77,16 @@ describe("RightPanelWidget", () => {
expect(card("shared")?.className).toContain("collapsed");
await renderWith({ id: "shared", counter: 2 });
expect(card("shared")?.className).not.toContain("collapsed");
+
+ // A widget the request opens is scrolled to once the body it opened is laid out; one that was
+ // open all along is scrolled to on the spot, which is all such a press amounts to.
+ const scrollIntoView = vi.fn();
+ const solitary = card("solitary");
+ if (solitary) solitary.scrollIntoView = scrollIntoView;
+ await renderWith({ id: "solitary", counter: 3 });
+ expect(card("solitary")?.className).not.toContain("collapsed");
+ expect(scrollIntoView).toHaveBeenCalledTimes(1);
+ await renderWith({ id: "solitary", counter: 4 });
+ expect(scrollIntoView).toHaveBeenCalledTimes(2);
});
});
diff --git a/apps/client/src/widgets/sidebar/RightPanelWidget.tsx b/apps/client/src/widgets/sidebar/RightPanelWidget.tsx
index 7ef101cb95..cd33cb360f 100644
--- a/apps/client/src/widgets/sidebar/RightPanelWidget.tsx
+++ b/apps/client/src/widgets/sidebar/RightPanelWidget.tsx
@@ -54,6 +54,7 @@ interface RightPanelWidgetProps {
export default function RightPanelWidget({ id, title, buttons, children, containerRef: externalContainerRef, contextMenuItems, grow, keepMounted }: RightPanelWidgetProps) {
const [ rightPaneCollapsedItems, setRightPaneCollapsedItems ] = useTriliumOptionJson<string[]>("rightPaneCollapsedItems");
const [ collapsedByUser, setCollapsedByUser ] = useState(rightPaneCollapsedItems.includes(id));
+ const [ scrollPending, setScrollPending ] = useState(false);
const containerRef = useSyncedRef<HTMLDivElement>(externalContainerRef, null);
const parentComponent = useContext(ParentComponent);
const collapsible = useContext(CollapsibleWidgets);
@@ -63,17 +64,27 @@ export default function RightPanelWidget({ id, title, buttons, children, contain
const expanded = collapsible ? !collapsedByUser : true;
// Being asked for by name (see ExpandWidgetRequests) opens the widget, and is remembered as if the
- // user had opened it themselves — so it is still open the next time its tab is built.
+ // user had opened it themselves — so it is still open the next time its tab is built. Strictly
+ // once per request: a collapse of the widget afterwards is the user's and stands.
useEffect(() => {
if (expandRequest?.id !== id) return;
setCollapsedByUser(false);
if (rightPaneCollapsedItems.includes(id)) {
void setRightPaneCollapsedItems(rightPaneCollapsedItems.filter((item) => item !== id));
}
+ setScrollPending(true);
// The request alone triggers this; the remembered list is read as it stands at that moment.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ expandRequest, id ]);
+ // Scrolled to only once the body it opened is laid out, hence the pass of its own — which is also
+ // all such a press amounts to for a widget that was open all along but out of sight below the rest.
+ useEffect(() => {
+ if (!scrollPending || !expanded) return;
+ containerRef.current?.scrollIntoView({ block: "nearest" });
+ setScrollPending(false);
+ }, [ scrollPending, expanded, containerRef ]);
+
if (parentComponent) {
parentComponent.initialized = Promise.resolve();
}