diff --git a/.changeset/window-initial-offset-current-axis.md b/.changeset/window-initial-offset-current-axis.md new file mode 100644 index 000000000..ec8aaccc4 --- /dev/null +++ b/.changeset/window-initial-offset-current-axis.md @@ -0,0 +1,8 @@ +--- +'@tanstack/virtual-core': patch +'@tanstack/lit-virtual': patch +'@tanstack/marko-virtual': patch +'@tanstack/svelte-virtual': patch +--- + +Pass the virtualizer to `initialOffset` when it is a function. The Lit and Svelte window virtualizers use it to read the current `horizontal` option, so a list whose axis changed while `enabled: false` starts from the correct window scroll position when it is enabled again. The Marko tags accept the same callback type. diff --git a/docs/api/virtualizer.md b/docs/api/virtualizer.md index dba8cc240..e6f022258 100644 --- a/docs/api/virtualizer.md +++ b/docs/api/virtualizer.md @@ -125,10 +125,10 @@ The padding to apply to the end of the virtualizer in pixels when scrolling to a ### `initialOffset` ```tsx -initialOffset?: number | (() => number) +initialOffset?: number | ((instance: Virtualizer) => number) ``` -The position where the list is scrolled to on render. This is useful if you are rendering the virtualizer in a SSR environment or are conditionally rendering the virtualizer. +The position where the list is scrolled to on render. This is useful if you are rendering the virtualizer in a SSR environment or are conditionally rendering the virtualizer. A function is passed the virtualizer instance, and must not call methods that read the scroll offset, such as `getVirtualItems`. ### `getItemKey` diff --git a/docs/framework/marko/marko-virtual.md b/docs/framework/marko/marko-virtual.md index 1392f01b2..b2c853ab1 100644 --- a/docs/framework/marko/marko-virtual.md +++ b/docs/framework/marko/marko-virtual.md @@ -217,7 +217,7 @@ Both tags are self-closing and expose the same tag-variable shape. Capture it wi | `scrollPaddingEnd` | `number` | — | Scroll padding for `scrollToIndex` | | `gap` | `number` | — | Gap between items in px | | `lanes` | `number` | `1` | Lanes for masonry layouts | -| `initialOffset` | `number \| (() => number)` | — | Scroll offset (px) for the server slice — server-render at a scroll position (deep link / restore). Element only; see [SSR](#ssr) | +| `initialOffset` | `number \| ((instance: Virtualizer) => number)` | — | Scroll offset (px) for the server slice — server-render at a scroll position (deep link / restore). Element only; see [SSR](#ssr) | | `initialRect` | `{ width: number; height: number }` | — | Viewport hint for a server-rendered slice (SSR). When set, the server paints the initial visible rows; omit for client-only fill. See [SSR](#ssr). | | `getItemKey` | `(index: number) => number \| string \| bigint` | the index | Stable per-item identity so cached measurements survive reorder | | `rangeExtractor` | `(range: Range) => number[]` | `defaultRangeExtractor` | Hook over the visible range: force extra indexes (e.g. a pinned sticky header) into the rendered window. Compose with `defaultRangeExtractor` from `@tanstack/virtual-core` | diff --git a/packages/lit-virtual/src/index.ts b/packages/lit-virtual/src/index.ts index 7136c396b..dedfb180f 100644 --- a/packages/lit-virtual/src/index.ts +++ b/packages/lit-virtual/src/index.ts @@ -158,9 +158,9 @@ export class WindowVirtualizerController< observeElementRect: observeWindowRect, observeElementOffset: observeWindowOffset, scrollToFn: windowScroll, - initialOffset: () => + initialOffset: (instance) => typeof document !== 'undefined' - ? options.horizontal + ? instance.options.horizontal ? window.scrollX : window.scrollY : 0, diff --git a/packages/lit-virtual/tests/set-options.test.ts b/packages/lit-virtual/tests/set-options.test.ts index 5fd421563..e2197c154 100644 --- a/packages/lit-virtual/tests/set-options.test.ts +++ b/packages/lit-virtual/tests/set-options.test.ts @@ -160,6 +160,64 @@ test('WindowVirtualizerController seeds a horizontal list from window.scrollX', } }) +test('WindowVirtualizerController seeds from the current axis after re-enabling', () => { + Object.defineProperties(window, { + scrollX: { configurable: true, value: 300 }, + scrollY: { configurable: true, value: 1000 }, + }) + const { host } = createHost() + const controller = new WindowVirtualizerController(host, { + count: 100, + estimateSize: () => 50, + }) + try { + controller.hostConnected() + expect(controller.getVirtualizer().scrollOffset).toBe(1000) + + controller.setOptions({ enabled: false }) + controller.setOptions({ horizontal: true }) + controller.setOptions({ enabled: true }) + + const virtualizer = controller.getVirtualizer() + expect(virtualizer.getVirtualItems()[0]?.index).toBe(5) + expect(virtualizer.scrollOffset).toBe(300) + } finally { + controller.hostDisconnected() + Object.defineProperties(window, { + scrollX: { configurable: true, value: 0 }, + scrollY: { configurable: true, value: 0 }, + }) + } +}) + +test('WindowVirtualizerController keeps an explicit initialOffset', () => { + Object.defineProperties(window, { + scrollX: { configurable: true, value: 300 }, + scrollY: { configurable: true, value: 1000 }, + }) + const { host } = createHost() + const controller = new WindowVirtualizerController(host, { + count: 100, + estimateSize: () => 50, + initialOffset: () => 150, + }) + try { + controller.hostConnected() + + controller.setOptions({ enabled: false }) + controller.setOptions({ horizontal: true }) + controller.setOptions({ enabled: true }) + + expect(controller.getVirtualizer().scrollOffset).toBe(150) + } finally { + controller.hostDisconnected() + Object.defineProperties(window, { + scrollX: { configurable: true, value: 0 }, + scrollY: { configurable: true, value: 0 }, + }) + } +}) + @customElement('test-list-set-options') class ListSetOptions extends LitElement { @property({ type: Number }) diff --git a/packages/marko-virtual/src/tags/virtualizer/index.marko b/packages/marko-virtual/src/tags/virtualizer/index.marko index 720ff6e54..c2eb93b8b 100644 --- a/packages/marko-virtual/src/tags/virtualizer/index.marko +++ b/packages/marko-virtual/src/tags/virtualizer/index.marko @@ -29,7 +29,9 @@ export interface Input { scrollPaddingEnd?: number gap?: number lanes?: number - initialOffset?: number | (() => number) + initialOffset?: + | number + | ((instance: Virtualizer) => number) initialRect?: { width: number; height: number } getItemKey?: (index: number) => number | string | bigint rangeExtractor?: (range: Range) => number[] diff --git a/packages/marko-virtual/src/tags/virtualizer/options.ts b/packages/marko-virtual/src/tags/virtualizer/options.ts index d478225fd..1bac5511a 100644 --- a/packages/marko-virtual/src/tags/virtualizer/options.ts +++ b/packages/marko-virtual/src/tags/virtualizer/options.ts @@ -25,7 +25,7 @@ export interface VirtualizerInput { scrollPaddingEnd?: number gap?: number lanes?: number - initialOffset?: number | (() => number) + initialOffset?: number | ((instance: Virtualizer) => number) // Viewport size used for the render-time (server / pre-mount) slice. When omitted, // virtual-core's default 0x0 rect yields an empty window (totalSize is still correct). initialRect?: { width: number; height: number } diff --git a/packages/marko-virtual/src/tags/window-virtualizer/index.marko b/packages/marko-virtual/src/tags/window-virtualizer/index.marko index 995c02680..6d7fedad7 100644 --- a/packages/marko-virtual/src/tags/window-virtualizer/index.marko +++ b/packages/marko-virtual/src/tags/window-virtualizer/index.marko @@ -28,7 +28,9 @@ export interface Input { scrollPaddingEnd?: number gap?: number lanes?: number - initialOffset?: number | (() => number) + initialOffset?: + | number + | ((instance: Virtualizer) => number) initialRect?: { width: number; height: number } getItemKey?: (index: number) => number | string | bigint rangeExtractor?: (range: Range) => number[] diff --git a/packages/marko-virtual/src/tags/window-virtualizer/options.ts b/packages/marko-virtual/src/tags/window-virtualizer/options.ts index 4564e868e..ce738906e 100644 --- a/packages/marko-virtual/src/tags/window-virtualizer/options.ts +++ b/packages/marko-virtual/src/tags/window-virtualizer/options.ts @@ -28,7 +28,7 @@ export interface WindowVirtualizerInput { // Starting scroll offset. Defaults to reading the live window scroll position // (window.scrollY, or window.scrollX when horizontal) on the client and 0 on the // server. Pass a number to seed a server slice at a position (pairs with initialRect). - initialOffset?: number | (() => number) + initialOffset?: number | ((instance: Virtualizer) => number) // Window viewport size for the render-time (server / pre-mount) slice. When omitted, // there is no server slice. initialRect?: { width: number; height: number } diff --git a/packages/svelte-virtual/src/index.ts b/packages/svelte-virtual/src/index.ts index 4cad45b43..145371d5b 100644 --- a/packages/svelte-virtual/src/index.ts +++ b/packages/svelte-virtual/src/index.ts @@ -103,9 +103,9 @@ export function createWindowVirtualizer( observeElementRect: observeWindowRect, observeElementOffset: observeWindowOffset, scrollToFn: windowScroll, - initialOffset: () => + initialOffset: (instance) => typeof document !== 'undefined' - ? options.horizontal + ? instance.options.horizontal ? window.scrollX : window.scrollY : 0, diff --git a/packages/svelte-virtual/tests/window-initial-offset.test.ts b/packages/svelte-virtual/tests/window-initial-offset.test.ts new file mode 100644 index 000000000..ef7250acc --- /dev/null +++ b/packages/svelte-virtual/tests/window-initial-offset.test.ts @@ -0,0 +1,59 @@ +// @vitest-environment jsdom +import { get } from 'svelte/store' +import { afterEach, expect, test } from 'vitest' +import { createWindowVirtualizer } from '../src' + +function setWindowScroll(x: number, y: number) { + Object.defineProperties(window, { + scrollX: { configurable: true, value: x }, + scrollY: { configurable: true, value: y }, + }) +} + +afterEach(() => { + setWindowScroll(0, 0) +}) + +test('seeds from the current axis after re-enabling', () => { + setWindowScroll(300, 1000) + const store = createWindowVirtualizer({ + count: 100, + estimateSize: () => 50, + }) + const unsubscribe = store.subscribe(() => {}) + const virtualizer = get(store) + + try { + expect(virtualizer.scrollOffset).toBe(1000) + + virtualizer.setOptions({ enabled: false }) + virtualizer.setOptions({ horizontal: true }) + virtualizer.setOptions({ enabled: true }) + + expect(virtualizer.getVirtualItems()[0]?.index).toBe(5) + expect(virtualizer.scrollOffset).toBe(300) + } finally { + unsubscribe() + } +}) + +test('keeps an explicit initialOffset', () => { + setWindowScroll(300, 1000) + const store = createWindowVirtualizer({ + count: 100, + estimateSize: () => 50, + initialOffset: () => 150, + }) + const unsubscribe = store.subscribe(() => {}) + const virtualizer = get(store) + + try { + virtualizer.setOptions({ enabled: false }) + virtualizer.setOptions({ horizontal: true }) + virtualizer.setOptions({ enabled: true }) + + expect(virtualizer.scrollOffset).toBe(150) + } finally { + unsubscribe() + } +}) diff --git a/packages/virtual-core/src/index.ts b/packages/virtual-core/src/index.ts index 4bd3531db..675a79db6 100644 --- a/packages/virtual-core/src/index.ts +++ b/packages/virtual-core/src/index.ts @@ -382,7 +382,9 @@ export interface VirtualizerOptions< paddingEnd?: number scrollPaddingStart?: number scrollPaddingEnd?: number - initialOffset?: number | (() => number) + initialOffset?: + | number + | ((instance: Virtualizer) => number) getItemKey?: (index: number) => Key rangeExtractor?: (range: Range) => Array scrollMargin?: number @@ -1623,7 +1625,7 @@ export class Virtualizer< this.scrollOffset = this.scrollOffset ?? (typeof this.options.initialOffset === 'function' - ? this.options.initialOffset() + ? this.options.initialOffset(this) : this.options.initialOffset) return this.scrollOffset diff --git a/packages/virtual-core/tests/index.test.ts b/packages/virtual-core/tests/index.test.ts index 8f287a1f1..e778606c5 100644 --- a/packages/virtual-core/tests/index.test.ts +++ b/packages/virtual-core/tests/index.test.ts @@ -628,6 +628,24 @@ test('setOptions: explicit value overrides default', () => { expect(virtualizer.options.lanes).toBe(3) }) +test('initialOffset: receives the virtualizer with its current options', () => { + const virtualizer = new Virtualizer({ + count: 100, + estimateSize: () => 50, + getScrollElement: () => null, + scrollToFn: vi.fn(), + observeElementRect: vi.fn(), + observeElementOffset: vi.fn(), + enabled: false, + initialOffset: (instance) => (instance.options.horizontal ? 300 : 1000), + }) + + virtualizer.setOptions({ ...virtualizer.options, horizontal: true }) + virtualizer.setOptions({ ...virtualizer.options, enabled: true }) + + expect(virtualizer['getScrollOffset']()).toBe(300) +}) + // ─── Public-exports lockdown ───────────────────────────────────────────────── // If any of these go missing the next minor bump silently breaks consumers.