debian-mirror-gitlab/app/assets/javascripts/lib/utils/intersection_observer.js
2022-01-26 12:08:38 +05:30

28 lines
753 B
JavaScript

import { memoize } from 'lodash';
import { uuids } from './uuids';
export const create = memoize((options = {}) => {
const id = uuids()[0];
return {
id,
observer: new IntersectionObserver((entries) => {
entries.forEach((entry) => {
entry.target.dispatchEvent(
new CustomEvent(`IntersectionUpdate`, { detail: { entry, observer: id } }),
);
if (entry.isIntersecting) {
entry.target.dispatchEvent(
new CustomEvent(`IntersectionAppear`, { detail: { observer: id } }),
);
} else {
entry.target.dispatchEvent(
new CustomEvent(`IntersectionDisappear`, { detail: { observer: id } }),
);
}
});
}, options),
};
});