Reuse stories in other stories

Created on 18 April 2024, 9 months ago
Updated 16 September 2024, 4 months ago

Problem/Motivation

I have created stories for components.
I would like to compose other stories with some of these, like this (instead of copy/pasting)

...
{% story carousel %}
  {% include_story card_demo1 %}
  {% include_story card_demo2 %}
  ..
{% endstory %}

Where `card_demo1` and `card_demo2` are defined in the card.stories.twig

...
{% story card_demo1 with...
💬 Support request
Status

Active

Version

1.0

Component

Twig

Created by

miiimooo Europe

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @miiimooo
  • 🇳🇱Netherlands Web-Beest

    +1: I was looking for the same thing :-)

  • I ran into the same issue while trying the same thing as Web-Beest.

    How did you end up solving/bypassing this issue?

  • 🇳🇱Netherlands Web-Beest

    So, in hindsight, the args is not the place where you want to do this. It would result (if it worked) in rendered HTML appearing in your controls-pane in Storybook.

    I've combined Storybook with SDC and came up with the following solution. I define the arguments I want to use in the args so I can alter them in controls. If you don't need to alter them, just define them as variables in Twig.

    In the component I've defined properties and slots. The slots are there so you can embed large blocks as content without it being a property of that component. So for instance a fieldset has a legend and 'children' (which can be anything). This is a place where you can embed a large piece of content.

    Fieldset template

    <fieldset>
      <legend>{{ legend_title }}</legend>
      {% block children %}
      {{ children }}
      {% endblock %}
    </fieldset>
    

    In my story I embed the component with a block:

        {% embed 'theme:fieldset' with {
          legend: 'Fieldset legend'
        }
        %}
          {% block children %}
            {{ include('theme:component') }}
          {% endblock %}
        {% endembed %}
    

    This way you can reuse stories / components in other stories.

  • First commit to issue fork.
  • Pipeline finished with Success
    4 months ago
    Total: 146s
    #284476
  • 🇺🇸United States brayn7 Lexington, Ky

    I added a small twig extension. Probably be nice to harden validation. I think this will be very helpful in creating stories!

  • Pipeline finished with Success
    4 months ago
    Total: 222s
    #284482
  • 🇮🇹Italy tanc Italy

    @brayn7 I gave your extension a quick go. It worked nicely in my test but the findFileInDirectory function has a hard coded path to components in the theme or module. Would it be possible for this to be a configurable or at least able to be overridden? We keep our stories in [themename]/stories so it would be nice to be able to set that path.

  • Pipeline finished with Success
    3 months ago
    Total: 140s
    #325120
  • 🇺🇸United States brayn7 Lexington, Ky

    @tanc I have revised the lookup to include the whole module or theme. Unsure how performant that is looking in the whole directory so someone chime in about the efficiency of that. We could optionally pass one more arg in the twig function to designate what directory to look in with the default being /components.

  • 🇳🇱Netherlands tim_dj Netherlands

    Patch works as advertised. Does it make sense to search for stories outside of components? Then it's not exactly single direcctory component anymore?

  • 🇮🇹Italy tanc Italy

    It makes complete sense if you're not using SDCs. I'd like to see a default with the ability to override the path

Production build 0.71.5 2024