// Simple Svelte scrollend detection action.
// ===================================================================
//
// ### Example usage
//
// Simple form (with default 100px threshold):
// ```html
//
{ console.log("end reached") }}>
// ...
//
// ```
//
// With custom threshold:
// ```html
// { console.log("end reached") }
// }}>
// ...
//
// ```
// ===================================================================
function normalize(rawData) {
if (typeof rawData === "function") {
return {
threshold: 100,
callback: rawData,
}
}
return rawData || {};
}
export default function scrollend(node, options) {
options = normalize(options);
options?.callback && options.callback();
function onScroll(e) {
if (!options?.callback) {
return;
}
const offset = e.target.scrollHeight - e.target.clientHeight - e.target.scrollTop;
if (offset <= options.threshold) {
options.callback();
}
}
node.addEventListener("scroll", onScroll);
node.addEventListener("resize", onScroll);
return {
update(newOptions) {
options = normalize(newOptions);
},
destroy() {
node.removeEventListener("scroll", onScroll);
node.removeEventListener("resize", onScroll);
},
};
}