fix animation frame resize observer

This commit is contained in:
Laila Los
2022-12-06 15:31:57 +01:00
parent d739c7a24e
commit 4ae456b854
2 changed files with 22 additions and 8 deletions
@@ -70,8 +70,8 @@ export default {
const isScrollable = ref(false);
const { arrived } = useAnimationFrameScroll(scrollContainer);
useAnimationFrameResizeObserver(scrollContainer, () => {
isScrollable.value = scrollContainer.value.scrollWidth >= scrollContainer.value.clientWidth;
useAnimationFrameResizeObserver(scrollContainer, ({ clientSize, scrollSize }) => {
isScrollable.value = scrollSize.width >= clientSize.width + 1;
});
const scrolledLeft = computed(() => !isScrollable.value || arrived.left);
@@ -2,16 +2,30 @@ import { useAnimationFrame } from "./animationFrame";
import { unref } from "vue";
export function useAnimationFrameResizeObserver(element, callback) {
var lastSize = { width: 0, height: 0 };
var clientSize = { width: 0, height: 0 };
var scrollSize = { width: 0, height: 0 };
const isSameSize = (a, b) => {
return a.width === b.width && a.height === b.height;
};
const { stop } = useAnimationFrame(() => {
const el = unref(element);
const width = el.offsetWidth;
const height = el.offsetHeight;
if (lastSize.width !== width || lastSize.height !== height) {
callback({ width, height }, lastSize);
lastSize = { width, height };
const newClientSize = {
width: el.clientWidth,
height: el.clientHeight,
};
const newScrollSize = {
width: el.scrollWidth,
height: el.scrollHeight,
};
if (!isSameSize(clientSize, newClientSize) || !isSameSize(scrollSize, newScrollSize)) {
callback({ clientSize: newClientSize, scrollSize: newScrollSize }, { clientSize, scrollSize });
clientSize = newClientSize;
scrollSize = newScrollSize;
}
});