1
1
import React , { useState , useEffect , useRef } from "react" ;
2
2
import { Box , Input , VStack } from "@chakra-ui/react" ;
3
3
4
+ import { motion } from "framer-motion" ;
5
+
6
+ const MotionBox = motion ( Box as any ) ;
7
+
4
8
const ChakraBoxes = ( ) => {
5
9
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
7
11
const [ highlightedIndex , setHighlightedIndex ] = useState ( null ) ; // Index of the box in the center
8
12
const [ offset , setOffset ] = useState ( 0 ) ;
9
13
const boxRefs = useRef ( [ ] ) ; // Ref array to hold references to each box
10
- const stackRef = useRef ( ) ;
14
+ const canvasRef = useRef ( ) ;
11
15
12
16
// Function to handle number of boxes change
13
17
const handleNumBoxesChange = ( e ) => {
@@ -16,12 +20,12 @@ const ChakraBoxes = () => {
16
20
17
21
// Update the indentation array when number of boxes changes
18
22
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
20
24
) ;
21
25
} ;
22
26
23
27
const calculateClosestBoxToCenter = ( ) => {
24
- if ( ! stackRef . current ) {
28
+ if ( ! canvasRef . current ) {
25
29
return ;
26
30
}
27
31
const viewportHeight = window . innerHeight ;
@@ -67,31 +71,41 @@ const ChakraBoxes = () => {
67
71
placeholder = "Enter number of boxes"
68
72
width = "200px"
69
73
/>
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` }
74
85
>
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 >
93
108
</ Box >
94
-
95
109
) ;
96
110
} ;
97
111
0 commit comments