index.d.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * The PositionObserver class is a utility class that observes the position
  3. * of DOM elements and triggers a callback when their position changes.
  4. */
  5. declare class PositionObserver {
  6. entries: Map<Element, PositionObserverEntry>;
  7. static version: string;
  8. private _tick;
  9. private _root;
  10. private _callback;
  11. /**
  12. * The constructor takes two arguments, a `callback`, which is called
  13. * whenever the position of an observed element changes and an `options` object.
  14. * The callback function should take an array of `PositionObserverEntry` objects
  15. * as its only argument, but it's not required.
  16. *
  17. * @param callback the callback that applies to all targets of this observer
  18. * @param options the options of this observer
  19. */
  20. constructor(callback: PositionObserverCallback, options?: Partial<PositionObserverOptions>);
  21. /**
  22. * Start observing the position of the specified element.
  23. * If the element is not currently attached to the DOM,
  24. * it will NOT be added to the entries.
  25. *
  26. * @param target an `Element` target
  27. */
  28. observe: (target: Element) => void;
  29. /**
  30. * Stop observing the position of the specified element.
  31. *
  32. * @param target an `Element` target
  33. */
  34. unobserve: (target: Element) => void;
  35. /**
  36. * Private method responsible for all the heavy duty,
  37. * the observer's runtime.
  38. */
  39. private _runCallback;
  40. /**
  41. * Check intersection status and resolve it
  42. * right away.
  43. *
  44. * @param target an `Element` target
  45. */
  46. private _new;
  47. /**
  48. * Find the entry for a given target.
  49. *
  50. * @param target an `HTMLElement` target
  51. */
  52. getEntry: (target: Element) => PositionObserverEntry | undefined;
  53. /**
  54. * Immediately stop observing all elements.
  55. */
  56. disconnect: () => void;
  57. }
  58. export default PositionObserver;
  59. declare type PositionObserverCallback = (entries: PositionObserverEntry[], observer: PositionObserver) => void;
  60. declare type PositionObserverEntry = {
  61. target: Element;
  62. boundingClientRect: DOMRect;
  63. clientHeight: number;
  64. clientWidth: number;
  65. };
  66. declare type PositionObserverOptions = {
  67. root: HTMLElement;
  68. };
  69. export { }