From 9041bae89f54efce14239768e642f99d1f0b35d1 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 4 Apr 2021 23:06:00 +0900 Subject: Make resize to a custom hook --- src/console/hooks/useAutoResize.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/console/hooks/useAutoResize.ts (limited to 'src/console/hooks') diff --git a/src/console/hooks/useAutoResize.ts b/src/console/hooks/useAutoResize.ts new file mode 100644 index 0000000..4253606 --- /dev/null +++ b/src/console/hooks/useAutoResize.ts @@ -0,0 +1,28 @@ +import React from "react"; +import ConsoleFrameClient from "../clients/ConsoleFrameClient"; + +const useAutoResize = () => { + const [prevWidth, setPrevWidth] = React.useState(-1); + const [prevHeight, setPrevHeight] = React.useState(-1); + + const consoleFrameClient = React.useMemo(() => { + return new ConsoleFrameClient(); + }, []); + + React.useLayoutEffect(() => { + const { + scrollWidth: width, + scrollHeight: height, + } = document.getElementById("vimvixen-console")!; + consoleFrameClient.resize(width, height); + + if (width === prevWidth && height === prevHeight) { + return; + } + + setPrevWidth(width); + setPrevHeight(height); + }); +}; + +export default useAutoResize; -- cgit v1.2.3