mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-14 20:51:50 +00:00
improvement: allow users to expand collapsed environment view header
This commit is contained in:
@@ -4,6 +4,7 @@ export { usePagination } from "./usePagination";
|
||||
export { usePersistentState } from "./usePersistentState";
|
||||
export { usePopUp } from "./usePopUp";
|
||||
export { useResetPageHelper } from "./useResetPageHelper";
|
||||
export * from "./useResizableHeaderHeight";
|
||||
export { useSyntaxHighlight } from "./useSyntaxHighlight";
|
||||
export { useTimedReset } from "./useTimedReset";
|
||||
export { useToggle } from "./useToggle";
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
import { MouseEvent, useCallback, useEffect, useRef, useState } from "react";
|
||||
|
||||
type Params = {
|
||||
minHeight: number;
|
||||
maxHeight: number;
|
||||
initialHeight: number;
|
||||
};
|
||||
|
||||
export const useResizableHeaderHeight = ({ minHeight, maxHeight, initialHeight }: Params) => {
|
||||
const [headerHeight, setHeaderHeight] = useState(initialHeight);
|
||||
const [isResizing, setIsResizing] = useState(false);
|
||||
const startY = useRef(0);
|
||||
const startHeight = useRef(0);
|
||||
|
||||
const handleMouseDown = useCallback(
|
||||
(e: MouseEvent<HTMLDivElement>) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
setIsResizing(true);
|
||||
startY.current = e.clientY;
|
||||
startHeight.current = headerHeight;
|
||||
},
|
||||
[headerHeight]
|
||||
);
|
||||
|
||||
const handleMouseMove = useCallback(
|
||||
(e: MouseEvent) => {
|
||||
if (!isResizing) return;
|
||||
|
||||
const deltaY = e.clientY - startY.current;
|
||||
const newHeight = Math.max(minHeight, Math.min(maxHeight, startHeight.current + deltaY));
|
||||
|
||||
setHeaderHeight(newHeight);
|
||||
},
|
||||
[isResizing]
|
||||
);
|
||||
|
||||
const handleMouseUp = useCallback(() => {
|
||||
setIsResizing(false);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (isResizing) {
|
||||
document.addEventListener(
|
||||
"mousemove",
|
||||
// @ts-expect-error native discrepancy
|
||||
handleMouseMove
|
||||
);
|
||||
document.addEventListener("mouseup", handleMouseUp);
|
||||
document.body.style.cursor = "ns-resize";
|
||||
document.body.style.userSelect = "none";
|
||||
}
|
||||
|
||||
return () => {
|
||||
document.removeEventListener(
|
||||
"mousemove",
|
||||
// @ts-expect-error native discrepancy
|
||||
handleMouseMove
|
||||
);
|
||||
document.removeEventListener("mouseup", handleMouseUp);
|
||||
document.body.style.cursor = "";
|
||||
document.body.style.userSelect = "";
|
||||
};
|
||||
}, [isResizing, handleMouseMove, handleMouseUp]);
|
||||
|
||||
return {
|
||||
headerHeight,
|
||||
handleMouseDown,
|
||||
isResizing
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user