We need to have a discussion about how we’re going to handle examples going forward. We have several ways to present examples and their output, some of which are useful for very specific use cases and others which are more generally useful. All of them have issues that make them a non-starter as a one-size-fits-all solution for example presentation.
At the most basic level, when just throwing a few lines of code into the page, with no intent to show any output or the like, we can just use a <pre>
block. That’s fine and works reasonably well (although the feature to highlight specific lines of code needs to be exposed to the UI somehow, ideally).
Common forms of example
Things get much more complicated when you have larger examples that you want to present output for. There are a few use cases here. I’ll present the ones I can think of and the issues that will affect our decision-making as to a long-term solution for example presentation.
Short, interactive, syntax demonstration snippets
This is the kind of short example that shows how to use a feature at a fairly fundamental level. What do the different parameters do? What affect do these different values have on a CSS property? That kind of thing.
These need to have an inline editor, need to show all of the code involved, and need to provide immediate presentation of the effect of changes the reader makes to the example code. They don’t need to show more than one language, and generally only need to deal with examples under 15 lines long or so.
Example: CSS transform
property
Large, monolithic examples
These are longer, more involved examples, possibly including any combination of CSS, JavaScript, and HTML. Each language’s code is presented in one long <pre>
block. The output of the code is also presented. There is not necessarily any editing support (currently not, but there’s no reason not to allow it in the future if so desired).
Example: Lighting in WebGL (this is actually a segmented example, but it uses very large chunks of code instead of small ones, so it’s close enough)
Large, segmented examples
These are similar to the monolithic examples, in that they are longer examples and may include any combination of the three major languages. However, the code may be broken into smaller chunks for easier reading and to allow explanatory text to be interspersed among the sections of code. These, too, don’t currently require editing support but could in the future if so desired.
Example: Timing element visibility with the Intersection Observer API
Step-by-step or related examples
These examples are monolithic or segmented examples, except there are a series of them throughout the same article. In the case of a step-by-step example, an article may have a number of sections, each of which presents an example which builds on the code from the previous example. For instance, the first section may show how to initialize a WebGL context. The second section might add code to clear it to a specific color. The third might demonstrate drawing a box. The next section might add textures. And so forth. Each section requires code from the previous section and builds upon it. Currently, we have to actually repeat the shared code (possibly hidden using CSS, but still repeated).
Related examples may not build on each other, but might share the same CSS, for example. Here, too, we have to repeat the shared code.
Repeating code bloats the page, slowing load times. It also, perhaps more importantly, means that the writers have to remember to make changes in multiple places in order to fix bugs or make improvements to code that’s shared, rather than only having to change one place.
Tools
These are essentially monolithic examples, except the code is not displayed to the user. For example, the MDN CSS color picker tool.
Our current example presentations
We have several ways to present examples at present:
- Simple inline blocks for each language, with
{{EmbedLiveSample}}
if we wish to show the output - Zero or more
<pre>
blocks for any combination of the languages, along with{{EmbedLiveSample}}
to assemble all of the code blocks together and show the results in an<iframe>
- Example on GitHub and
{{EmbedGHLiveSample}}
(doesn’t show the code, just the result though) - Example on GitHub and
{{EmbedInteractiveExample}}
(shows the code in an editable widget, with live refreshes of the result)
The interactive example editor by design supports only short and simple snippets, and only supports a single language (CSS, JavaScript, or HTML). It doesn’t really handle larger examples, and isn’t currently ready to be used more than once on a page.
The older, so-called live sample mechanism, lets us present multiple chunks of code and assemble them into a visual representation of the output, which is great. However, it doesn’t allow editing and experimenting with the code (aside from providing links to CodePen and JSFiddle). It also doesn’t support sharing of code among examples; currently, we instead have to duplicate the common code and use the “hidden” class on it if we don’t want to see it.
What’s the plan?
We need a long-term plan for examples on MDN. The interactive example editor is amazing but isn’t (at least at this time) capable of handling all the use cases for examples.
We need to make some decisions.
- Do we enhance the interactive example editor to cover the other use cases it doesn’t support now?
- Do we discard the current live sample system and replace it with a new mechanism that lives side-by-side with the interactive examples?
- Do we find ways to improve the existing live sample system to correct some of its deficiencies?
- Do we drop some of our use cases entirely?
I expect there are some strong opinions on this. I know I have them, anyway. I will share my personal thoughts in my next post.
Sheppy