aboutsummaryrefslogtreecommitdiff
path: root/src/console/hooks/useAutoResize.ts
blob: 425360646bac87321382b7c7fa1e204e3433cb96 (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
27
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;