Skip to content

Build your personal knowledge base with Trilium Notes

feat(mobile): put the note panel tabs' controls in its header

Elian Doran712a6ee9943c
diff --git a/apps/client/src/stylesheets/theme-next/ios.css b/apps/client/src/stylesheets/theme-next/ios.css
index f83ec3f19c..36b5af99ce 100644
--- a/apps/client/src/stylesheets/theme-next/ios.css
+++ b/apps/client/src/stylesheets/theme-next/ios.css
@@ -94,6 +94,8 @@ body.mobile.ios {
--note-panel-group-spacing: 22px;
--note-panel-group-background: var(--grouped-card-background-color);
--note-panel-card-padding: 12px 16px;
+ /* The close button's 44px and the 8px gap before it. */
+ --note-panel-close-width: 52px;
--note-panel-separator-color: var(--main-border-color);
/* Starts under the attribute's name: the row's padding, the 1.1em kind icon and the 6px gap. */
--note-panel-separator-inset: calc(16px + 1.1em + 6px);
@@ -405,11 +407,11 @@ body.mobile.ios {
.tn-master-detail-header {
padding-inline-start: 52px;
- &:has(> button.icon-action) {
+ &:has(> button.icon-action, > .note-panel-header-slot:not([hidden], :empty)) {
padding-inline-start: 0;
}
- > button.icon-action {
+ button.icon-action {
--icon-button-size: 44px;
--icon-button-icon-ratio: 0.55;
--icon-button-color: var(--main-text-color);
diff --git a/apps/client/src/widgets/mobile_widgets/MobileNotePanel.css b/apps/client/src/widgets/mobile_widgets/MobileNotePanel.css
index 8753eff179..8c2f639bc4 100644
--- a/apps/client/src/widgets/mobile_widgets/MobileNotePanel.css
+++ b/apps/client/src/widgets/mobile_widgets/MobileNotePanel.css
@@ -7,6 +7,45 @@ body.mobile:has(.virtual-keyboard-opened) .modal.mobile-note-panel .modal-footer
}
body.mobile .modal.mobile-note-panel {
+ .note-panel-header-slot {
+ display: flex;
+ flex: 1;
+ align-items: center;
+ gap: 4px;
+ min-width: 0;
+
+ &[hidden],
+ &:empty {
+ display: none;
+ }
+ }
+
+ :is(.modal-header, .tn-master-detail-header):has(> .note-panel-header-slot:not([hidden], :empty)) > :is(.modal-title, .tn-master-detail-title) {
+ display: none;
+ }
+
+ /* Centred on the sheet: the close button after the slot is offset by the same width before it. */
+ .note-panel-header-slot .sidebar-chat-title-dropdown {
+ flex: 1;
+ min-width: 0;
+ display: flex;
+ justify-content: center;
+ padding-inline-start: var(--note-panel-close-width, 0px);
+
+ > button {
+ max-width: 100%;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ border: 0;
+ background: transparent;
+ box-shadow: none;
+ color: var(--main-text-color);
+ font-size: 1.1em;
+ font-weight: 600;
+ }
+ }
+
.modal-footer {
padding: 4px 8px calc(max(var(--safe-area-inset-bottom, env(safe-area-inset-bottom)), 8px));
border-top: 1px solid var(--main-border-color);
@@ -250,9 +289,13 @@ body.mobile .modal.mobile-note-panel {
.attribute-toolbar {
--icon-button-size: 36px;
- height: 48px;
+ position: static;
+ flex: 1;
+ min-width: 0;
+ height: auto;
+ padding: 0;
border-bottom: 0;
- background-color: var(--modal-background-color);
+ background: transparent;
.form-checkbox label,
.attribute-selection-count,
diff --git a/apps/client/src/widgets/mobile_widgets/MobileNotePanel.tsx b/apps/client/src/widgets/mobile_widgets/MobileNotePanel.tsx
index fc71e3a69c..daa8d50b5c 100644
--- a/apps/client/src/widgets/mobile_widgets/MobileNotePanel.tsx
+++ b/apps/client/src/widgets/mobile_widgets/MobileNotePanel.tsx
@@ -1,7 +1,7 @@
import "./MobileNotePanel.css";
import { createPortal } from "preact";
-import { useState } from "preact/hooks";
+import { useCallback, useRef, useState } from "preact/hooks";
import type { WidgetsByParent } from "../../services/bundle";
import { t } from "../../services/i18n";
@@ -56,6 +56,16 @@ function MobileNotePanelModal({ shown, onHidden, widgetsByParent, selectedTabId,
}) {
const tabs = useRightPaneTabs(shown, widgetsByParent).map(withFixedHeightWidgetsLast);
const activeTab = tabs.find((tab) => tab.id === selectedTabId) ?? tabs[0];
+ const [ headerSlots, setHeaderSlotRef ] = useHeaderSlots();
+ // One slot per tab, only the active one shown; a tab that fills its slot replaces the title.
+ const headerSlotElements = tabs.map((tab) => (
+ <div
+ key={tab.id}
+ className="note-panel-header-slot"
+ hidden={tab.id !== activeTab?.id}
+ ref={setHeaderSlotRef(tab.id)}
+ />
+ ));
return (
// A master-detail modal, so that editing an attribute is a page of its own, headed by a way
@@ -64,6 +74,8 @@ function MobileNotePanelModal({ shown, onHidden, widgetsByParent, selectedTabId,
className="mobile-note-panel"
size="md"
title={activeTab?.title}
+ header={headerSlotElements}
+ listActions={headerSlotElements}
footer={(
<TabStrip
className="mobile-note-panel-tabs" labelled withIcons
@@ -79,13 +91,30 @@ function MobileNotePanelModal({ shown, onHidden, widgetsByParent, selectedTabId,
mobile layout has no other element for. */}
<div id="right-pane">
<NavigatedInNote.Provider value={onHidden}>
- <RightPaneTabBodies tabs={tabs} activeTabId={activeTab?.id} flat />
+ <RightPaneTabBodies tabs={tabs} activeTabId={activeTab?.id} headerSlots={headerSlots} flat />
</NavigatedInNote.Provider>
</div>
</MasterDetailModal>
);
}
+/** The header slot of each tab, with a stable ref callback per tab to collect them. */
+function useHeaderSlots() {
+ const [ slots, setSlots ] = useState<Partial<Record<RightPaneTabId, HTMLElement | null>>>({});
+ const callbacks = useRef(new Map<RightPaneTabId, (element: HTMLElement | null) => void>());
+
+ const refFor = useCallback((tabId: RightPaneTabId) => {
+ let callback = callbacks.current.get(tabId);
+ if (!callback) {
+ callback = (element) => setSlots((prev) => prev[tabId] === element ? prev : { ...prev, [tabId]: element });
+ callbacks.current.set(tabId, callback);
+ }
+ return callback;
+ }, []);
+
+ return [ slots, refFor ] as const;
+}
+
/**
* Moves `NoteMap` and `PdfPages` to the end of the tab: both are drawn to a fixed height, which in a
* tab that scrolls as a whole would sit between the sections around them.
diff --git a/apps/client/src/widgets/sidebar/AttributeList.tsx b/apps/client/src/widgets/sidebar/AttributeList.tsx
index e0dcc3aaf3..552fdac870 100644
--- a/apps/client/src/widgets/sidebar/AttributeList.tsx
+++ b/apps/client/src/widgets/sidebar/AttributeList.tsx
@@ -36,7 +36,7 @@ import { ParentComponent } from "../react/react_utils";
import OptionsSection from "../type_widgets/options/components/OptionsSection";
import AttributeCreationEditor from "./AttributeCreationEditor";
import AttributeValueEditor, { resolveValueField } from "./AttributeValueEditor";
-import RightPanelWidget, { CollapsibleWidgets } from "./RightPanelWidget";
+import RightPanelWidget, { CollapsibleWidgets, TabHeaderSlot } from "./RightPanelWidget";
import SidebarHelp from "./SidebarHelp";
/**
@@ -56,6 +56,7 @@ export default function AttributeList() {
const { note } = useActiveNoteContext();
const canEdit = !!note && note.isContentWritable;
const { isMasterDetail } = useMasterDetail();
+ const headerSlot = useContext(TabHeaderSlot);
const parentComponent = useContext(ParentComponent);
const containerRef = useRef<HTMLDivElement>(null);
const [ detail, setDetail ] = useState<AttributeDetailOpts | null>(null);
@@ -814,7 +815,7 @@ export default function AttributeList() {
// A card per run. With a single card, there is nothing to collapse it in favor of.
const sectionList = (
<CollapsibleWidgets.Provider value={runs.length > 1}>
- {toolbar}
+ {toolbar && headerSlot ? createPortal(toolbar, headerSlot) : toolbar}
{runs.map((run, index) => (
<AttributeSection
diff --git a/apps/client/src/widgets/sidebar/RightPanelContainer.tsx b/apps/client/src/widgets/sidebar/RightPanelContainer.tsx
index 778fc42fe0..0fd6a1f286 100644
--- a/apps/client/src/widgets/sidebar/RightPanelContainer.tsx
+++ b/apps/client/src/widgets/sidebar/RightPanelContainer.tsx
@@ -29,7 +29,7 @@ import PdfAnnotations from "./pdf/PdfAnnotations";
import PdfAttachments from "./pdf/PdfAttachments";
import PdfLayers from "./pdf/PdfLayers";
import PdfPages from "./pdf/PdfPages";
-import RightPanelWidget, { CollapsibleWidgets, ExpandWidgetRequest, ExpandWidgetRequests, StackedWidgets } from "./RightPanelWidget";
+import RightPanelWidget, { CollapsibleWidgets, ExpandWidgetRequest, ExpandWidgetRequests, StackedWidgets, TabHeaderSlot } from "./RightPanelWidget";
import RightPaneTabs, { RIGHT_PANE_TABS, RightPaneTabDefinition, RightPaneTabId } from "./RightPaneTabs";
import TableOfContents from "./TableOfContents";
@@ -196,7 +196,14 @@ export default function RightPanelContainer({ widgetsByParent }: { widgetsByPare
* A `flat` tab scrolls as a whole, its widgets stacked at their content's height with nothing to
* collapse, as the mobile note panel lays it out.
*/
-export function RightPaneTabBodies({ tabs, activeTabId, expandRequest, flat }: { tabs: RightPaneTabContents[]; activeTabId?: RightPaneTabId; expandRequest?: ExpandWidgetRequest | null; flat?: boolean }) {
+export function RightPaneTabBodies({ tabs, activeTabId, expandRequest, flat, headerSlots }: {
+ tabs: RightPaneTabContents[];
+ activeTabId?: RightPaneTabId;
+ expandRequest?: ExpandWidgetRequest | null;
+ flat?: boolean;
+ /** Per tab, the host's header element its widgets put their controls in (see `TabHeaderSlot`). */
+ headerSlots?: Partial<Record<RightPaneTabId, HTMLElement | null>>;
+}) {
const openedTabs = useRef(new Set<RightPaneTabId>());
if (activeTabId) {
openedTabs.current.add(activeTabId);
@@ -214,11 +221,13 @@ export function RightPaneTabBodies({ tabs, activeTabId, expandRequest, flat }: {
<CollapsibleWidgets.Provider value={!flat && tab.items.length > 1}>
<StackedWidgets.Provider value={!!flat && tab.items.length > 1}>
<ExpandWidgetRequests.Provider value={expandRequest ?? null}>
- {tab.items.length > 0 ? tab.items : (
- // A tab that stays for a note it has nothing to show
- // (see RightPaneTabDefinition.alwaysShown) says so.
- <NoItems icon={tab.icon} text={t("right_pane.empty_message")} />
- )}
+ <TabHeaderSlot.Provider value={headerSlots?.[tab.id] ?? null}>
+ {tab.items.length > 0 ? tab.items : (
+ // A tab that stays for a note it has nothing to show
+ // (see RightPaneTabDefinition.alwaysShown) says so.
+ <NoItems icon={tab.icon} text={t("right_pane.empty_message")} />
+ )}
+ </TabHeaderSlot.Provider>
</ExpandWidgetRequests.Provider>
</StackedWidgets.Provider>
</CollapsibleWidgets.Provider>
diff --git a/apps/client/src/widgets/sidebar/RightPanelWidget.tsx b/apps/client/src/widgets/sidebar/RightPanelWidget.tsx
index 49eb92cec4..8a91f32c22 100644
--- a/apps/client/src/widgets/sidebar/RightPanelWidget.tsx
+++ b/apps/client/src/widgets/sidebar/RightPanelWidget.tsx
@@ -32,6 +32,12 @@ export const ExpandWidgetRequests = createContext<ExpandWidgetRequest | null>(nu
*/
export const StackedWidgets = createContext(false);
+/**
+ * An element in the host's header where the widgets of a tab put their controls instead of their own
+ * header. Set per tab by the mobile note panel, `null` elsewhere.
+ */
+export const TabHeaderSlot = createContext<HTMLElement | null>(null);
+
/**
* Called after a widget scrolls the note to a place in it (a heading, a PDF page), set by a host that
* covers the note, such as the mobile note panel, so that it can close and show that place.
diff --git a/apps/client/src/widgets/sidebar/SidebarChat.spec.tsx b/apps/client/src/widgets/sidebar/SidebarChat.spec.tsx
index e04ef0f892..c20bd3f30a 100644
--- a/apps/client/src/widgets/sidebar/SidebarChat.spec.tsx
+++ b/apps/client/src/widgets/sidebar/SidebarChat.spec.tsx
@@ -72,10 +72,15 @@ vi.mock("../react/FormList.js", () => ({
<li data-icon={icon} onClick={onClick}>{children}</li>,
FormDropdownDivider: () => null
}));
-vi.mock("./RightPanelWidget.js", () => ({
- default: ({ buttons, children }: { buttons: ComponentChildren; children: ComponentChildren }) => <div>{buttons}{children}</div>
-}));
+vi.mock("./RightPanelWidget.js", async () => {
+ const { createContext } = await import("preact");
+ return {
+ default: ({ buttons, children }: { buttons: ComponentChildren; children: ComponentChildren }) => <div>{buttons}{children}</div>,
+ TabHeaderSlot: createContext<HTMLElement | null>(null)
+ };
+});
+import { TabHeaderSlot } from "./RightPanelWidget.js";
import SidebarChat from "./SidebarChat";
describe("SidebarChat", () => {
@@ -104,6 +109,22 @@ describe("SidebarChat", () => {
expect(fakeChat.submittedTo).toBe("chatNew1");
expect(serverChats).toEqual([ "chatNew1", "chatOlder" ]);
});
+
+ it("puts its controls in the host's header slot when there is one", async () => {
+ const slot = document.createElement("div");
+ document.body.appendChild(slot);
+ const container = renderInto(
+ <TabHeaderSlot.Provider value={slot}>
+ <SidebarChat />
+ </TabHeaderSlot.Provider>
+ );
+ await vi.waitFor(() => expect(fakeChat.chatNoteId).toBe("chatOpen"));
+
+ expect(slot.querySelector("[data-icon='bx bx-plus']")).not.toBeNull();
+ expect(slot.querySelector("li[data-icon='bx bx-trash']")).not.toBeNull();
+ expect(container.querySelector("[data-icon='bx bx-plus']")).toBeNull();
+ slot.remove();
+ });
});
function createChat() {
diff --git a/apps/client/src/widgets/sidebar/SidebarChat.tsx b/apps/client/src/widgets/sidebar/SidebarChat.tsx
index 66f3174608..1d1a8ed802 100644
--- a/apps/client/src/widgets/sidebar/SidebarChat.tsx
+++ b/apps/client/src/widgets/sidebar/SidebarChat.tsx
@@ -1,7 +1,8 @@
import "./SidebarChat.css";
import type { SaveLlmChatResponse } from "@triliumnext/commons";
-import { useCallback, useEffect, useRef, useState } from "preact/hooks";
+import { createPortal } from "preact";
+import { useCallback, useContext, useEffect, useRef, useState } from "preact/hooks";
import appContext from "../../components/app_context.js";
import dateNoteService, { type RecentLlmChat } from "../../services/date_notes.js";
@@ -22,7 +23,7 @@ import ChatMessageList from "../type_widgets/llm_chat/ChatMessageList.js";
import ChatReadOnlyNotice from "../type_widgets/llm_chat/ChatReadOnlyNotice.js";
import type { LlmChatContent } from "../type_widgets/llm_chat/llm_chat_types.js";
import { useLlmChat } from "../type_widgets/llm_chat/useLlmChat.js";
-import RightPanelWidget from "./RightPanelWidget.js";
+import RightPanelWidget, { TabHeaderSlot } from "./RightPanelWidget.js";
/**
* Sidebar chat widget that appears in the right panel.
@@ -38,6 +39,7 @@ export default function SidebarChat() {
const [chatNoteId, setChatNoteId] = useState<string | null>(null);
const [recentChats, setRecentChats] = useState<RecentLlmChat[]>([]);
const historyDropdownRef = useRef<DropdownHandle | null>(null);
+ const headerSlot = useContext(TabHeaderSlot);
// Get the current active note context
const { noteId: activeNoteId, note: activeNote } = useActiveNoteContext();
@@ -292,6 +294,96 @@ export default function SidebarChat() {
}
}, [chatNoteId, chatNote]);
+ const newChatButton = (
+ <ActionButton
+ icon="bx bx-plus"
+ text={t("sidebar_chat.new_chat")}
+ onClick={handleNewChat}
+ />
+ );
+ const historyItems = (
+ <>
+ {recentChats.length === 0 ? (
+ <FormListItem disabled>
+ {t("sidebar_chat.no_chats")}
+ </FormListItem>
+ ) : (
+ recentChats.map(chatItem => (
+ <FormListItem
+ key={chatItem.noteId}
+ icon="bx bx-message-square-dots"
+ className={`sidebar-chat-history-item ${chatItem.noteId === chatNoteId ? "active" : ""}`}
+ onClick={() => handleSelectChat(chatItem.noteId)}
+ >
+ <div className="sidebar-chat-history-item-content">
+ {chatItem.noteId === chatNoteId
+ ? <strong>{chatItem.title}</strong>
+ : <span>{chatItem.title}</span>}
+ <span className="sidebar-chat-history-date">
+ {formatDateTime(new Date(chatItem.dateModified), "short", "short")}
+ </span>
+ </div>
+ <ActionButton
+ icon="bx bx-trash"
+ text={t("sidebar_chat.delete_chat")}
+ className="sidebar-chat-history-delete"
+ onClick={(e) => {
+ e.stopPropagation();
+ void handleDeleteChat(chatItem.noteId);
+ }}
+ />
+ </FormListItem>
+ ))
+ )}
+ <FormDropdownDivider />
+ <FormListItem
+ icon="bx bx-folder-open"
+ onClick={handleViewAllChats}
+ >
+ {t("sidebar_chat.view_all_chats")}
+ </FormListItem>
+ </>
+ );
+ const historyProps = {
+ title: t("sidebar_chat.history"),
+ dropdownRef: historyDropdownRef,
+ onShown: loadRecentChats,
+ mobileBottomSheet: true
+ };
+ const moreActions = (
+ <Dropdown
+ text=""
+ buttonClassName="bx bx-dots-vertical-rounded"
+ title={t("sidebar_chat.more_actions")}
+ iconAction
+ hideToggleArrow
+ mobileBottomSheet
+ >
+ <FormListItem
+ icon="bx bx-save"
+ onClick={() => void handleSaveChat()}
+ disabled={chat.messages.length === 0}
+ >
+ {t("sidebar_chat.save_chat")}
+ </FormListItem>
+ <FormListItem
+ icon="bx bx-edit-alt"
+ onClick={() => void handleRenameChat()}
+ disabled={!chatNoteId}
+ >
+ {t("sidebar_chat.rename")}
+ </FormListItem>
+ <FormDropdownDivider />
+ <FormListItem
+ icon="bx bx-trash"
+ onClick={() => { if (chatNoteId) void handleDeleteChat(chatNoteId); }}
+ disabled={!chatNoteId}
+ >
+ {t("sidebar_chat.delete")}
+ </FormListItem>
+ </Dropdown>
+ );
+
return (
<RightPanelWidget
id="sidebar-chat"
@@ -301,97 +393,26 @@ export default function SidebarChat() {
// highlight / scroll listeners are wired in this (always-mounted) component, so unmounting the
// body on collapse would orphan them and they wouldn't re-attach on expand.
keepMounted
- buttons={
+ buttons={!headerSlot && (
<>
- <ActionButton
- icon="bx bx-plus"
- text={t("sidebar_chat.new_chat")}
- onClick={handleNewChat}
- />
- <Dropdown
- text=""
- buttonClassName="bx bx-history"
- title={t("sidebar_chat.history")}
- iconAction
- hideToggleArrow
- dropdownRef={historyDropdownRef}
- onShown={loadRecentChats}
- mobileBottomSheet
- >
- {recentChats.length === 0 ? (
- <FormListItem disabled>
- {t("sidebar_chat.no_chats")}
- </FormListItem>
- ) : (
- recentChats.map(chatItem => (
- <FormListItem
- key={chatItem.noteId}
- icon="bx bx-message-square-dots"
- className={`sidebar-chat-history-item ${chatItem.noteId === chatNoteId ? "active" : ""}`}
- onClick={() => handleSelectChat(chatItem.noteId)}
- >
- <div className="sidebar-chat-history-item-content">
- {chatItem.noteId === chatNoteId
- ? <strong>{chatItem.title}</strong>
- : <span>{chatItem.title}</span>}
- <span className="sidebar-chat-history-date">
- {formatDateTime(new Date(chatItem.dateModified), "short", "short")}
- </span>
- </div>
- <ActionButton
- icon="bx bx-trash"
- text={t("sidebar_chat.delete_chat")}
- className="sidebar-chat-history-delete"
- onClick={(e) => {
- e.stopPropagation();
- void handleDeleteChat(chatItem.noteId);
- }}
- />
- </FormListItem>
- ))
- )}
- <FormDropdownDivider />
- <FormListItem
- icon="bx bx-folder-open"
- onClick={handleViewAllChats}
- >
- {t("sidebar_chat.view_all_chats")}
- </FormListItem>
- </Dropdown>
- <Dropdown
- text=""
- buttonClassName="bx bx-dots-vertical-rounded"
- title={t("sidebar_chat.more_actions")}
- iconAction
- hideToggleArrow
- mobileBottomSheet
- >
- <FormListItem
- icon="bx bx-save"
- onClick={() => void handleSaveChat()}
- disabled={chat.messages.length === 0}
- >
- {t("sidebar_chat.save_chat")}
- </FormListItem>
- <FormListItem
- icon="bx bx-edit-alt"
- onClick={() => void handleRenameChat()}
- disabled={!chatNoteId}
- >
- {t("sidebar_chat.rename")}
- </FormListItem>
- <FormDropdownDivider />
- <FormListItem
- icon="bx bx-trash"
- onClick={() => { if (chatNoteId) void handleDeleteChat(chatNoteId); }}
- disabled={!chatNoteId}
- >
- {t("sidebar_chat.delete")}
- </FormListItem>
+ {newChatButton}
+ <Dropdown text="" buttonClassName="bx bx-history" iconAction hideToggleArrow {...historyProps}>
+ {historyItems}
</Dropdown>
+ {moreActions}
</>
- }
+ )}
>
+ {/* In a host header, the conversation's title opens the history. */}
+ {headerSlot && createPortal((
+ <>
+ {newChatButton}
+ <Dropdown text={chatTitle} className="sidebar-chat-title-dropdown" noSelectButtonStyle {...historyProps}>
+ {historyItems}
+ </Dropdown>
+ {moreActions}
+ </>
+ ), headerSlot)}
<div className="sidebar-chat-container">
<ChatMessageList
chat={chat}