Skip to content

Improve source code previews in Storybook #1004

@tylersticka

Description

@tylersticka

By default, Storybook displays the JavaScript source for stories when you click the "show code" button on story previews in docs mode:

Screen Shot 2020-10-26 at 3 55 54 PM

This is not particularly useful for this project, where most of the patterns are Twig templates. There's never a place where that code would be copied and pasted anywhere.

We recently updated Storybook to 6.x, which included a few improvements to the customization of source previews. Unfortunately these don't seem to be capable of full automation, but it would allow us to do something like this...

import { Story, Canvas, Meta } from '@storybook/addon-docs/blocks';
import template from './example/template.twig';
import templateSrc from '!!raw-loader!./example/template.twig;
// ...

<Canvas>
  <Story name="Example" parameters={{
    docs: { source: { code: templateSrc } }
  }}>
    {(args) => template(args)}
  </Story>
</Canvas>

This would get us a lot closer, but it does call into question our current way of keeping things DRY through demos. Consider these two examples, a default and a prose container:

Screen Shot 2020-10-26 at 4 54 52 PM

Screen Shot 2020-10-26 at 4 55 00 PM

Notice that the examples are the same, and aren't really indicative of how things would work in the wild. That's because the demos were written from the point of view of the maintainer keeping things DRY and not as a point of reference for the viewer. Does this mean rewriting all of the demos so far to keep them more relevant?

Alternatively, we could manually include an additional example for each pattern:

<Source
  language="twig"
  code={`{% embed '@cloudfour/objects/container/container.twig' %}
  {% block content %}
    Hello world
  {% endblock %}
{% endembed %}`}
></Source>

Which would display outside the story:

Screen Shot 2020-10-26 at 5 01 25 PM

This would give us a lot more freedom, but is it too much extra work? And can we disable the normal "show code" buttons to reduce noise of the docs?

Metadata

Metadata

Assignees

Labels

⁉️ questionFurther information is requested👷 environmentSetup, build tools, configuration, etc.📚 documentationImprovements or additions to documentation

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions