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): let every pane divide itself the same way
Elian Doran4d4652058aa4
diff --git a/apps/client/src/widgets/react/TabStrip.css b/apps/client/src/widgets/react/TabStrip.cssnew file mode 100644index 0000000000..de565e6efa--- /dev/null+++ b/apps/client/src/widgets/react/TabStrip.css@@ -0,0 +1,58 @@+/*+ * The strip centres its group on the row rather than running it up against the start. `safe`+ * centring gives way to the start of the row rather than pushing the first tab out of reach, for a+ * host too narrow to hold the whole group.+ */+.tab-strip {+ display: flex;+ align-items: center;+ justify-content: safe center;+ flex-shrink: 0;+ /* More tabs than the host is wide for is possible at the smallest widths; they scroll rather+ than wrap, which would break the fixed height of the header row they sit in. */+ overflow-x: auto;+ overflow-y: hidden;+ scrollbar-width: none;++ &::-webkit-scrollbar {+ display: none;+ }++ /*+ * The group is the theme's (recessed track, raised active segment); only its size is the strip's+ * own business, the theme building it for a toolbar, where it stands taller than a header row.+ * The button selector out-specifies the theme's own sizing of the segments, which is a tie+ * without it.+ */+ div.btn-group {+ flex-shrink: 0;+ /* As wide as its tabs, whatever a host makes of the button groups among its own fields:+ the strip is centred on the row, which a group stretched across it cannot be. */+ width: auto;+ gap: 2px;+ padding: 2px;++ button.tab-strip-tab {+ --icon-button-size: 26px;+ /* Kept well inside the segment: the theme's own ratio is meant for a segment half again+ as tall, where the same glyph has room to breathe. */+ --icon-button-icon-ratio: 0.62;++ cursor: pointer;+ transition: background-color 0.2s ease;+ }+ }++ .tab-strip-tab-icon {+ /* Sized by the same formula the theme sizes an icon button's glyph by. It is spelled out+ here because that rule reaches the glyph through `::before`, and this one is a span of its+ own — which is what the strip needs to keep the segment and its icon separately sized. */+ font-size: calc(var(--icon-button-size) * var(--icon-button-icon-ratio));+ /* The glyph is what is being centred: themes centre icons with `text-align`, which leaves it+ on the text baseline of its span rather than in the middle of the segment. */+ display: flex;+ align-items: center;+ justify-content: center;+ line-height: 1;+ }+}diff --git a/apps/client/src/widgets/react/TabStrip.tsx b/apps/client/src/widgets/react/TabStrip.tsxnew file mode 100644index 0000000000..7658a94691--- /dev/null+++ b/apps/client/src/widgets/react/TabStrip.tsx@@ -0,0 +1,76 @@+import "./TabStrip.css";++import clsx from "clsx";+import { useRef } from "preact/hooks";++import { useStaticTooltip } from "./hooks";++export interface TabStripTabDefinition<T extends string> {+ id: T;+ title: string;+ /** A full icon class, e.g. `bx bx-list-ul`. */+ icon: string;+}++interface TabStripProps<T extends string> {+ tabs: TabStripTabDefinition<T>[];+ activeTabId?: T;+ onSelect: (tabId: T) => void;+ /** The host's own class, for the geometry of the row the strip heads. */+ className?: string;+}++/**+ * A row of tabs shown as icons alone, each named by a tooltip: the header of a panel divided into+ * groups of widgets or fields.+ *+ * Drawn as a button group, which is how Trilium draws a choice between several things: a recessed+ * track with the one on show raised out of it. The theme dresses the class; all the strip does is+ * size it to a header row, and leave the row itself to whoever it heads (see {@link className}).+ */+export default function TabStrip<T extends string>({ tabs, activeTabId, onSelect, className }: TabStripProps<T>) {+ return (+ // Centred in the row rather than run up against its start (see TabStrip.css).+ <div class={clsx("tab-strip", className)}>+ <div class="btn-group tab-strip-group" role="tablist">+ {tabs.map((tab) => (+ <TabStripTab+ key={tab.id}+ tab={tab}+ active={tab.id === activeTabId}+ onSelect={() => onSelect(tab.id)}+ />+ ))}+ </div>+ </div>+ );+}++function TabStripTab<T extends string>({ tab, active, onSelect }: { tab: TabStripTabDefinition<T>; active: boolean; onSelect: () => void }) {+ const buttonRef = useRef<HTMLButtonElement>(null);+ // No tab is named in the strip, the selected one included: every tab is the same icon-sized+ // segment, which is what keeps a whole group inside a panel narrow enough to only fit the icons.+ // The name is the tooltip, and it is the only place it appears.+ useStaticTooltip(buttonRef, {+ title: tab.title,+ placement: "bottom",+ fallbackPlacements: [ "bottom" ],+ animation: false+ });++ return (+ <button+ ref={buttonRef}+ type="button"+ role="tab"+ aria-selected={active}+ aria-label={tab.title}+ // `icon-action` is what the button group sizes and fills its segments by; `active` is what+ // it raises the one on show with.+ class={clsx("tab-strip-tab icon-action", active && "active")}+ onClick={onSelect}+ >+ <span class={clsx("tab-strip-tab-icon tn-icon", tab.icon)} />+ </button>+ );+}diff --git a/apps/client/src/widgets/sidebar/RightPaneTabs.css b/apps/client/src/widgets/sidebar/RightPaneTabs.cssindex cbf303765d..4f519c06ac 100644--- a/apps/client/src/widgets/sidebar/RightPaneTabs.css+++ b/apps/client/src/widgets/sidebar/RightPaneTabs.css@@ -1,55 +1,13 @@/** The tab strip is also the pane's header row: the pin/close actions are absolutely positioned over its* end (see RightPanelContainer.css), and as much room again is kept free at the start, so the group is- * centred in the pane rather than in what is left of the row. `safe` centring gives way to the start of- * the row rather than pushing the first tab out of reach, for a pane too narrow to hold the group.+ * centred in the pane rather than in what is left of the row.+ *+ * Everything the strip itself is — the centring, the group, the size of a tab — is shared with the other+ * strips (see TabStrip.css); what is left here is the row it is given to head.*/.right-pane-tabs {- display: flex;- align-items: center;- justify-content: safe center;- flex-shrink: 0;height: var(--right-pane-header-height);padding-inline: 50px;border-bottom: 1px solid var(--main-border-color);- /* More tabs than the pane is wide for is possible at the smallest widths; they scroll rather than- wrap, which would break the fixed header height. */- overflow-x: auto;- overflow-y: hidden;- scrollbar-width: none;-- &::-webkit-scrollbar {- display: none;- }-- /*- * The group is the theme's (recessed track, raised active segment); only its size is this pane's own- * business, the theme building it for a toolbar, where it stands taller than a header row. The- * button selector out-specifies the theme's own sizing of the segments, which is a tie without it.- */- div.btn-group {- flex-shrink: 0;- gap: 2px;- padding: 2px;-- button.right-pane-tab {- --icon-button-size: 26px;- /* Kept well inside the segment: the theme's own ratio is meant for a segment half again as- tall, where the same glyph has room to breathe. */- --icon-button-icon-ratio: 0.62;- }- }-- .right-pane-tab-icon {- /* Sized by the same formula the theme sizes an icon button's glyph by. It is spelled out here- because that rule reaches the glyph through `::before`, and this one is a span of its own —- which is what the strip needs to keep the segment and its icon separately sized. */- font-size: calc(var(--icon-button-size) * var(--icon-button-icon-ratio));- /* The glyph is what is being centred: themes centre icons with `text-align`, which leaves it on- the text baseline of its span rather than in the middle of the segment. */- display: flex;- align-items: center;- justify-content: center;- line-height: 1;- }}diff --git a/apps/client/src/widgets/sidebar/RightPaneTabs.tsx b/apps/client/src/widgets/sidebar/RightPaneTabs.tsxindex c60dfcf17e..f4e3dc9e2d 100644--- a/apps/client/src/widgets/sidebar/RightPaneTabs.tsx+++ b/apps/client/src/widgets/sidebar/RightPaneTabs.tsx@@ -1,17 +1,11 @@import "./RightPaneTabs.css";-import clsx from "clsx";-import { useRef } from "preact/hooks";-import { t } from "../../services/i18n";-import { useStaticTooltip } from "../react/hooks";+import TabStrip, { type TabStripTabDefinition } from "../react/TabStrip";export type RightPaneTabId = "outline" | "attributes" | "connections" | "chat" | "widgets";-export interface RightPaneTabDefinition {- id: RightPaneTabId;- title: string;- icon: string;+export interface RightPaneTabDefinition extends TabStripTabDefinition<RightPaneTabId> {/*** Keep the tab in the strip for a note it has nothing to show, saying so in its body instead of* going away. For a tab whose widgets come and go with the type of the note being read: leaving@@ -47,54 +41,16 @@ interface RightPaneTabsProps {/*** The right pane's header: which group of widgets is on show. It doubles as the row the pane's own- * pin/close actions sit in, so it is rendered whenever the pane is open.+ * pin/close actions sit in, so it is rendered whenever the pane is open — which is what the row it+ * hands the shared strip is for (see RightPaneTabs.css).*/export default function RightPaneTabs({ tabs, activeTabId, onSelect }: RightPaneTabsProps) {return (- // Centred in the row rather than run up against its start (see RightPaneTabs.css).- <div class="right-pane-tabs">- {/* A button group, which is how Trilium draws a choice between several things: a recessed- track with the one on show raised out of it. The theme dresses the class; all this pane- does is size it to a header row (see RightPaneTabs.css). */}- <div class="btn-group right-pane-tab-group" role="tablist">- {tabs.map((tab) => (- <RightPaneTab- key={tab.id}- tab={tab}- active={tab.id === activeTabId}- onSelect={() => onSelect(tab.id)}- />- ))}- </div>- </div>- );-}--function RightPaneTab({ tab, active, onSelect }: { tab: RightPaneTabDefinition; active: boolean; onSelect: () => void }) {- const buttonRef = useRef<HTMLButtonElement>(null);- // No tab is named in the strip, the selected one included: every tab is the same icon-sized segment,- // which is what keeps the whole group inside a pane narrow enough to only fit the icons. The name is- // the tooltip, and it is the only place it appears.- useStaticTooltip(buttonRef, {- title: tab.title,- placement: "bottom",- fallbackPlacements: [ "bottom" ],- animation: false- });-- return (- <button- ref={buttonRef}- type="button"- role="tab"- aria-selected={active}- aria-label={tab.title}- // `icon-action` is what the button group sizes and fills its segments by; `active` is what- // it raises the one on show with.- class={clsx("right-pane-tab icon-action", active && "active")}- onClick={onSelect}- >- <span class={clsx("right-pane-tab-icon tn-icon", tab.icon)} />- </button>+ <TabStrip+ className="right-pane-tabs"+ tabs={tabs}+ activeTabId={activeTabId}+ onSelect={onSelect}+ />);}diff --git a/apps/client/src/widgets/type_widgets/mind_map/NodePanel.css b/apps/client/src/widgets/type_widgets/mind_map/NodePanel.cssindex 2927fc199f..52414e6721 100644--- a/apps/client/src/widgets/type_widgets/mind_map/NodePanel.css+++ b/apps/client/src/widgets/type_widgets/mind_map/NodePanel.css@@ -23,24 +23,14 @@}/*- * The strip parts from the tab under it by a line the width of the panel, so that it reads as the- * panel's header rather than as one more choice among the fields — which is what the same button- * group means everywhere else in here (the font sizes, the picture's shapes).+ * The strip is the right pane's, at the size it stands there (see TabStrip.css); what is left here+ * is the row it heads. It parts from the tab under it by a line the width of the panel, so that it+ * reads as the panel's header rather than as one more choice among the fields — which is what a+ * button group means everywhere else in here (the font sizes, the picture's shapes).*/.mind-map-node-panel .mind-map-node-panel-tabs {- display: flex;- justify-content: center;- flex-shrink: 0;padding: 8px;border-bottom: 1px solid var(--main-border-color);-- .mind-map-node-panel-tab {- /* The icon leads the name, as it does on a tab of the right pane. */- display: flex;- align-items: center;- justify-content: center;- gap: 5px;- }}/* Each tab holds the padding, rather than the panel: the memo's is its own (see below). */diff --git a/apps/client/src/widgets/type_widgets/mind_map/NodePanel.tsx b/apps/client/src/widgets/type_widgets/mind_map/NodePanel.tsxindex e95c46831b..ef3af490c9 100644--- a/apps/client/src/widgets/type_widgets/mind_map/NodePanel.tsx+++ b/apps/client/src/widgets/type_widgets/mind_map/NodePanel.tsx@@ -22,6 +22,7 @@ import IconPicker from "../../react/IconPicker";import NoteAutocomplete from "../../react/NoteAutocomplete";import { refToJQuerySelector } from "../../react/react_utils";import SegmentedChoice from "../../react/SegmentedChoice";+import TabStrip, { type TabStripTabDefinition } from "../../react/TabStrip";import { fitNodeImage, getNodeImageShape, nearestNodeImageWidth, NODE_IMAGE_SHAPES, NODE_IMAGE_WIDTHS, type NodeImage as NodeImageData, type NodeImageShape, shapeNodeImage, uploadNodeImage } from "./images";import { describeExternalLink, linkFromSuggestion } from "./links";import NodeMemo, { getNodeMemo } from "./NodeMemo";@@ -131,7 +132,12 @@ export default function NodePanel({ mind, noteId, nodes }: NodePanelProps) {onMouseDown={(e) => e.stopPropagation()}onKeyDown={(e) => e.stopPropagation()}>- <NodePanelTabs activeTabId={activeTabId} onSelect={setActiveTabId} />+ <TabStrip+ className="mind-map-node-panel-tabs"+ tabs={buildTabs()}+ activeTabId={activeTabId}+ onSelect={setActiveTabId}+ /><divrole="tabpanel"@@ -266,47 +272,13 @@ type NodePanelTabId = "format" | "memo";* rather than picked from, and a paragraph of prose wants the room the whole panel has — which, at* the foot of eight other fields, meant scrolling to the bottom to reach a box three lines tall.*/-function buildTabs(): { id: NodePanelTabId, title: string, icon: string }[] {+function buildTabs(): TabStripTabDefinition<NodePanelTabId>[] {return [{ id: "format", title: t("mind-map.tab-format"), icon: "bx bx-palette" },{ id: "memo", title: t("mind-map.memo"), icon: "bx bx-notepad" }];}-/**- * The panel's header: which group of fields is on show.- *- * Built as the right pane's tab strip is — a button group as a tablist, the tab on show raised out- * of the recessed track by the theme — but with each tab named beside its icon rather than left to- * a tooltip. That strip is icon-only because five tabs have to fit a pane; two tabs have the width- * of this panel between them, and the same button group the panel already uses for its font sizes- * spells its segments out.- */-function NodePanelTabs({ activeTabId, onSelect }: { activeTabId: NodePanelTabId, onSelect: (tabId: NodePanelTabId) => void }) {- return (- <div className="mind-map-node-panel-tabs">- <div className="btn-group btn-group-sm" role="tablist">- {buildTabs().map((tab) => (- <button- key={tab.id}- type="button"- role="tab"- aria-selected={tab.id === activeTabId}- // `active` is what the theme raises the tab on show with; the rest is the- // segment the panel's other button groups are built from, so the strip- // carries the same weight as the rows under it.- className={clsx("btn btn-secondary btn-sm mind-map-node-panel-tab", tab.id === activeTabId && "active")}- onClick={() => onSelect(tab.id)}- >- <Icon icon={tab.icon} />- {tab.title}- </button>- ))}- </div>- </div>- );-}-/*** A node of medium size is one with no size of its own: that is what a node comes with, and it* leaves the root, which is larger by default, at the size its level implies rather than pinning