Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(models): pin the library group header to the visible row, not the overscan

joshistoast54bac0ed7fa5
diff --git a/invokeai/frontend/webv2/src/features/models/ui/library/ModelLibraryList.browser.test.tsx b/invokeai/frontend/webv2/src/features/models/ui/library/ModelLibraryList.browser.test.tsx
index a56909d460..7b6e62cdf6 100644
--- a/invokeai/frontend/webv2/src/features/models/ui/library/ModelLibraryList.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/features/models/ui/library/ModelLibraryList.browser.test.tsx
@@ -102,3 +102,72 @@ describe('ModelLibraryList filter transitions', () => {
expect(names).toContain('Model lora-2');
});
});
+
+describe('ModelLibraryList pinned group header', () => {
+ let host: HTMLDivElement;
+ let root: Root;
+
+ beforeEach(() => {
+ accountLifecycle.activate('library-pin-test-a', ':user:library-pin-test-a');
+ // Enough rows in both groups that the second group's header can reach the
+ // top of an 800px viewport with the first group still inside the
+ // virtualizer's overscan window.
+ setModelsSnapshotForTests({
+ models: [
+ ...Array.from({ length: 20 }, (_, i) => model(`main-${i}`, 'main')),
+ ...Array.from({ length: 20 }, (_, i) => model(`lora-${i}`, 'lora')),
+ ],
+ status: 'loaded',
+ });
+ host = document.createElement('div');
+ document.body.append(host);
+ root = createRoot(host);
+ });
+
+ afterEach(async () => {
+ await act(() => root.unmount());
+ host.remove();
+ accountLifecycle.invalidate();
+ });
+
+ const settleFrame = async () => {
+ await act(async () => {
+ await Promise.resolve();
+ });
+ await act(async () => {
+ await new Promise((resolve) => {
+ requestAnimationFrame(() => resolve(undefined));
+ });
+ });
+ };
+
+ const pinnedHeaderText = () => host.querySelector<HTMLElement>('[data-pinned-group-header]')?.textContent ?? '';
+
+ it('swaps the pinned header when a group scrolls under it, not an overscan later', async () => {
+ await act(() => root.render(<Harness filters={DEFAULT_LIBRARY_FILTERS} />));
+ await settleFrame();
+
+ const viewport = host.querySelector<HTMLElement>('[aria-label="models.library"]');
+
+ expect(viewport).not.toBeNull();
+ expect(pinnedHeaderText()).toContain('Main Models');
+
+ const scrollTo = async (top: number) => {
+ await act(async () => {
+ viewport!.scrollTop = top;
+ viewport!.dispatchEvent(new Event('scroll'));
+ await new Promise((resolve) => {
+ requestAnimationFrame(() => resolve(undefined));
+ });
+ });
+ };
+
+ // Rows: main header (30px) + 20 main rows (56px each) put the LoRAs header
+ // at 1150px; one row past it, the first visible row is a lora.
+ await scrollTo(1206);
+ expect(pinnedHeaderText()).toContain('LoRAs');
+
+ await scrollTo(0);
+ expect(pinnedHeaderText()).toContain('Main Models');
+ });
+});
diff --git a/invokeai/frontend/webv2/src/features/models/ui/library/ModelLibraryList.tsx b/invokeai/frontend/webv2/src/features/models/ui/library/ModelLibraryList.tsx
index 4bfe74f04a..3a2d88b6fb 100644
--- a/invokeai/frontend/webv2/src/features/models/ui/library/ModelLibraryList.tsx
+++ b/invokeai/frontend/webv2/src/features/models/ui/library/ModelLibraryList.tsx
@@ -138,7 +138,10 @@ export const ModelLibraryList = ({
);
}
- const firstVisibleIndex = virtualizer.virtualItems[0]?.index ?? 0;
+ // The visible range, not `virtualItems[0]`: the virtual window starts
+ // `overscan` rows above the fold, which held the previous group's header
+ // pinned ~450px past its last row.
+ const firstVisibleIndex = virtualizer.range?.startIndex ?? 0;
const pinnedHeaderIndex = headerIndexes.reduce<number | null>(
(pinnedIndex, index) => (firstVisibleIndex >= index ? index : pinnedIndex),
headerIndexes[0] ?? null
@@ -195,7 +198,18 @@ export const ModelLibraryList = ({
{/* Pinned copy of the current group's header, above the viewport.
aria-hidden: it duplicates a header already in the list. */}
{pinnedRow?.kind === 'header' ? (
- <Box aria-hidden bg="bg" left="0" pointerEvents="none" position="absolute" right="0" top="0" zIndex={1} px="3">
+ <Box
+ aria-hidden
+ bg="bg"
+ data-pinned-group-header=""
+ left="0"
+ pointerEvents="none"
+ position="absolute"
+ right="0"
+ top="0"
+ zIndex={1}
+ px="3"
+ >
<GroupHeader count={pinnedRow.group.models.length} label={pinnedRow.group.label} />
</Box>
) : null}