bigbluebutton-Github/bigbluebutton-html5/imports/ui/hooks/useStickyScroll.ts

80 lines
2.1 KiB
TypeScript
Raw Normal View History

import {
useCallback, useEffect, useRef,
} from 'react';
interface Handlers {
startObserving(): void;
stopObserving(): void;
}
const useStickyScroll = (
stickyElement: HTMLElement | null,
onResizeOf: HTMLElement | null,
operator: 'ne' | 'gt' = 'gt',
) => {
const elHeight = useRef(stickyElement?.offsetHeight ?? 0);
Refactor: Make bundle using webpack (#20811) * Refactor: Make bundle using webpack * Fix: restore after install codes and a few settings * Fix: build script folder permission * Refactor: Remove support to async import on audio bridges * Upgrade npm using nvm * Avoid questions on npm ci execution * Let npm ci install dev dependencies (as we need the build tools here) * Fix: enconding * Fix: old lock files * Remove: bbb-config dependency to bbb-html5 service, bbb-html5 isn't a service anymore * Fix: TS errors * Fix: eslint * Fix: chat styles * npm install with "lockfileVersion": 3 (newer npm) * build: allow nodejs 22 * node 22; drop meteor from CI and bbb-conf * TEMP: use bbb-install without mongo but with node 22 and newer image * build: relax nodejs condition to not trip 22.6 * build: ensure dir /usr/share/bigbluebutton/nginx exists * init sites-available/bbb; drop disable-transparent- * nginx complaining of missing file and ; * TMP: print status of services * WIP: tweak nginx location to debug * Fix: webcam widgets alignments * akka-apps -- update location of settings.yml * build: add locales path for nginx * docs and config changes for removal of meteor * Fix: build encoding and locales enpoint folder path * build: set wss url for media * Add: Enable minimizer and modify to Terser * Fix: TS errors --------- Co-authored-by: Tiago Jacobs <tiago.jacobs@gmail.com> Co-authored-by: Anton Georgiev <anto.georgiev@gmail.com> Co-authored-by: Anton Georgiev <antobinary@users.noreply.github.com>
2024-08-10 01:58:44 +08:00
const timeout = useRef<ReturnType<typeof setTimeout>>();
const observer = useRef<ResizeObserver | null>(null);
const handlers = useRef<Handlers>({
startObserving: () => {},
stopObserving: () => {},
});
useEffect(() => {
if (observer.current) {
observer.current.disconnect();
}
observer.current = new ResizeObserver((entries) => {
entries.forEach((entry) => {
const { target } = entry;
if (target instanceof HTMLElement) {
let elementHeightChanged = false;
switch (operator) {
case 'ne': {
elementHeightChanged = target.offsetHeight !== elHeight.current;
break;
}
case 'gt':
default: {
elementHeightChanged = target.offsetHeight > elHeight.current;
break;
}
}
if (elementHeightChanged) {
elHeight.current = target.offsetHeight;
2024-10-03 06:28:02 +08:00
if (stickyElement) {
// eslint-disable-next-line no-param-reassign
stickyElement.scrollTop = stickyElement.scrollHeight + stickyElement.clientHeight;
}
}
}
});
});
}, [stickyElement, operator]);
handlers.current.startObserving = useCallback(() => {
2024-10-03 06:28:02 +08:00
if (!onResizeOf) return;
clearTimeout(timeout.current);
observer.current?.observe(onResizeOf);
}, [onResizeOf]);
handlers.current.stopObserving = useCallback(() => {
2024-10-03 06:28:02 +08:00
if (!onResizeOf) return;
timeout.current = setTimeout(() => {
observer.current?.unobserve(onResizeOf);
}, 500);
}, [onResizeOf]);
useEffect(
() => () => {
observer.current?.disconnect();
},
[],
);
return handlers.current;
};
export default useStickyScroll;