Skip to content

Commit 96e0d2a

Browse files
committed
Add experimental code
1 parent 47d69a7 commit 96e0d2a

File tree

10 files changed

+1708
-673
lines changed

10 files changed

+1708
-673
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React, { useState } from "react";
2+
import { Box, Button, Input, VStack } from "@chakra-ui/react";
3+
4+
const ChakraBoxes = () => {
5+
const [numBoxes, setNumBoxes] = useState(100); // Default number of boxes
6+
const [indentations, setIndentations] = useState(Array.from({ length: numBoxes }, (_, i) => i * 10)); // Initial indentations
7+
8+
// Function to handle number of boxes change
9+
const handleNumBoxesChange = (e) => {
10+
const value = parseInt(e.target.value) || 0;
11+
setNumBoxes(value);
12+
13+
// Update the indentation array when number of boxes changes
14+
setIndentations(
15+
Array.from({ length: value }, (_, i) => i * 10) // Example: indentation increases by 10px per box
16+
);
17+
};
18+
19+
return (
20+
<VStack spacing={4} overflowX={'hidden'}>
21+
<Input
22+
type="number"
23+
value={numBoxes}
24+
onChange={handleNumBoxesChange}
25+
placeholder="Enter number of boxes"
26+
width="200px"
27+
/>
28+
29+
{Array.from({ length: numBoxes }).map((_, index) => (
30+
<Box
31+
key={index}
32+
width="200px"
33+
height="50px"
34+
bg="teal.300"
35+
mt={4}
36+
ml={`${indentations[index]}px`} // Adjust left margin for indentation
37+
display="flex"
38+
alignItems="center"
39+
justifyContent="center"
40+
>
41+
Box {index + 1}
42+
</Box>
43+
))}
44+
</VStack>
45+
);
46+
};
47+
48+
export default ChakraBoxes;

example/components/deep-tree.tsx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import React from 'react';
2+
3+
interface DataNode {
4+
readonly content: any;
5+
readonly children: DataNode[];
6+
}
7+
export type { DataNode };
8+
9+
interface TreeNodeProps {
10+
data: DataNode;
11+
ListItem?: React.ElementType;
12+
List?: React.ElementType;
13+
ContentFrame?: React.ElementType;
14+
}
15+
16+
export function TreeNode({ data, ListItem = 'li', List = 'ul', ContentFrame = 'div' }: TreeNodeProps) {
17+
const { content, children } = data;
18+
let item;
19+
let treeNodesList;
20+
21+
if (content !== undefined) {
22+
item = <ContentFrame>{content}</ContentFrame>;
23+
}
24+
if (children && children.length > 0) {
25+
const treeNodes = children.map((element, index) => (
26+
<TreeNode data={element} key={index} ListItem={ListItem} List={List} ContentFrame={ContentFrame} />
27+
));
28+
treeNodesList = <List>{treeNodes}</List>;
29+
}
30+
return (
31+
<ListItem>
32+
{item}
33+
{treeNodesList}
34+
</ListItem>
35+
);
36+
}
37+
38+
interface DeepTreeProps {
39+
data: DataNode[];
40+
ListItem?: React.ElementType;
41+
List?: React.ElementType;
42+
ContentFrame?: React.ElementType;
43+
TreeFrame?: React.ElementType;
44+
}
45+
46+
export function DeepTree({ data, ListItem = 'li', List = 'ul', ContentFrame = 'div', TreeFrame='div' }: DeepTreeProps) {
47+
return (
48+
<TreeFrame>
49+
<List>
50+
{data.map((object, index) => (
51+
<TreeNode key={index} data={object} ListItem={ListItem} List={List} ContentFrame={ContentFrame} />
52+
))}
53+
</List>
54+
</TreeFrame>
55+
);
56+
}
57+
58+
export default DeepTree;

example/components/scroll-sync.tsx

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import React, { useEffect, useState, useRef } from 'react';
2+
import { Box, Flex, VStack } from "@chakra-ui/react";
3+
4+
const ScrollSync = () => {
5+
const [currentBoxIndex, setCurrentBoxIndex] = useState(0);
6+
const containerRef = useRef(null);
7+
const horizontalScrollRef = useRef(null);
8+
9+
const boxes = [
10+
{ bg: "tomato", height: "300px" },
11+
{ bg: "blue.300", height: "400px" },
12+
{ bg: "green.300", height: "350px" },
13+
{ bg: "yellow.400", height: "500px" },
14+
];
15+
16+
const handleScroll = () => {
17+
const container = containerRef.current;
18+
const containerHeight = container.offsetHeight;
19+
const scrollPosition = container.scrollTop;
20+
const middlePosition = scrollPosition + containerHeight / 2;
21+
22+
// Check which box is in the middle of the viewport
23+
let closestBoxIndex = 0;
24+
let closestDistance = Infinity;
25+
26+
boxes.forEach((box, index) => {
27+
const boxElement = document.getElementById(`box-${index}`);
28+
const boxTop = boxElement.offsetTop;
29+
const boxHeight = boxElement.offsetHeight;
30+
const boxMiddle = boxTop + boxHeight / 2;
31+
32+
const distance = Math.abs(middlePosition - boxMiddle);
33+
34+
if (distance < closestDistance) {
35+
closestDistance = distance;
36+
closestBoxIndex = index;
37+
}
38+
});
39+
40+
setCurrentBoxIndex(closestBoxIndex);
41+
};
42+
43+
useEffect(() => {
44+
const container = containerRef.current;
45+
container.addEventListener("scroll", handleScroll);
46+
47+
return () => {
48+
container.removeEventListener("scroll", handleScroll);
49+
};
50+
}, []);
51+
52+
useEffect(() => {
53+
// Sync horizontal scroll when the box in the middle changes
54+
const horizontalContainer = horizontalScrollRef.current;
55+
const boxWidth = 200; // Adjust the width based on your box size
56+
horizontalContainer.scrollLeft = currentBoxIndex * boxWidth;
57+
}, [currentBoxIndex]);
58+
59+
return (
60+
<Flex direction="column" align="center" height="100vh" overflow="hidden">
61+
<Flex ref={horizontalScrollRef} width="100%" overflowX="auto">
62+
{boxes.map((box, index) => (
63+
<Box key={index} width="200px" height="50px" bg={box.bg} />
64+
))}
65+
</Flex>
66+
67+
<VStack
68+
ref={containerRef}
69+
spacing={6}
70+
height="100%"
71+
overflowY="auto"
72+
width="100%"
73+
p={4}
74+
>
75+
{boxes.map((box, index) => (
76+
<Box
77+
key={index}
78+
id={`box-${index}`}
79+
bg={box.bg}
80+
height={box.height}
81+
width="100%"
82+
>
83+
Box {index + 1}
84+
</Box>
85+
))}
86+
</VStack>
87+
</Flex>
88+
);
89+
};
90+
91+
export default ScrollSync;

example/next-env.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
/// <reference types="next" />
2-
/// <reference types="next/types/global" />
2+
/// <reference types="next/image-types/global" />
3+
4+
// NOTE: This file should not be edited
5+
// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information.

0 commit comments

Comments
 (0)