1
1
import React , { useState , useEffect , useRef } from "react" ;
2
2
import { Box , VStack , Text } from "@chakra-ui/react" ;
3
- import { motion } from "framer-motion" ;
4
-
5
- const MotionBox = motion ( Box as any ) ;
6
3
7
4
const numBoxes = 100 ;
8
5
const indentations = Array . from ( { length : numBoxes } , ( _ , i ) => i * 100 ) ;
9
6
10
7
const ChakraBoxes = ( ) => {
11
- const [ highlightedIndex , setHighlightedIndex ] = useState ( 0 ) ; // Directly calculate index
8
+ const [ highlightedIndex , setHighlightedIndex ] = useState ( 0 ) ;
12
9
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 ) ;
15
11
16
12
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 ;
20
16
21
17
const totalScrollable = docHeight - winHeight ;
22
18
const scrolled = ( scrollTop / totalScrollable ) * 100 ;
23
19
24
20
setScrollPercentage ( scrolled ) ;
25
21
26
- // Calculate the index based on the scroll percentage
27
22
const calculatedIndex = Math . round ( ( scrolled / 100 ) * ( numBoxes - 1 ) ) ;
28
23
setHighlightedIndex ( calculatedIndex ) ;
29
24
setOffset ( indentations [ calculatedIndex ] ) ;
30
25
} ;
31
26
32
27
useEffect ( ( ) => {
33
- // Call the function initially and on scroll
34
28
calculateScrollPercentage ( ) ;
35
-
36
29
window . addEventListener ( 'scroll' , calculateScrollPercentage ) ;
37
-
38
- // Cleanup the scroll event listener on component unmount
39
30
return ( ) => {
40
31
window . removeEventListener ( 'scroll' , calculateScrollPercentage ) ;
41
32
} ;
42
33
} , [ numBoxes ] ) ;
43
34
44
35
return (
45
36
< 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' ) } %
48
39
</ 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"
56
43
paddingTop = { `50vh` }
57
44
paddingBottom = { `50vh` }
58
45
>
@@ -62,9 +49,9 @@ const ChakraBoxes = () => {
62
49
key = { index }
63
50
width = "200px"
64
51
height = "50px"
65
- bg = { highlightedIndex === index ? "yellow.300" : "teal.300" } // Highlight the box closest to center
52
+ bg = { highlightedIndex === index ? "yellow.300" : "teal.300" }
66
53
mt = { 4 }
67
- ml = { `${ indentations [ index ] } px` } // Adjust left margin for indentation
54
+ ml = { `${ indentations [ index ] } px` }
68
55
display = "flex"
69
56
alignItems = "center"
70
57
justifyContent = "center"
@@ -74,7 +61,7 @@ const ChakraBoxes = () => {
74
61
</ Box >
75
62
) ) }
76
63
</ VStack >
77
- </ MotionBox >
64
+ </ Box >
78
65
</ Box >
79
66
) ;
80
67
} ;
0 commit comments