vuestic-ui
Version:
Vue 3 UI Framework
79 lines (78 loc) • 2.65 kB
JavaScript
import { t as throttle } from "../../utils/throttle.mjs";
function getWindowHeight() {
return document.documentElement.clientHeight || window.innerHeight || document.body.clientHeight;
}
function computeAffixedState({
coordinates,
offsetTop,
offsetBottom,
target
}) {
let isTopAffixed = false;
let isBottomAffixed = false;
const windowHeight = getWindowHeight();
if (offsetTop != null && windowHeight) {
if (target === window) {
isTopAffixed = coordinates.top <= offsetTop;
} else {
const { top } = target.getBoundingClientRect();
isTopAffixed = coordinates.top - top <= offsetTop;
}
}
if (offsetBottom != null && windowHeight) {
if (target === window) {
isBottomAffixed = coordinates.bottom >= windowHeight - offsetBottom;
} else {
const { bottom } = target.getBoundingClientRect();
isBottomAffixed = bottom - coordinates.bottom <= offsetBottom;
}
}
return {
isTopAffixed,
isBottomAffixed
};
}
function checkAffixedStateChange(currentState, nextState) {
return currentState.isTopAffixed !== nextState.isTopAffixed || currentState.isBottomAffixed !== nextState.isBottomAffixed;
}
function handleThrottledEvent(eventName, context) {
const { target, element, offsetTop, offsetBottom, setState, getState, initialPosition } = context;
if (!element) {
return;
}
const isInitialCall = !eventName;
const coordinates = element.getBoundingClientRect();
const options = {
offsetBottom,
offsetTop,
target
};
const nextState = isInitialCall && initialPosition ? computeAffixedState({ coordinates: initialPosition, ...options }) : computeAffixedState({ coordinates, ...options });
const prevState = getState();
if (checkAffixedStateChange(prevState, nextState)) {
setState({ ...nextState, width: coordinates.width });
} else if (prevState.width !== coordinates.width) {
setState({ ...prevState, width: coordinates.width });
}
}
function useCaptureDefault(eventName) {
return eventName === "scroll";
}
function useEventsHandlerWithThrottle(events, {
handler,
useCapture = useCaptureDefault,
wait = 50
}) {
const clearHandlersArray = events.map((eventName) => {
const _handler = throttle((event) => handler(eventName, event), wait);
window.addEventListener(eventName, _handler, useCapture(eventName));
return () => window.removeEventListener(eventName, _handler, useCapture(eventName));
});
return () => clearHandlersArray.forEach((clear) => clear());
}
export {
getWindowHeight as g,
handleThrottledEvent as h,
useEventsHandlerWithThrottle as u
};
//# sourceMappingURL=VaAffix-utils.mjs.map