mirror of
https://github.com/wavetermdev/waveterm.git
synced 2025-01-04 18:59:08 +01:00
6413d49119
This makes the background for the "wave-theme-dark" theme transparent. The light theme is still opaque because otherwise it will look somewhat dark. This also suppresses TypeScript/JavaScript import errors in the default linter, since we don't have support for project directories. This also reworks the useWidth and useHeight hooks to use the useResizeObserver hook, which limits the number of ResizeObserver instances floating around, thereby improving performance
68 lines
2.4 KiB
TypeScript
68 lines
2.4 KiB
TypeScript
import useResizeObserver from "@react-hook/resize-observer";
|
|
import { useCallback, useRef, useState } from "react";
|
|
import { debounce } from "throttle-debounce";
|
|
|
|
/**
|
|
* Get the current dimensions for the specified element, and whether it is currently changing size. Update when the element resizes.
|
|
* @param ref The reference to the element to observe.
|
|
* @param delay The debounce delay to use for updating the dimensions.
|
|
* @returns The dimensions of the element, and direction in which the dimensions are changing.
|
|
*/
|
|
const useDimensions = (ref: React.RefObject<HTMLElement>, delay = 0) => {
|
|
const [dimensions, setDimensions] = useState<{
|
|
height: number | null;
|
|
width: number | null;
|
|
widthDirection?: string;
|
|
heightDirection?: string;
|
|
}>({
|
|
height: null,
|
|
width: null,
|
|
});
|
|
|
|
const previousDimensions = useRef<{ height: number | null; width: number | null }>({
|
|
height: null,
|
|
width: null,
|
|
});
|
|
|
|
const updateDimensions = useCallback((entry: ResizeObserverEntry) => {
|
|
const parentHeight = entry.contentRect.height;
|
|
const parentWidth = entry.contentRect.width;
|
|
|
|
let widthDirection = "";
|
|
let heightDirection = "";
|
|
|
|
if (previousDimensions.current.width !== null && previousDimensions.current.height !== null) {
|
|
if (parentWidth > previousDimensions.current.width) {
|
|
widthDirection = "expanding";
|
|
} else if (parentWidth < previousDimensions.current.width) {
|
|
widthDirection = "shrinking";
|
|
} else {
|
|
widthDirection = "unchanged";
|
|
}
|
|
|
|
if (parentHeight > previousDimensions.current.height) {
|
|
heightDirection = "expanding";
|
|
} else if (parentHeight < previousDimensions.current.height) {
|
|
heightDirection = "shrinking";
|
|
} else {
|
|
heightDirection = "unchanged";
|
|
}
|
|
}
|
|
|
|
previousDimensions.current = { height: parentHeight, width: parentWidth };
|
|
|
|
setDimensions({ height: parentHeight, width: parentWidth, widthDirection, heightDirection });
|
|
}, []);
|
|
|
|
const fUpdateDimensions = useCallback(delay > 0 ? debounce(delay, updateDimensions) : updateDimensions, [
|
|
updateDimensions,
|
|
delay,
|
|
]);
|
|
|
|
useResizeObserver(ref, fUpdateDimensions);
|
|
|
|
return dimensions;
|
|
};
|
|
|
|
export { useDimensions };
|