@fluid-topics/ft-infinite-scroll 1.1.41 → 1.1.42

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
@@ -1,4 +1,4 @@
1
- An infinite scroller.
1
+ A container for infinite scrolling.
2
2
 
3
3
  ## Install
4
4
 
@@ -66,7 +66,6 @@ export declare class FtInfiniteScroll<T> extends FtLitElement implements FtInfin
66
66
  private onMutation;
67
67
  private mutationObserver;
68
68
  disconnectedCallback(): void;
69
- protected firstUpdated(_changedProperties: PropertyValues): void;
70
69
  protected update(props: PropertyValues<FtInfiniteScroll<T>>): void;
71
70
  protected updated(props: PropertyValues<FtInfiniteScroll<T>>): void;
72
71
  private lastNotOkVisibleItems?;
@@ -9,7 +9,7 @@ import { property, query, state } from "lit/decorators.js";
9
9
  import { repeat } from "lit/directives/repeat.js";
10
10
  import { cancelable, Debouncer, deepEqual, FtLitElement, minmax, waitUntil } from "@fluid-topics/ft-wc-utils";
11
11
  import { unsafeHTML } from "lit/directives/unsafe-html.js";
12
- import { styles } from "./ft-infinite-scroll.css";
12
+ import { styles } from "./ft-infinite-scroll.styles";
13
13
  export class VisibleItemsChangeEvent extends CustomEvent {
14
14
  constructor(indexes, items) {
15
15
  super("visible-items-change", {
@@ -156,21 +156,18 @@ class FtInfiniteScroll extends FtLitElement {
156
156
  this.visibleItems = [];
157
157
  const targetIndex = this.resolveScrollToIndex();
158
158
  this.renderedIndexes.add(targetIndex);
159
- this.scrollToTarget(targetIndex);
160
159
  this.scrolledToTarget = false;
161
- this.scrollDebouncer.run(() => {
162
- this.scrollToTarget(targetIndex);
163
- this.onMutation();
164
- this.scrollDoneDebouncer.run(() => {
165
- if (this.ensureScrollToTarget && targetIndex >= 0) {
166
- this.scrollDebouncer.run(() => {
167
- if (Math.abs(this.scrollable.scrollTop - this.getOffset(targetIndex)) > 5) {
168
- this.scrollToTarget(targetIndex);
169
- }
170
- }, 500);
171
- }
172
- this.scrolledToTarget = true;
173
- });
160
+ this.scrollToTarget(targetIndex);
161
+ this.onMutation();
162
+ this.scrollDoneDebouncer.run(() => {
163
+ if (this.ensureScrollToTarget && targetIndex >= 0) {
164
+ this.scrollDebouncer.run(() => {
165
+ if (Math.abs(this.scrollable.scrollTop - this.getOffset(targetIndex)) > 5) {
166
+ this.scrollToTarget(targetIndex);
167
+ }
168
+ }, 500);
169
+ }
170
+ this.scrolledToTarget = true;
174
171
  });
175
172
  }
176
173
  resolveScrollToIndex() {
@@ -220,6 +217,7 @@ class FtInfiniteScroll extends FtLitElement {
220
217
  this.initIntersectionObserver();
221
218
  this.mutationObserver.disconnect();
222
219
  this.mutationObserver.observe(this.itemsContainer, { childList: true });
220
+ this.resetScroll();
223
221
  }, 0);
224
222
  }
225
223
  initIntersectionObserver() {
@@ -290,10 +288,6 @@ class FtInfiniteScroll extends FtLitElement {
290
288
  this.scrollableResizeObserver.disconnect();
291
289
  this.mutationObserver.disconnect();
292
290
  }
293
- firstUpdated(_changedProperties) {
294
- super.firstUpdated(_changedProperties);
295
- this.resetScroll();
296
- }
297
291
  update(props) {
298
292
  super.update(props);
299
293
  if (props.has("items")) {
@@ -40,4 +40,4 @@
40
40
  data-item-index="${i}">
41
41
  ${h?(()=>{const e=this.renderItem(t,i);return"string"==typeof e?n.unsafeHTML(e):e})():e.nothing}
42
42
  </div>
43
- `}prepareRenderIfNeeded(t){const i=null!=this.renderApprovalTimeouts[t];this.inRenderRange(t)&&!i&&(this.renderApprovalTimeouts[t]=setTimeout((()=>{this.inRenderRange(t)?this.renderedIndexes.add(t):this.renderApprovalTimeouts[t]=void 0,this.requestUpdate()}),300))}inRenderRange(t){return t>=this.visibleItems[0]-this.renderBeforeFirst&&t<=(null!=(i=this.visibleItems)?i:[])[(null!=i?i:[]).length-1]+this.renderAfterLast;var i}resetScroll(){this.triggerFindScrollableParent(),this.intersectionObserver.disconnect(),this.itemsResizeObserver.disconnect(),this.scrollableResizeObserver.disconnect(),this.visibleItems=[];const t=this.resolveScrollToIndex();this.renderedIndexes.add(t),this.scrollToTarget(t),this.scrolledToTarget=!1,this.scrollDebouncer.run((()=>{this.scrollToTarget(t),this.onMutation(),this.scrollDoneDebouncer.run((()=>{this.ensureScrollToTarget&&t>=0&&this.scrollDebouncer.run((()=>{Math.abs(this.scrollable.scrollTop-this.getOffset(t))>5&&this.scrollToTarget(t)}),500),this.scrolledToTarget=!0}))}))}resolveScrollToIndex(){var t;let i=null!==(t=this.scrollToIndex)&&void 0!==t?t:this.scrollToItem?this.items.indexOf(this.scrollToItem):-1;return i>=this.items.length?-1:i}getItem(t){var i,e,s;return null!==(e=null===(i=this.shadowRoot)||void 0===i?void 0:i.querySelector(`#item-${t}`))&&void 0!==e?e:null===(s=this.shadowRoot)||void 0===s?void 0:s.querySelector("#item-0")}scrollToTarget(t){t<=0?(this.scrollable.scrollTop=0,this.scrollRestorationOffset=-this.getOffset(t)):(this.scrollable.scrollTop=this.getOffset(t),this.scrollRestorationOffset=0),this.scrollRestorationItem=t>=0?t:void 0}getOffset(t){var i;let e=0,s="number"==typeof t?this.getItem(t):t;for(;s&&s.offsetParent!==this.scrollable.offsetParent;)e+=s.offsetTop,s=s.offsetParent;return e+(null!==(i=null==s?void 0:s.offsetTop)&&void 0!==i?i:0)-this.scrollable.offsetTop}getLastKnownHeight(t){var i;return+(null!==(i=null==t?void 0:t.getAttribute("data-last-known-height"))&&void 0!==i?i:1e3)}appendItems(...t){this.items=[...this.items,...t]}prependItems(...t){this.items=[...t,...this.items]}connectedCallback(){super.connectedCallback(),setTimeout((()=>{this.triggerFindScrollableParent(),this.initIntersectionObserver(),this.mutationObserver.disconnect(),this.mutationObserver.observe(this.itemsContainer,{childList:!0})}),0)}initIntersectionObserver(){this.intersectionObserver.disconnect(),this.intersectionObserver=new IntersectionObserver(this.onVisibilityChange,{root:this.scrollable,rootMargin:"-8px",threshold:[0,.01,.1,1]})}triggerFindScrollableParent(){var t;null===(t=this.itemsContainer)||void 0===t||t.dispatchEvent(new Event("find-scrollable-parent",{composed:!0}))}findScrollableParent(t){var i;let e,s;t.stopPropagation();for(let i of t.composedPath()){const t=i,h=this.elementCanScroll(t);if(h&&t.clientHeight&&t.clientHeight<t.scrollHeight){e=t;break}h&&(s=t)}let h=e||s;h!==this.firstScrollableParent&&(null===(i=this.firstScrollableParent)||void 0===i||i.removeEventListener("scroll",this.scrollListener),null==h||h.addEventListener("scroll",this.scrollListener),this.firstScrollableParent=h,this.initIntersectionObserver(),this.resetScroll())}elementCanScroll(t){try{return["auto","scroll"].includes(getComputedStyle(t).overflowY)}catch(t){return!1}}searchFirstVisibleItem(t,i,e){if(i=null!=i?i:0,(e=null!=e?e:t.length-1)<=i)return t[i];const s=Math.floor((e-i)/2)+i,h=t[s];return this.getOffset(h)>this.scrollable.scrollTop?this.searchFirstVisibleItem(t,i,s-1):this.getOffset(h)+this.getLastKnownHeight(h)<this.scrollable.scrollTop?this.searchFirstVisibleItem(t,s+1,e):h}disconnectedCallback(){super.disconnectedCallback(),this.intersectionObserver.disconnect(),this.itemsResizeObserver.disconnect(),this.scrollableResizeObserver.disconnect(),this.mutationObserver.disconnect()}firstUpdated(t){super.firstUpdated(t),this.resetScroll()}update(t){super.update(t),t.has("items")&&(this.renderedIndexes=new Set,this.renderApprovalTimeouts=[]),!t.has("scrollToItem")&&!t.has("scrollToIndex")||null==this.scrollToItem&&null==this.scrollToIndex||this.resetScroll()}updated(t){super.updated(t),(t.has("visibleItems")||t.has("items"))&&this.onVisibleItemsChange(),t.has("scrolledToTarget")&&this.scrolledToTarget&&(null!=this.scrollToItem||null!=this.scrollToIndex)&&this.dispatchEvent(new d)}onVisibleItemsChange(){this.visibleItems.every(((t,i)=>null==this.visibleItems[i+1]||t+1===this.visibleItems[i+1]))||i.deepEqual(this.visibleItems,this.lastNotOkVisibleItems)?(this.resetVisibleItemsDebouncer.cancel(),this.dispatchVisibleItemsEvent()):this.resetVisibleItemsDebouncer.run((()=>{this.lastNotOkVisibleItems=[...this.visibleItems],this.visibleItems=[],this.initIntersectionObserver(),this.onMutation()}))}dispatchVisibleItemsEvent(){var t;null===(t=this.cancelableDispatchEvent)||void 0===t||t.cancel(),this.cancelableDispatchEvent=i.cancelable(i.waitUntil((()=>!this.scrolling))),this.cancelableDispatchEvent.then((()=>this.dispatchEvent(new a(this.visibleItems,this.visibleItems.map((t=>this.items[t])))))).catch((()=>null))}}c.styles=r,o([s.property({type:Array})],c.prototype,"items",void 0),o([s.property({attribute:!1})],c.prototype,"renderItem",void 0),o([s.property({attribute:!1})],c.prototype,"getItemKey",void 0),o([s.property({type:Object})],c.prototype,"scrollToItem",void 0),o([s.property({type:Number})],c.prototype,"scrollToIndex",void 0),o([s.property({type:Boolean})],c.prototype,"internalScroll",void 0),o([s.property({type:Number})],c.prototype,"renderBeforeFirst",void 0),o([s.property({type:Number})],c.prototype,"renderAfterLast",void 0),o([s.property({type:Boolean})],c.prototype,"ensureScrollToTarget",void 0),o([s.state({hasChanged:(t,i)=>null!=t&&null==i||t.length!==i.length||t[0]!==i[0]})],c.prototype,"visibleItems",void 0),o([s.query(".scrollable")],c.prototype,"internalScrollable",void 0),o([s.query(".items-container")],c.prototype,"itemsContainer",void 0),o([s.state()],c.prototype,"scrolledToTarget",void 0),o([s.state()],c.prototype,"scrolling",void 0),o([s.state({hasChanged:(t,e)=>!i.deepEqual(t,e)})],c.prototype,"renderedIndexes",void 0),i.customElement("ft-infinite-scroll")(c),t.FtInfiniteScroll=c,t.FtInfiniteScrollCssVariables=l,t.ScrolledToTargetEvent=d,t.VisibleItemsChangeEvent=a,t.styles=r}({},ftGlobals.wcUtils,ftGlobals.lit,ftGlobals.litDecorators,ftGlobals.litRepeat,ftGlobals.litUnsafeHTML);
43
+ `}prepareRenderIfNeeded(t){const i=null!=this.renderApprovalTimeouts[t];this.inRenderRange(t)&&!i&&(this.renderApprovalTimeouts[t]=setTimeout((()=>{this.inRenderRange(t)?this.renderedIndexes.add(t):this.renderApprovalTimeouts[t]=void 0,this.requestUpdate()}),300))}inRenderRange(t){return t>=this.visibleItems[0]-this.renderBeforeFirst&&t<=(null!=(i=this.visibleItems)?i:[])[(null!=i?i:[]).length-1]+this.renderAfterLast;var i}resetScroll(){this.triggerFindScrollableParent(),this.intersectionObserver.disconnect(),this.itemsResizeObserver.disconnect(),this.scrollableResizeObserver.disconnect(),this.visibleItems=[];const t=this.resolveScrollToIndex();this.renderedIndexes.add(t),this.scrolledToTarget=!1,this.scrollToTarget(t),this.onMutation(),this.scrollDoneDebouncer.run((()=>{this.ensureScrollToTarget&&t>=0&&this.scrollDebouncer.run((()=>{Math.abs(this.scrollable.scrollTop-this.getOffset(t))>5&&this.scrollToTarget(t)}),500),this.scrolledToTarget=!0}))}resolveScrollToIndex(){var t;let i=null!==(t=this.scrollToIndex)&&void 0!==t?t:this.scrollToItem?this.items.indexOf(this.scrollToItem):-1;return i>=this.items.length?-1:i}getItem(t){var i,e,s;return null!==(e=null===(i=this.shadowRoot)||void 0===i?void 0:i.querySelector(`#item-${t}`))&&void 0!==e?e:null===(s=this.shadowRoot)||void 0===s?void 0:s.querySelector("#item-0")}scrollToTarget(t){t<=0?(this.scrollable.scrollTop=0,this.scrollRestorationOffset=-this.getOffset(t)):(this.scrollable.scrollTop=this.getOffset(t),this.scrollRestorationOffset=0),this.scrollRestorationItem=t>=0?t:void 0}getOffset(t){var i;let e=0,s="number"==typeof t?this.getItem(t):t;for(;s&&s.offsetParent!==this.scrollable.offsetParent;)e+=s.offsetTop,s=s.offsetParent;return e+(null!==(i=null==s?void 0:s.offsetTop)&&void 0!==i?i:0)-this.scrollable.offsetTop}getLastKnownHeight(t){var i;return+(null!==(i=null==t?void 0:t.getAttribute("data-last-known-height"))&&void 0!==i?i:1e3)}appendItems(...t){this.items=[...this.items,...t]}prependItems(...t){this.items=[...t,...this.items]}connectedCallback(){super.connectedCallback(),setTimeout((()=>{this.triggerFindScrollableParent(),this.initIntersectionObserver(),this.mutationObserver.disconnect(),this.mutationObserver.observe(this.itemsContainer,{childList:!0}),this.resetScroll()}),0)}initIntersectionObserver(){this.intersectionObserver.disconnect(),this.intersectionObserver=new IntersectionObserver(this.onVisibilityChange,{root:this.scrollable,rootMargin:"-8px",threshold:[0,.01,.1,1]})}triggerFindScrollableParent(){var t;null===(t=this.itemsContainer)||void 0===t||t.dispatchEvent(new Event("find-scrollable-parent",{composed:!0}))}findScrollableParent(t){var i;let e,s;t.stopPropagation();for(let i of t.composedPath()){const t=i,h=this.elementCanScroll(t);if(h&&t.clientHeight&&t.clientHeight<t.scrollHeight){e=t;break}h&&(s=t)}let h=e||s;h!==this.firstScrollableParent&&(null===(i=this.firstScrollableParent)||void 0===i||i.removeEventListener("scroll",this.scrollListener),null==h||h.addEventListener("scroll",this.scrollListener),this.firstScrollableParent=h,this.initIntersectionObserver(),this.resetScroll())}elementCanScroll(t){try{return["auto","scroll"].includes(getComputedStyle(t).overflowY)}catch(t){return!1}}searchFirstVisibleItem(t,i,e){if(i=null!=i?i:0,(e=null!=e?e:t.length-1)<=i)return t[i];const s=Math.floor((e-i)/2)+i,h=t[s];return this.getOffset(h)>this.scrollable.scrollTop?this.searchFirstVisibleItem(t,i,s-1):this.getOffset(h)+this.getLastKnownHeight(h)<this.scrollable.scrollTop?this.searchFirstVisibleItem(t,s+1,e):h}disconnectedCallback(){super.disconnectedCallback(),this.intersectionObserver.disconnect(),this.itemsResizeObserver.disconnect(),this.scrollableResizeObserver.disconnect(),this.mutationObserver.disconnect()}update(t){super.update(t),t.has("items")&&(this.renderedIndexes=new Set,this.renderApprovalTimeouts=[]),!t.has("scrollToItem")&&!t.has("scrollToIndex")||null==this.scrollToItem&&null==this.scrollToIndex||this.resetScroll()}updated(t){super.updated(t),(t.has("visibleItems")||t.has("items"))&&this.onVisibleItemsChange(),t.has("scrolledToTarget")&&this.scrolledToTarget&&(null!=this.scrollToItem||null!=this.scrollToIndex)&&this.dispatchEvent(new d)}onVisibleItemsChange(){this.visibleItems.every(((t,i)=>null==this.visibleItems[i+1]||t+1===this.visibleItems[i+1]))||i.deepEqual(this.visibleItems,this.lastNotOkVisibleItems)?(this.resetVisibleItemsDebouncer.cancel(),this.dispatchVisibleItemsEvent()):this.resetVisibleItemsDebouncer.run((()=>{this.lastNotOkVisibleItems=[...this.visibleItems],this.visibleItems=[],this.initIntersectionObserver(),this.onMutation()}))}dispatchVisibleItemsEvent(){var t;null===(t=this.cancelableDispatchEvent)||void 0===t||t.cancel(),this.cancelableDispatchEvent=i.cancelable(i.waitUntil((()=>!this.scrolling))),this.cancelableDispatchEvent.then((()=>this.dispatchEvent(new a(this.visibleItems,this.visibleItems.map((t=>this.items[t])))))).catch((()=>null))}}c.styles=r,o([s.property({type:Array})],c.prototype,"items",void 0),o([s.property({attribute:!1})],c.prototype,"renderItem",void 0),o([s.property({attribute:!1})],c.prototype,"getItemKey",void 0),o([s.property({type:Object})],c.prototype,"scrollToItem",void 0),o([s.property({type:Number})],c.prototype,"scrollToIndex",void 0),o([s.property({type:Boolean})],c.prototype,"internalScroll",void 0),o([s.property({type:Number})],c.prototype,"renderBeforeFirst",void 0),o([s.property({type:Number})],c.prototype,"renderAfterLast",void 0),o([s.property({type:Boolean})],c.prototype,"ensureScrollToTarget",void 0),o([s.state({hasChanged:(t,i)=>null!=t&&null==i||t.length!==i.length||t[0]!==i[0]})],c.prototype,"visibleItems",void 0),o([s.query(".scrollable")],c.prototype,"internalScrollable",void 0),o([s.query(".items-container")],c.prototype,"itemsContainer",void 0),o([s.state()],c.prototype,"scrolledToTarget",void 0),o([s.state()],c.prototype,"scrolling",void 0),o([s.state({hasChanged:(t,e)=>!i.deepEqual(t,e)})],c.prototype,"renderedIndexes",void 0),i.customElement("ft-infinite-scroll")(c),t.FtInfiniteScroll=c,t.FtInfiniteScrollCssVariables=l,t.ScrolledToTargetEvent=d,t.VisibleItemsChangeEvent=a,t.styles=r}({},ftGlobals.wcUtils,ftGlobals.lit,ftGlobals.litDecorators,ftGlobals.litRepeat,ftGlobals.litUnsafeHTML);