2022-05-13 04:21:08 +00:00
|
|
|
/**
|
2024-05-26 03:18:18 +00:00
|
|
|
* Initialize a direction observer based on scroll behavior.
|
2022-11-10 01:02:21 +00:00
|
|
|
*
|
2024-05-26 03:18:18 +00:00
|
|
|
* @param {Function} onScrollDown - Function to be called when scrolling down.
|
|
|
|
* @param {Function} onScrollUp - Function to be called when scrolling up.
|
|
|
|
* @param {number} threshold - The threshold for significant scroll position change.
|
2024-11-07 00:31:49 +00:00
|
|
|
* @return {Object}
|
2022-11-10 01:02:21 +00:00
|
|
|
*/
|
2024-11-07 00:31:49 +00:00
|
|
|
function initDirectionObserver( onScrollDown, onScrollUp, threshold = 0 ) {
|
2024-11-08 01:33:56 +00:00
|
|
|
let lastScrollPosition = 0;
|
2024-11-02 06:24:54 +00:00
|
|
|
let lastScrollDirection = '';
|
|
|
|
|
2024-11-08 01:33:56 +00:00
|
|
|
const onScroll = () => {
|
|
|
|
const currentScrollPosition = window.scrollY;
|
|
|
|
const scrollDiff = currentScrollPosition - lastScrollPosition;
|
2024-11-07 00:31:49 +00:00
|
|
|
|
2024-11-08 01:33:56 +00:00
|
|
|
if ( Math.abs( scrollDiff ) < threshold ) {
|
2024-11-07 00:31:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-11-10 01:02:21 +00:00
|
|
|
|
2024-11-08 01:33:56 +00:00
|
|
|
if ( scrollDiff > 0 && lastScrollDirection !== 'down' ) {
|
2024-11-07 00:31:49 +00:00
|
|
|
lastScrollDirection = 'down';
|
|
|
|
onScrollDown();
|
2024-11-08 01:33:56 +00:00
|
|
|
} else if ( scrollDiff < 0 && lastScrollDirection !== 'up' ) {
|
2024-11-07 00:31:49 +00:00
|
|
|
lastScrollDirection = 'up';
|
|
|
|
onScrollUp();
|
|
|
|
}
|
2024-11-08 01:33:56 +00:00
|
|
|
lastScrollPosition = currentScrollPosition <= 0 ? 0 : currentScrollPosition;
|
2024-11-07 00:31:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
resume: () => {
|
2024-11-08 01:53:21 +00:00
|
|
|
window.addEventListener( 'scroll', mw.util.throttle( onScroll, 100 ) );
|
2024-11-07 00:31:49 +00:00
|
|
|
},
|
|
|
|
pause: () => {
|
2024-11-08 01:53:21 +00:00
|
|
|
window.removeEventListener( 'scroll', mw.util.throttle( onScroll, 100 ) );
|
2024-11-07 00:31:49 +00:00
|
|
|
}
|
|
|
|
};
|
2022-11-10 01:02:21 +00:00
|
|
|
}
|
|
|
|
|
2024-09-10 22:28:12 +00:00
|
|
|
/**
|
|
|
|
* Create an observer for showing/hiding feature and for firing scroll event hooks.
|
|
|
|
*
|
|
|
|
* @param {Function} show functionality for when feature is visible
|
|
|
|
* @param {Function} hide functionality for when feature is hidden
|
|
|
|
* @return {IntersectionObserver}
|
|
|
|
*/
|
|
|
|
function initScrollObserver( show, hide ) {
|
|
|
|
/* eslint-disable-next-line compat/compat */
|
|
|
|
return new IntersectionObserver( ( entries ) => {
|
|
|
|
if ( !entries[ 0 ].isIntersecting && entries[ 0 ].boundingClientRect.top < 0 ) {
|
|
|
|
// Viewport has crossed the bottom edge of the target element.
|
|
|
|
show();
|
|
|
|
} else {
|
|
|
|
// Viewport is above the bottom edge of the target element.
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2022-05-13 04:21:08 +00:00
|
|
|
module.exports = {
|
2022-11-10 01:02:21 +00:00
|
|
|
initDirectionObserver,
|
2024-09-10 22:28:12 +00:00
|
|
|
initScrollObserver
|
2022-05-13 04:21:08 +00:00
|
|
|
};
|