Skip to content

Commit 502fc5b

Browse files
committed
Do not use framer motion for animation
1 parent 12f60de commit 502fc5b

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed

example/components/chakra-boxes.tsx

+13-26
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,45 @@
11
import React, { useState, useEffect, useRef } from "react";
22
import { Box, VStack, Text } from "@chakra-ui/react";
3-
import { motion } from "framer-motion";
4-
5-
const MotionBox = motion(Box as any);
63

74
const numBoxes = 100;
85
const indentations = Array.from({ length: numBoxes }, (_, i) => i * 100);
96

107
const ChakraBoxes = () => {
11-
const [highlightedIndex, setHighlightedIndex] = useState(0); // Directly calculate index
8+
const [highlightedIndex, setHighlightedIndex] = useState(0);
129
const [offset, setOffset] = useState(0);
13-
const [scrollPercentage, setScrollPercentage] = useState(0); // State for scroll percentage
14-
const canvasRef = useRef();
10+
const [scrollPercentage, setScrollPercentage] = useState(0);
1511

1612
const calculateScrollPercentage = () => {
17-
const scrollTop = window.scrollY; // Distance from the top
18-
const docHeight = document.documentElement.scrollHeight; // Full document height
19-
const winHeight = window.innerHeight; // Visible window height
13+
const scrollTop = window.scrollY;
14+
const docHeight = document.documentElement.scrollHeight;
15+
const winHeight = window.innerHeight;
2016

2117
const totalScrollable = docHeight - winHeight;
2218
const scrolled = (scrollTop / totalScrollable) * 100;
2319

2420
setScrollPercentage(scrolled);
2521

26-
// Calculate the index based on the scroll percentage
2722
const calculatedIndex = Math.round((scrolled / 100) * (numBoxes - 1));
2823
setHighlightedIndex(calculatedIndex);
2924
setOffset(indentations[calculatedIndex]);
3025
};
3126

3227
useEffect(() => {
33-
// Call the function initially and on scroll
3428
calculateScrollPercentage();
35-
3629
window.addEventListener('scroll', calculateScrollPercentage);
37-
38-
// Cleanup the scroll event listener on component unmount
3930
return () => {
4031
window.removeEventListener('scroll', calculateScrollPercentage);
4132
};
4233
}, [numBoxes]);
4334

4435
return (
4536
<Box overflowX={'hidden'}>
46-
<Text position="fixed" top={0} right={0} p={4} fontSize="lg" zIndex={1}>
47-
Scroll Percentage: {scrollPercentage.toFixed(2)}%
37+
<Text fontFamily="monospace" position="fixed" top={0} right={0} p={4} fontSize="lg" zIndex={1}>
38+
Scroll Percentage: {scrollPercentage.toFixed(2).padStart(5, '0')}%
4839
</Text>
49-
<MotionBox
50-
ref={canvasRef}
51-
animate={{ marginLeft: `${-offset}px` }} // Animation on margin change
52-
transition={{
53-
duration: 0.4,
54-
ease: "easeOut", // Built-in easing function
55-
}} // Adjust the transition duration
40+
<Box
41+
marginLeft={`${-offset}px`}
42+
transition="margin-left 0.3s ease-out"
5643
paddingTop={`50vh`}
5744
paddingBottom={`50vh`}
5845
>
@@ -62,9 +49,9 @@ const ChakraBoxes = () => {
6249
key={index}
6350
width="200px"
6451
height="50px"
65-
bg={highlightedIndex === index ? "yellow.300" : "teal.300"} // Highlight the box closest to center
52+
bg={highlightedIndex === index ? "yellow.300" : "teal.300"}
6653
mt={4}
67-
ml={`${indentations[index]}px`} // Adjust left margin for indentation
54+
ml={`${indentations[index]}px`}
6855
display="flex"
6956
alignItems="center"
7057
justifyContent="center"
@@ -74,7 +61,7 @@ const ChakraBoxes = () => {
7461
</Box>
7562
))}
7663
</VStack>
77-
</MotionBox>
64+
</Box>
7865
</Box>
7966
);
8067
};

0 commit comments

Comments
 (0)