@tanstack/lit-virtual 3.14.10 → 3.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -59,6 +59,18 @@ class MyVirtualElement extends LitElement {
59
59
 
60
60
  Note that a [Ref](https://lit.dev/docs/templates/directives/#ref) is attached to the scrolling container to allow the virtualizer to interact with it.
61
61
 
62
+ Options passed to the constructor are captured once. When a reactive property changes, merge the new values in `willUpdate` with `setOptions`. The same method exists on `WindowVirtualizerController`.
63
+
64
+ ```ts
65
+ protected willUpdate(changed: PropertyValues<this>) {
66
+ if (changed.has('count')) {
67
+ this.virtualizerController.setOptions({ count: this.count })
68
+ }
69
+ }
70
+ ```
71
+
72
+ `setOptions` merges a partial update over the controller's current options and keeps the `onChange` wrapper that re-renders the host. `undefined` values are ignored. Use the controller method rather than `getVirtualizer().setOptions(...)`, which replaces options wholesale and drops that wrapper.
73
+
62
74
  ## `WindowVirtualizerController`
63
75
 
64
76
  You can also create a virtualizer controller that attaches to the Window:
@@ -5,20 +5,30 @@ class VirtualizerControllerBase {
5
5
  constructor(host, options) {
6
6
  this.cleanup = () => {
7
7
  };
8
- const resolvedOptions = {
9
- ...options,
10
- onChange: (instance, sync) => {
11
- var _a;
12
- this.host.updateComplete.then(() => this.host.requestUpdate());
13
- (_a = options.onChange) == null ? void 0 : _a.call(options, instance, sync);
14
- }
15
- };
16
- this.virtualizer = new virtualCore.Virtualizer(resolvedOptions);
17
- (this.host = host).addController(this);
8
+ this.host = host;
9
+ this.options = { ...options };
10
+ this.virtualizer = new virtualCore.Virtualizer(this.resolveOptions(this.options));
11
+ this.host.addController(this);
18
12
  }
19
13
  getVirtualizer() {
20
14
  return this.virtualizer;
21
15
  }
16
+ /**
17
+ * Merge a partial options update into the options this controller already
18
+ * holds, push them to the virtualizer, and request a host update. Lit folds
19
+ * a request made from `willUpdate` into the update already in progress.
20
+ *
21
+ * Keys set to `undefined` are ignored, matching core `setOptions`, so a
22
+ * partial update cannot wipe an option stored earlier. Defaults supplied by
23
+ * `VirtualizerController` / `WindowVirtualizerController` stay in place
24
+ * unless a later call replaces them.
25
+ */
26
+ setOptions(options) {
27
+ if (!this.applyOptions(options)) return;
28
+ this.virtualizer.setOptions(this.resolveOptions(this.options));
29
+ this.virtualizer._willUpdate();
30
+ this.host.requestUpdate();
31
+ }
22
32
  hostConnected() {
23
33
  this.cleanup = this.virtualizer._didMount();
24
34
  }
@@ -28,6 +38,33 @@ class VirtualizerControllerBase {
28
38
  hostDisconnected() {
29
39
  this.cleanup();
30
40
  }
41
+ applyOptions(partial) {
42
+ const next = {
43
+ ...this.options
44
+ };
45
+ let changed = false;
46
+ for (const key in partial) {
47
+ if (!Object.prototype.hasOwnProperty.call(partial, key)) continue;
48
+ const value = partial[key];
49
+ if (value === void 0) continue;
50
+ if (!Object.is(next[key], value)) {
51
+ changed = true;
52
+ Object.assign(next, { [key]: value });
53
+ }
54
+ }
55
+ if (changed) this.options = next;
56
+ return changed;
57
+ }
58
+ resolveOptions(options) {
59
+ return {
60
+ ...options,
61
+ onChange: (instance, sync) => {
62
+ var _a;
63
+ this.host.updateComplete.then(() => this.host.requestUpdate());
64
+ (_a = options.onChange) == null ? void 0 : _a.call(options, instance, sync);
65
+ }
66
+ };
67
+ }
31
68
  }
32
69
  class VirtualizerController extends VirtualizerControllerBase {
33
70
  constructor(host, options) {
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../../src/index.ts"],"sourcesContent":["import {\n Virtualizer,\n elementScroll,\n observeElementOffset,\n observeElementRect,\n observeWindowOffset,\n observeWindowRect,\n windowScroll,\n} from '@tanstack/virtual-core'\nimport type { ReactiveController, ReactiveControllerHost } from 'lit'\nimport type { PartialKeys, VirtualizerOptions } from '@tanstack/virtual-core'\n\nexport * from '@tanstack/virtual-core'\n\nclass VirtualizerControllerBase<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n> implements ReactiveController {\n host: ReactiveControllerHost\n\n private readonly virtualizer: Virtualizer<TScrollElement, TItemElement>\n\n private cleanup: () => void = () => {}\n\n constructor(\n host: ReactiveControllerHost,\n options: VirtualizerOptions<TScrollElement, TItemElement>,\n ) {\n const resolvedOptions: VirtualizerOptions<TScrollElement, TItemElement> = {\n ...options,\n onChange: (instance, sync) => {\n this.host.updateComplete.then(() => this.host.requestUpdate())\n options.onChange?.(instance, sync)\n },\n }\n this.virtualizer = new Virtualizer(resolvedOptions)\n ;(this.host = host).addController(this)\n }\n\n public getVirtualizer() {\n return this.virtualizer\n }\n\n hostConnected() {\n this.cleanup = this.virtualizer._didMount()\n }\n\n hostUpdated() {\n this.virtualizer._willUpdate()\n }\n\n hostDisconnected() {\n this.cleanup()\n }\n}\n\nexport class VirtualizerController<\n TScrollElement extends Element,\n TItemElement extends Element,\n> extends VirtualizerControllerBase<TScrollElement, TItemElement> {\n constructor(\n host: ReactiveControllerHost,\n options: PartialKeys<\n VirtualizerOptions<TScrollElement, TItemElement>,\n 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'\n >,\n ) {\n super(host, {\n observeElementRect: observeElementRect,\n observeElementOffset: observeElementOffset,\n scrollToFn: elementScroll,\n ...options,\n })\n }\n}\n\nexport class WindowVirtualizerController<\n TItemElement extends Element,\n> extends VirtualizerControllerBase<Window, TItemElement> {\n constructor(\n host: ReactiveControllerHost,\n options: PartialKeys<\n VirtualizerOptions<Window, TItemElement>,\n | 'getScrollElement'\n | 'observeElementRect'\n | 'observeElementOffset'\n | 'scrollToFn'\n >,\n ) {\n super(host, {\n getScrollElement: () => (typeof document !== 'undefined' ? window : null),\n observeElementRect: observeWindowRect,\n observeElementOffset: observeWindowOffset,\n scrollToFn: windowScroll,\n initialOffset: () =>\n typeof document !== 'undefined' ? window.scrollY : 0,\n ...options,\n })\n }\n}\n"],"names":["Virtualizer","observeElementRect","observeElementOffset","elementScroll","observeWindowRect","observeWindowOffset","windowScroll"],"mappings":";;;AAcA,MAAM,0BAG0B;AAAA,EAO9B,YACE,MACA,SACA;AALF,SAAQ,UAAsB,MAAM;AAAA,IAAC;AAMnC,UAAM,kBAAoE;AAAA,MACxE,GAAG;AAAA,MACH,UAAU,CAAC,UAAU,SAAS;;AAC5B,aAAK,KAAK,eAAe,KAAK,MAAM,KAAK,KAAK,eAAe;AAC7D,sBAAQ,aAAR,iCAAmB,UAAU;AAAA,MAC/B;AAAA,IAAA;AAEF,SAAK,cAAc,IAAIA,YAAAA,YAAY,eAAe;AACjD,KAAC,KAAK,OAAO,MAAM,cAAc,IAAI;AAAA,EACxC;AAAA,EAEO,iBAAiB;AACtB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,gBAAgB;AACd,SAAK,UAAU,KAAK,YAAY,UAAA;AAAA,EAClC;AAAA,EAEA,cAAc;AACZ,SAAK,YAAY,YAAA;AAAA,EACnB;AAAA,EAEA,mBAAmB;AACjB,SAAK,QAAA;AAAA,EACP;AACF;AAEO,MAAM,8BAGH,0BAAwD;AAAA,EAChE,YACE,MACA,SAIA;AACA,UAAM,MAAM;AAAA,MAAA,oBACVC,YAAAA;AAAAA,MAAA,sBACAC,YAAAA;AAAAA,MACA,YAAYC,YAAAA;AAAAA,MACZ,GAAG;AAAA,IAAA,CACJ;AAAA,EACH;AACF;AAEO,MAAM,oCAEH,0BAAgD;AAAA,EACxD,YACE,MACA,SAOA;AACA,UAAM,MAAM;AAAA,MACV,kBAAkB,MAAO,OAAO,aAAa,cAAc,SAAS;AAAA,MACpE,oBAAoBC,YAAAA;AAAAA,MACpB,sBAAsBC,YAAAA;AAAAA,MACtB,YAAYC,YAAAA;AAAAA,MACZ,eAAe,MACb,OAAO,aAAa,cAAc,OAAO,UAAU;AAAA,MACrD,GAAG;AAAA,IAAA,CACJ;AAAA,EACH;AACF;;;;;;;;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../../src/index.ts"],"sourcesContent":["import {\n Virtualizer,\n elementScroll,\n observeElementOffset,\n observeElementRect,\n observeWindowOffset,\n observeWindowRect,\n windowScroll,\n} from '@tanstack/virtual-core'\nimport type { ReactiveController, ReactiveControllerHost } from 'lit'\nimport type { PartialKeys, VirtualizerOptions } from '@tanstack/virtual-core'\n\nexport * from '@tanstack/virtual-core'\n\nclass VirtualizerControllerBase<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n> implements ReactiveController {\n host: ReactiveControllerHost\n\n private readonly virtualizer: Virtualizer<TScrollElement, TItemElement>\n\n private options: VirtualizerOptions<TScrollElement, TItemElement>\n\n private cleanup: () => void = () => {}\n\n constructor(\n host: ReactiveControllerHost,\n options: VirtualizerOptions<TScrollElement, TItemElement>,\n ) {\n this.host = host\n this.options = { ...options }\n this.virtualizer = new Virtualizer(this.resolveOptions(this.options))\n this.host.addController(this)\n }\n\n public getVirtualizer() {\n return this.virtualizer\n }\n\n /**\n * Merge a partial options update into the options this controller already\n * holds, push them to the virtualizer, and request a host update. Lit folds\n * a request made from `willUpdate` into the update already in progress.\n *\n * Keys set to `undefined` are ignored, matching core `setOptions`, so a\n * partial update cannot wipe an option stored earlier. Defaults supplied by\n * `VirtualizerController` / `WindowVirtualizerController` stay in place\n * unless a later call replaces them.\n */\n public setOptions(\n options: Partial<VirtualizerOptions<TScrollElement, TItemElement>>,\n ) {\n if (!this.applyOptions(options)) return\n\n this.virtualizer.setOptions(this.resolveOptions(this.options))\n this.virtualizer._willUpdate()\n this.host.requestUpdate()\n }\n\n hostConnected() {\n this.cleanup = this.virtualizer._didMount()\n }\n\n hostUpdated() {\n this.virtualizer._willUpdate()\n }\n\n hostDisconnected() {\n this.cleanup()\n }\n\n private applyOptions(\n partial: Partial<VirtualizerOptions<TScrollElement, TItemElement>>,\n ): boolean {\n const next: VirtualizerOptions<TScrollElement, TItemElement> = {\n ...this.options,\n }\n let changed = false\n\n for (const key in partial) {\n if (!Object.prototype.hasOwnProperty.call(partial, key)) continue\n\n const value = partial[key as keyof typeof partial]\n if (value === undefined) continue\n\n if (!Object.is(next[key as keyof typeof next], value)) {\n changed = true\n Object.assign(next, { [key]: value })\n }\n }\n\n if (changed) this.options = next\n return changed\n }\n\n private resolveOptions(\n options: VirtualizerOptions<TScrollElement, TItemElement>,\n ): VirtualizerOptions<TScrollElement, TItemElement> {\n return {\n ...options,\n onChange: (instance, sync) => {\n this.host.updateComplete.then(() => this.host.requestUpdate())\n options.onChange?.(instance, sync)\n },\n }\n }\n}\n\nexport class VirtualizerController<\n TScrollElement extends Element,\n TItemElement extends Element,\n> extends VirtualizerControllerBase<TScrollElement, TItemElement> {\n constructor(\n host: ReactiveControllerHost,\n options: PartialKeys<\n VirtualizerOptions<TScrollElement, TItemElement>,\n 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'\n >,\n ) {\n super(host, {\n observeElementRect: observeElementRect,\n observeElementOffset: observeElementOffset,\n scrollToFn: elementScroll,\n ...options,\n })\n }\n}\n\nexport class WindowVirtualizerController<\n TItemElement extends Element,\n> extends VirtualizerControllerBase<Window, TItemElement> {\n constructor(\n host: ReactiveControllerHost,\n options: PartialKeys<\n VirtualizerOptions<Window, TItemElement>,\n | 'getScrollElement'\n | 'observeElementRect'\n | 'observeElementOffset'\n | 'scrollToFn'\n >,\n ) {\n super(host, {\n getScrollElement: () => (typeof document !== 'undefined' ? window : null),\n observeElementRect: observeWindowRect,\n observeElementOffset: observeWindowOffset,\n scrollToFn: windowScroll,\n initialOffset: () =>\n typeof document !== 'undefined' ? window.scrollY : 0,\n ...options,\n })\n }\n}\n"],"names":["Virtualizer","observeElementRect","observeElementOffset","elementScroll","observeWindowRect","observeWindowOffset","windowScroll"],"mappings":";;;AAcA,MAAM,0BAG0B;AAAA,EAS9B,YACE,MACA,SACA;AALF,SAAQ,UAAsB,MAAM;AAAA,IAAC;AAMnC,SAAK,OAAO;AACZ,SAAK,UAAU,EAAE,GAAG,QAAA;AACpB,SAAK,cAAc,IAAIA,YAAAA,YAAY,KAAK,eAAe,KAAK,OAAO,CAAC;AACpE,SAAK,KAAK,cAAc,IAAI;AAAA,EAC9B;AAAA,EAEO,iBAAi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AAAA,MACA,YAAYC,YAAAA;AAAAA,MACZ,GAAG;AAAA,IAAA,CACJ;AAAA,EACH;AACF;AAEO,MAAM,oCAEH,0BAAgD;AAAA,EACxD,YACE,MACA,SAOA;AACA,UAAM,MAAM;AAAA,MACV,kBAAkB,MAAO,OAAO,aAAa,cAAc,SAAS;AAAA,MACpE,oBAAoBC,YAAAA;AAAAA,MACpB,sBAAsBC,YAAAA;AAAAA,MACtB,YAAYC,YAAAA;AAAAA,MACZ,eAAe,MACb,OAAO,aAAa,cAAc,OAAO,UAAU;AAAA,MACrD,GAAG;AAAA,IAAA,CACJ;AAAA,EACH;AACF;;;;;;;;;"}
@@ -4,12 +4,26 @@ export * from '@tanstack/virtual-core';
4
4
  declare class VirtualizerControllerBase<TScrollElement extends Element | Window, TItemElement extends Element> implements ReactiveController {
5
5
  host: ReactiveControllerHost;
6
6
  private readonly virtualizer;
7
+ private options;
7
8
  private cleanup;
8
9
  constructor(host: ReactiveControllerHost, options: VirtualizerOptions<TScrollElement, TItemElement>);
9
10
  getVirtualizer(): Virtualizer<TScrollElement, TItemElement>;
11
+ /**
12
+ * Merge a partial options update into the options this controller already
13
+ * holds, push them to the virtualizer, and request a host update. Lit folds
14
+ * a request made from `willUpdate` into the update already in progress.
15
+ *
16
+ * Keys set to `undefined` are ignored, matching core `setOptions`, so a
17
+ * partial update cannot wipe an option stored earlier. Defaults supplied by
18
+ * `VirtualizerController` / `WindowVirtualizerController` stay in place
19
+ * unless a later call replaces them.
20
+ */
21
+ setOptions(options: Partial<VirtualizerOptions<TScrollElement, TItemElement>>): void;
10
22
  hostConnected(): void;
11
23
  hostUpdated(): void;
12
24
  hostDisconnected(): void;
25
+ private applyOptions;
26
+ private resolveOptions;
13
27
  }
14
28
  export declare class VirtualizerController<TScrollElement extends Element, TItemElement extends Element> extends VirtualizerControllerBase<TScrollElement, TItemElement> {
15
29
  constructor(host: ReactiveControllerHost, options: PartialKeys<VirtualizerOptions<TScrollElement, TItemElement>, 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'>);
@@ -4,12 +4,26 @@ export * from '@tanstack/virtual-core';
4
4
  declare class VirtualizerControllerBase<TScrollElement extends Element | Window, TItemElement extends Element> implements ReactiveController {
5
5
  host: ReactiveControllerHost;
6
6
  private readonly virtualizer;
7
+ private options;
7
8
  private cleanup;
8
9
  constructor(host: ReactiveControllerHost, options: VirtualizerOptions<TScrollElement, TItemElement>);
9
10
  getVirtualizer(): Virtualizer<TScrollElement, TItemElement>;
11
+ /**
12
+ * Merge a partial options update into the options this controller already
13
+ * holds, push them to the virtualizer, and request a host update. Lit folds
14
+ * a request made from `willUpdate` into the update already in progress.
15
+ *
16
+ * Keys set to `undefined` are ignored, matching core `setOptions`, so a
17
+ * partial update cannot wipe an option stored earlier. Defaults supplied by
18
+ * `VirtualizerController` / `WindowVirtualizerController` stay in place
19
+ * unless a later call replaces them.
20
+ */
21
+ setOptions(options: Partial<VirtualizerOptions<TScrollElement, TItemElement>>): void;
10
22
  hostConnected(): void;
11
23
  hostUpdated(): void;
12
24
  hostDisconnected(): void;
25
+ private applyOptions;
26
+ private resolveOptions;
13
27
  }
14
28
  export declare class VirtualizerController<TScrollElement extends Element, TItemElement extends Element> extends VirtualizerControllerBase<TScrollElement, TItemElement> {
15
29
  constructor(host: ReactiveControllerHost, options: PartialKeys<VirtualizerOptions<TScrollElement, TItemElement>, 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'>);
package/dist/esm/index.js CHANGED
@@ -4,20 +4,30 @@ class VirtualizerControllerBase {
4
4
  constructor(host, options) {
5
5
  this.cleanup = () => {
6
6
  };
7
- const resolvedOptions = {
8
- ...options,
9
- onChange: (instance, sync) => {
10
- var _a;
11
- this.host.updateComplete.then(() => this.host.requestUpdate());
12
- (_a = options.onChange) == null ? void 0 : _a.call(options, instance, sync);
13
- }
14
- };
15
- this.virtualizer = new Virtualizer(resolvedOptions);
16
- (this.host = host).addController(this);
7
+ this.host = host;
8
+ this.options = { ...options };
9
+ this.virtualizer = new Virtualizer(this.resolveOptions(this.options));
10
+ this.host.addController(this);
17
11
  }
18
12
  getVirtualizer() {
19
13
  return this.virtualizer;
20
14
  }
15
+ /**
16
+ * Merge a partial options update into the options this controller already
17
+ * holds, push them to the virtualizer, and request a host update. Lit folds
18
+ * a request made from `willUpdate` into the update already in progress.
19
+ *
20
+ * Keys set to `undefined` are ignored, matching core `setOptions`, so a
21
+ * partial update cannot wipe an option stored earlier. Defaults supplied by
22
+ * `VirtualizerController` / `WindowVirtualizerController` stay in place
23
+ * unless a later call replaces them.
24
+ */
25
+ setOptions(options) {
26
+ if (!this.applyOptions(options)) return;
27
+ this.virtualizer.setOptions(this.resolveOptions(this.options));
28
+ this.virtualizer._willUpdate();
29
+ this.host.requestUpdate();
30
+ }
21
31
  hostConnected() {
22
32
  this.cleanup = this.virtualizer._didMount();
23
33
  }
@@ -27,6 +37,33 @@ class VirtualizerControllerBase {
27
37
  hostDisconnected() {
28
38
  this.cleanup();
29
39
  }
40
+ applyOptions(partial) {
41
+ const next = {
42
+ ...this.options
43
+ };
44
+ let changed = false;
45
+ for (const key in partial) {
46
+ if (!Object.prototype.hasOwnProperty.call(partial, key)) continue;
47
+ const value = partial[key];
48
+ if (value === void 0) continue;
49
+ if (!Object.is(next[key], value)) {
50
+ changed = true;
51
+ Object.assign(next, { [key]: value });
52
+ }
53
+ }
54
+ if (changed) this.options = next;
55
+ return changed;
56
+ }
57
+ resolveOptions(options) {
58
+ return {
59
+ ...options,
60
+ onChange: (instance, sync) => {
61
+ var _a;
62
+ this.host.updateComplete.then(() => this.host.requestUpdate());
63
+ (_a = options.onChange) == null ? void 0 : _a.call(options, instance, sync);
64
+ }
65
+ };
66
+ }
30
67
  }
31
68
  class VirtualizerController extends VirtualizerControllerBase {
32
69
  constructor(host, options) {
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["import {\n Virtualizer,\n elementScroll,\n observeElementOffset,\n observeElementRect,\n observeWindowOffset,\n observeWindowRect,\n windowScroll,\n} from '@tanstack/virtual-core'\nimport type { ReactiveController, ReactiveControllerHost } from 'lit'\nimport type { PartialKeys, VirtualizerOptions } from '@tanstack/virtual-core'\n\nexport * from '@tanstack/virtual-core'\n\nclass VirtualizerControllerBase<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n> implements ReactiveController {\n host: ReactiveControllerHost\n\n private readonly virtualizer: Virtualizer<TScrollElement, TItemElement>\n\n private cleanup: () => void = () => {}\n\n constructor(\n host: ReactiveControllerHost,\n options: VirtualizerOptions<TScrollElement, TItemElement>,\n ) {\n const resolvedOptions: VirtualizerOptions<TScrollElement, TItemElement> = {\n ...options,\n onChange: (instance, sync) => {\n this.host.updateComplete.then(() => this.host.requestUpdate())\n options.onChange?.(instance, sync)\n },\n }\n this.virtualizer = new Virtualizer(resolvedOptions)\n ;(this.host = host).addController(this)\n }\n\n public getVirtualizer() {\n return this.virtualizer\n }\n\n hostConnected() {\n this.cleanup = this.virtualizer._didMount()\n }\n\n hostUpdated() {\n this.virtualizer._willUpdate()\n }\n\n hostDisconnected() {\n this.cleanup()\n }\n}\n\nexport class VirtualizerController<\n TScrollElement extends Element,\n TItemElement extends Element,\n> extends VirtualizerControllerBase<TScrollElement, TItemElement> {\n constructor(\n host: ReactiveControllerHost,\n options: PartialKeys<\n VirtualizerOptions<TScrollElement, TItemElement>,\n 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'\n >,\n ) {\n super(host, {\n observeElementRect: observeElementRect,\n observeElementOffset: observeElementOffset,\n scrollToFn: elementScroll,\n ...options,\n })\n }\n}\n\nexport class WindowVirtualizerController<\n TItemElement extends Element,\n> extends VirtualizerControllerBase<Window, TItemElement> {\n constructor(\n host: ReactiveControllerHost,\n options: PartialKeys<\n VirtualizerOptions<Window, TItemElement>,\n | 'getScrollElement'\n | 'observeElementRect'\n | 'observeElementOffset'\n | 'scrollToFn'\n >,\n ) {\n super(host, {\n getScrollElement: () => (typeof document !== 'undefined' ? window : null),\n observeElementRect: observeWindowRect,\n observeElementOffset: observeWindowOffset,\n scrollToFn: windowScroll,\n initialOffset: () =>\n typeof document !== 'undefined' ? window.scrollY : 0,\n ...options,\n })\n }\n}\n"],"names":[],"mappings":";;AAcA,MAAM,0BAG0B;AAAA,EAO9B,YACE,MACA,SACA;AALF,SAAQ,UAAsB,MAAM;AAAA,IAAC;AAMnC,UAAM,kBAAoE;AAAA,MACxE,GAAG;AAAA,MACH,UAAU,CAAC,UAAU,SAAS;;AAC5B,aAAK,KAAK,eAAe,KAAK,MAAM,KAAK,KAAK,eAAe;AAC7D,sBAAQ,aAAR,iCAAmB,UAAU;AAAA,MAC/B;AAAA,IAAA;AAEF,SAAK,cAAc,IAAI,YAAY,eAAe;AACjD,KAAC,KAAK,OAAO,MAAM,cAAc,IAAI;AAAA,EACxC;AAAA,EAEO,iBAAiB;AACtB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,gBAAgB;AACd,SAAK,UAAU,KAAK,YAAY,UAAA;AAAA,EAClC;AAAA,EAEA,cAAc;AACZ,SAAK,YAAY,YAAA;AAAA,EACnB;AAAA,EAEA,mBAAmB;AACjB,SAAK,QAAA;AAAA,EACP;AACF;AAEO,MAAM,8BAGH,0BAAwD;AAAA,EAChE,YACE,MACA,SAIA;AACA,UAAM,MAAM;AAAA,MACV;AAAA,MACA;AAAA,MACA,YAAY;AAAA,MACZ,GAAG;AAAA,IAAA,CACJ;AAAA,EACH;AACF;AAEO,MAAM,oCAEH,0BAAgD;AAAA,EACxD,YACE,MACA,SAOA;AACA,UAAM,MAAM;AAAA,MACV,kBAAkB,MAAO,OAAO,aAAa,cAAc,SAAS;AAAA,MACpE,oBAAoB;AAAA,MACpB,sBAAsB;AAAA,MACtB,YAAY;AAAA,MACZ,eAAe,MACb,OAAO,aAAa,cAAc,OAAO,UAAU;AAAA,MACrD,GAAG;AAAA,IAAA,CACJ;AAAA,EACH;AACF;"}
1
+ {"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["import {\n Virtualizer,\n elementScroll,\n observeElementOffset,\n observeElementRect,\n observeWindowOffset,\n observeWindowRect,\n windowScroll,\n} from '@tanstack/virtual-core'\nimport type { ReactiveController, ReactiveControllerHost } from 'lit'\nimport type { PartialKeys, VirtualizerOptions } from '@tanstack/virtual-core'\n\nexport * from '@tanstack/virtual-core'\n\nclass VirtualizerControllerBase<\n TScrollElement extends Element | Window,\n TItemElement extends Element,\n> implements ReactiveController {\n host: ReactiveControllerHost\n\n private readonly virtualizer: Virtualizer<TScrollElement, TItemElement>\n\n private options: VirtualizerOptions<TScrollElement, TItemElement>\n\n private cleanup: () => void = () => {}\n\n constructor(\n host: ReactiveControllerHost,\n options: VirtualizerOptions<TScrollElement, TItemElement>,\n ) {\n this.host = host\n this.options = { ...options }\n this.virtualizer = new Virtualizer(this.resolveOptions(this.options))\n this.host.addController(this)\n }\n\n public getVirtualizer() {\n return this.virtualizer\n }\n\n /**\n * Merge a partial options update into the options this controller already\n * holds, push them to the virtualizer, and request a host update. Lit folds\n * a request made from `willUpdate` into the update already in progress.\n *\n * Keys set to `undefined` are ignored, matching core `setOptions`, so a\n * partial update cannot wipe an option stored earlier. Defaults supplied by\n * `VirtualizerController` / `WindowVirtualizerController` stay in place\n * unless a later call replaces them.\n */\n public setOptions(\n options: Partial<VirtualizerOptions<TScrollElement, TItemElement>>,\n ) {\n if (!this.applyOptions(options)) return\n\n this.virtualizer.setOptions(this.resolveOptions(this.options))\n this.virtualizer._willUpdate()\n this.host.requestUpdate()\n }\n\n hostConnected() {\n this.cleanup = this.virtualizer._didMount()\n }\n\n hostUpdated() {\n this.virtualizer._willUpdate()\n }\n\n hostDisconnected() {\n this.cleanup()\n }\n\n private applyOptions(\n partial: Partial<VirtualizerOptions<TScrollElement, TItemElement>>,\n ): boolean {\n const next: VirtualizerOptions<TScrollElement, TItemElement> = {\n ...this.options,\n }\n let changed = false\n\n for (const key in partial) {\n if (!Object.prototype.hasOwnProperty.call(partial, key)) continue\n\n const value = partial[key as keyof typeof partial]\n if (value === undefined) continue\n\n if (!Object.is(next[key as keyof typeof next], value)) {\n changed = true\n Object.assign(next, { [key]: value })\n }\n }\n\n if (changed) this.options = next\n return changed\n }\n\n private resolveOptions(\n options: VirtualizerOptions<TScrollElement, TItemElement>,\n ): VirtualizerOptions<TScrollElement, TItemElement> {\n return {\n ...options,\n onChange: (instance, sync) => {\n this.host.updateComplete.then(() => this.host.requestUpdate())\n options.onChange?.(instance, sync)\n },\n }\n }\n}\n\nexport class VirtualizerController<\n TScrollElement extends Element,\n TItemElement extends Element,\n> extends VirtualizerControllerBase<TScrollElement, TItemElement> {\n constructor(\n host: ReactiveControllerHost,\n options: PartialKeys<\n VirtualizerOptions<TScrollElement, TItemElement>,\n 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'\n >,\n ) {\n super(host, {\n observeElementRect: observeElementRect,\n observeElementOffset: observeElementOffset,\n scrollToFn: elementScroll,\n ...options,\n })\n }\n}\n\nexport class WindowVirtualizerController<\n TItemElement extends Element,\n> extends VirtualizerControllerBase<Window, TItemElement> {\n constructor(\n host: ReactiveControllerHost,\n options: PartialKeys<\n VirtualizerOptions<Window, TItemElement>,\n | 'getScrollElement'\n | 'observeElementRect'\n | 'observeElementOffset'\n | 'scrollToFn'\n >,\n ) {\n super(host, {\n getScrollElement: () => (typeof document !== 'undefined' ? window : null),\n observeElementRect: observeWindowRect,\n observeElementOffset: observeWindowOffset,\n scrollToFn: windowScroll,\n initialOffset: () =>\n typeof document !== 'undefined' ? window.scrollY : 0,\n ...options,\n })\n }\n}\n"],"names":[],"mappings":";;AAcA,MAAM,0BAG0B;AAAA,EAS9B,YACE,MACA,SACA;AALF,SAAQ,UAAsB,MAAM;AAAA,IAAC;AAMnC,SAAK,OAAO;AACZ,SAAK,UAAU,EAAE,GAAG,QAAA;AACpB,SAAK,cAAc,IAAI,YAAY,KAAK,eAAe,KAAK,OAAO,CAAC;AACpE,SAAK,KAAK,cAAc,IAAI;AAAA,EAC9B;AAAA,EAE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Z,GAAG;AAAA,IAAA,CACJ;AAAA,EACH;AACF;AAEO,MAAM,oCAEH,0BAAgD;AAAA,EACxD,YACE,MACA,SAOA;AACA,UAAM,MAAM;AAAA,MACV,kBAAkB,MAAO,OAAO,aAAa,cAAc,SAAS;AAAA,MACpE,oBAAoB;AAAA,MACpB,sBAAsB;AAAA,MACtB,YAAY;AAAA,MACZ,eAAe,MACb,OAAO,aAAa,cAAc,OAAO,UAAU;AAAA,MACrD,GAAG;AAAA,IAAA,CACJ;AAAA,EACH;AACF;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/lit-virtual",
3
- "version": "3.14.10",
3
+ "version": "3.15.0",
4
4
  "description": "Headless UI for virtualizing scrollable elements in Lit",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -43,7 +43,7 @@
43
43
  "src"
44
44
  ],
45
45
  "dependencies": {
46
- "@tanstack/virtual-core": "3.19.6"
46
+ "@tanstack/virtual-core": "3.19.8"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@open-wc/testing": "^4.0.0",
package/src/index.ts CHANGED
@@ -20,27 +20,44 @@ class VirtualizerControllerBase<
20
20
 
21
21
  private readonly virtualizer: Virtualizer<TScrollElement, TItemElement>
22
22
 
23
+ private options: VirtualizerOptions<TScrollElement, TItemElement>
24
+
23
25
  private cleanup: () => void = () => {}
24
26
 
25
27
  constructor(
26
28
  host: ReactiveControllerHost,
27
29
  options: VirtualizerOptions<TScrollElement, TItemElement>,
28
30
  ) {
29
- const resolvedOptions: VirtualizerOptions<TScrollElement, TItemElement> = {
30
- ...options,
31
- onChange: (instance, sync) => {
32
- this.host.updateComplete.then(() => this.host.requestUpdate())
33
- options.onChange?.(instance, sync)
34
- },
35
- }
36
- this.virtualizer = new Virtualizer(resolvedOptions)
37
- ;(this.host = host).addController(this)
31
+ this.host = host
32
+ this.options = { ...options }
33
+ this.virtualizer = new Virtualizer(this.resolveOptions(this.options))
34
+ this.host.addController(this)
38
35
  }
39
36
 
40
37
  public getVirtualizer() {
41
38
  return this.virtualizer
42
39
  }
43
40
 
41
+ /**
42
+ * Merge a partial options update into the options this controller already
43
+ * holds, push them to the virtualizer, and request a host update. Lit folds
44
+ * a request made from `willUpdate` into the update already in progress.
45
+ *
46
+ * Keys set to `undefined` are ignored, matching core `setOptions`, so a
47
+ * partial update cannot wipe an option stored earlier. Defaults supplied by
48
+ * `VirtualizerController` / `WindowVirtualizerController` stay in place
49
+ * unless a later call replaces them.
50
+ */
51
+ public setOptions(
52
+ options: Partial<VirtualizerOptions<TScrollElement, TItemElement>>,
53
+ ) {
54
+ if (!this.applyOptions(options)) return
55
+
56
+ this.virtualizer.setOptions(this.resolveOptions(this.options))
57
+ this.virtualizer._willUpdate()
58
+ this.host.requestUpdate()
59
+ }
60
+
44
61
  hostConnected() {
45
62
  this.cleanup = this.virtualizer._didMount()
46
63
  }
@@ -52,6 +69,42 @@ class VirtualizerControllerBase<
52
69
  hostDisconnected() {
53
70
  this.cleanup()
54
71
  }
72
+
73
+ private applyOptions(
74
+ partial: Partial<VirtualizerOptions<TScrollElement, TItemElement>>,
75
+ ): boolean {
76
+ const next: VirtualizerOptions<TScrollElement, TItemElement> = {
77
+ ...this.options,
78
+ }
79
+ let changed = false
80
+
81
+ for (const key in partial) {
82
+ if (!Object.prototype.hasOwnProperty.call(partial, key)) continue
83
+
84
+ const value = partial[key as keyof typeof partial]
85
+ if (value === undefined) continue
86
+
87
+ if (!Object.is(next[key as keyof typeof next], value)) {
88
+ changed = true
89
+ Object.assign(next, { [key]: value })
90
+ }
91
+ }
92
+
93
+ if (changed) this.options = next
94
+ return changed
95
+ }
96
+
97
+ private resolveOptions(
98
+ options: VirtualizerOptions<TScrollElement, TItemElement>,
99
+ ): VirtualizerOptions<TScrollElement, TItemElement> {
100
+ return {
101
+ ...options,
102
+ onChange: (instance, sync) => {
103
+ this.host.updateComplete.then(() => this.host.requestUpdate())
104
+ options.onChange?.(instance, sync)
105
+ },
106
+ }
107
+ }
55
108
  }
56
109
 
57
110
  export class VirtualizerController<