forked from microbit-foundation/cctd-ml-machine
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCodeViewCard.tsx
More file actions
92 lines (88 loc) · 2.34 KB
/
CodeViewCard.tsx
File metadata and controls
92 lines (88 loc) · 2.34 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* (c) 2024, Micro:bit Educational Foundation and contributors
*
* SPDX-License-Identifier: MIT
*/
import { Box, Card, CardProps, SkeletonText, VStack } from "@chakra-ui/react";
import {
BlockLayout,
MakeCodeBlocksRendering,
MakeCodeProject,
} from "@microbit/makecode-embed/react";
import { memo, useLayoutEffect, useRef, useState } from "react";
import { tourElClassname } from "../tours";
interface CodeViewCardProps extends CardProps {
project: MakeCodeProject;
parentRef: React.RefObject<HTMLDivElement>;
}
const CodeViewCard = ({ project, parentRef, ...props }: CodeViewCardProps) => {
// This is used to set the tour cutout as the card can be taller than
// the parent in a scrollable area.
const [observableHeight, setObservableHeight] = useState<number | string>(
"full"
);
const ref = useRef<HTMLDivElement>(null);
const paddingTop = 8;
useLayoutEffect(() => {
const resizeObserver = new ResizeObserver(() => {
if (parentRef.current && ref.current) {
if (
parentRef.current.clientHeight - paddingTop <
ref.current.clientHeight
) {
setObservableHeight(parentRef.current.clientHeight - paddingTop);
} else {
setObservableHeight(ref.current.clientHeight);
}
}
});
if (parentRef.current) {
resizeObserver.observe(parentRef.current);
}
if (ref.current) {
resizeObserver.observe(ref.current);
}
return () => {
resizeObserver.disconnect();
};
}, [parentRef]);
return (
<VStack
alignSelf="start"
display="flex"
flexDirection="column"
h="full"
w="full"
borderColor="brand.500"
justifyContent="center"
>
<Card
ref={ref}
w="full"
h="full"
p={5}
objectFit="contain"
position="relative"
{...props}
>
<Box
position="absolute"
zIndex={-1}
w="full"
h={observableHeight}
top={0}
left={0}
className={tourElClassname.makeCodeCodeView}
/>
<MakeCodeBlocksRendering
code={project}
layout={BlockLayout.Flow}
loaderCmp={
<SkeletonText w="xs" noOfLines={5} spacing="5" skeletonHeight="2" />
}
/>
</Card>
</VStack>
);
};
export default memo(CodeViewCard);