You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: xmdx/pages/content.md
+65-35Lines changed: 65 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,94 +22,124 @@ For example, here we are changing all the h1s, adding a purple border.
22
22
23
23
---
24
24
25
-
A special component we can override is the Wrapper. The wrapper is the component that wraps the content from the markdown file. Here we are just adding a border to it. But the cool thing about this component, is thatwe get all the content as React elements in the children prop.
25
+
A special component we can override is the Wrapper. The wrapper is the component that wraps the content. Here we are just adding a border to it. But the cool thing about this component, is that, in the children prop, we get all the content from the markdown file as React elements .
26
26
27
27
---
28
28
29
-
>
29
+
And React elements are just javascript objects. So here we are rendering the wrapper children as JSON, and filtering some properties to make it easier to read.
30
30
31
-
If we explore the children, you'll see they come with an mdxType. We can use that type to reorder or change what we render.
31
+
You'll see that it is an array. The first element is an h1, the second a paragraph. Each element comes with an mdxType prop, we can use that type to extract information about the content or to change the elements.
32
32
33
-
In the MDX docs there's a section called "Manipulating children" with more info about this. But don't google "manipulate children", you don't want that in your google history.
33
+
---
34
+
35
+
For example we could get a list of all the H1s from the children, and render that list as a table of contents, before rendering the actual content.
36
+
37
+
This is a simple example, but it illustrates the pattern we are going to use on the rest of the examples from this talk. First we extract some data from the children, and then we render it in a specific way.
34
38
35
-
(OR an alternative title for this talk was "Manipulating children for fun and profit" but I guess it could be misunderstood )
39
+
Keep in mind that this runs on every render. For most cases, it isn't a performance problem, but if it is, you can move it to a plugin, and run the transformation on build-time.
36
40
37
41
---
38
42
39
-
OK, quick example about this. We could get all the H1s, by filtering all the children that have an mdxType equals to h1, and prepend a table of contents to the actual content.
43
+
I usually write content that has steps, like tutorials or any type of walkthrough where you explain something step by step.
44
+
45
+
Markdown doesn't have any specific syntax for grouping things in steps. But we can use MDX to extend markdown and introduce our own syntax. The implementation of the Step component we are using here doesn't matter, we are just using it for grouping elements.
40
46
41
-
Keep in mind we are doing this on run-time. There are a lot of plugins, for both markdown and mdx, that do things like these on build-time. In fact, we could write plugins to dmove to build-time most of the things we are going to do in this talk. Plugins are a bit more messy because you manipulate the abstract syntax tree instead of React children, but you should definitly consider them if you need better performance.
47
+
If you are new to MDX, this may not be the best introduction. The typical use-case for MDX is embeding interactive components in markdown. But here we are taking a different approach, and using it more as a syntax extension for markdown.
42
48
43
49
---
44
50
45
-
So far we used markdown. Now let's move to MDX.
51
+
Now, based on the MDX file that has steps, we can write another Wrapper component. In this case, in the children prop, we get one React element for each step.
46
52
47
-
I usually write content that has steps, like tutorials or any type of walkthrough where you explain something step by step.
53
+
So we can keep track of what step we are showing using React state, and let the user change the current step with a button.
48
54
49
-
Markdown doesn't have any specific syntax for grouping things in steps. So, here, we can use MDX to fill that gap.
55
+
-> click
50
56
51
-
For those unfamiliar with MDX, this isn't the best introduction. The most common usage of MDX is to embbed interactive components inside the markdown content. Here we are using it for something different, we are using MDX for augmenting the markdown content with extra syntax or conventions.
57
+
-> click
58
+
59
+
Ok, now I want to show the same content but with different layout. There's a technique called scrollytelling. You may have seen it on some websites, as the user scrolls down there's some part of the layout that sticks to the screen while the rest is scrolled away. Let's do that.
52
60
53
61
---
54
62
55
-
For example, we can put every step on it's own page. And add some state to the Wrapper, so the user can now change the page.
63
+
Since this is a lightning talk I'll import the ScrollytellingLayout component. I'll share the link to the repo later if you want to see how it works.
56
64
57
-
That's a pretty basic example, let's try something more ambitious.
65
+
The ScrollytellingLayout component takes two props, one for the left-side that can be scrolled, and the other for the sticky part on the right.
58
66
59
-
---
67
+
When the user scrolls to a new step
68
+
69
+
--> scroll
70
+
71
+
we show the corresponding element from the sticker list.
72
+
73
+
--> scroll
60
74
61
-
There's a technique called scrollytelling. You may have seen it on some websites. You have content in steps that can be scrolled, and you have a piece of you screen that has fixed or sticky position, and it changes when you scroll to a new step.
75
+
--> scroll
62
76
63
-
In modern browsers you can combine features like Intersection Observers and position sticky to make a scrollytelling layout, but luckily for us, I can import a React component that already does that.
77
+
Now, instead of showing the step number, let's add the sticker content to the MDX file.
64
78
65
-
The important thing to see here is that, using the same MDX as in the previous example, we can use it as the source to a very different layout.
79
+
---
66
80
67
-
Now showing the step number in the right is boring. Usually we would want to show some content also from the MDX here.
81
+
Suppose we want to show some code in the sticky part of the layout. There isn't any specific syntax for this, so we need to create our own convention. Like, for example, we put the sticky part of the step as the first element.
68
82
69
83
---
70
84
71
-
So the steps will have two parts. We already have the content we show on the left, so we need a place to add the part of step that we want to show on the sticky section on the right.
85
+
Now doing some array transformation, we get the list of steps and the list of stickers and pass them to the same Layout component.
86
+
87
+
So when the user scrolls
88
+
89
+
--> scrolls
72
90
73
-
We are the ones that manipulate the wrapper children, so we could put it anywhere we prefer, we could add a new component or pass it as props. I like to add the convention that the first child of the step is the sticky part and the rest is the scrollable part.
91
+
the code on the right
74
92
75
-
For example we added some code as the first child on each step.
93
+
--> scrolls
94
+
95
+
should change accordingly
76
96
77
97
---
78
98
79
-
// TODO remove images step
99
+
Just for fun, I have a Terminal component that animates between code transitions, so we can use for the stickers.
100
+
101
+
--> scrolls
102
+
103
+
--> scrolls
80
104
81
-
And we can change the part where we read the wrapper children, and extract each first child to another list.
105
+
--> scrolls
82
106
83
-
Then we show that first child on the sticky part of the layout when the step changes.
107
+
--> scrolls 0
108
+
109
+
I've been experimenting with another layout for walkthroughs, instead of changing the steps using the scroll like in this example, we can synchronize the steps with some media, like a video or an audio, maybe a podcast, and change the steps as the media progress.
84
110
85
111
---
86
112
87
-
I just happen to have a specific component to show terminal code. Which is cool because it also animates the transitions.
113
+
To do that, in the MDX file, we need to specify the media file and the time range for each step.
88
114
89
115
---
90
116
91
-
Now, I've been experimenting with another layout for walkthroughs. Instead of changing the steps using the scroll like in the scrollytelling layout, we can synchronize the steps with some media, like a video or an audio, maybe a podcast, and change the steps as the media progress.
117
+
Once we have that, we can extract it from the children on the Wrapper, and pass it to another React component. This time is the TalkLayout component, that solves all the synching for us.
92
118
93
-
So the extra information we need for that kind of layout is the name of the file, and a range of time when the step will show.
119
+
And the steps should change every time I snap the fingers.
94
120
95
-
---
121
+
--> snap 1
96
122
97
-
I also happen to have a React component for this. So we only need to extract the info from the props and pass it to the Video component.
123
+
--> snap 2
98
124
99
-
You'll see how the step changes as soon as I snap my fingers, right... now.
125
+
Some of you may have noticed that this looks similar to the layout of this talk that I'm giving right now.
100
126
101
-
Now, some of you may have noticed that this look similar to the layout of the video of this talk that I'm giving right now.
127
+
And it is.
102
128
103
129
---
104
130
105
-
And it is, because this talk was built using this same technique. It's all mdx. It's allways have been.
131
+
This talk was built using this same technique. It's all MDX.
132
+
133
+
For example, here on the left you can see the code for the step you are currently watching.
134
+
135
+
---
106
136
107
-
In fact if you go to https://mdxconf.pomb.us you can interact with the components while you watch the talk, you can select and copy the code or interact with the iframes in the mini browser.
137
+
And the next step.
108
138
109
139
---
110
140
111
-
Ok, that's all I have. I leave you here the links to the repo of the talk. Not the slides, but the talk itself. You run yarn dev and you can watch this talk again.
141
+
Ok, that's all. I leave you here the links to the repo of the talk. Not the slides, but the talk itself. You run yarn dev and you can watch this talk again.
112
142
113
-
Also there's also my twitter, and the components we used, most of them come from a new project I'm working on, it's called Code Hike and it focuses on code walkthroughs, and tools for making it easy to explain code.
143
+
Also there's my twitter, and the components we used, most of them come from a new project I'm working on, it's called Code Hike and it focuses on code walkthroughs and tools for making it easy to explain code.
0 commit comments