-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathSplitViewSized.tsx
50 lines (47 loc) · 1.29 KB
/
SplitViewSized.tsx
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* (c) 2021, Micro:bit Educational Foundation and contributors
*
* SPDX-License-Identifier: MIT
*/
import { Box } from "@chakra-ui/layout";
import { createRef, useEffect } from "react";
import { dimensionPropName, useSplitViewContext } from "./context";
interface SizedPaneProps {
children: JSX.Element;
}
/**
* The pane we give an explicit size to.
*
* The other pane takes the remaining space.
*/
const SplitViewSized = ({ children }: SizedPaneProps) => {
const { direction, sizedPaneSize, compactSize, mode, dragging } =
useSplitViewContext();
const ref = createRef<HTMLDivElement>();
useEffect(() => {
if (ref.current) {
const size = (() => {
switch (mode) {
case "collapsed":
return "0px";
case "open":
return `${sizedPaneSize}px`;
case "compact":
return `${compactSize}px`;
}
})();
ref.current.style[dimensionPropName(direction)] = size;
}
}, [ref, mode, direction, sizedPaneSize, compactSize]);
return (
<Box
pointerEvents={dragging ? "none" : "unset"}
display={mode === "collapsed" ? "none" : undefined}
visibility={mode === "collapsed" ? "hidden" : undefined}
ref={ref}
>
{children}
</Box>
);
};
export default SplitViewSized;