@@ -44,23 +44,70 @@ function useStepSpring(
44
44
step : CodeStep ,
45
45
springConfig : SpringConfig = defaultSpring
46
46
) : { tween : FullTween < CodeStep > ; t : number } {
47
- const [ { target, tween } , setState ] = React . useState ( {
48
- target : 0 ,
49
- tween : { prev : step , next : step } ,
50
- } )
47
+ const [ { target, steps, index } , setState ] =
48
+ React . useState < StepSpringState > ( {
49
+ target : 2 ,
50
+ steps : [ step , step , step ] ,
51
+ index : 0 ,
52
+ } )
51
53
52
54
React . useEffect ( ( ) => {
53
- if ( tween . next != step ) {
54
- setState ( s => ( {
55
- target : s . target + 1 ,
56
- tween : { prev : tween . next , next : step } ,
57
- } ) )
55
+ const lastStep = steps [ steps . length - 1 ]
56
+ if ( lastStep != step ) {
57
+ setState ( s => updateStepSpring ( s , step , progress ) )
58
58
}
59
59
} , [ step ] )
60
60
61
61
const [ progress ] = useSpring ( target , springConfig )
62
62
63
- const t = progress % 1
63
+ const trioProgress = progress - index
64
64
65
- return { tween, t : t || 1 }
65
+ const result =
66
+ trioProgress <= 1
67
+ ? {
68
+ tween : { prev : steps [ 0 ] , next : steps [ 1 ] } ,
69
+ t : trioProgress ,
70
+ }
71
+ : {
72
+ tween : { prev : steps [ 1 ] , next : steps [ 2 ] } ,
73
+ t : trioProgress - 1 ,
74
+ }
75
+
76
+ return result
77
+ }
78
+
79
+ type StepSpringState = {
80
+ target : number
81
+ steps : [ CodeStep , CodeStep , CodeStep ]
82
+ index : number
83
+ }
84
+
85
+ function updateStepSpring (
86
+ state : StepSpringState ,
87
+ newStep : CodeStep ,
88
+ progress : number
89
+ ) : StepSpringState {
90
+ const { steps, target, index } = state
91
+ const stepsClone =
92
+ steps . slice ( ) as StepSpringState [ "steps" ]
93
+
94
+ const trioProgress = progress - index
95
+
96
+ if ( trioProgress < 1 ) {
97
+ stepsClone [ 2 ] = newStep
98
+ return {
99
+ ...state ,
100
+ steps : stepsClone ,
101
+ }
102
+ } else {
103
+ stepsClone [ 0 ] = steps [ 1 ]
104
+ stepsClone [ 1 ] = steps [ 2 ]
105
+ stepsClone [ 2 ] = newStep
106
+ return {
107
+ ...state ,
108
+ steps : stepsClone ,
109
+ target : target + 1 ,
110
+ index : index + 1 ,
111
+ }
112
+ }
66
113
}
0 commit comments