Coder Social home page Coder Social logo

Comments (3)

MichaelAllenWarner avatar MichaelAllenWarner commented on September 26, 2024 2

@philwolstenholme and whoever else this helps:

You can work around the Promise issue in Storybook HTML by using Storybook's (experimental) loaders feature. A story itself cannot be an async function, but you can set it to render the result of an async loader.

The pattern I'm using looks like this:

import button from './button.twig';
import { ButtonSpan } from './ButtonSpan.stories';

export default { title: 'Button' };

// never call this directly (only gets called by Storybook);
// EVERY Twig-story should be a function that looks like this.
export const Button = (_, { loaded: { component } }) => component;

// THIS is what you call directly if there's need
Button.render = async args => {
  // Rendering logic goes here.
  // If, instead of `include` or `embed`, you'd like to
  // inject another Twig-story directly (e.g., if you have
  // `{{ buttonSpan }}` in `button.twig`), `await` its `render()` method:
  const buttonSpan = await ButtonSpan.render({ text: args.text });
  // Same goes for any `.twig` imports used directly:
  const renderedButton = await button({ buttonSpan });
  return renderedButton;
};

Button.args = {
  text: 'some button text'
};

This works in conjunction with the following global loader, set in preview.js:

export const loaders = [
  async ({ args, originalStoryFn }) => {
    if (originalStoryFn.render) {
      const component = await originalStoryFn.render(args);
      return { component };
    }
  }
];

from twing-loader.

philwolstenholme avatar philwolstenholme commented on September 26, 2024 1

Thinking about it a bit more, the issue here is probably that I am using Storybook HTML, and if I were to use Storybook with a JS flavour then the async nature would probably be fine.

from twing-loader.

philwolstenholme avatar philwolstenholme commented on September 26, 2024

If I switch to using

    "twing": "2.3.7",
    "twing-loader": "2.0.2"

Then this works fine:

const template = require('./button.twig');

export default {
  title: 'Components/Button',
};

export const Default = () =>
  template({
    message: 'bar',
  });

But it feels unprofessional to use 2 major versions older version of a dependency on a new project…

from twing-loader.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.