3
3
EditorProps ,
4
4
EditorStep ,
5
5
} from "@code-hike/mini-editor"
6
- import { Code } from "./code"
6
+ import { InnerCode , updateEditorStep } from "./code"
7
7
import {
8
8
Scroller ,
9
9
Step as ScrollerStep ,
@@ -25,12 +25,26 @@ export function Scrollycoding({
25
25
} ) {
26
26
const stepsChildren = React . Children . toArray ( children )
27
27
28
- const [ stepIndex , setIndex ] = React . useState ( start )
29
- const tab = editorSteps [ stepIndex ]
28
+ const [ state , setState ] = React . useState ( {
29
+ stepIndex : start ,
30
+ step : editorSteps [ start ] ,
31
+ } )
32
+
33
+ const tab = state . step
30
34
31
35
function onStepChange ( index : number ) {
32
- setIndex ( index )
36
+ setState ( { stepIndex : index , step : editorSteps [ index ] } )
37
+ }
38
+
39
+ function onTabClick ( filename : string ) {
40
+ const newStep = updateEditorStep (
41
+ state . step ,
42
+ filename ,
43
+ null
44
+ )
45
+ setState ( { ...state , step : newStep } )
33
46
}
47
+
34
48
return (
35
49
< section
36
50
className = { `ch-scrollycoding ${
@@ -44,10 +58,10 @@ export function Scrollycoding({
44
58
as = "div"
45
59
key = { i }
46
60
index = { i }
47
- onClick = { ( ) => setIndex ( i ) }
61
+ onClick = { ( ) => onStepChange ( i ) }
48
62
className = "ch-scrollycoding-step-content"
49
63
data-selected = {
50
- i === stepIndex ? "true" : undefined
64
+ i === state . stepIndex ? "true" : undefined
51
65
}
52
66
>
53
67
{ children }
@@ -56,7 +70,11 @@ export function Scrollycoding({
56
70
</ Scroller >
57
71
</ div >
58
72
< div className = "ch-scrollycoding-sticker" >
59
- < Code { ...( tab as any ) } codeConfig = { codeConfig } />
73
+ < InnerCode
74
+ { ...( tab as any ) }
75
+ codeConfig = { codeConfig }
76
+ onTabClick = { onTabClick }
77
+ />
60
78
{ presetConfig && (
61
79
< Preview
62
80
className = "ch-scrollycoding-preview"
0 commit comments