aboutsummaryrefslogtreecommitdiff
path: root/src/console/hooks/useAutoResize.ts
blob: 26f1d76a005c5d0bb86e178c49a939e1cf03e017 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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;