Skip to content

Commit

Permalink
fix: recalculate bounding client size on window resize
Browse files Browse the repository at this point in the history
  • Loading branch information
bcho committed Sep 6, 2022
1 parent d0fc16e commit 202d85a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
16 changes: 11 additions & 5 deletions browser-ui/src/component/Editor/EditorPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { editor } from "monaco-editor";
import React from "react";
import { useLoadedEditor } from "../../atom/editorAtom";
import { grpcClient } from "../../client/api";
import useWindowSize from "../../hook/useWindowSize";
import KustoEditor from "./KustoEditor";
import ResultTable from "./ResultTable";
import { ResultTableViewModel, RunQueryViewModel, useResultTableViewModel, useRunQueryAction } from "./viewModel";
Expand Down Expand Up @@ -60,12 +61,17 @@ function EditorBody(props: EditorBodyProps) {

const [editorHeight, setEditorHeight] = React.useState(200);
const [viewWidth, setViewWidth] = React.useState(0);
const [windowWidth] = useWindowSize();

const setViewRef = React.useCallback((view: HTMLDivElement | null) => {
if (view) {
setViewWidth(view.getBoundingClientRect().width);
}
}, []);
const setViewRef = React.useCallback(
(view: HTMLDivElement | null) => {
if (view) {
setViewWidth(view.getBoundingClientRect().width);
}
},
// NOTE: ensure recalculate bounding client size on window resize
[windowWidth],
);

return (
<div className="flex-1" ref={setViewRef}>
Expand Down
14 changes: 14 additions & 0 deletions browser-ui/src/hook/useWindowSize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useLayoutEffect, useState } from 'react';

export default function useWindowSize() {
const [size, setSize] = useState([0, 0]);
useLayoutEffect(() => {
function updateSize() {
setSize([window.innerWidth, window.innerHeight]);
}
window.addEventListener('resize', updateSize);
updateSize();
return () => window.removeEventListener('resize', updateSize);
}, []);
return size;
}

0 comments on commit 202d85a

Please sign in to comment.