Skip to content

Commit

Permalink
fix: Dimensions context
Browse files Browse the repository at this point in the history
  • Loading branch information
v-almonacid committed Nov 30, 2023
1 parent a652c8c commit 0c2b0a5
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion packages/frontend/src/api/store/providers/dimensions-context.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createContext, FC, useEffect, useRef, useState } from "react";
import { IWindowsSize, useWindowSize } from "src/api/hooks";
import { Logger } from "src/api/utils/logging";
import CONFIG from "src/config";

const { WIDGET_SIZE_TRACKING_ID } = CONFIG.UI;
Expand Down Expand Up @@ -29,7 +30,15 @@ export const DimensionsProvider: FC<{ children?: React.ReactNode }> = ({
const [widgetsSize, setWidgetSize] = useState<IWidgetsSize>();
const timeOutRef = useRef<ReturnType<typeof setTimeout>>();

const previousElement = useRef<HTMLElement | null>(null);
const currElement = document.getElementById(WIDGET_SIZE_TRACKING_ID);

const shouldReRegister =
previousElement.current !== null &&
previousElement.current !== currElement;

useEffect(() => {
Logger.debug("dimensions-context: registering observer");
function handleResize() {
const elem = document.getElementById(WIDGET_SIZE_TRACKING_ID);

Expand All @@ -45,12 +54,14 @@ export const DimensionsProvider: FC<{ children?: React.ReactNode }> = ({
150
);
}
previousElement.current = null;

const resizeObserver = new ResizeObserver(handleResize);

const mutationObserver = new MutationObserver(function Observe() {
const elem = document.getElementById(WIDGET_SIZE_TRACKING_ID);
if (elem) {
previousElement.current = elem;
resizeObserver.observe(elem);
mutationObserver.disconnect();
}
Expand All @@ -59,12 +70,13 @@ export const DimensionsProvider: FC<{ children?: React.ReactNode }> = ({
mutationObserver.observe(document, { childList: true, subtree: true });

return () => {
Logger.debug("dimensions-context: unmounting, cleaning up");
const elem = document.getElementById(WIDGET_SIZE_TRACKING_ID);
if (elem) resizeObserver?.unobserve(elem);
if (timeOutRef.current) clearTimeout(timeOutRef.current);
mutationObserver.disconnect();
};
}, []);
}, [shouldReRegister]);
return (
// eslint-disable-next-line react/jsx-no-constructed-context-values
<DimensionsContext.Provider value={{ widgetsSize, windowSize }}>
Expand Down

0 comments on commit 0c2b0a5

Please sign in to comment.