Problem/Motivation

In UI Patterns 1.x it i possible to specify templates for previews (there is only one story by component in UI Patterns 1.x, and it is always called "preview")

Examples:

Sometimes, it is used to disabled the preview: https://git.drupalcode.org/project/ui_suite_uswds/-/blob/3.0.x/templates...

Proposed resolution

They don't belong to the components/{component_id} folder but anywhere in the templates/ folder. They are regular Drupal theme templates from hook_theme(): component-story--{component_id}.html.twig

Examples:

  • component-story--modal.html.twig
  • component-story--card-body.html.twig

By story: component-story--{component_id}--{story_id}.html.twig

Examples:

  • component-story--modal–preview.html.twig
  • component-story--card-body–long-text.html.twig

No templates suggestions per variant.

Alternative

Can we provide this feature without this hook_theme ?

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

pdureau created an issue. See original summary.

pdureau’s picture

Component: Code » UI Patterns Library
pdureau’s picture

Can we "adopt" https://www.drupal.org/project/storybook (411 installs) format ?

The deep purpose of this module (stories as Twig templates) is far away from UI Patterns philosophy (stories as Drupal renderable), however:

  • they allow the file to be in the component folder ({component_id}.stories.twig) while we plan to use hook_theme templates from templates/ directory
  • we will be able to show those stories if the theme is using them instead of UI Patterns one
{# some/path/in/your/code/base/my-card.stories.twig #}
{% stories my_card %}
  {% story default } %}
   The stuff we want to add to the story, like wrappers.
   {{ component(...) }}
  {% endstory %}
{% endstories %}

Let's list SDC themes using this format in drupal.org first

pdureau’s picture

pdureau’s picture

Assigned: Unassigned » pdureau
pdureau’s picture

Here are all the preview templates of the public U Suite :

ui_suite_bootstrap

  • badge/pattern-badge--preview.html.twig >> ✅ "addClass", so doable in UIP2 stories
  • card_body/pattern-card-body--preview.html.twig >> ⚠️ HTML wrapper, because sub-component
  • card_overlay/pattern-card-overlay--preview.html.twig >> ✅ "addClass", so doable in UIP2 stories
  • card/pattern-card--preview.html.twig >> ✅ "setAttribute" and little safe stuff, so doable in UIP2 stories
  • carousel_item/pattern-carousel-item--preview.html.twig >> ⚠️ HTML wrapper, because sub-component
  • close_button/pattern-close-button--preview.html.twig >> ⚠️ A weird stuff with "bg-dark", do we still need that?
  • modal/pattern-modal--preview.html.twig >> ❌ Add a button, so we may still need a template
  • navbar/pattern-navbar--preview.html.twig >> ✅ "addClass", so doable in UIP2 stories. It may be 2 different stories
  • nav_item/pattern-nav-item--preview.html.twig >> ⚠️ HTML wrapper, because sub-component
  • offcanvas/pattern-offcanvas--preview.html.twig > ❌ Add a button, so we may still need a template
  • toast_container/pattern-toast-container--preview.html.twig >> ✅ "addClass", so doable in UIP2 stories
  • toast/pattern-toast--preview.html.twig >> ❌ HTML wrapper for 'position-static", so we may still need a template

ui_suite_dsfr

  • consent_banner/pattern-consent-banner--preview.html.twig >> ✅ "setAttribute", so doable in UIP2 stories
  • display/pattern-display--preview.html.twig >> ❌ Add a button, so we may still need a template
  • modal/pattern-modal--preview.html.twig >> ❌ Add a button, so we may still need a template

ui_suite_material

  • card/pattern-card--preview.html.twig >> ✅ "setAttribute", so doable in UIP2 stories

ui_suite_uswds

  • accordion_item/pattern-accordion-item--preview.html.twig >> ✅ "No preview", so no stories in UIP2
  • card/pattern-card--variant-flag--preview.html.twig >> ✅ "addClass", so doable in UIP2 stories
  • character_count/pattern-character-count--preview.html.twig >> ✅ Empty
  • checkbox/pattern-checkbox--preview.html.twig >> ❌ careful: Some logic
  • combo_box/pattern-combo-box--preview.html.twig >> ✅ Empty
  • date_picker/pattern-date-picker--preview.html.twig >> ✅ "setAttribute", so doable in UIP2 stories
  • header/pattern-header--preview.html.twig >> ✅ "No preview", so no stories in UIP2
  • in_page_nav/pattern-in-page-nav--preview.html.twig >> ✅ "No preview", so no stories in UIP2
  • input_prefix_suffix/pattern-input-prefix-suffix--preview.html.twig >> ✅ "setAttribute", so doable in UIP2 stories
  • nav_footer/pattern-nav-footer--preview.html.twig >> ✅ "No preview", so no stories in UIP2
  • nav/pattern-nav--preview.html.twig >> ✅ "No preview", so no stories in UIP2
  • process_list_item/pattern-process-list-item--preview.html.twig >> ✅ "No preview", so no stories in UIP2
  • radio/pattern-radio--preview.html.twig >> ❌ careful: Some logic
  • range_slider/pattern-range-slider--preview.html.twig >> ✅ "setAttribute", so doable in UIP2 stories
  • time_picker/pattern-time-picker--preview.html.twig >> ✅ Empty

So, we have:

  • 4 "⚠️ HTML wrapper, because sub-component". Once #3351682: [2.2.0] UI Patterns Library: add sub-components is delivered, they will not be needed, because the preview will be done inside the parent component preview.
  • 2 "❌ careful: Some logic" but Steven told us "Since I can pass any number of stories no I don’t think I need it "
  • 1 "❌ HTML wrapper for 'position-static"
  • 4 "❌ Add a button"

So, for only 5 use cases in our most complete themes, do we still really need those story templates?

pdureau’s picture

Issue tags: +HTML wrapper

Proposal to cover both "HTML wrapper" and "Add a button" without the need of such a template, let's introduce a library_wrapper property, with a Twig template as a value.

This property will be ignored by ComponentElementAlter but used by the library pages controller (or formatter, after #3438360: Use Drupal entities & plugins for the Library pages).

It will be resolved as an inline_template render element, with those values in #context:

  • all variables already injected in the component
  • a special story variable with the renderable of the story

So, if a story miss library_wrapper (which will be the case for 90% of the stories), it will be processed as: library_wrapper: '{{ story }}'

For example, to replace this template: https://git.drupalcode.org/project/ui_suite_bootstrap/-/blob/5.0.x/templ...

stories:
  preview:
    title: Preview
    library_wrapper: '<div class="card">{{ story }}</div>'
    slots:
      title: 'Card title'
      subtitle: 'Card subtitle'
      text: 'Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.'
    props:
      heading_level: 5

What will happen if there is already a story slot or prop? Maybe _story would be safer.

grimreaper’s picture

Participating in the discussion in off with @pdureau ;)

pdureau’s picture

pdureau’s picture

Title: [2.0.0-beta2] Add component story template » [2.0.0-beta2] Allow library wrappers around stories
pdureau’s picture

Title: [2.0.0-beta2] Allow library wrappers around stories » [2.0.0-beta3] Allow library wrappers around stories
Status: Active » Postponed
pdureau’s picture

Title: [2.0.0-beta3] Allow library wrappers around stories » [2.0.0-beta4] Allow library wrappers around stories
pdureau’s picture

Title: [2.0.0-beta4] Allow library wrappers around stories » [2.0.0-beta5] Allow library wrappers around stories
pdureau’s picture

Assigned: pdureau » Unassigned
Status: Postponed » Active

We can start this task

pdureau’s picture

Assigned: Unassigned » pdureau

pdureau’s picture

Assigned: pdureau » grimreaper
Status: Active » Needs review

Grimreaper, what do you think about the current state of work? https://git.drupalcode.org/project/ui_patterns/-/merge_requests/261/diffs

It is not finished, I will need to inject the plugin.manager.component_story dependency for example, but it works and it is advanced enough to talk:

  • is TwigExtension the best place to put the logic?
  • We trigegr the logic only for single component library page ( ui-patterns-stories-full.html.twig) ?
  • _story or story for the variable injected in library_wrapper ?
  • ...
grimreaper’s picture

Assigned: grimreaper » pdureau

Ok for me.

Code review we made together addressed.

  • pdureau committed d2ca5776 on 2.0.x
    Issue #3462325 by pdureau, grimreaper: Allow library wrappers around...
pdureau’s picture

Assigned: pdureau » Unassigned
Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.