-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves: flyteorg/flyte#5102 Signed-off-by: Chi-Sheng Liu <[email protected]>
- Loading branch information
1 parent
eb7fe2b
commit 1a9ff50
Showing
3 changed files
with
69 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Reference: https://stackoverflow.com/a/68742668 | ||
|
||
import { useCallback, useEffect, useState } from 'react'; | ||
|
||
type UseResizeProps = { | ||
minWidth: number; | ||
}; | ||
|
||
type UseResizeReturn = { | ||
width: number; | ||
enableResize: () => void; | ||
}; | ||
|
||
const useResize = ({ minWidth }: UseResizeProps): UseResizeReturn => { | ||
const [isResizing, setIsResizing] = useState(false); | ||
const [width, setWidth] = useState(minWidth); | ||
|
||
const enableResize = useCallback(() => { | ||
setIsResizing(true); | ||
}, [setIsResizing]); | ||
|
||
const disableResize = useCallback(() => { | ||
setIsResizing(false); | ||
}, [setIsResizing]); | ||
|
||
const resize = useCallback( | ||
(e: MouseEvent) => { | ||
if (isResizing) { | ||
const newWidth = document.body.offsetWidth - e.clientX; // You may want to add some offset here from props | ||
if (newWidth >= minWidth) { | ||
setWidth(newWidth); | ||
} | ||
} | ||
}, | ||
[minWidth, isResizing, setWidth], | ||
); | ||
|
||
useEffect(() => { | ||
document.addEventListener('mousemove', resize); | ||
document.addEventListener('mouseup', disableResize); | ||
|
||
return () => { | ||
document.removeEventListener('mousemove', resize); | ||
document.removeEventListener('mouseup', disableResize); | ||
}; | ||
}, [disableResize, resize]); | ||
|
||
return { width, enableResize }; | ||
}; | ||
|
||
export default useResize; |