feat: Keep expanded collection sticky at the top of the sidebar (#14907)

* feat: Keep expanded collection sticky at the top of the sidebar

* fix: Uncover rows under the sticky collection row, contain archived collection rows
This commit is contained in:
Tom Moor authored and GitHub committed 2026-12-05 22:21:40 -05:00
1 parent 89f296edc2
commit 410f9d2ce2
3 files changed
+34 -12

No files matched your search

@@ -26,13 +26,16 @@ export function ArchivedCollectionLink({ collection, depth = 0 }: Props) {
setExpanded(true);
}, []);
// The wrapper limits the sticky collection row to its own documents.
return (
<CollectionLink
depth={depth}
collection={collection}
expanded={expanded}
onDisclosureClick={handleDisclosureClick}
onClick={handleClick}
/>
<div>
<CollectionLink
depth={depth}
collection={collection}
expanded={expanded}
onDisclosureClick={handleDisclosureClick}
onClick={handleClick}
/>
</div>
);
}
@@ -6,6 +6,8 @@ import { flushSync } from "react-dom";
import { useTranslation } from "react-i18next";
import { mergeRefs } from "react-merge-refs";
import type { match } from "react-router";
import styled from "styled-components";
import { s } from "@shared/styles";
import { isTouchDevice } from "@shared/utils/browser";
import { CollectionValidation, DocumentValidation } from "@shared/validations";
import type Collection from "~/models/Collection";
@@ -17,7 +19,6 @@ import Tooltip from "~/components/Tooltip";
import useBoolean from "~/hooks/useBoolean";
import { ActionContextProvider } from "~/hooks/useActionContext";
import DropToImport from "./DropToImport";
import Relative from "./Relative";
import SidebarLink from "./SidebarLink";
import { StaticLink } from "./StaticLink";
import type { SidebarContextType } from "./SidebarContext";
@@ -257,11 +258,11 @@ function CollectionRow({
<ActionContextProvider
value={{ activeModels: [collection], sidebarContext }}
>
<Relative ref={mergedRef}>
<StickyRow ref={mergedRef}>
<DropToImport collectionId={collection.id}>
{sidebarLinkElement}
</DropToImport>
</Relative>
</StickyRow>
{children(
isAddingNewChild && onCreateChild ? (
<SidebarLink
@@ -287,4 +288,12 @@ function CollectionRow({
);
}
// Keeps the collection visible while scrolling through its documents.
const StickyRow = styled.div`
position: sticky;
top: 0;
z-index: 1;
background: ${s("sidebarBackground")};
`;
export default observer(CollectionRow);
@@ -4,7 +4,9 @@ import { noop } from "es-toolkit/compat";
import { observer } from "mobx-react";
import * as React from "react";
import scrollIntoView from "scroll-into-view-if-needed";
import styled from "styled-components";
import type { NavigationNode } from "@shared/types";
import { isMobile } from "@shared/utils/browser";
import { sortNavigationNodes } from "@shared/utils/collections";
import {
flattenExpandedTree,
@@ -196,6 +198,9 @@ export const DocumentTree = observer(function DocumentTree({
behavior: "auto",
boundary: (parent) => parent.id !== "sidebar",
});
// Uncovers a row that is under the sticky collection row.
element.scrollIntoView({ block: "nearest" });
}
// Scroll when the active row appears, not when rows above it change.
// oxlint-disable-next-line react-hooks/exhaustive-deps
@@ -251,7 +256,7 @@ export const DocumentTree = observer(function DocumentTree({
return (
<React.Fragment key={item.key}>
{gap > 0 && <div style={{ height: gap }} />}
<div data-index={item.index} ref={virtualizer.measureElement}>
<Row data-index={item.index} ref={virtualizer.measureElement}>
<DocumentLink
node={row.node}
collection={collection}
@@ -264,7 +269,7 @@ export const DocumentTree = observer(function DocumentTree({
hasChildren={row.hasChildren}
trailingAncestors={row.trailingAncestors}
/>
</div>
</Row>
</React.Fragment>
);
});
@@ -280,6 +285,11 @@ export const DocumentTree = observer(function DocumentTree({
);
});
// Leaves space for the sticky collection row when a row scrolls into view.
const Row = styled.div`
scroll-margin-top: ${isMobile() ? 48 : ROW_HEIGHT}px;
`;
/**
* Moves the descendants of root rows at depth 0 one level deeper, so that the
* rows match childDepthFor.