Skip to content

Commit 0dc5533

Browse files
committed
Fix transitions
1 parent 8c67c2f commit 0dc5533

File tree

5 files changed

+135
-28
lines changed

5 files changed

+135
-28
lines changed

.gitpod.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# This configuration file was automatically generated by Gitpod.
2+
# Please adjust to your needs (see https://www.gitpod.io/docs/config-gitpod-file)
3+
# and commit this file to your remote git repository to share the goodness with others.
4+
5+
tasks:
6+
- init: yarn install && yarn run build
7+
command: yarn run playground
8+
9+

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
},
2020
"devDependencies": {
2121
"auto": "^10.18.7",
22-
"lerna": "^4.0.0"
22+
"lerna": "^4.0.0",
23+
"prettier": "^2.5.1"
2324
},
2425
"repository": "code-hike/codehike",
2526
"author": "pomber <[email protected]>",

packages/mini-editor/src/editor-spring.tsx

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,28 +60,73 @@ function EditorSpring({
6060
function useStepSpring(
6161
step: EditorStep,
6262
springConfig: SpringConfig = defaultSpring
63-
) {
64-
const [{ target, prev, next }, setState] = React.useState(
65-
{
66-
target: 0,
67-
prev: step,
68-
next: step,
69-
}
70-
)
63+
): { prev: EditorStep; next: EditorStep; t: number } {
64+
const [{ target, steps, index }, setState] =
65+
React.useState<StepSpringState>({
66+
target: 2,
67+
steps: [step, step, step],
68+
index: 0,
69+
})
7170

7271
React.useEffect(() => {
73-
if (next != step) {
74-
setState(s => ({
75-
target: s.target + 1,
76-
prev: next,
77-
next: step,
78-
}))
72+
const lastStep = steps[steps.length - 1]
73+
if (lastStep != step) {
74+
setState(s => updateStepSpring(s, step, progress))
7975
}
8076
}, [step])
8177

8278
const [progress] = useSpring(target, springConfig)
8379

84-
const t = progress % 1
80+
const trioProgress = progress - index
81+
82+
const result =
83+
trioProgress <= 1
84+
? {
85+
prev: steps[0],
86+
next: steps[1],
87+
t: trioProgress,
88+
}
89+
: {
90+
prev: steps[1],
91+
next: steps[2],
92+
t: trioProgress - 1,
93+
}
94+
95+
return result
96+
}
97+
98+
type StepSpringState = {
99+
target: number
100+
steps: [EditorStep, EditorStep, EditorStep]
101+
index: number
102+
}
85103

86-
return { prev, next, t: t || 1 }
104+
function updateStepSpring(
105+
state: StepSpringState,
106+
newStep: EditorStep,
107+
progress: number
108+
): StepSpringState {
109+
const { steps, target, index } = state
110+
const stepsClone =
111+
steps.slice() as StepSpringState["steps"]
112+
113+
const trioProgress = progress - index
114+
115+
if (trioProgress < 1) {
116+
stepsClone[2] = newStep
117+
return {
118+
...state,
119+
steps: stepsClone,
120+
}
121+
} else {
122+
stepsClone[0] = steps[1]
123+
stepsClone[1] = steps[2]
124+
stepsClone[2] = newStep
125+
return {
126+
...state,
127+
steps: stepsClone,
128+
target: target + 1,
129+
index: index + 1,
130+
}
131+
}
87132
}

packages/smooth-code/src/code-spring.tsx

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,70 @@ function useStepSpring(
4444
step: CodeStep,
4545
springConfig: SpringConfig = defaultSpring
4646
): { 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+
})
5153

5254
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))
5858
}
5959
}, [step])
6060

6161
const [progress] = useSpring(target, springConfig)
6262

63-
const t = progress % 1
63+
const trioProgress = progress - index
6464

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+
}
66113
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15110,6 +15110,11 @@ prelude-ls@~1.1.2:
1511015110
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
1511115111
integrity sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=
1511215112

15113+
prettier@^2.5.1:
15114+
version "2.5.1"
15115+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.5.1.tgz#fff75fa9d519c54cf0fce328c1017d94546bc56a"
15116+
integrity sha512-vBZcPRUR5MZJwoyi3ZoyQlc1rXeEck8KgeC9AwwOn+exuxLxq5toTRDTSaVrXHxelDMHy9zlicw8u66yxoSUFg==
15117+
1511315118
prettier@~2.2.1:
1511415119
version "2.2.1"
1511515120
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.2.1.tgz#795a1a78dd52f073da0cd42b21f9c91381923ff5"

0 commit comments

Comments
 (0)