You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

45 lines
1.3 KiB

  1. export function initialize(lastItemIndicator, componentInstance) {
  2. const options = {
  3. root: findClosestScrollContainer(lastItemIndicator),
  4. rootMargin: '0px',
  5. threshold: 0,
  6. };
  7. const observer = new IntersectionObserver(async (entries) => {
  8. // When the lastItemIndicator element is visible => invoke the C# method `LoadMoreItems`
  9. for (const entry of entries) {
  10. if (entry.isIntersecting) {
  11. await componentInstance.invokeMethodAsync("LoadMoreItems");
  12. }
  13. }
  14. }, options);
  15. observer.observe(lastItemIndicator);
  16. // Allow to cleanup resources when the Razor component is removed from the page
  17. return {
  18. dispose: () => dispose(observer),
  19. onNewItems: () => {
  20. observer.unobserve(lastIndicator);
  21. observer.observe(lastIndicator);
  22. },
  23. };
  24. }
  25. // Cleanup resources
  26. function dispose(observer) {
  27. observer.disconnect();
  28. }
  29. // Find the parent element with a vertical scrollbar
  30. // This container should be use as the root for the IntersectionObserver
  31. function findClosestScrollContainer(element) {
  32. while (element) {
  33. const style = getComputedStyle(element);
  34. if (style.overflowY !== 'visible') {
  35. return element;
  36. }
  37. element = element.parentElement;
  38. }
  39. return null;
  40. }