Skip to content

Commit 7246e49

Browse files
committed
Enable animation
1 parent 4cbec48 commit 7246e49

File tree

1 file changed

+41
-27
lines changed

1 file changed

+41
-27
lines changed

example/components/chakra-boxes.tsx

+41-27
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import React, { useState, useEffect, useRef } from "react";
22
import { Box, Input, VStack } from "@chakra-ui/react";
33

4+
import { motion } from "framer-motion";
5+
6+
const MotionBox = motion(Box as any);
7+
48
const ChakraBoxes = () => {
59
const [numBoxes, setNumBoxes] = useState(100); // Default number of boxes
6-
const [indentations, setIndentations] = useState(Array.from({ length: numBoxes }, (_, i) => i * 10)); // Initial indentations
10+
const [indentations, setIndentations] = useState(Array.from({ length: numBoxes }, (_, i) => i * 100)); // Initial indentations
711
const [highlightedIndex, setHighlightedIndex] = useState(null); // Index of the box in the center
812
const [offset, setOffset] = useState(0);
913
const boxRefs = useRef([]); // Ref array to hold references to each box
10-
const stackRef = useRef();
14+
const canvasRef = useRef();
1115

1216
// Function to handle number of boxes change
1317
const handleNumBoxesChange = (e) => {
@@ -16,12 +20,12 @@ const ChakraBoxes = () => {
1620

1721
// Update the indentation array when number of boxes changes
1822
setIndentations(
19-
Array.from({ length: value }, (_, i) => i * 10) // Example: indentation increases by 10px per box
23+
Array.from({ length: value }, (_, i) => i * 100) // Example: indentation increases by 10px per box
2024
);
2125
};
2226

2327
const calculateClosestBoxToCenter = () => {
24-
if (!stackRef.current) {
28+
if (!canvasRef.current) {
2529
return;
2630
}
2731
const viewportHeight = window.innerHeight;
@@ -67,31 +71,41 @@ const ChakraBoxes = () => {
6771
placeholder="Enter number of boxes"
6872
width="200px"
6973
/>
70-
<VStack
71-
ref={stackRef}
72-
spacing={4}
73-
marginLeft={`${-offset}px`}
74+
<MotionBox
75+
ref={canvasRef}
76+
animate={{ marginLeft: `${-offset}px` }} // Animation on margin change
77+
transition={{
78+
duration: 0.4,
79+
// ease: "easeIn", // Built-in easing function
80+
ease: "easeOut", // Built-in easing function
81+
// ease: "linear", // Built-in easing function
82+
}} // Adjust the transition duration
83+
paddingTop={`50vh`}
84+
paddingBottom={`50vh`}
7485
>
75-
{Array.from({ length: numBoxes }).map((_, index) => (
76-
<Box
77-
key={index}
78-
ref={(el) => (boxRefs.current[index] = el)} // Assign the ref to the box
79-
width="200px"
80-
height="50px"
81-
bg={highlightedIndex === index ? "yellow.300" : "teal.300"} // Highlight the box closest to center
82-
mt={4}
83-
ml={`${indentations[index]}px`} // Adjust left margin for indentation
84-
display="flex"
85-
alignItems="center"
86-
justifyContent="center"
87-
transition="background-color 0.3s ease"
88-
>
89-
Box {index + 1}
90-
</Box>
91-
))}
92-
</VStack>
86+
<VStack
87+
spacing={4}
88+
>
89+
{Array.from({ length: numBoxes }).map((_, index) => (
90+
<Box
91+
key={index}
92+
ref={(el) => (boxRefs.current[index] = el)} // Assign the ref to the box
93+
width="200px"
94+
height="50px"
95+
bg={highlightedIndex === index ? "yellow.300" : "teal.300"} // Highlight the box closest to center
96+
mt={4}
97+
ml={`${indentations[index]}px`} // Adjust left margin for indentation
98+
display="flex"
99+
alignItems="center"
100+
justifyContent="center"
101+
transition="background-color 0.3s ease"
102+
>
103+
Box {index + 1}
104+
</Box>
105+
))}
106+
</VStack>
107+
</MotionBox>
93108
</Box>
94-
95109
);
96110
};
97111

0 commit comments

Comments
 (0)