index.mjs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Utilities
  2. import { SUPPORTS_INTERSECTION } from "../../util/index.mjs"; // Types
  3. function mounted(el, binding) {
  4. if (!SUPPORTS_INTERSECTION) return;
  5. const modifiers = binding.modifiers || {};
  6. const value = binding.value;
  7. const {
  8. handler,
  9. options
  10. } = typeof value === 'object' ? value : {
  11. handler: value,
  12. options: {}
  13. };
  14. const observer = new IntersectionObserver(function () {
  15. let entries = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  16. let observer = arguments.length > 1 ? arguments[1] : undefined;
  17. const _observe = el._observe?.[binding.instance.$.uid];
  18. if (!_observe) return; // Just in case, should never fire
  19. const isIntersecting = entries.some(entry => entry.isIntersecting);
  20. // If is not quiet or has already been
  21. // initted, invoke the user callback
  22. if (handler && (!modifiers.quiet || _observe.init) && (!modifiers.once || isIntersecting || _observe.init)) {
  23. handler(isIntersecting, entries, observer);
  24. }
  25. if (isIntersecting && modifiers.once) unmounted(el, binding);else _observe.init = true;
  26. }, options);
  27. el._observe = Object(el._observe);
  28. el._observe[binding.instance.$.uid] = {
  29. init: false,
  30. observer
  31. };
  32. observer.observe(el);
  33. }
  34. function unmounted(el, binding) {
  35. const observe = el._observe?.[binding.instance.$.uid];
  36. if (!observe) return;
  37. observe.observer.unobserve(el);
  38. delete el._observe[binding.instance.$.uid];
  39. }
  40. export const Intersect = {
  41. mounted,
  42. unmounted
  43. };
  44. export default Intersect;
  45. //# sourceMappingURL=index.mjs.map