index-CnhA8i0D.mjs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const k = (e) => e != null && typeof e == "object" || !1, m = (e) => k(e) && typeof e.nodeType == "number" && [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].some(
  2. (t) => e.nodeType === t
  3. ) || !1, a = (e) => m(e) && e.nodeType === 1 || !1, w = (e) => typeof e == "function" || !1, p = "1.0.7", u = "PositionObserver Error";
  4. class y {
  5. entries;
  6. static version = p;
  7. _tick;
  8. _root;
  9. _callback;
  10. /**
  11. * The constructor takes two arguments, a `callback`, which is called
  12. * whenever the position of an observed element changes and an `options` object.
  13. * The callback function should take an array of `PositionObserverEntry` objects
  14. * as its only argument, but it's not required.
  15. *
  16. * @param callback the callback that applies to all targets of this observer
  17. * @param options the options of this observer
  18. */
  19. constructor(t, i) {
  20. if (!w(t))
  21. throw new Error(`${u}: ${t} is not a function.`);
  22. this.entries = /* @__PURE__ */ new Map(), this._callback = t, this._root = a(i?.root) ? i.root : document?.documentElement, this._tick = 0;
  23. }
  24. /**
  25. * Start observing the position of the specified element.
  26. * If the element is not currently attached to the DOM,
  27. * it will NOT be added to the entries.
  28. *
  29. * @param target an `Element` target
  30. */
  31. observe = (t) => {
  32. if (!a(t))
  33. throw new Error(
  34. `${u}: ${t} is not an instance of Element.`
  35. );
  36. this._root.contains(t) && this._new(t).then(({ boundingClientRect: i }) => {
  37. if (i && !this.getEntry(t)) {
  38. const { clientWidth: s, clientHeight: n } = this._root;
  39. this.entries.set(t, {
  40. target: t,
  41. boundingClientRect: i,
  42. clientWidth: s,
  43. clientHeight: n
  44. });
  45. }
  46. this._tick || (this._tick = requestAnimationFrame(this._runCallback));
  47. });
  48. };
  49. /**
  50. * Stop observing the position of the specified element.
  51. *
  52. * @param target an `Element` target
  53. */
  54. unobserve = (t) => {
  55. this.entries.has(t) && this.entries.delete(t);
  56. };
  57. /**
  58. * Private method responsible for all the heavy duty,
  59. * the observer's runtime.
  60. */
  61. _runCallback = () => {
  62. if (!this.entries.size) return;
  63. const { clientWidth: t, clientHeight: i } = this._root, s = new Promise((n) => {
  64. const r = [];
  65. this.entries.forEach(
  66. ({
  67. target: o,
  68. boundingClientRect: c,
  69. clientWidth: _,
  70. clientHeight: b
  71. }) => {
  72. this._root.contains(o) && this._new(o).then(({ boundingClientRect: h, isIntersecting: d }) => {
  73. if (!d) return;
  74. const { left: g, top: f } = h;
  75. if (c.top !== f || c.left !== g || _ !== t || b !== i) {
  76. const l = {
  77. target: o,
  78. boundingClientRect: h,
  79. clientHeight: i,
  80. clientWidth: t
  81. };
  82. this.entries.set(o, l), r.push(l);
  83. }
  84. });
  85. }
  86. ), n(r);
  87. });
  88. this._tick = requestAnimationFrame(async () => {
  89. const n = await s;
  90. n.length && this._callback(n, this), this._runCallback();
  91. });
  92. };
  93. /**
  94. * Check intersection status and resolve it
  95. * right away.
  96. *
  97. * @param target an `Element` target
  98. */
  99. _new = (t) => new Promise((i) => {
  100. new IntersectionObserver(
  101. ([s], n) => {
  102. n.disconnect(), i(s);
  103. }
  104. ).observe(t);
  105. });
  106. /**
  107. * Find the entry for a given target.
  108. *
  109. * @param target an `HTMLElement` target
  110. */
  111. getEntry = (t) => this.entries.get(t);
  112. /**
  113. * Immediately stop observing all elements.
  114. */
  115. disconnect = () => {
  116. cancelAnimationFrame(this._tick), this.entries.clear(), this._tick = 0;
  117. };
  118. }
  119. export {
  120. y
  121. };
  122. //# sourceMappingURL=index-CnhA8i0D.mjs.map